MACRO LIBRARY

Create a circular pattern feature

Free public access. No account or subscription required.

Files

Associated model(s) Create a circular pattern feature

Code

'This macro automatically creates a axis and then patterns a feature about the axis
'Preconditions: circular pattern.sldprt is open and active
'Postconditions: circular pattern is created
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
    
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim vBodies As Variant
Dim swBody As SldWorks.Body2
Dim vFaces As Variant
Dim swFace As SldWorks.Face2
Dim swSurface As SldWorks.Surface
Dim swEnt As Entity
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelData As SldWorks.SelectData
Dim swFeat As SldWorks.Feature
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    Set swPart = swDoc
    Set swSelMgr = swDoc.SelectionManager
    Set swSelData = swSelMgr.CreateSelectData
    
    'Find and select circular face in order to create axis
    vBodies = swPart.GetBodies2(SwConst.swBodyType_e.swSolidBody, True)
    Set swBody = vBodies(0)
    vFaces = swBody.GetFaces
    For i = 0 To UBound(vFaces)
        Set swFace = vFaces(i)
        Set swSurface = swFace.GetSurface
        If swSurface.IsCylinder Then
            Set swEnt = swFace
            swEnt.Select4 False, swSelData
        End If
    Next i
    swDoc.InsertAxis2 False
    
    'Find and select the feature to be patterned
    Set swFeat = swDoc.FirstFeature
    While Not swFeat Is Nothing
        If swFeat.GetTypeName2 = "ICE" Then     'ICE because of the way boss was created
            swFeat.Select2 False, 4     'must be marked as 4
        End If
        Set swFeat = swFeat.GetNextFeature
    Wend
    
    'Find and select the pattern axis
    Set swFeat = swDoc.FirstFeature
    While Not swFeat Is Nothing
        If swFeat.GetTypeName2 = "RefAxis" Then
            swFeat.Select2 True, 1      'must be marked as 1
        End If
        Set swFeat = swFeat.GetNextFeature
    Wend
    
    swDoc.FeatureManager.FeatureCircularPattern2 19, 2 * 3.14159 / 19, False, "NULL", False
End Sub
    
'IFeatureManager::FeatureCircularPattern2 requires that the feature(s) be selected first _
and marked as 4, and the axis selected second and marked as 1.