Hello
The goal of my work is to add a part to a set of parts, already existing, in the Solidwoks software. In addition, it must be assembled with this set using the VB.NET APIs .
So, I managed to add the part to the set of parts in Solidworks, but the problem that I can't assemble it with VB.NET, I did several searches on the net and I found this algorithm, when I run it it works without error but it doesn't perform the assembly action.
Here is the algorithm:
Code:
strCompName = swComponent.Name2() Debug.Print(" part is " & strCompName) swModel.ClearSelection2(True) swDocExt = swModel.Extension() MateName = "Concentric" + strCompName FirstSelection = "Point1@Origin@" + strCompName & "@" + AssemblyName SecondSelection = "Point1@Origin@" + AssemblyName boolstat = swDocExt.SelectByID2(FirstSelection, "CONCENTRIC", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault) boolstat = swDocExt.SelectByID2(SecondSelection, "CONCENTRIC", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault) matefeature = swAssemblyDoc.AddMate3(swMateType_e.swMateCONCENTRIC, swMateAlign_e.swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError) swModel.ClearSelection2(True) swModel.ViewZoomtofit2()in addition I tried to add MsgBox(mateError), and it gives me 4 as a result.
I'm waiting for your answers and thank you in advance.
Hello
First of all, you need to check that this macro selects the faces on which you want to apply the constraints. That is, check that the definitions of "FirstSelection" and "SecondSelection" work.
Then the constraint you add must match. In the given example, you are trying to apply a "coaxial" stress.
Hello and thank you for your answer,
Yes, the goal of my algorithm is to apply a coaxiality constraint between an added part and the set of parts.
For firstselection and secondselection, I think everything is fine now, I made a small revision:
FirstSelection="Point1@Origin" + strCompName & "@" + AssemblyName SecondSelection="Point1@Origin" + AssemblyName
But the problem remains the same, there are no constraints added to the parts.
On the other hand, I think the problem comes from these lines: (it's just a proposal while waiting for your help):
boolstat = swDocExt.SelectByID2(FirstSelection, "CONCENTRIC", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault) boolstat = swDocExt.SelectByID2(SecondSelection, "CONCENTRIC", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault) matefeature = swAssemblyDoc.AddMate3(swMateType_e.swMateCONCENTRIC, swMateAlign_e.swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError) swModel.ClearSelection2(True)
Hi
You cannot add a concentric mate between two points. so the mate type you should be trying to use is a Coincident. This likely to be your primary issue.
Also I think your SelectByID2 lines need some work
This page gives some detail, but I think yours lines should look more like:
boolstat = swDocExt.SelectByID2(FirstSelection, "", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)
Hello
In the code you got back, you use AddMate3 which is obsolete, replace it with AddMate5 (see here) and as said by Gauthik67 and David pay attention to your SelectByID2 function, the second value corresponds to the type of object you select, either empty or to be taken from swSelectType_e Enumeration, and not the type of constraint as you put. Also pay attention to the constraint you put in place, it must be consistent with your selections. You can find an example of what you want to do here.
Kind regards
Hello and thank you for your answers,
I made the changes as you asked me to make them:
for the SelectBylD2:
boolstat = swDocExt.SelectByID2(FirstSelection, "", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)
boolstat = swDocExt.SelectByID2(SecondSelection, "", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)
for the addMate5, it gives me this error message:
The 'AddMate5' public member of type 'AssemblyDoc' was not found
I think because I'm using VB.NET 2013, and I don't think it's a big problem because AddMAte3 is doing the bolo.
I tried to add this code to define where the problem is:
matefeature = swAssemblyDoc.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)
If matefeature Is Nothing Then
MsgBox("" mate failed! ")
End If
and it gives me back the message "mate failed", that means the matefeature function didn't work.
I am waiting for your help and thank you.
Hello
Are you sure that the selection of your elements goes well? (I think there is a missing "e" at the end of the origin word in the declaration of your FirstSelection and SecondSelection). Also replace the "" with "EXTSKETCHPOINT" in the 2 SelectByID2 lines.
If you want to constrain the origin of your part to the origin of your assembly then replace "swMateType_e.swMateCOINCIDENT" with "swMateType_e.swMateCOORDINATE" and "swMateAlign_e.swMateAlignALIGNED" with "-1" in the AddMate3 function.
For the AddMate5 function, which version of Solidworks are you on?
Here's an example code:
Dim FirstSelection As String
FirstSelection = "Point1@Origine@Pièce1-1@" & AssemblyName
Dim SecondSelection As String
SecondSelection = "Point1@Origine@" & AssemblyName
Part.ClearSelection2 True
boolstatus = swAssemblyDoc.Extension.SelectByID2(FirstSelection, "EXTSKETCHPOINT", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = swAssemblyDoc.Extension.SelectByID2(SecondSelection, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0)
Dim matefeature As Mate2
Set matefeature = swAssemblyDoc.AddMate3(swMateType_e.swMateCOORDINATE, -1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)
Part.ClearSelection2 True
Part.EditRebuild3
Kind regards
Hello and thank you for your answer,
I have the 2013 version of Solidworks.
For "Point1@Origine@", in the SolidWorks Help, origin is written without e.
I tested your version of code, but I didn't get any results.
pff this code is driving me crazy, I'm almost 1 month old that I'm stuck '(
Hello
To the remark " for "Point1@Origine@", in the solidworks help, origin is written without e. " I would tend to say that this is normal since the Solidworks help gives code examples for an English version of Solidworks so origin without e but if your solidworks is in french then origin takes an e, Look at how it is written in your building tree.
Kind regards
Also put MsgBox FirstSelection and MsgBox SecondSelection after their value assignments to check that they correspond to what you expect.
Hello and thank you very much for this remark,
I found "Origin" in the build tree.
and I made the MsgBoxes for
FirstSelection: " Point1@Origin@Key-1@Assembly "
SecondSelection: " Point1@Origin@Assembly "
I think they have given good results now.
it remains for me only to coincide these two points to assemble the two pieces.
Uh, a little check, your parts are in solved mode in Solidworks otherwise it doesn't work since references such as part origins are not loaded!!!
Kind regards
To put parts in code-resolved mode, see http://help.solidworks.com/2013/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IAssemblyDoc~SetComponentSuppression.html?verRedirect=1 and http://help.solidworks.com/2013/English/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swComponentSuppressionState_e.html.
Kind regards
Yes Mr. D.Roger, I changed the mode of the parts to solved by clicking on the assembly and changing the mode, now, I have managed to assemble the origin of the part at the origin of the assembly.
Thank you very much sir for your help and to all the forum members they participated in this topic.
Hello
Please, I have two quick questions:
1/ I want to assemble two pieces using the axes I created them.
I tried this code:
Code
FirstSelection ="Axe1@" + strCompName & "@" + AssemblyName
SecondSelection = "Axe2@" + strCompModelname2 & "@" + AssemblyName
'Axe1 and Axe2 are the names of the axes'
boolstat = swDocExt.SelectByID2(FirstSelection, "AXIS", 0, 0, 0, True, 1, Nothing, 0)
boolstat = swDocExt.SelectByID2(SecondSelection, "AXIS", 0, 0, 0, True, 1, Nothing, 0)
matefeature = swAssemblyDoc.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)
But, it showed me that the axis selection is incorrect.
2/ how I can select one face of the part to join two parts using the faces.
Hello
To find the right values to put in the "SelectByID2" and "AddMate3" lines you can use the Solidworks macro recorder (see here), then all you have to do is copy the lines in question and adapt the names of the elements according to your needs.
Kind regards