MACRO LIBRARY

Macro Feature and PMP skeleton for modifying input body

Free public access. No account or subscription required.

Files

Macro Feature and PMP skeleton for modifying input body

Code

Module: mMain

'This is a fully functioning macro that change a selected input body into a cube.
'However it also doubles as a skeleton for a macro feature that uses a PMP and _
    creates and modifies an input body.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'The main module contains the initial sub procedure as well as the macro feature _
    functions
'The PropMgr class contains a layout function for the PMP. Do not pass variables _
    to this class' functions,  instead use global variables. The last line of the _
    layout function should show the page.
'The PropMgrHdler class should also use variables declared publicly in main module
    
'To incorporate your content:
'If your macro simply modifies selected input body, then place your body modification _
    code in the Rebuild function and make sure the body is set to swBody. Modify the _
    PMP layout accordingly.
'If your macro creates a body, you will want to remove the selection box from the PMP _
    layout and modify the OnSubmitSelection handler function. Also set the EditBody _
    argument in IFeatureManager::InsertMacroFeature3 to Nothing and remove any uses of _
    swBody before it is set in the Rebuild function, as well as any uses of it in the _
    edit macro feature function.
'Note that the input body is retrieved using IMacroFeatureData::EditBodies. If you need _
    the component transform then use IMacroFeatureData::GetSelections3 instead, which _
    will return the input body and the component transforms. More precisely, this line _
    can be used instead for the current macro: _
    swMacroFeatData.GetSelections3 vSelections, Nothing, Nothing, Nothing, Nothing
    'In the Cut Body In Half (VBA) macro, the above line and the following line are _
    used in the OnClose handler function before IFeature::ModifyDefinition. Not sure of _
    their purpose. No errors encountered when these lines are removed, even in the original. _
    swMacroFeatData.SetSelections2 vSelections, Nothing, Nothing
    
Option Explicit
    
Public swApp As SldWorks.SldWorks
Public swModel As SldWorks.ModelDoc2
Public swFeat As SldWorks.feature
Public swMacroFeatData As SldWorks.MacroFeatureData
Public swBody As SldWorks.Body2
Public pm As PropMgr
Public vSelections As Variant
    
Public STATE As Integer
Public Const INSERT As Integer = 1
Public Const EDIT As Integer = 2
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swFeat = Nothing
    Set pm = New PropMgr
    STATE = INSERT
    pm.Layout
End Sub

Module: mMacroFeature

'Insert the macro feature
Sub InsertMacroFeature()
    Dim strMacroMethods(0 To 8) As String
    strMacroMethods(0) = swApp.GetCurrentMacroPathName
    strMacroMethods(1) = "mMacroFeature"
    strMacroMethods(2) = "swmRebuild"
    strMacroMethods(3) = swApp.GetCurrentMacroPathName
    strMacroMethods(4) = "mMacroFeature"
    strMacroMethods(5) = "swmEdit"
    strMacroMethods(6) = Empty
    strMacroMethods(7) = Empty
    strMacroMethods(8) = Empty
    
    'Get EditBodies
    Dim vEditBody As Variant
    Dim swEditBody(0) As SldWorks.Body2
    Set swEditBody(0) = swBody
    vEditBody = swEditBody
    
    swModel.FeatureManager.InsertMacroFeature3 "MacroFeature", Empty, strMacroMethods, _
        Nothing, Nothing, Nothing, Nothing, Nothing, vEditBody, Nothing, swMacroFeatureByDefault
End Sub
    
'Macro feature rebuild function
Function swmRebuild(swAppIn, swPartIn, swFeatIn)
    Set swMacroFeatData = swFeatIn.GetDefinition
    
    '=====INSERT CODE THAT MODIFIES OR CREATES BODY======
    Dim swModeler As SldWorks.modeler
    Set swModeler = swAppIn.GetModeler
    Dim dblData(8) As Double
    dblData(0) = 0
    dblData(1) = 0
    dblData(2) = 0
    dblData(3) = 1
    dblData(4) = 0
    dblData(5) = 0
    dblData(6) = 0.1
    dblData(7) = 0.1
    dblData(8) = 0.1
    
    Set swBody = swModeler.CreateBodyFromBox(dblData)
    '=========================
    
    'Assign edge and face IDs
    Dim vFaces As Variant
    Dim vEdges 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
    
    'Return value
    Set swmRebuild = swBody
End Function
    
'Macro feature edit definition function
Sub swmEdit(swAppIn, swPartIn, swFeatIn)
    Set swFeat = swFeatIn
    STATE = EDIT
    Set swMacroFeatData = swFeatIn.GetDefinition
    swMacroFeatData.AccessSelections swPartIn, Nothing
    vSelections = swMacroFeatData.EditBodies
    Set swBody = vSelections(0)
    swBody.Select2 False, Nothing
    pm.Layout
End Sub

Class: PropMgr

Option Explicit
    
Private pmPage As PropertyManagerPage2
Private pmGroup As PropertyManagerPageGroup
Private pmSelBox As PropertyManagerPageSelectionbox
Private pmNumberBox As PropertyManagerPageNumberbox
Private pmTextBox As PropertyManagerPageTextbox
Private pmHandler As New PropMgrHdlr
    
Sub Layout()
    Set pmHandler = New PropMgrHdlr
    
    'Create PMP
    Set pmPage = swApp.CreatePropertyManagerPage("Macro feature", _
        swPropertyManagerOptions_OkayButton + swPropertyManagerOptions_CancelButton, _
        pmHandler, Empty)
    
    'Message
    pmPage.SetMessage "Select body to modify.", swImportantMessageBox
    
    'Group box
    Set pmGroup = pmPage.AddGroupBox(0, "Input body", _
        swGroupBoxOptions_Visible + swGroupBoxOptions_Expanded)
    
    'Selection box
    Set pmSelBox = pmGroup.AddControl(1, swControlType_Selectionbox, Empty, _
        swControlAlign_Indent, swControlOptions_Visible + swControlOptions_Enabled, _
        "Select body")
    Dim filterArray(0 To 1) As Long
    filterArray(0) = swSelSOLIDBODIES
    filterArray(1) = swSelSURFACEBODIES
    pmSelBox.SetSelectionFilters filterArray
    pmSelBox.SingleEntityOnly = True
    pmSelBox.Height = 12
    pmSelBox.SetStandardPictureLabel swBitmapLabel_SelectFace
    
    'Show
    pmPage.Show2 0
End Sub

Class: PropMgrHdler

Option Explicit
    
Implements PropertyManagerPage2Handler8
    
Private Sub PropertyManagerPage2Handler8_AfterActivation()
    
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 = EDIT Then 'Edit feature
            swFeat.ModifyDefinition swMacroFeatData, swModel, Nothing
        Else 'Insert feature
            Call InsertMacroFeature
        End If
    ElseIf Reason = swPropertyManagerPageClose_Cancel Then
        If STATE = EDIT Then swMacroFeatData.ReleaseSelectionAccess
    End If
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)
    
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
    Set swBody = Selection
    PropertyManagerPage2Handler8_OnSubmitSelection = True
    
    If STATE = EDIT Then
        Dim vEditBody As Variant
        Dim swEditBody(0) As SldWorks.Body2
        Set swEditBody(0) = swBody
        vEditBody = swEditBody
        swMacroFeatData.EditBodies = vEditBody
    End If
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