Applying Constraint References in VBA

Hi all

As part of a project, I am required to automate as much as possible the assembly of our products in Solidworks.

The idea is to start from our Excel file which contains the name and path of the components to be assembled.

I created a first macro that extracts these 2 columns to add them to a blank Excel file (which I use as a "software" for users of the program)

Once this information is added, a click opens Solidworks and adds each of the parts in the list to a new assembly file.

So far so good!

Wishing to optimize the assembly as I explained, I added constraint references to each of the components so that they "recognize" each other and assemble in the desired way. However, when I run my program, the pieces are simply open on top of each other... I tried to do the manipulation manually by recording the macro, but in the file obtained, I have the impression that the click on "smartmates" or the "alt"+dragged, are not recognized.

Do you have any idea what code I could use to do this?

Thank you in advance,

Anthony M. 

Hello Anthony;

There is an example of a macro (VB) to add a part in an assembly is to force it, to adapt....

->http://help.solidworks.com/2020/English/api/sldworksapi/Add_Component_and_Mate_Example_VB.htm?verRedirect=1

(Warning, this macro requires a class module)

otherwise the vb line to add constraints is commented here:

http://help.solidworks.com/2016/English/api/sldworksapi/SOLIDWORKS.Interop.sldworks~SOLIDWORKS.Interop.sldworks.IAssemblyDoc~AddMate5.html

Kind regards.

1 Like

Hello and thank you for this answer.

Using this method, it becomes possible to add constraints between parts based on references such as axes and planes.

My initial idea was to assign each component a "stress reference" and use these in the assembly automatically.

Is this possible?

Hello

For my part, I couldn't find anything to do this in the APIs.

Kind regards

Hello

Thank you for your message, so I will look for other solutions.

Kind regards

I did something similar

I have added coordinate systems to my parts

then I used the addmate API

 

    Part1 = SelectPcNameIece1 + "@" + PartName1 + "@" + Assembly
    Part2 = SelectPcNameIece2 + "@" + PartName2 + "@" + Assembly
    
    Added constraint
    swModel.Extension.SelectByID2 Piece1, "COORDSYS", 0, 0, 0, True, 0, Nothing, 0
    swModel.Extension.SelectByID2 Piece2, "COORDSYS", 0, 0, 0, True, 0, Nothing, 0
    swModel.AddMate5 20, -1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, longstatus
    Part.ClearSelection2 True
    Part.EditRebuild3

Hello gdm,

Thank you for this answer. 

If I understand correctly, this program does not use smartmates but it consists of defining for each part different coordinate systems at the places of connections and then it makes these systems coincide with each other to make the assembly. Is that right?

I assume that this function uses the magnetic constraints:

https://www.youtube.com/watch?v=IZCqdfRWyqs

A.Mastromauro

Absolutely, simpler to manage, inconvenient it is necessary to prepare the coordinate systems

 

 

Okay thanks, I'm going to modify all my pieces to make them compatible with this method.

On the other hand, if I understand correctly, making an assembly in this way blocks any relative movements of the parts?