Hide Table of Contents

Create and Convert Non-Manifold Bodies Example (VBA)

This example shows how to create non-manifold bodies, which by default are not allowed in SolidWorks, and then convert the non-manifold bodies to manifold bodies.




' Preconditions: Model document containing

'                tool and target bodies exists.


' Postconditions: Non-manifold bodies are created

'                and tessellated, and then

'                converted to manifold bodies.



Option Explicit


Dim swApp As SldWorks.SldWorks

Dim swModeler As SldWorks.Modeler

Dim swModel As SldWorks.ModelDoc2

Dim swModelDocExt As SldWorks.ModelDocExtension

Dim swSelMgr As SldWorks.SelectionMgr

Dim tess As SldWorks.Tessellation

Dim tool As SldWorks.Body2

Dim tgt1 As SldWorks.Body2

Dim tgt0 As SldWorks.Body2

Dim tgtVar As Variant

Dim resMass As Variant

Dim resVar As Variant

Dim resvar2 As Variant

Dim manifVar As Variant

Dim vFacetId As Variant

Dim vFinId As Variant

Dim vVertexId As Variant

Dim vVertex1 As Variant

Dim vVertex2 As Variant

Dim origMassProp As Variant

Dim f As Object

Dim boolstatus As Boolean

Dim bret As Boolean

Dim mass As Double

Dim longstatus As Long

Dim longwarnings As Long

Dim i As Long

Dim j As Long

Dim clr(0 To 1) As Long


Sub DisplayBody(ByVal b As Object, col As Long)

Call b.Display2(swModel, col, swTempBodySelectable)

End Sub


Sub HideBody(ByVal b As Object)

Call b.Hide(swModel)

End Sub


Sub main()


Set swApp = Application.SldWorks

Set swModeler = swApp.GetModeler

bret = swModeler.GeneralTopology

Debug.Print bret



' Open and activate sample part; substitute the path and filename of your document

Set swModel = swApp.OpenDoc6("C:\cheryle\samples\BodyOperationBodyDisplay\bodyBool.sldprt", 1, 0, "", longstatus, longwarnings)

Set swModel = swApp.ActivateDoc2("bodyBool.SLDPRT", False, longstatus)

Set swModelDocExt = swModel.Extension

Set swSelMgr = swModel.SelectionManager


'Make selections of target and tool bodies;

'Extrude1 is larger cuboid, whereas Extrude2[1]

'and Extrude2[2] are one-quarter the size of

'Extrude, so (Extrude - Extrude2[1])-Extrude2[2])

'results in non-manifold bodies; under normal conditions,

'i.e., when non-manifold bodies are not allowed,

'such an operation results in two bodies;

'when creation of non-manifold bodies is allowed,

'then one general body is the result

boolstatus = swModelDocExt.SelectByID2("Extrude1", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)

boolstatus = swModelDocExt.SelectByID2("Extrude2[1]", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0)

boolstatus = swModelDocExt.SelectByID2("Extrude2[2]", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0)


Set tool = swSelMgr.GetSelectedObject5(1)

Set tgt0 = swSelMgr.GetSelectedObject5(2)

Set tgt1 = swSelMgr.GetSelectedObject5(3)


origMassProp = tool.GetMassProperties(1)

'Fifth element is mass

Debug.Print "Original Mass : " & origMassProp(5)


'Create temporary bodies

Set tool = tool.Copy

Set tgt0 = tgt0.Copy

Set tgt1 = tgt1.Copy


swModel.ClearSelection2 True


'First cut operation : Extrude - Extrude2[1]

Dim errCode As Long

resVar = tool.Operations2(SWBODYCUT, tgt0, errCode)


'IMPORTANT: SolidWorks requires this option

'to be FALSE; thus, switch it back to FALSE

'as soon as your intended operations complete

swModeler.GeneralTopology = True


'Second cut operation: (Extrude - Extrude2[1])- Extrude2[2]

resvar2 = resVar(0).Operations2(SWBODYCUT, tgt1, errCode)


'Reset the option back to True

swModeler.GeneralTopology = False


mass = 0#

clr(0) = RGB(0, 0, 255)

clr(1) = RGB(255, 0, 0)

For i = LBound(resvar2) To UBound(resvar2)

    resMass = resvar2(i).GetMassProperties(1)

    mass = mass + resMass(5)

    Debug.Print "Body " & i; " Face Count: " & resvar2(i).GetFaceCount

    Call DisplayBody(resvar2(i), clr(i))

Next i

Debug.Print "Resultant mass : " & mass


'Hide the displayed bodies

For i = LBound(resvar2) To UBound(resvar2)

    HideBody (resvar2(i))

Next i


'Try tesselation


'Add sketch for this face

swModel.Insert3DSketch2 False


'Add lines directly to sketch to increase performance

swModel.SetAddToDB True


Set tess = resvar2(0).GetTessellation(Empty)

tess.NeedFaceFacetMap = True

tess.MatchType = swTesselationMatchFacetGeometry

boolstatus = tess.Tessellate

Set f = resvar2(0).GetFirstFace

While Not f Is Nothing

    vFacetId = tess.GetFaceFacets(f)

        For i = 0 To UBound(vFacetId)

            vFinId = tess.GetFacetFins(vFacetId(i))

            For j = 0 To 2

                'Should always be three fins per facet

                vVertexId = tess.GetFinVertices(vFinId(j))


                'Should always be two vertices per fin

                vVertex1 = tess.GetVertexPoint(vVertexId(0))

                vVertex2 = tess.GetVertexPoint(vVertexId(1))


                Call swModel.CreateLine2( _

                vVertex1(0), vVertex1(1), vVertex1(2), _

                vVertex2(0), vVertex2(1), vVertex2(2))

            Next j

        Next i

        Set f = f.GetNextFace



'Convert non-manifold bodies to manifold bodies

manifVar = swModeler.MakeManifoldBodies(resvar2(0))

For i = LBound(manifVar) To UBound(manifVar)

    Call DisplayBody(manifVar(i), RGB(0, 255, 0))

Next i


For i = LBound(manifVar) To UBound(manifVar)

    HideBody (manifVar(i))

Next i


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

Subject:   Feedback on Help Topics
Page:   Create and Convert Non-manifold Bodies Example (VBA)
*   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) 2010 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.