Hide Table of Contents

Set Body for View Example (VB.NET)

This example shows how to show just one body of a multibody part in a drawing view.


' Preconditions:

' 1. In SolidWorks, interactively open:

'    <SolidWorks_install_dir>\samples\tutorial\multibody\multi_inter.sldprt

' 2. In SolidWorks, save the part document as a drawing document:

'    a. Select File > Make Drawing from Part.

'    b. Click OK on the Sheet Format/Size dialog.

'    c. Drag the *Isometric view from the View Palette onto

'       the drawing sheet.

' 3. Run the macro.


' Postconditions: The drawing view shows one body of the multibody part.


' NOTE: Because the part document is used by a

'       SolidWorks online tutorial, do not save

'       any changes when closing the document.


Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports System

Imports System.Diagnostics

Imports System.Runtime.InteropServices


Partial Class SolidWorksMacro


    Public Sub main()


        Dim swModel As ModelDoc2

        Dim swSelMgr As SelectionMgr

        Dim swView As View

        Dim nbrBodies As Long

        Dim arrBody As Object

        Dim swBody As Body2

        Dim swFace As Face2

        Dim swEnt As Entity

        Dim swSelData As SelectData

        Dim bool As Boolean

        Dim arrBodiesIn(0) As DispatchWrapper

        Dim Bodies(0) As Object

        Dim i As Long

        Dim objType As Long


        swModel = swApp.ActiveDoc

        swSelMgr = swModel.SelectionManager

        swView = swSelMgr.GetSelectedObject6(1, -1)

        If (swView Is Nothing) Then

            MsgBox("View not selected.")

            Exit Sub

        End If

        nbrBodies = swView.GetBodiesCount

        Debug.Print("Number of bodies: " & nbrBodies)

        If (nbrBodies < 1) Then

            MsgBox("No bodies in selected view.")

            Exit Sub

        End If

        arrBody = swView.Bodies

        For i = 0 To UBound(arrBody)

            swBody = arrBody(i)

            swSelData = swSelMgr.CreateSelectData

            swSelData.View = swView

            bool = swBody.Select2(False, swSelData)

            ' Object type 76 is a solid body

            objType = swSelMgr.GetSelectedObjectType3(1, -1)

            If (objType = 76) Then

                Debug.Print(" Object type: solid body")

            End If

            If (Not (swSelectType_e.swSelSOLIDBODIES = swSelMgr.GetSelectedObjectType3(1, -1))) Then

                MsgBox("Solid body not found.")

            End If

            swFace = swBody.GetFirstFace

            Do While Not swFace Is Nothing

                swEnt = swFace

                ' Select using IEntity

                bool = swEnt.Select4(True, swSelData) : Debug.Assert(bool)

                swFace = swFace.GetNextFace


            Debug.Print("   Name of body: " & swBody.GetSelectionId)

        Next i






        ' Get the bodies from referenced model

        swModel = swView.ReferencedDocument

        arrBody = swModel.GetBodies2(swBodyType_e.swSolidBody, True)

        If (nbrBodies = 1) Then

            swView.Bodies = (arrBody)


            ' Set the body to include in the drawing view

            Bodies(0) = arrBody(0)

            arrBodiesIn(0) = New DispatchWrapper(Bodies(0))

            swView.Bodies = (arrBodiesIn)

        End If




    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks

End Class



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

Subject:   Feedback on Help Topics
Page:   Set Body for View Example (VB.NET)
*   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:


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

Web Help Content Version: API Help (English only) 2012 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.