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. Locate API: C#\VB.NET Add-ins to Create a Macro Feature in the SOLIDWORKS
'     API forum.
'  2. Click attachment InsertMacroFeature VB.NET Addin Mods for 64 bit.zip.
'  3. Download and unzip the file.
'  4. Open Visual Studio 2015 and 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 Project > SwVBAddinTest Properties > Application and change
'     Target framework to .NET FrameWork 4.
' 10. Click Compile > Build Events and replace the code in Post-build event
'     command line with this.
' 11. Click Build > Build Solution.
'     NOTE: The Output window notifies that types registered successfully.
'     If that message does not appear in the Output window, repeat from
'     step 1.
' 12. Open SOLIDWORKS.
' 13. Open public_documents\samples\tutorial\multibody\multi_local.sldprt.
'
' Postconditions: 
'  1. Click OK in each message box.
'  2. Displays VB Addin on the Tools menu.
'  3. Fires the ISldWorks_ActiveDocChangeNotify event and
'     inserts the macro feature, MacroFeature1, in the
'     FeatureManager design tree, which:
'     * Consumes the part's solid bodies, Fillet5 and Fillet6.
'     * Creates two solid bodies, MacroFeature1[1] and MacroFeature1[2].
'  4. Examine the graphics area and FeatureManager design tree.
'
' NOTE: Because the model is used elsewhere, do not save changes.
'----------------------------------------------------------------------------

'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

IF EXIST "$(TargetDir)$(TargetName).bmp" (GOTO REGISTRATION)
XCOPY "$(ProjectDir)AddinIcon.bmp" "$(TargetDir)" /F
REN "$(TargetDir)AddinIcon.bmp" "$(TargetName).bmp"

:REGISTRATION
IF "$(TargetFrameworkVersion)"=="v4.0" GOTO NET40
IF "$(TargetFrameworkVersion)"=="v3.5" GOTO NET20
IF "$(TargetFrameworkVersion)"=="v3.0" GOTO NET20
IF "$(TargetFrameworkVersion)"=="v2.0" GOTO NET20
GOTO END

:NET40
set FMWK="v4.0.30319"
GOTO REG

:NET20
set FMWK="v2.0.50727"
GOTO REG

:REG
IF "$(PlatformName)" == "AnyCPU" GOTO ANYCPU
IF "$(PlatformName)" == "x64" GOTO X64
GOTO END

:ANYCPU
IF EXIST "%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm.exe" "%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm" /codebase "$(TargetPath)"
GOTO END

:X64
IF EXIST "%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm.exe" "%Windir%\Microsoft.NET\Framework64\%FMWK%\regasm" /codebase "$(TargetPath)"
GOTO END

:END

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) 2018 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.