Create Sketch Path Example (VBA)
This example shows how to create a sketch path.
'****************************************************
'
' Preconditions: Open
part with a sketch with series of connected
' selected
segments selected.
'
' Postconditions: Sketch
segments are turned into a sketch path.
' Sketch
path information
' is
printed to debug window, and a tangent
' relation
is added between
' the
sketch path and a circle.
'
'****************************************************
Option Explicit
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFeat As
SldWorks.Feature
Dim
swSketch As
SldWorks.Sketch
Dim
i As
Long
Dim
bRet As
Boolean
Dim
vSketchPath As
Variant
Dim
swSketchPath As
SldWorks.SketchPath
Dim
nLength As
Double
Dim
vConstraint As
Variant
Dim
swSkRel As
SldWorks.SketchRelation
Dim
vRelation As
Variant
Dim
vSkRel As
Variant
Dim
vSketchSeg As
Variant
Dim
swSketchSeg As
SldWorks.SketchSegment
Dim
swSketchMgr As
SldWorks.SketchManager
Dim
swRelationMgr As
SldWorks.SketchRelationManager
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swSketchMgr = swModel.SketchManager
'
Select the sketch
bRet
= swModel.Extension.SelectByID2("Sketch1", "SKETCH",
0, 0, 0, False, 0, Nothing, 0)
Set
swFeat = swSelMgr.GetSelectedObject6(1,
-1)
Set
swSketch = swFeat.GetSpecificFeature2
'
Put the sketch in edit mode
swModel.EditSketch
Set
swRelationMgr = swSketch.RelationManager
'
Get the sketch segments
vSketchSeg
= swSketch.GetSketchSegments
Call
swModel.ClearSelection2(True)
For
i = 0 To UBound(vSketchSeg)
Set
swSketchSeg = vSketchSeg(i)
swSketchSeg.Select4 True, Nothing
Next
'
Create a chain using the selected sketch segments
bRet
= swSketchMgr.MakeSketchChain
Call
swModel.ClearSelection2(True)
'
Get the sketch path
vSketchPath
= swSketch.GetSketchPaths
'
Create a circle
swModel.CreateCircle -0.07515069296375, 0.04810565031983,
0, -0.06525335820896, 0.04189104477612, 0
'
Select the sketch path and the circle
Dim
vSketch(0 To 1) As Object
Set
vSketch(0) = swSelMgr.GetSelectedObject6(1,
-1)
Set
vSketch(1) = vSketchPath(0)
'
Add a tangent relation between the sketch path and circle
swRelationMgr.AddRelation vSketch, swConstraintType_TANGENT
swModel.ClearSelection2 True
'
Print to the debug window the number of constraints, number of relations,
'
path length, number of sketch segments, whether the path was selected,
'
type of constraints, and type of relations
For
i = 0 To UBound(vSketchPath)
Set
swSketchPath = vSketchPath(i)
Debug.Print
" Number of constraints: " & swSketchPath.GetConstraintsCount
Debug.Print
" Number of relations: " & swSketchPath.GetRelationsCount
Debug.Print
" Path length: " & swSketchPath.GetLength
Debug.Print
" Number of segments: " & swSketchPath.GetSketchSegmentCount
Debug.Print
" Selection result: " & swSketchPath.Select(False,
Nothing)
vConstraint
= swSketchPath.GetConstraints
Dim
j As Integer
j
= 0
If
(Not IsEmpty(vConstraint)) Then
For
j = 0 To UBound(vConstraint)
Debug.Print
" SketchSegConstraint["
& i & "] = " & vConstraint(j)
Next
j
End
If
vRelation
= swSketchPath.GetRelations
Dim
k As Integer
k
= 0
For
Each vSkRel In vRelation
Set
swSkRel = vSkRel
Debug.Print
" Relation("
& k & ")"
Debug.Print
" Type
=
" & swSkRel.GetRelationType
k
= k + 1
Next
'
Get the sketch segments in the sketch path and
'
their length
vSketchSeg
= swSketchPath.GetSketchSegments
Dim
l As Integer
For
l = 0 To UBound(vSketchSeg)
Set
swSketchSeg = vSketchSeg(l)
'
Ignore construction lines
If
swSketchSeg.ConstructionGeometry
= False Then
'
Ignore text
If
swSketchTEXT <> swSketchSeg.GetType
Then
nLength
= nLength + swSketchSeg.GetLength
End
If
End
If
Next
l
Debug.Print
" Total path length calculated by segment : " & nLength
Next
End Sub
'----------------------------------------------------