MACRO LIBRARY

Create linear pattern in two directions

Free public access. No account or subscription required.

Files

Create linear pattern in two directions

Code

Module: mMain

'Demonstrates how to create a linear pattern in two directions
    
'Preconditions: blank part is open
'Results: linear pattern is added in two directions
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSketchMgr As SldWorks.SketchManager
Dim swFeatMgr As SldWorks.FeatureManager
    

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSketchMgr = swModel.SketchManager
    Set swFeatMgr = swModel.FeatureManager
    
    'create plane and sketch profile
    swModel.Extension.SelectByID2 "FRONT PLANE", Plane, 0, 0, 0, False, 0, Nothing, 0
    swSketchMgr.InsertSketch True
    swSketchMgr.CreateCenterRectangle 0, 0, 0, 0.05, 0.05, 0
    swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", 0, 0, 0, False, 6, Nothing, 0
    swSketchMgr.FullyDefineSketch True, True, 1023, True, 1, Nothing, 1, Nothing, 0, 0
    
    'create mid-plane extrusion
    swFeatMgr.FeatureExtrusion2 True, False, False, 6, 0, 0.01, 0, False, False, False, _
          False, 0, 0, False, False, False, False, True, True, True, 0, 0, False
          
    'create extrude cut profile
    swModel.Extension.SelectByID2 "FRONT PLANE", Plane, 0, 0, 0, False, 0, Nothing, 0
    swSketchMgr.InsertSketch True
    swSketchMgr.CreateCircleByRadius -0.04, 0.04, 0, 0.004
    swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", 0, 0, 0, False, 6, Nothing, 0
    swSketchMgr.FullyDefineSketch True, True, 1023, True, 1, Nothing, 1, Nothing, 0, 0
    
    
    'create cut extrude
    swFeatMgr.FeatureCut3 True, False, False, 1, 0, 0.01, 0.01, False, False, False, _
           False, 0, 0, False, False, False, False, False, True, True, True, _
           True, False, 0, 0, False
    
    'create linear pattern of extrude cut
    swModel.Extension.SelectByID2 "Cut-Extrude1", "BODYFEATURE", 0, 0, 0, False, 4, Nothing, 0
    
    'for first axis, use edge with these vertices:
    '-0.05, 0.05, 0.005
    '0.05, 0.05, 0.005
    
    Dim dblPt1(2) As Double
    Dim dblPt2(2) As Double
        
    dblPt1(0) = -0.05
    dblPt1(1) = 0.05
    dblPt1(2) = 0.005
    
    dblPt2(0) = 0.05
    dblPt2(1) = 0.05
    dblPt2(2) = 0.005
    
    SelectEdgeForDirectionAxis 1, dblPt1(0), dblPt1(1), dblPt1(2), dblPt2(0), dblPt2(1), dblPt2(2)
    
    'for second axis, use edge with these vertices:
    '-0.05, 0.05, 0.005
    '-0.05, -0.05, 0.005
    
    dblPt1(0) = -0.05
    dblPt1(1) = 0.05
    dblPt1(2) = 0.005
    
    dblPt2(0) = -0.05
    dblPt2(1) = -0.05
    dblPt2(2) = 0.005
    
    SelectEdgeForDirectionAxis 2, dblPt1(0), dblPt1(1), dblPt1(2), dblPt2(0), dblPt2(1), dblPt2(2)
    
    swFeatMgr.FeatureLinearPattern3 5, 0.02, 5, 0.02, True, False, Empty, Empty, False, False
    
    swModel.EditRebuild3
End Sub
    
Sub SelectEdgeForDirectionAxis(intMark As Integer, dblPt1X As Double, dblPt1Y As Double, dblPt1Z As Double, dblPt2X As Double, dblPt2Y As Double, dblPt2Z As Double)
    Dim swPart As SldWorks.PartDoc
    Dim vBodies As Variant
    Dim swBody As SldWorks.Body2
    Dim vEdges As Variant
    Dim i As Integer
    Dim swEdge As SldWorks.Edge
    Dim swVertex1 As SldWorks.Vertex
    Dim swVertex2 As SldWorks.Vertex
    Dim vPt1 As Variant
    Dim vPt2 As Variant
    Dim swEnt As SldWorks.Entity
    
    Set swPart = swModel
    vBodies = swPart.GetBodies2(swSolidBody, False)
    Set swBody = vBodies(0) 'assume only one body in part
    vEdges = swBody.GetEdges
    
    For i = 0 To UBound(vEdges)
        Set swEdge = vEdges(i)
        
        Set swVertex1 = swEdge.GetStartVertex
        Set swVertex2 = swEdge.GetEndVertex
        
        If Not swVertex1 Is Nothing And Not swVertex2 Is Nothing Then
            vPt1 = swVertex1.GetPoint
            vPt2 = swVertex2.GetPoint
            
            'Since we don't know which vertex is considered the start vertex and which is considered _
                the end vertex, we must do comparison under both scenarios.
            If (dblPt1X = vPt1(0) And dblPt1Y = vPt1(1) And dblPt1Z = vPt1(2) And dblPt2X = vPt2(0) And dblPt2Y = vPt2(1) And dblPt2Z = vPt2(2)) Or _
                (dblPt2X = vPt1(0) And dblPt2Y = vPt1(1) And dblPt2Z = vPt1(2) And dblPt1X = vPt2(0) And dblPt1Y = vPt2(1) And dblPt1Z = vPt2(2)) Then
                Set swEnt = swEdge
                swEnt.Select2 True, intMark
            End If
        End If
    Next i
End Sub