I' m new user of SolidWorks API, and I'm tring to automate a modeling, and I'm need to create mates in the parts. But I'm having some issues on that, how to select faces and creat mates by API ?
Thanks and regards,
Have a look at IAssemblyDoc::CreateMate Method.
Look into AddMate5 to add mates.
To select faces, there are different methods depending of your application
Like : 2017 SOLIDWORKS API Help - Select Component Face By Name Example (VBA)
Thanks, that help me a lot., but I still having some problems when I try to select a face just by name, without the position. If I try to creat a cube with a side named "FaceTest", and try to select by api, it returns error.
Option ExplicitDim swApp As SldWorks.SldWorksDim swSelMgr As SldWorks.SelectionMgrDim swModel As SldWorks.ModelDoc2Dim swComp As SldWorks.Component2Dim swFace As SldWorks.Face2Dim boolstatus As LongPublic Sub SelectComponentFaceByName(faceName As String) Dim swBody As SldWorks.Body2 Dim swSelData As SldWorks.SelectData Dim currentFaceName As String Set swSelData = swSelMgr.CreateSelectData ' Get the component body Set swBody = swComp.GetBody() If (swBody Is Nothing) Then swApp.SendMsgToUser "Component body unavailable." swApp.SendMsgToUser "Make sure component is not lightweight or suppressed." Exit Sub End If Debug.Print "Traversing faces on component's body..." Set swFace = swBody.GetFirstFace Do While Not swFace Is Nothing currentFaceName = swModel.GetEntityName(swFace) If (currentFaceName = faceName) Then ' Select the face swFace.Select4 False, swSelData Debug.Print " Name of currently selected face; should match name of previously selected face: " & currentFaceName Exit Do End If Set swFace = swFace.GetNextFaceLoop' Subsequent code might select a second face,' edge, or feature, and then mate the two' items using AssemblyDoc::AddMate5End SubSub main() Dim swModelDocExt As SldWorks.ModelDocExtension Dim componentName As String Dim faceName As String Set swApp = CreateObject("SldWorks.Application") ' Get active assembly document Set swModel = swApp.ActiveDoc() Set swModelDocExt = swModel.Extension Set swSelMgr = swModel.SelectionManager ' Select a component and get its name boolstatus = swModelDocExt.SelectByID2("bench_quad-1@teste_projeto", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0) Set swComp = swSelMgr.GetSelectedObject6(1, -1) componentName = swComp.Name2 Debug.Print "Name of selected component: " & componentName swModel.ClearSelection2 True ' Select a face on the component and ' name it voadores@B-1 boolstatus = swModelDocExt.SelectByID2("Facetest@bench_quad-1", "FACE", 0, 0, 0, False, 0, Nothing, 0) Set swFace = swSelMgr.GetSelectedObject6(1, -1) boolstatus = swModel.SelectedFaceProperties(0, 0, 0, 0, 0, 0, 0, True, " ") faceName = swModel.GetEntityName(swFace) Debug.Print "Name of selected face on selected component: " & faceName SelectComponentFaceByName faceNameEnd Sub
Problem solved with this tutorial
2018 SOLIDWORKS API Help - Select Component Face By Name Example (VBA)
Here are some handy macros that do the job:
What is the meaning of swMateAlignCLOSEST
I have used these since Josh Brady shared them!
Retrieving data ...