Casey Balderson

API: Thin Sweep using Circular Profile and simulating using SelectionManager Example

Discussion created by Casey Balderson on Jan 17, 2020

I have struggled with using the "SelectionManager" (not IModelDoc2::ISelectionManager from the API) feature of Solidworks through the API, and it's been hard to research considering SolidWorks named the feature the same as the interface. I have also struggled with the Sweep-Thin using a Circular Profile for the Sweep feature. both ideas are relatively new to SolidWorks, but are very powerful, especially in my profession of round tubular bending.

Here's a VBA macro that exposes both functions with some info on what's going on.

You can use the attached "Sweep-Thin.SLDPRT" to make your selections from different sketches.

' creates a basic Sweep-Thin feature using "Circular Profile"
' through selected path of sketch lines of uncommon sketches.
' Preconditions:
' Sequentially select a series of lines for sweep path.
' Change "ThinSweep" values as necessary.
'
' Postconditions:
' Sweep/Sweep-Thin Feature using "Open Group" and Circular Profile.

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim vSkLines As Variant
Dim swSkSeg As SketchSegment
Dim j As Integer
Dim b As Boolean

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then End
    If swModel.GetType <> swconst.swDocumentTypes_e.swDocPART Then
        MsgBox "Run in part only."
        End
    End If
    Set swSelMgr = swModel.SelectionManager
    j = swSelMgr.GetSelectedObjectCount2(-1)
    If j = 0 Then
        MsgBox "Please select a series of lines for sweep."
        End
    End If
    ReDim vSkLines(j - 1) As Variant
    For i = 0 To j - 1
        If swSelMgr.GetSelectedObjectType3((i + 1), -1) <> swSelEXTSKETCHSEGS Then
            MsgBox "Non-SketchSegment selected." & vbCrLf & "Macro will End"
            End
        End If
        Set vSkLines(i) = swSelMgr.GetSelectedObject6((i + 1), -1)
    Next i
    swModel.ClearSelection2 (True)
    For i = 0 To j - 1
        swModel.Extension.SelectByID2 LineName(vSkLines(i)), "SKETCHSEGMENT", 0, 0, 0, True, 4, Nothing, 0
    Next i
   
    'Store selected entities as a selected Object Group for Path.
    'Mark as "4" to identify as path for sweep
    swModel.Extension.SelectByID2 "Unknown", "SELOBJGROUP", 0, 0, 0, False, 4, Nothing, 0
    b = ThinSweep(0.006, 0.001, True)
    If (b = False) Then MsgBox ("Feature not created")
End Sub

Function LineName(ByVal line As SketchLine) As String
    Set swSkSeg = line
    Dim swSketch As Sketch
    Set swSketch = swSkSeg.GetSketch
    Dim swFeat As Feature
    Set swFeat = swSketch
    Dim strname As String
    strname = (swSkSeg.GetName & "@" & swFeat.Name)
    LineName = strname
End Function

Function ThinSweep(ByVal diameter As Double, ByVal thickness As Double, ByVal merge As Boolean) As Boolean
    Dim swFeatMgr As FeatureManager
    Set swFeatMgr = swModel.FeatureManager
    Dim swFeatData As SweepFeatureData
    Set swFeatData = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmSweep)
    swFeatData.AdvancedSmoothing = False
    swFeatData.AlignWithEndFaces = 0
    swFeatData.AutoSelect = True
    swFeatData.CircularProfile = True
    swFeatData.CircularProfileDiameter = diameter
    swFeatData.D1ReverseTwistDir = False
    swFeatData.EndTangencyType = 0
    swFeatData.FeatureScope = True
    swFeatData.MaintainTangency = False
    swFeatData.merge = merge
    swFeatData.MergeSmoothFaces = True
    swFeatData.PathAlignmentType = 10
    swFeatData.StartTangencyType = 0
    swFeatData.ThinWallType = 0
    swFeatData.TwistControlType = 0
    swFeatData.SetTwistAngle 0
    swFeatData.ThinFeature = False
    If thickness > 0 Then
        swFeatData.ThinFeature = True
        swFeatData.SetWallThickness True, thickness
    End If
    Set swFeat = swFeatMgr.CreateFeature(swFeatData)
    If Not swFeat Is Nothing Then ThinSweep = True
End Function

 

If you have any suggestions for more reliable/simpler code, please feel free to contact me.

Attachments

Outcomes