Hide Table of Contents

Get Sectioned Bodies Example (VBA)

This example shows how to get sectioned bodies and create a new part using the sectioned bodies.

'-------------------------------------------------------
' Preconditions:
' 1. Verify that the specified assembly document to open exists.
' 2. Verify that the specified part document template exists.
' 3. Open the Immediate window.
' 4. Step through the macro by pressing F8.
'
' Postconditions:
' 1. Opens the specified assembly document.
' 2. Creates a section view.
' 3. Selects a component in the section view.
' 4. Creates a new part.
' 5. Creates a feature using the body in the component
'    in the section view selected in step 3.
' 6. Examine the FeatureManager design tree, graphics
'    area, and Immediate window.
'
' NOTE: Because the assembly is used elsewhere, do not
' save changes.
'--------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swModelViewMgr As SldWorks.ModelViewManager
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swModelView As SldWorks.ModelView
Dim swSectionViewData As SldWorks.SectionViewData
Dim vBodyArr As Variant
Dim vBody As Variant
Dim swBody As SldWorks.Body2
Dim swNewPart As SldWorks.PartDoc
Dim swFeat As SldWorks.Feature
Dim status As Boolean
Dim errors As Long
Dim warnings As Long
Dim fileName As String
Sub main()
    Set swApp = Application.SldWorks
    'Open assembly,create section view, and select
    'a component in the section view
    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\landing_gear.sldasm"
    Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swModelDocExt = swModel.Extension
    status = swModelDocExt.SelectByID2("Plane1", "PLANE", 0, 0, 0, True, 1, Nothing, 0)
    Set swModelViewMgr = swModel.ModelViewManager
    Set swSectionViewData = swModelViewMgr.CreateSectionViewData()
    swSectionViewData.FirstPlane = Nothing
    swSectionViewData.FirstReverseDirection = False
    swSectionViewData.FirstOffset = 0
    swSectionViewData.FirstRotationX = 0
    swSectionViewData.FirstRotationY = 0
    swSectionViewData.FirstColor = 16711680
    swSectionViewData.ShowSectionCap = True
    swSectionViewData.KeepCapColor = False
    swSectionViewData.GraphicsOnlySection = False
    status = swModelViewMgr.CreateSectionView(swSectionViewData)
    swModel.ClearSelection2 True
    status = swModelDocExt.SelectByID2("oleostrut-1@landing_gear", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    Set swSelMgr = swModel.SelectionManager
    Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
    Set swModelView = swModel.ActiveView
    status = swModelView.GetDisplayState(swViewDisplayType_e.swIsViewSectioned)
    If status Then
         'Create new part using selected component
         Set swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2014\templates\part.prtdot", swDwgPaperAsize, 0, 0)
         Set swNewPart = swModel
         vBodyArr = swComp.GetSectionedBodies(swModelView)
         For Each vBody In vBodyArr
             Set swBody = vBody
             Set swFeat = swNewPart.CreateFeatureFromBody3(swBody, False, swCreateFeatureBodyCheck)
         Next vBody
    Else
        Debug.Print "Model view does not contain a section view."
    End If         
End Sub


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:   Get Sectioned Bodies Example (VBA)
*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.