Get Body Outline Example (VBA)
This example shows how to get the outline of a face on a solid body.
This example also creates and inserts a 3D sketch of that outline.
'--------------------------------------
'
' Preconditions: Part document containing at
' least
body is open.
'
' Postconditions: A 3D of the outline of the
' body
is created.
'
'--------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.PartDoc
Dim swModel As SldWorks.ModelDoc2
Dim swMathVector As SldWorks.MathVector
Dim swMathUtility As SldWorks.MathUtility
Dim swModeler As SldWorks.Modeler
Dim swBody As Body2
Dim dirVar As Variant
Dim bVar As Variant
Dim crvOut As Variant
Dim topol As Variant
Dim outline As Variant
Dim sEva As Variant
Dim eEva As Variant
Dim dirArr(0 To 2) As Double
Dim s As Double
Dim e As Double
Dim nCt As Long
Dim i As Long
Dim isClosed As Boolean
Dim isPer As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swPart = swApp.ActiveDoc
Set swModel = swPart
'Get the bodies in this part
bVar = swPart.GetBodies2(swSolidBody,
False)
Set swModeler = swApp.GetModeler
Set swMathUtility = swApp.GetMathUtility
dirArr(0) = 0#
dirArr(1) = 0#
dirArr(2) = 1#
dirVar = dirArr
'Create a MathVector
Set swMathVector = swMathUtility.CreateVector(dirVar)
'Copy the bodies in the part
Set bVar(0) = bVar(0).Copy
'Get the number of curves in the body outlines
nCt = swModeler.GetBodyOutline((bVar),
swMathVector, 0.000001, crvOut, topol, outline)
'Open a 3D sketch in the part document
swPart.Insert3DSketch
' Using the end conditions of the curves, create a 2D
sketch of each curve
For i = 0 To nCt - 1
crvOut(i).GetEndParams s, e, isClosed, isPer
sEva
= crvOut(i).Evaluate(s)
eEva
= crvOut(i).Evaluate(e)
swModel.CreateLine2 sEva(0), sEva(1), sEva(2),
eEva(0), eEva(1), eEva(2)
Next i
'Insert the sketches
swModel.InsertSketch2
True
End Sub