SolidWorks API - Add mates by edge name
SolidWorks API - Add mates by edge name
(OP)
Hey everyone, this may be hard to explain but I'll try my best.
I'm trying to select an edge of a part inside an assembly automatically by a macro. I've defined an "Edge Entity Name" (RMB on edge > Edge Properties > Entity Name) as "MateEdge" on the bottom of "Base-Revolve" feature of the part file. My problem is I haven't had any luck calling it with the SelectByID2 method:
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("MateEdge@Base-Revolve@NHT-SCHS-188SS-0.750-16-UNF(dot)SLDPRT", "EDGE", 0, 0, 0, True, 0, Nothing, 0)
(After stepping through - boolstatus = false)
Am I defining the name correct? Is the syntax "Entity Name@Feature Name@Part Name" ? Do I need to include the configuration name also? The SolidWorks API help well... wasn't much help.
Any help would be greatly appreciated!!
I'm trying to select an edge of a part inside an assembly automatically by a macro. I've defined an "Edge Entity Name" (RMB on edge > Edge Properties > Entity Name) as "MateEdge" on the bottom of "Base-Revolve" feature of the part file. My problem is I haven't had any luck calling it with the SelectByID2 method:
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("MateEdge@Base-Revolve@NHT-SCHS-188SS-0.750-16-UNF(dot)SLDPRT", "EDGE", 0, 0, 0, True, 0, Nothing, 0)
(After stepping through - boolstatus = false)
Am I defining the name correct? Is the syntax "Entity Name@Feature Name@Part Name" ? Do I need to include the configuration name also? The SolidWorks API help well... wasn't much help.
Any help would be greatly appreciated!!






RE: SolidWorks API - Add mates by edge name
Set Part = swApp.ActiveDoc
will set Part to the assembly object. You could try to get the part from the assembly and then the edge from the part. I expect that once Part is set to the part, the string for the SelectByID2 call would be simply "MateEdge".
Eric
RE: SolidWorks API - Add mates by edge name
The Name argument is not intended for selection of faces, edges, and so on. This is a case-sensitive string for objects that are automatically named by SolidWorks during entity creation, such as dimensions, drawing views, and so on. This method tries to find and select an object whose name matches the Name argument; however, the match needs to be exact for this method to return True.
SolidWorks assemblies are not made up of parts, so trying to point to some portion of a .sldprt will not work. They are made of Components. Components may be defined by parts or assemblies. You can't select any entity of a part in the context of an assembly. You can only select entities of Components. This is because multiple identical components may be defined by the same part.
I don't have SW at home right now (just web access to the API help), but I believe the way you would need to do this would be:
1. Get a pointer to the Component2 object in the assembly for the component whose MateEdge you want to find. You will need some way to specify which component you are interested in, perhaps using GetSelectedObjectsComponent?
2. Get a pointer to the ModelDoc2 object of the part file that defines the component.
3. Use the GetEntityByName method of the ModelDoc2 object of the part file to get a pointer to the Entity object for the edge.
4. Use the GetCorrespondingEntity method of the Component2 object in the assembly to get the edge of the component (which is not the same thing as the edge of the part) corresponding to the edge you are interested in from the part.
-handleman, CSWP (The new, easy test)
RE: SolidWorks API - Add mates by edge name
Thanks for the input, I'm on the right track. I now have a pointer to the body that the edge exists on. I can't seem to understand how to implement the GetEdges() method but the GetEdgeCount() works and returns a value...
-----------------------------------------------------------------
Sub main()
Dim swApp As Object
Dim AssyDoc As Object
Dim SelMgr As Object
Dim Comp As Object
Dim Body As Object
Dim swEdge As Object
Dim EdgeCount As Integer
Dim CurEdgeName, CompName, EdgeName As String
Const swSelCOMPONENTS = 20
CompName = "NHT-SHCS-188SS-0.750-16-UNF-1@1420-030 MAIN SHAFT SEAL ASSEMBLY"
EdgeName = "MateEdge"
Set swApp = CreateObject("SldWorks.Application")
Set AssyDoc = swApp.ActiveDoc
Set SelMgr = AssyDoc.SelectionManager
AssyDoc.SelectByID CompName, "COMPONENT", 0, 0, 0
Set Comp = SelMgr.GetSelectedObject6(1, -1)
Set Body = Comp.GetBody()
If (Body Is Nothing) Then
swApp.SendMsgToUser "Component Body Unavailable."
swApp.SendMsgToUser "Make sure not lightweight or suppressed"
Exit Sub
End If
EdgeCount = Body.GetEdgeCount
Set swEdge = Body.GetEdges()
Do While Not swEdge Is Nothing
CurEdgeName = AssyDoc.GetEntityName(swEdge)
If (CurEdgeName = EdgeName) Then
swEdge.Select (0)
Exit Do
End If
'MsgBox CurEdgeName
Loop
End Sub
----------------------------------------------------------------
While stepping through, EdgeCount returns "69" then breaks at Set swEdge = Body.GetEdges() with a "Object Required" error.
Any ideas? How does GetEdges() work?
RE: SolidWorks API - Add mates by edge name
* SOLUTION *
'----------------------------------------
' Preconditions:
' (1) Assembly document (ASSYNAME) is open and has one or more components (PARTNAME)
' (2) One part has a defined edge entity (ENTNAME)
' Postconditions: Defined edge entity is highlighted.
'
'----------------------------------------
Option Explicit
Sub main()
Dim swApp As Object
Dim AssyDoc As Object
Dim SelMgr As Object
Dim Comp As Object
Dim Body As Object
Dim Edge As SldWorks.Edge
Dim vobj As Variant
Dim swEdge As Variant
Dim EdgeCount As Integer
Dim CurEdgeName, CompName, EdgeName As String
Const swSelCOMPONENTS = 20
CompName = "PARTNAME-1@ASSYNAME" 'Component name that contains the edge entity (remember to include the instance number of that component, hence the -1)
EdgeName = "ENTNAME" 'Defined edge entity name in the component
Set swApp = CreateObject("SldWorks.Application")
Set AssyDoc = swApp.ActiveDoc()
Set SelMgr = AssyDoc.SelectionManager()
AssyDoc.SelectByID CompName, "COMPONENT", 0, 0, 0
Set Comp = SelMgr.GetSelectedObject6(1, -1)
If (SelMgr.GetSelectedObjectType3(1, -1) <> swSelCOMPONENTS) Then
MsgBox "Please Select an Assembly Component.", vbOKOnly + vbCritical
Exit Sub
End If
Set Body = Comp.GetBody()
If (Body Is Nothing) Then
MsgBox "Component Body Unavailable. Make sure the model is not lightweight or suppressed.", vbOKOnly + vbCritical
Exit Sub
End If
EdgeCount = Body.GetEdgeCount
swEdge = Body.GetEdges 'stores all edge names (NOTHING if no edge name is defined)
For Each vobj In swEdge
Set Edge = vobj
CurEdgeName = AssyDoc.GetEntityName(Edge)
If (CurEdgeName = EdgeName) Then
Edge.Select (0) ' Selects the edge
Exit For
End If
Next
End Sub
A very useful code snippet to automate adding concentric mates!
RE: SolidWorks API - Add mates by edge name
Dim swAssy As SldWorks.ModelDoc2
Dim swCompDoc As SldWorks.ModelDoc2
Dim swCompPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim myNamedEnt As SldWorks.Entity
Dim myCompEnt As SldWorks.Entity
Const FINDEDGENAME As String = "MyOtherEdge"
Sub main()
Set swApp = Application.SldWorks
Set swAssy = swApp.ActiveDoc
If swAssy.GetType <> swDocASSEMBLY Then
MsgBox "This macro works in assemblies only"
Exit Sub
End If
Set swSelMgr = swAssy.SelectionManager
Set swComp = swSelMgr.GetSelectedObjectsComponent3(swSelMgr.GetSelectedObjectCount2(-1), -1)
If swComp Is Nothing Then
MsgBox "Component not selected."
Exit Sub
End If
Set swCompDoc = swComp.GetModelDoc2
If Not swCompDoc.GetType = swDocPART Then
MsgBox "Selection is not a component defined by a part document."
Exit Sub
End If
Set swCompPart = swComp.GetModelDoc2
Set myNamedEnt = swCompPart.GetEntityByName(FINDEDGENAME, swSelEDGES)
If myNamedEnt Is Nothing Then
swCompDoc.ShowConfiguration2 swComp.ReferencedConfiguration
Set myNamedEnt = swCompPart.GetEntityByName(FINDEDGENAME, swSelEDGES)
If myNamedEnt Is Nothing Then
MsgBox "Unable to find edge named" & vbCrLf & """" & FINDEDGENAME & """" & vbCrLf & "in part model " & vbCrLf & swCompPart.GetTitle
Exit Sub
End If
End If
Set myCompEnt = swComp.GetCorrespondingEntity(myNamedEnt)
If myCompEnt Is Nothing Then
MsgBox "Unable to find edge named" & vbCrLf & """" & FINDEDGENAME & """" & vbCrLf & "on component " & vbCrLf & swComp.Name2
Else
swSelMgr.DeSelect2 swSelMgr.GetSelectedObjectCount2(-1), -1
myCompEnt.Select4 True, Nothing
End If
End Sub
-handleman, CSWP (The new, easy test)
RE: SolidWorks API - Add mates by edge name
-handleman, CSWP (The new, easy test)