Hi all,
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,
Filipe Lazzarini
Hi all,
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,
Filipe Lazzarini
Hello.
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)
Hello,
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 Explicit
Dim swApp As SldWorks.SldWorks
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModel As SldWorks.ModelDoc2
Dim swComp As SldWorks.Component2
Dim swFace As SldWorks.Face2
Dim boolstatus As Long
Public 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.GetNextFace
Loop
' Subsequent code might select a second face,
' edge, or feature, and then mate the two
' items using AssemblyDoc::AddMate5
End Sub
Sub 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 faceName
End Sub
Problem solved with this tutorial
2018 SOLIDWORKS API Help - Select Component Face By Name Example (VBA)
Thanks ALL
Here are some handy macros that do the job:
What is the meaning of swMateAlignCLOSEST
I have used these since Josh Brady shared them!
Have a look at IAssemblyDoc::CreateMate Method.