Create Body Using Trimmed Surfaces Example (VBA)
The basic outline for creating a body using trimmed surfaces is as follows:
Create a new temporary
body in a part using IPartDoc::CreateNewBody.
-
Create the trimmed surfaces in the shape of the new
body (for example, six square surfaces that intersect at the edges to
form a cube).
Create
a planar surface based on a root point and normal (two, three cell VARIANT
arrays) using IBody2::CreatePlanarSurface(RootPoint, Normal).
Add a
trimming loop to the planar surface using
ISurface::AddTrimmingLoop2(Numcurves, _
Order, _
Dimen, _
Periodic, _
NumKnots, _
NumCtrlPoints, _
Knots, _
CtrlPointDbls, _
UVRange)
-
Create a trimmed surface on the body
based on the trimming loop that was just created. The arguments for Surface::AddTrimmingLoop2
and their values for a square are:
Argument |
Description |
NumCurves |
Number of curves that make up the loop (4 Long) |
Order |
Orders for the spline curves ({2, 2, 2, 2} Array of Longs) |
Dimen |
Dimension of the control points for the spline curves ({2, 2,
2, 2} Array of Longs) |
Periodic |
Periodicity of the spline curves ({0, 0, 0, 0} Array of Longs) |
NumKnots |
Number of Knots of the spline curves ({4, 4, 4, 4} Array of
Longs) |
NumCtrlPoints |
Number of Control points for the spline curves ({2, 2, 2, 2}
Array of Longs) |
Knots |
Describes the locations of the knots ({0, 0, 1, 1, 0, 0, 1,
1, 0, 0, 1, 1, 0, 0, 1, 1} Array of Doubles. Each knot represented by
four numbers 0, 0, 1, 1) |
CtrlPointDbls |
Control points for the TrimmingLoop ({0, 0, 1, 0, 1, 0, 1, 1,
1, 1, 0, 1, 0, 1, 0, 0} Array of Doubles. Describes the corners of the
square) |
UVRange |
Min and max for the U and V values ({0, 1, 0, 1} Array of Doubles) |
Sew the surfaces together
into a new body using IBody2::CreateBodyFromSurfaces.
This example shows how to create a temporary surface using temporary
bodies.
'--------------------------------------------
' Preconditions: Verify that the specified
' part document template exists.
'
' Postconditions:
' 1. Opens a new part document.
' 2. Creates a temporary body.
' 3. Creates the trimmed surfaces.
' 4. Creates a planar surface.
' 5. Adds a trimming loop to the planar surface.
' 6. Creates a trimmed surface on the body based
' on the trimming loop.
' 7. Sews the surfaces together into a new body.
' 8. Examine the FeatureManager design tree and
' graphics area.
'----------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim Model As SldWorks.ModelDoc2
Dim Part As SldWorks.PartDoc
Dim RootPoint(2) As Double
Dim Normal(2) As Double
Dim TempBody As SldWorks.Body2
Dim isGood As Boolean
Sub main()
'Get the SOLIDWORKS application
Set swApp = CreateObject("SldWorks.Application")
swApp.UserControl = True
'Create a new blank document
Set swModel = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2015\templates\part.prtdot", 0, 0, 0)
Set Part = Model
'Create a new temporary body in the part
Set TempBody = Part.CreateNewBody
If TempBody Is Nothing Then
MsgBox "Could not create the new body."
Exit Sub
End If
'Create the trimmed surfaces for a cube 1 meter per side
'LEFT
RootPoint(0) = 0 'X
RootPoint(1) = 0 'Y
RootPoint(2) = 0 'Z
Normal(0) = 1 'X
Normal(1) = 0 'Y
Normal(2) = 0 'Z
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, True)
'RIGHT
RootPoint(0) = 1
RootPoint(1) = 0
RootPoint(2) = 0
Normal(0) = 1
Normal(1) = 0
Normal(2) = 0
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, True)
'BOTTOM
RootPoint(0) = 0
RootPoint(1) = 0
RootPoint(2) = -1
Normal(0) = 0
Normal(1) = 1
Normal(2) = 0
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, True)
'TOP
RootPoint(0) = 0
RootPoint(1) = 1
RootPoint(2) = -1
Normal(0) = 0
Normal(1) = 1
Normal(2) = 0
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, True)
'FRONT
RootPoint(0) = 0
RootPoint(1) = 0
RootPoint(2) = 0
Normal(0) = 0
Normal(1) = 0
Normal(2) = 1
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, True)
'BACK
RootPoint(0) = 0
RootPoint(1) = 0
RootPoint(2) = -1
Normal(0) = 0
Normal(1) = 0
Normal(2) = 1
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, True)
'Create the body from the trimmed surfaces just created
TempBody.CreateBodyFromSurfaces
'Create an offset surface from the back
RootPoint(0) = 0
RootPoint(1) = 0
RootPoint(2) = -2
Normal(0) = 0
Normal(1) = 0
Normal(2) = 1
isGood = CreateSquareSurface(Part, TempBody, RootPoint, Normal, False)
Model.ViewZoomtofit2
'Clean up the memory
Set swApp = Nothing
Set Model = Nothing
Set Part = Nothing
End Sub
'CreateSquareSurface creates a square trimmed surface
Private Function CreateSquareSurface(Part As PartDoc, SurfaceBody As Body2, RootPoint As Variant, Normal As Variant, IsPartOfTempBody As Boolean) As Boolean
Dim isGood As Boolean
'Temporary surface
Dim TmpSurf As SldWorks.Surface
'Arguments that define the trimming loop
Dim NumCurves As Long
Dim Order(3) As Long
Dim Dimen(3) As Long
Dim Periodic(3) As Long
Dim NumKnots(3) As Long
Dim NumCtrlPoints(3) As Long
Dim Knots(15) As Double
Dim CtrlPointDbls(15) As Double
Dim UVRange(3) As Double
'Initially this function has no problems,
'set this value to false if errors encountered
CreateSquareSurface = True
'Create a new planar surface based at RootPoint with the Normal vector Normal
Set TmpSurf = SurfaceBody.CreatePlanarSurface(RootPoint, Normal)
If TmpSurf Is Nothing Then
CreateSquareSurface = False
Exit Function
End If
'Set the arguments to define a square trimming loop
'There are four curves (four sides)
NumCurves = 4
'Orders of the spline curves
Order(0) = 2
Order(1) = 2
Order(2) = 2
Order(3) = 2
'There are two dimensions
Dimen(0) = 2
Dimen(1) = 2
Dimen(2) = 2
Dimen(3) = 2
'No periodics
Periodic(0) = 0
Periodic(1) = 0
Periodic(2) = 0
Periodic(3) = 0
'There are four knots (corners)
NumKnots(0) = 4
NumKnots(1) = 4
NumKnots(2) = 4
NumKnots(3) = 4
'A square has two control points
NumCtrlPoints(0) = 2
NumCtrlPoints(1) = 2
NumCtrlPoints(2) = 2
NumCtrlPoints(3) = 2
'The locations of the knots
Knots(0) = 0
Knots(1) = 0
Knots(2) = 1
Knots(3) = 1
Knots(4) = 0
Knots(5) = 0
Knots(6) = 1
Knots(7) = 1
Knots(8) = 0
Knots(9) = 0
Knots(10) = 1
Knots(11) = 1
Knots(12) = 0
Knots(13) = 0
Knots(14) = 1
Knots(15) = 1
'Set the actual trimming corners
CtrlPointDbls(0) = 0: CtrlPointDbls(1) = 0
CtrlPointDbls(2) = 1: CtrlPointDbls(3) = 0
CtrlPointDbls(4) = 1: CtrlPointDbls(5) = 0
CtrlPointDbls(6) = 1: CtrlPointDbls(7) = 1
CtrlPointDbls(8) = 1: CtrlPointDbls(9) = 1
CtrlPointDbls(10) = 0: CtrlPointDbls(11) = 1
CtrlPointDbls(12) = 0: CtrlPointDbls(13) = 1
CtrlPointDbls(14) = 0: CtrlPointDbls(15) = 0
'The possible range for the UV values
UVRange(0) = 0
UVRange(1) = 1
UVRange(2) = 0
UVRange(3) = 1
'Create the trimming loop on the surface
isGood = TmpSurf.AddTrimmingLoop2(NumCurves, Order, Dimen, Periodic, NumKnots, NumCtrlPoints, Knots, CtrlPointDbls, UVRange)
If isGood = False Then
CreateSquareSurface = False
Exit Function
End If
'Create the trimmed surface on the body
'based on the trimming loop just created
isGood = SurfaceBody.CreateTrimmedSurface
If isGood = False Then
CreateSquareSurface = False
Exit Function
End If
If IsPartOfTempBody Then
'If this surface is to be used in
'a temporary body, then you must generate it
Else
SurfaceBody.CreateBodyFromSurfaces
End If
End Function