Hello! I want to automate smart feature insertion by extracting faces for smart feature from mates data.
I have part "box" (smart component) with face "box_face" and part "block".
They are mated coincident by box_face<->block_someFace.
I want to run macro, to find face that mated with box_face and later apply smart feature to that face.
Pseudo code:
Dim targetFace as Face
for i=1 to box.mates.count
mateInfo=box.mates(i)
if (mateInfo.face(1)='box_face') then 'if first face is our marker face
targetFace=mateInfo.face(2); 'then we found it, store second face
end if
next i
box.insertSmartFeature(targetFace)
---------------------
Now i m stuck at getting the face info from mates.
... got all smart part mates in Mates array
For Each SingleMate In Mates
Dim swMate As SldWorks.Mate2
Dim typeOfMate As Long
If TypeOf SingleMate Is SldWorks.Mate2 Then
Set swMate = SingleMate
typeOfMate = swMate.Type
If typeOfMate = 0 Then
Debug.Print "Mate type: Coincident"
Dim mateEntityCount As Integer
Dim meI As Integer
Dim ents As SldWorks.MateEntity2
Dim zzzz As Variant
Dim zzzz2 As Variant
mateEntityCount = swMate.GetMateEntityCount
For meI = 0 To count - 1
Set ents= swMate.MateEntity(meI)
Set zzzz = ents.ReferenceComponent
Set zzzz2 = ents.Reference
'zzzz and zzzz2 will contain Component, not Face. How can i get faces for current swMate??
Next meI
End If
End If
Next
Thanks!
SolidworksApi macros
What does 'ents' contain?
Dim ents As SldWorks.MateEntity2
help.solidworks.com/2015/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMateEntity2.html
The following tables list the members exposed by IMateEntity2.
Public Properties [
Property EntityParams Gets the parameters for this mate entity.
Property Reference Gets the mate reference for this mate entity.
Property ReferenceComponent Gets the reference component for this mate entity.
Property ReferenceType Obsolete. Superseded by IMate2Entity::Reference2.
Property ReferenceType2 Gets the mate entity reference type for this mate entity.
]
Public Methods [
Method GetEntityParamsSize Gets the number of parameters for this mate entity.
Method IGetEntityParams Gets the parameters of this mate entity.
]
Reference and ReferenceComponent both return component.
Params contain some(?) component point
This seems to work for me. Notice the comments in the SelectMateEntity method. Reference IS returning the face.
'---------------------------------------------------------------------------
' Preconditions:
' 1. Open an assembly with a mate.
' 2. Select a mate.
'
' Postconditions: Selected assembly mate references are selected.
'
'----------------------------------------------------------------------------
Option Explicit
Function SelectMateEntity(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, swMateEnt As SldWorks.MateEntity2, nMark As Long) As Boolean
Dim swEnt As SldWorks.Entity
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim bRet As Boolean
Select Case swMateEnt.ReferenceType
Case swMateEntity2ReferenceType_Point, swMateEntity2ReferenceType_Line, swMateEntity2ReferenceType_Circle, swMateEntity2ReferenceType_Plane, swMateEntity2ReferenceType_Cylinder, swMateEntity2ReferenceType_Sphere, swMateEntity2ReferenceType_Cone, swMateEntity2ReferenceType_SweptSurface
Set swSelMgr = swModel.SelectionManager
Set swSelData = swSelMgr.CreateSelectData
' this is returning the correct face for the entity
Set swEnt = swMateEnt.Reference
swSelData.Mark = nMark
' and this is selecting it
bRet = swEnt.Select4(True, swSelData)
SelectMateEntity = bRet
Exit Function
Case swMateEntity2ReferenceType_Set, swMateEntity2ReferenceType_MultipleSurface, swMateEntity2ReferenceType_GenSurface, swMateEntity2ReferenceType_Ellipse, swMateEntity2ReferenceType_GeneralCurve, swMateEntity2ReferenceType_UNKNOWN
Case Else
End Select
SelectMateEntity = False
End Function
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFeat As SldWorks.Feature
Dim swMate As SldWorks.Mate2
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim sVarType As String
Dim nVarFactor As Double
Dim nMateDist As Double
Dim nNumMateEnt As Long
Dim swMateEnt() As SldWorks.MateEntity2
Dim vMateEntPar As Variant
Dim swComp As SldWorks.Component2
Dim nRetVal As Long
Dim i As Long
Dim bRet As Boolean
Dim vDimValueArr As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Set swSelMgr = swModel.SelectionManager
Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
Set swMate = swFeat.GetSpecificFeature2
Set swDispDim = swMate.DisplayDimension2(0)
Debug.Print "File = " & swModel.GetPathName
Debug.Print " " & swFeat.Name
Debug.Print " Type = " & swMate.Type
Debug.Print " Alignment = " & swMate.Alignment
Debug.Print " CanBeFlipped = " & swMate.CanBeFlipped
nNumMateEnt = swMate.GetMateEntityCount
ReDim swMateEnt(nNumMateEnt)
For i = 0 To nNumMateEnt - 1
Set swMateEnt(i) = swMate.MateEntity(i)
Set swComp = swMateEnt(i).ReferenceComponent
vMateEntPar = swMateEnt(i).EntityParams
Debug.Print " RefType(" & i & ") = " & swMateEnt(i).ReferenceType
Debug.Print " Component = " & swComp.Name2 & " (" & swComp.ReferencedConfiguration & ") --> " & swComp.GetPathName
Debug.Print " Point = (" & vMateEntPar(0) * 1000# & ", " & vMateEntPar(1) * 1000# & ", " & vMateEntPar(2) * 1000# & ") mm"
Debug.Print " Vector = (" & vMateEntPar(3) & ", " & vMateEntPar(4) & ", " & vMateEntPar(5) & ")"
Debug.Print " Radius 1 = " & vMateEntPar(6) * 1000# & " mm"
Debug.Print " Radius 2 = " & vMateEntPar(7) * 1000# & " mm"
Next i
swModel.ClearSelection2 True
For i = 0 To nNumMateEnt - 1
bRet = SelectMateEntity(swApp, swModel, swMateEnt(i), 1)
Next i
End Sub
Thanks a lot! It works!
But now i'm stuck with the next step =)
i need to identify exact face, can i name part face somehow? via gui? some "face properties" dialog. Can't find it.
my part has few mates:
mates array {
entity1: part_someface1 <=> otherPartFace1
entity2: part_someface2 <=> otherPartFace2
entity3: part_box_face <=> otherPartFace4 '<--------- here, named "box_face" face, so we can identify this
}
i have tried "SetEntityName" in part doc
and read using "GetEntityName" in assembly. But it returns empty strings. Looks like SetEntityName does not save information in file, just in macro memory.
code from here https://forum.solidworks.com/thread/67809
Found your message here https://forum.solidworks.com/thread/85961
it says EntityName is persistent between saves and on mating it automatically must assign EntityName.
Maybe i use wrong code to read EntityName for mateEntity?
Set mateEntity = swMate.mateEntity(meI)
Dim rComp As SldWorks.Component2
Dim planeFace As Object
Dim swPart As SldWorks.PartDoc
Dim swEnt As SldWorks.MateReference
Set planeFace = mateEntity.Reference
Set rComp = mateEntity.ReferenceComponent
Debug.Print rComp.Name
Dim swRefPart As SldWorks.PartDoc
Set swRefPart = rComp.GetModelDoc2
Set planeFace = swRefPart.Extension.GetCorresponding(planeFace)
Debug.Print "name=" & swRefPart.GetEntityName(planeFace)