Files
Associated model(s) Assembly automation example - with transforms
Code
'Simplified version of API Fundamentals lesson 5 "AssemblyAutomation.swp"
'Uses transforms and NOT mates
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
'Preconditions: face of Assy.SLDASM is selected
'Postconditions: pin components are inserted in cylindrical holes
Option Explicit
Public swApp As SldWorks.SldWorks
Public swModel As SldWorks.ModelDoc2
Public swAssy As SldWorks.AssemblyDoc
Public swSelMgr As SldWorks.SelectionMgr
Public swFace As SldWorks.Face2
Public swSelFaceEnt As SldWorks.Entity
Public collCircEdges As New Collection
Public collCylFaces As New Collection
Public collCylSafeFaces As New Collection
Public vCircleParams As Variant
Public dblHoleParam(2) As Double
Public swBlockComp As SldWorks.Component2
Public i As Integer
Public errors As Long
Public warnings As Long
Public Const strCompPath As String = _
"C:CADSharpAssembly Automationpin.SLDPRT"
Sub GetFace()
Set swSelMgr = swModel.SelectionManager
If swSelMgr.GetSelectedObjectType3(1, -1) = swSelectType_e.swSelFACES Then
Set swFace = swSelMgr.GetSelectedObject6(1, -1)
Set swSelFaceEnt = swFace
Set swSelFaceEnt = swSelFaceEnt.GetSafeEntity
'GET THE BLOCK COMPONENT
Set swBlockComp = swSelMgr.GetSelectedObjectsComponent3(1, 0)
Else
swApp.SendMsgToUser2 "Hey ace, select a face.", swMbWarning, swMbOk
End
End If
End Sub
Sub GetCircularEdges()
Dim swLoop As SldWorks.Loop2
Dim swEdges As Variant
Dim swCurve As SldWorks.Curve
Set swFace = swSelFaceEnt
Set swLoop = swFace.GetFirstLoop
While Not swLoop Is Nothing
If Not swLoop.IsOuter Then
swEdges = swLoop.GetEdges
For i = 0 To UBound(swEdges)
Set swCurve = swEdges(i).GetCurve
If swCurve.IsCircle Then
'add to a circular face collection
collCircEdges.Add swEdges(i)
End If
Next i
End If
Set swLoop = swLoop.GetNext
Wend
End Sub
Sub GetCylFaces()
Dim vFaces As Variant
Dim swSurface1 As SldWorks.Surface
Dim swSurface2 As SldWorks.Surface
Dim swCylFaceEnt As SldWorks.Entity
For i = 1 To collCircEdges.Count
vFaces = collCircEdges.Item(i).GetTwoAdjacentFaces2
Set swSurface1 = vFaces(0).GetSurface
Set swSurface2 = vFaces(1).GetSurface
If swSurface1.IsCylinder Then
Set swCylFaceEnt = vFaces(0)
Set swCylFaceEnt = swCylFaceEnt.GetSafeEntity
collCylFaces.Add swCylFaceEnt
ElseIf swSurface2.IsCylinder Then
Set swCylFaceEnt = vFaces(1)
Set swCylFaceEnt = swCylFaceEnt.GetSafeEntity
collCylFaces.Add swCylFaceEnt
End If
Next i
End Sub
'this needs to be present because AddComponent5 requires a loaded document to add
Sub OpenCompModel()
swApp.DocumentVisible False, swDocPART
swApp.OpenDoc6 strCompPath, 1, 0, "", errors, warnings
End Sub
Sub AddComponents()
Dim swComp As SldWorks.Component2
Dim strCompName As String
Dim SelData As SldWorks.SelectData
Dim strings As Variant
Dim strAssyName As String
strings = Split(swModel.GetTitle, ".")
strAssyName = strings(0)
'transforms
Dim swMathUtil As SldWorks.MathUtility
Dim swMathPoint As SldWorks.MathPoint
Dim swPinTransform As SldWorks.MathTransform
'1. Add the pin
Set swComp = swAssy.AddComponent4(strCompPath, "", 0, 0, 0)
'2. Rotate the pin so that it is aligned with the block
swComp.Transform2 = swBlockComp.Transform2
'3. Move the pin to the center of the hole
Set swMathUtil = swApp.GetMathUtility
For i = 1 To collCircEdges.Count
vCircleParams = collCircEdges(i).CircleParams
dblHoleParam(0) = vCircleParams(0)
dblHoleParam(1) = vCircleParams(1)
dblHoleParam(2) = vCircleParams(2)
Set swMathPoint = swMathUtil.CreatePoint(dblHoleParam)
Set swMathPoint = swMathPoint.MultiplyTransform(swCompTransform)
Next i
End Sub
Sub Finalize()
Set collCircEdges = Nothing
swApp.DocumentVisible True, swDocPART
swModel.ClearSelection2 True
End Sub
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swAssy = swModel
Call GetFace 'get selected face as a safe entity
swAssy.EditAssembly 'make sure that user isn't in edit part mode, which can mess things up later on
Call GetCircularEdges 'search face for loops and see if they are circular
Call GetCylFaces 'if yes, then find cylindrical faces
Call OpenCompModel 'open the component model that will be added to the assy
Call AddComponents 'bring in pins and place/align them using transforms
Call Finalize 'garbage collection, revert settings, etc
End Sub
'NOTES
'If you get a pointer to an entity and then add another component or the current comp _
undergoes a geometrical change, you will lose the pointer. Prevent this by storing the _
entity as a safe entity.
'The AddMate3 argument that controls alignment doesn't cause any change if moved from _
aligned to anti-aligned. Apparently you would need to add the component using _
"swMateAlignCLOSEST" and then run some interference detection, then modify the _
definition of that mate to reverse mate alignment