Files
Associated model(s) Assembly automation example - without transforms
Code
'Simplified version of API Fundamentals lesson 5 "AssemblyAutomation.swp"
'Does not use Transforms or forms
'Please download the model files from www.cadsharp.com
'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 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
Else
swApp.SendMsgToUser2 "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)
For i = 1 To collCircEdges.Count
Set swComp = swAssy.AddComponent4(strCompPath, "", 0, 0, 0)
strCompName = swComp.Name2
swSelFaceEnt.Select4 False, SelData
swModel.Extension.SelectByID2 _
"Front Plane@" & strCompName & "@" & strAssyName, "PLANE", 0, 0, 0, True, 1, Nothing, 0
swAssy.AddMate3 _
swMateCOINCIDENT, swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, errors
'make the origin of the component concentric with the cylindrical face (won't work with circular edge)
collCylFaces.Item(i).Select4 False, SelData
swModel.Extension.SelectByID2 _
"Point1@Origin" & "@" & strCompName & "@" & strAssyName, "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0
swAssy.AddMate3 _
swMateCONCENTRIC, swMateAlignCLOSEST, False, 0, 0, 0, 0, 0, 0, 0, 0, False, errors
Next i
End Sub
Sub Finalize()
Set collCircEdges = Nothing
Set collCylFaces = 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 mate them
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