API Problem Selection & Constraints

Hello 

for a project I have to make a SolidWorks macro in order to automate a certain task. So far I have managed to create the parts automatically, and to assemble them in an assembly.

Nevertheless, I am now faced with a problem: I have to link the parts together with constraints. So I try to do it automatically. Here's the code:

 

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

' Mate
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Removed mat.-Extru.1@disque1-1@Assemblage1", "BODYFEATURE", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Boss.-Extru.6@Pièce1-1@Assemblage1", "BODYFEATURE", 0, 0, 0, True, 1, Nothing, 0)
Dim swMate As Mate2
Set swMate = Part.AddMate5(1, 0, False, 3.90150366882658E-02, 0.001, 0.001, 0.001, 0.001, 0, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)
Part.ClearSelection2 True
Part.EditRebuild3

End Sub

 

(on two pieces.)

The problem is as follows:

When I try to do the constraint manually (so by removing the associated line in the code above), I have two choices depending on the markers that it puts in my code (the arguments of the SelectID2 methods)

  • -If I use marker 0, the parts are not actually selected in the constraints menu (screenshot 1)
  • - If I use marker 1, I only have one constraint choice!

So I think there must be a parameter of my methods that is not the right one, but despite long searches I haven't found it. So I come to ask you the question: how can this constraint be achieved automatically?

 

Thank you in advance for the help you can give me.

 

 

 


captures_ecran.rar

Hello

It's normal that you only have one type of constraint available on image 2 with the selections you make because these are bodies that you select ("bodyfeature") so you can only "block" them between them. So you have to change the type of selection by faces or others according to your needs, look by ICI.

Kind regards

2 Likes

Hello 

I'm coming back to you because I had already tried to change these settings without success. Out of conscience I redid the tests (with many types present in the link you sent me) but the problem persists. That said, I may be wrong about the nature of the objects.

I am attaching my assembly so that you can help me more easily. I just want to make the pivot link.

 

Thank you in advance for your help!


dossier_test_pieces.rar

Hello

If you want to put a concentricity constraint between faces then you will have to name your faces and then select them by the "GetEntityByName" function as it is marked in the remarks on the following page ("To get IFace2, IEdge or IVertex objects by name, use IPartDoc:: GetEntityByName or IPartDoc::IGetEntityByName.").

Putting a pivot constraint between faces by PLCs is not necessarily that simple,  it may be better to put reference axes in your parts and build them already thinking about the constraints you want to put in order to create your pivot function by a coincidence between the 2 axes and another between 2 planes for example, these elements can be directly selected in the "SelectById2" function as "AXIS" and "PLANE".

It is on the other hand true that in what I think is a study project (student SW degree) it is more interesting and cleaner to go through the selection of faces!!!

Kind regards

3 Likes

It does work.

Thank you for your help!