Get Name of Sketch Segment Example (VBA)
This examples shows how to get the name of a sketch segment in a sheet
metal part or drawing containing a sheet metal part.
'---------------------------------------------
'
' Preconditions:
' (1)
Sheet metal part or a drawing containing a sheet metal part is open.
' (2)
Sheet metal part is flattened.
' (3)
Bend feature is selected in the FeatureManager design tree.
'
' Postconditions: None
'
'-----------------------------------------------
Option Explicit
Public Enum swSelectType_e
swSelDATUMPLANES
= 4 '
"PLANE"
swSelSKETCHES
= 9 '
"SKETCH"
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swMathUtil As
SldWorks.MathUtility
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFeat As
SldWorks.feature
Dim
swOneBend As
SldWorks.OneBendFeatureData
Dim
swSketchSeg As
SldWorks.SketchSegment
Dim
swSketch As
SldWorks.sketch
Dim
swSketchFeat As
SldWorks.feature
Dim
swSketchLine As
SldWorks.SketchLine
Dim
swSkStartPt As
SldWorks.SketchPoint
Dim
swSkEndPt As
SldWorks.SketchPoint
Dim
swSelData As
SldWorks.SelectData
Dim
nPt(2) As
Double
Dim
vPt As
Variant
Dim
swStartPt As
SldWorks.MathPoint
Dim
swEndPt As
SldWorks.MathPoint
Dim
swEnt As
SldWorks.entity
Dim
swSkXform As
SldWorks.MathTransform
Dim
vID As
Variant
Dim
nEntType As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swMathUtil = swApp.GetMathUtility
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swFeat = swSelMgr.GetSelectedObject5(1)
Set
swSelData = swSelMgr.CreateSelectData
Set
swOneBend = swFeat.GetDefinition
Set
swSketchSeg = swOneBend.FlatPatternSketchSegment
Set
swSketch = swSketchSeg.GetSketch
Set
swSketchLine = swSketchSeg
Set
swSkStartPt = swSketchLine.GetStartPoint2
Set
swSkEndPt = swSketchLine.GetEndPoint2
vID
= swSketchSeg.GetId
'
This will be a datum/reference plane, swSelDATUMPLANES
Set
swEnt = swSketch.GetReferenceEntity(nEntType)
'
Cannot select entity (plane) if obtained from
'
OneBendFeatureData::FlatPatternSketchSegment
bRet
= swEnt.Select4(False, swSelData):
Debug.Assert bRet
'
This will be a sketch, swSelSKETCHES
Set
swEnt = swSketch
'
Cannot select entity (sketch) if obtained from
'
OneBendFeatureData::FlatPatternSketchSegment
bRet
= swEnt.Select4(False, swSelData):
Debug.Assert bRet
'
Get sketch feature
Set
swSketchFeat = swSketch
Set
swSkXform = swSketch.ModelToSketchTransform
Set
swSkXform = swSkXform.Inverse
nPt(0)
= swSkStartPt.x
nPt(1)
= swSkStartPt.y
nPt(2)
= swSkStartPt.z
vPt
= nPt
Set
swStartPt = swMathUtil.CreatePoint(vPt)
Set
swStartPt = swStartPt.MultiplyTransform(swSkXform)
nPt(0)
= swSkEndPt.x
nPt(1)
= swSkEndPt.y
nPt(2)
= swSkEndPt.z
vPt
= nPt
Set
swEndPt = swMathUtil.CreatePoint(vPt)
Set
swEndPt = swEndPt.MultiplyTransform(swSkXform)
'
Cannot select sketch feature if obtained from
'
OneBendFeatureData::FlatPatternSketchSegment
bRet
= swSketchFeat.Select4(False,
swSelData): Debug.Assert bRet
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" Feature
= " & swFeat.Name &
" [" & swFeat.GetTypeName
& "]"
Debug.Print
" Sketch
=
" & swSketchFeat.Name
Debug.Print
" SegID
=
[" & vID(0) & ", " & vID(1) & "]"
Debug.Print
" Start
wrt sketch =
(" & swSkStartPt.x *
1000# & ", " & swSkStartPt.y
* 1000# & ", " & swSkStartPt.z
* 1000# & ") mm"
Debug.Print
" End
wrt
sketch =
(" & swSkEndPt.x * 1000#
& ", " & swSkEndPt.y
* 1000# & ", " & swSkEndPt.z
* 1000# & ") mm"
Debug.Print
" Start
wrt model =
(" & swStartPt.ArrayData(0)
* 1000# & ", " & swStartPt.ArrayData(1)
* 1000# & ", " & swStartPt.ArrayData(2)
* 1000# & ") mm"
Debug.Print
" End
wrt
model =
(" & swEndPt.ArrayData(0)
* 1000# & ", " & swEndPt.ArrayData(1)
* 1000# & ", " & swEndPt.ArrayData(2)
* 1000# & ") mm"
End Sub
'---------------------------------------------