MACRO LIBRARY

Macro feature and PMP skeleton for creating a body

Free public access. No account or subscription required.

Files

Macro feature and PMP skeleton for creating a body

Code

Module: mMain

'Demonstrates macro features (including preview). Create a new body and edit it later.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: new part is open; be sure to run "main" sub proc in mMain module first
    
'Notes: To create a body and to be able to edit it later when the feature is edited, you need _
    to create the body and get the pointer (in swBody) BEFORE you use IFeatureManager:: _
    InsertMacroFeature3. Moreover, you must use swBody for the EditBodies argument.
    
Public swApp As SldWorks.SldWorks
Public swModel As SldWorks.ModelDoc2
Public pm As clsPMP
Public dblDepth As Double
Public swFeat As SldWorks.Feature
Public swMacroFeatData As SldWorks.MacroFeatureData
Public vSelections As Variant
Public swBody As SldWorks.Body2
    
Public Const INSERT As Integer = 2
Public Const EDIT As Integer = 1
Public STATE As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set pm = New clsPMP
    
    STATE = INSERT
    pm.Layout
End Sub

Module: mMacroFeature

Sub InsertMacroFeature()
    'MACRO METHODS
    Dim strMacroMethods(8) As String
    'Regen function
    strMacroMethods(0) = swApp.GetCurrentMacroPathName
    strMacroMethods(1) = "mMacroFeature"
    strMacroMethods(2) = "swmRebuild"
    'Edit definition function
    strMacroMethods(3) = swApp.GetCurrentMacroPathName
    strMacroMethods(4) = "mMacroFeature"
    strMacroMethods(5) = "swmEditDefinition"
    'Security function (optional)
    strMacroMethods(6) = swApp.GetCurrentMacroPathName
    strMacroMethods(7) = "mMacroFeature"
    strMacroMethods(8) = "swmSecurity"
    
    'Parameters (used to remember PMP control values during edit definition)
    Dim strParamNames(0) As String
    Dim lngParamTypes(0) As Long
    Dim strParamValues(0) As String
    strParamNames(0) = "depth"
    lngParamTypes(0) = 1
    strParamValues(0) = Str(dblDepth)
    
    swModel.FeatureManager.InsertMacroFeature3 "Cylinder", Empty, strMacroMethods, _
        strParamNames, lngParamTypes, strParamValues, Nothing, Nothing, Nothing, Nothing, 0
End Sub
    
Function swmRebuild(app As Variant, model As Variant, feat As Variant) As Variant
    Set swApp = app
    Set swMacroFeatData = feat.GetDefinition
    If STATE = 0 Then
        swMacroFeatData.GetDoubleByName "depth", dblDepth
        Call CreateBody
    End If
    
    'Assign edge and face IDs
    Dim vEdges As Variant
    Dim vFaces As Variant
    vEdges = swBody.GetEdges
    vFaces = swBody.GetFaces
    Dim i As Integer
    For i = 0 To UBound(vEdges)
        swMacroFeatData.SetEdgeUserId vEdges(i), i, 0
    Next i
    For i = 0 To UBound(vFaces)
        swMacroFeatData.SetFaceUserId vFaces(i), i, 0
    Next i
    
    'store current depth value in macro feature
    swMacroFeatData.SetDoubleByName "depth", dblDepth
    
    'return value
    Set swmRebuild = swBody
    STATE = 0
End Function
    
Function swmEditDefinition(app As Variant, model As Variant, feat As Variant) As Variant
    STATE = EDIT
    'since swFeat and swModel are used during editing in other classes, it is important to set them
    Set swFeat = feat
    Set swModel = model
    Set swMacroFeatData = swFeat.GetDefinition
    swMacroFeatData.AccessSelections swModel, Nothing
    swMacroFeatData.GetDoubleByName "depth", dblDepth   'get current depth value
    pm.Layout
End Function
    
Function swmSecurity(app As Variant, model As Variant, feat As Variant) As Variant
    swmSecurity = SwConst.swMacroFeatureSecurityOptions_e.swMacroFeatureSecurityByDefault
End Function

Module: mCreateBody

Sub CreateBody()
    Dim swModeler As SldWorks.Modeler
    Set swModeler = swApp.GetModeler
    Dim dblData(8) As Double
    dblData(0) = 0
    dblData(1) = 0
    dblData(2) = 0
    dblData(3) = 0
    dblData(4) = 0
    dblData(5) = 1
    dblData(6) = 0.05
    dblData(7) = dblDepth
    Set swBody = swModeler.CreateBodyFromCyl(dblData)
    If STATE <> 0 Then swBody.Display3 swModel, 255, 0
End Sub

Class: clsPMP

Dim pmHandler As clsPMPHandler
Dim pmPage As SldWorks.PropertyManagerPage2
Dim pmGroup As SldWorks.PropertyManagerPageGroup
Dim pmNumBoxExtrude As SldWorks.PropertyManagerPageNumberbox
Dim lngErrors As Long
    
Sub Layout()
    Set pmHandler = New clsPMPHandler
    Set pmPage = swApp.CreatePropertyManagerPage("Create Cylinder", _
        swPropertyManagerOptions_OkayButton + swPropertyManagerOptions_CancelButton + _
        swPropertyManagerOptions_LockedPage, pmHandler, lngErrors)
    
    If lngErrors = 0 Then
        Set pmGroup = pmPage.AddGroupBox(0, "Depth", swGroupBoxOptions_Expanded + _
                swGroupBoxOptions_Visible)
        Set pmNumBoxExtrude = pmGroup.AddControl(1, swControlType_Numberbox, Empty, _
            swControlAlign_LeftEdge, swControlOptions_Visible + swControlOptions_Enabled, _
            "Depth")
        pmNumBoxExtrude.SetRange2 swNumberBox_Length, 0, 1, True, 0.005, 0.005, 0.005
        If STATE = INSERT Then dblDepth = 0.005
        pmNumBoxExtrude.Value = dblDepth
        
        pmPage.Show2 0
    Else
        Debug.Print "Error creating PropertyManager page."
        Debug.Print "Error code: " & lngErrors
    End If
End Sub

Class: clsPMPHandler

Implements PropertyManagerPage2Handler8
    
Private Sub PropertyManagerPage2Handler8_AfterActivation()
    Call CreateBody
    swModel.ViewZoomtofit2
End Sub
    
Private Sub PropertyManagerPage2Handler8_AfterClose()
    
End Sub
    
Private Function PropertyManagerPage2Handler8_OnActiveXControlCreated(ByVal Id As Long, ByVal Status As Boolean) As Long
    
End Function
    
Private Sub PropertyManagerPage2Handler8_OnButtonPress(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnCheckboxCheck(ByVal Id As Long, ByVal Checked As Boolean)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnClose(ByVal Reason As Long)
    If Reason = swPropertyManagerPageClose_Okay Then
        If STATE = INSERT Then
            Call InsertMacroFeature
        ElseIf STATE = EDIT Then
            swFeat.ModifyDefinition swMacroFeatData, swModel, Nothing
        End If
    Else
        swMacroFeatData.ReleaseSelectionAccess
        Set swBody = Nothing
        STATE = 0
    End If
    swModel.ClearSelection2 True
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnComboboxEditChanged(ByVal Id As Long, ByVal Text As String)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnComboboxSelectionChanged(ByVal Id As Long, ByVal Item As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnGainedFocus(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnGroupCheck(ByVal Id As Long, ByVal Checked As Boolean)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnGroupExpand(ByVal Id As Long, ByVal Expanded As Boolean)
    
End Sub
    
Private Function PropertyManagerPage2Handler8_OnHelp() As Boolean
    
End Function
    
Private Function PropertyManagerPage2Handler8_OnKeystroke(ByVal Wparam As Long, ByVal Message As Long, ByVal Lparam As Long, ByVal Id As Long) As Boolean
    
End Function
    
Private Sub PropertyManagerPage2Handler8_OnListboxRMBUp(ByVal Id As Long, ByVal PosX As Long, ByVal PosY As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnListboxSelectionChanged(ByVal Id As Long, ByVal Item As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnLostFocus(ByVal Id As Long)
    
End Sub
    
Private Function PropertyManagerPage2Handler8_OnNextPage() As Boolean
    
End Function
    
Private Sub PropertyManagerPage2Handler8_OnNumberboxChanged(ByVal Id As Long, ByVal Value As Double)
    If Id = 1 Then dblDepth = Value
    Call CreateBody
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnOptionCheck(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnPopupMenuItem(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnPopupMenuItemUpdate(ByVal Id As Long, retval As Long)
    
End Sub
    
Private Function PropertyManagerPage2Handler8_OnPreview() As Boolean
    
End Function
    
Private Function PropertyManagerPage2Handler8_OnPreviousPage() As Boolean
    
End Function
    
Private Sub PropertyManagerPage2Handler8_OnRedo()
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnSelectionboxCalloutCreated(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnSelectionboxCalloutDestroyed(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnSelectionboxFocusChanged(ByVal Id As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnSelectionboxListChanged(ByVal Id As Long, ByVal Count As Long)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnSliderPositionChanged(ByVal Id As Long, ByVal Value As Double)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnSliderTrackingCompleted(ByVal Id As Long, ByVal Value As Double)
    
End Sub
    
Private Function PropertyManagerPage2Handler8_OnSubmitSelection(ByVal Id As Long, ByVal Selection As Object, ByVal SelType As Long, ItemText As String) As Boolean
    
End Function
    
Private Function PropertyManagerPage2Handler8_OnTabClicked(ByVal Id As Long) As Boolean
    
End Function
    
Private Sub PropertyManagerPage2Handler8_OnTextboxChanged(ByVal Id As Long, ByVal Text As String)
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnUndo()
    
End Sub
    
Private Sub PropertyManagerPage2Handler8_OnWhatsNew()
    
End Sub
    
Private Function PropertyManagerPage2Handler8_OnWindowFromHandleControlCreated(ByVal Id As Long, ByVal Status As Boolean) As Long
    
End Function