Hide Table of Contents

Create Multibody Macro Feature Example (VB.NET)

This example shows how to create a multibody macro feature using a VB.NET SolidWorks add-in.

'----------------------------------------------------------------------------
' Preconditions:
'  1. Open https://forum.solidworks.com/community/solidworks/api?view=documents.
'     a. Log in.
'     b. Click SolidWorks > API to open the SolidWorks API forum.
'     c. Click the Documents tab.
'     d. Locate the API: C#\VB.NET Add-ins to Create a Macro Feature document.
'  2. If you have a:
'     * 64-bit machine, click attachment,
'       InsertMacroFeature VB.NET Addin Mods for 64 bit.zip.
'     * 32-bit machine, click attachment, VB NET Addin InsertMacroFeature.zip.
'  3. Download and unpack the ZIP file in a convenient location.
'  4. Open and, if necessary, convert the VB.NET solution in Visual Studio.
'  5. Modify the project references to point to your SolidWorks primary interop
'     assemblies.
'  6. Double-click SwAddin.vb in the Solution Explorer.
'  7. Replace Region "ISwComFeature Implementation" with this.
'  8. Replace AddMacroFeature() with this.
'  9. Click Build > Build Solution.
'     The Output window notifies that types registered successfully.
'     If that message does not appear in the Output window, repeat from
'     step 1.
' 10. Open SolidWorks.
' 11. Open
'     install_dir\samples\tutorial\multibody\multi_local.sldprt.
' 12. Click OK in each message box.
'
' Postconditions: 
' 1. When SolidWorks opens, VB Addin appears in the SolidWorks menu.
' 2. When the document opens, the ISldWorks_ActiveDocChangeNotify event fires,
'    and the add-in inserts the macro feature, MacroFeature1, in the
'    FeatureManager design tree.
' 3. The macro feature:
'    * Consumes the part's solid bodies, Fillet5 and Fillet6.
'    * Creates two solid bodies, MacroFeature1[1] and MacroFeature1[2].
'
' NOTE: Because the model is used elsewhere, do not save changes
' when closing it.
'----------------------------------------------------------------------------

'Region ISwComFeature Implementation

#Region "ISwComFeature Implementation" 
 
    Function Edit(ByVal app As ObjectByVal modelDoc As ObjectByVal feature As ObjectAs Object Implements SwComFeature.Edit
        MsgBox("Macro feature edit")
        Return Nothing
    End Function
 
    Function Regenerate(ByVal app As ObjectByVal modelDoc As ObjectByVal feature As ObjectAs Object Implements SwComFeature.Regenerate
        Dim OutputBodies As New Collection
        Dim swBody As Body2
        Dim swBodies() As Body2
        Dim swMacroFeatData As MacroFeatureData
        swMacroFeatData = feature.GetDefinition
        swMacroFeatData.EnableMultiBodyConsume = True
 
        Dim swModeler As Modeler
        swModeler = app.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
 
        'Output body 1
        swBody = swModeler.CreateBodyFromBox3(dblData)
        OutputBodies.Add(swBody)
 
        'Output body 2
        dblData(1) = 0.15
        swBody = swModeler.CreateBodyFromBox3(dblData)
        OutputBodies.Add(swBody)
 
        Dim i As Integer, j As Integer
        Dim vFaces As Object
        Dim vEdges As Object
        ReDim swBodies(OutputBodies.Count - 1)
 
        For i = 1 To OutputBodies.Count
            swBody = OutputBodies.Item(i)
            vEdges = swBody.GetEdges
            vFaces = swBody.GetFaces
 
            For j = 0 To UBound(vEdges)
                swMacroFeatData.SetEdgeUserId(vEdges(j), j, 0)
            Next j
            For j = 0 To UBound(vFaces)
                swMacroFeatData.SetFaceUserId(vFaces(j), j, 0)
            Next j
 
            swBodies(i - 1) = OutputBodies.Item(i)
        Next i
 
        Regenerate = swBodies
        MsgBox("Macro feature rebuild")
 
    End Function
 
    Function Security(ByVal app As ObjectByVal modelDoc As ObjectByVal feature As ObjectAs Object Implements SwComFeature.Security
        MsgBox("Macro feature security")
        Return Nothing
    End Function
 
#End Region

Back to top

'AddMacroFeature

   Function AddMacroFeature() As Boolean
 
        Dim moddoc As ModelDoc2
        Dim FeatMgr As FeatureManager
        Dim MacroFeature As Feature
 
        moddoc = Me.iSwApp.ActiveDoc
        FeatMgr = moddoc.FeatureManager
 
        'Collect input bodies
        Dim vBodies As Object
        vBodies = moddoc.GetBodies2(swBodyType_e.swAllBodies, False)
 
        'Create the macro feature
        MacroFeature = FeatMgr.InsertMacroFeature3("MacroFeature""swVBAddinTest.SwAddin"Nothing, _
            NothingNothingNothingNothingNothing, vBodies, NothingswMacroFeatureOptions_e.swMacroFeatureByDefault)
        
    End Function

Back to top



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Create Multibody Macro Feature Example (VB.NET)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.