Hi everyone,
I’ve developed a Python script that automatically generates parts and assembles them in SolidWorks 2025. The part generation and assembly creation work fine, but I’m stuck when trying to add mates (coaxial/concentric and coincident).
In the new version, CreateMate()
should replace AddMate5()
, but I haven’t managed to get it working. I’ve tried different approaches — both with SelectByRay()
and SelectByID2()
— but the mate is never created.
Here are two code snippets I tested (shortened for readability):
Example 1:
boolstatus1 = Assembly.Extension.SelectByRay(0, 0, 0, 1, 0, 0, dextcorps, 2, False, 1, 0)
boolstatus2 = Assembly.Extension.SelectByRay(0, 0, 0, 1, 0, 0, dextesocle, 2, True, 2, 0)
if boolstatus1 and boolstatus2:
print("Selections successful")
MateData = Assembly.CreateMateData(1) # concentric
ent1 = Assembly.SelectionManager.GetSelectedObject6(1, -1)
ent2 = Assembly.SelectionManager.GetSelectedObject6(2, -1)
entities_array = win32com.client.VARIANT(
pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, (ent1, ent2)
)
MateData.EntitiesToMate = entities_array
MateFeature = Assembly.CreateMate(MateData)
Example 2:
boolstatus1 = Assembly.Extension.SelectByID2("Axis1@Corps", "AXIS", 0, 0, 0, True, 1, pythoncom.Nothing, 0)
boolstatus2 = Assembly.Extension.SelectByID2("Axis1@Socle", "AXIS", 0, 0, 0, True, 1, pythoncom.Nothing, 0)
if boolstatus1 and boolstatus2:
MateData = Assembly.CreateMateData(1) # concentric
selMgr = Assembly.SelectionManager
ent1 = selMgr.GetSelectedObject6(1, -1)
ent2 = selMgr.GetSelectedObject6(2, -1)
MateData.EntitiesToMate = [ent1, ent2]
MateFeature = Assembly.CreateMate(MateData)
Despite the selections returning True, the mate is never created.
Has anyone successfully created mates in SolidWorks 2025 using CreateMate() with Python? Any example code or workaround would be greatly appreciated.
Thanks in advance!