1

I am writing a Python script using the Solidworks 2022 API (via COM). I am trying to call the SelectByID2 method to select both a named edge and a named face for use in the InsertMoldPartingLine method. The InsertMoldPartingLine method requires the direction of pull and edge to be selected via SelectByID2 as specified in the API documentation (InsertMoldPartingLine Method (IFeatureManager) - 2022 - SOLIDWORKS API Help). I’m having two issues:

  1. I can’t seem to select named edges or faces using SelectByID2. I won’t be able to use coordinates for selecting edges/ faces because this script will be used for varying geometries. a. I’ve tried naming edges manually in Solidworks and using the SetEntityName API method, neither work when I try to call them in SelectByID2. I’ve also tried using the full naming convention when calling SelectByID2 (i.e. Edge@Revolve1@[part_file_name]), this also didn’t work.
  2. I can’t get the InsertMoldPartingLine method to work, even when successfully selecting edges/faces using coordinates. I may be selecting the wrong feature to specify the direction of pull but using a face works when I recorded a macro. Here is my code:
import win32com.client
import pythoncom

sw = win32com.client.Dispatch("SldWorks.Application")
revNum = sw.RevisionNumber # 30.2.0
sw.CloseDoc("")
argNone = win32com.client.VARIANT(pythoncom.VT_DISPATCH, None)
partPath = r"C:\\Users\\Documents\\part_file.SLDPRT"
sw.OpenDoc(partPath,1)
model = sw.ActiveDoc
modelExt = model.Extension
selMgr = model.SelectionManager
featureMgr = model.FeatureManager
sketchMgr = model.SketchManager
modelView = model.ActiveView
docName = model.GetTitle

### SOLID BODIES

solidBodyFolder = model.FeatureByName('Solid Bodies')
solidBodies = solidBodyFolder.GetSpecificFeature2
body = solidBodies.GetBodies\[0\] # IBody2 class
faces = body.GetFaces() # array of IFace2 objects
edges = body.GetEdges() # array of IEdge objects
moldEdge = edges\[0\]
\[model.SetEntityName(face,'Face'+str(i)) for i,face in enumerate(faces)\]

### PARTING LINE

model.ClearSelection2(True)
print(modelExt.SelectByID2("Face0","FACE",0,0,0,False,1,argNone,0))
print(modelExt.SelectByID2("Mold Edge","EDGE",0,0,0,False,4,argNone,0))
featureMgr.InsertMoldPartingLine(False)

Troubleshooting detailed above. Expected to see a "True" printed when attempting to select face/edge by name. Expected a mold parting line to be inserted into the Solidworks feature tree.

Any help is greatly appreciated!

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Mark
  • 11
  • 1

0 Answers0