SolidWorks Secondary Development -07-Control Sketch Object
Get All Elements of Sketch Example (VB)
The control of the sketch in SolidWork, the example below is very detailed. The sketch under the feature is actually characterized in SolidWork, and we can get feature GetFirstSubfeature, and getNextSubfeature.
If you need everyone, you can find the operation of the straight line, arc, and circle. The code is the sample file of SolidWorks, which is full of debug.print, just the result of the program execution to the user.
This Example Shows How To Get All of The Elements of A Sketch.
'---------------------------------------------
'Preconditions: Model Document Is Open and A Sketch Is Selected.
'Postconditions: None
'---------------------------------------------
Option expedition
Public Enum swsksegments_e
SWSKETCHLINE = 0
SWSKETCHARC = 1
SWSTCHELLIPSE = 2
SWSKETCHSPLINE = 3
SWSKETCHTEXT = 4
SWSKETCHPARABOLA = 5
END ENUM
SUB ProcessTextFormat_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SwTextFormat as SLDWORKS.TEXTFORMAT _
)
Debug.print "Backwards =" & SwTextFormat.Backwards
Debug.print "bold =" & swtextformat.bold
Debug.print "charheight =" & swtextformat.charheight
Debug.print "charheightinpts =" & swtextformat.charheightinpts
Debug.print "charspacingfactor =" & swtextformat.charspacingfactor
Debug.print "escapement =" & swtextFormat.escape
Debug.print "isheightspecifiedinpts =" & swtextformat.isHeightSpecifiedinpts
Debug.print "Italic =" & SwTextFormat.italic
Debug.print "LINELENGTH =" & swtextformat.LineLengthDebug.print "linespace =" & swtextformat.linespace
Debug.print "obliqueangle =" & swtextformat.obliqueangle
Debug.print "strikeout =" & swtextformat.strikeout
Debug.print "Typefacename =" & swtextformat.typefacename
Debug.print "underline =" & swtextformat.underline
Debug.print "Upsidedown =" & SwTextFormat.upsidedown
Debug.print "vertical =" & swtextformat.Vertical
Debug.print "widthfactor =" & swtextformat.widthfactor
Debug.print ""
End Sub
Function TransformsketchPointTomodelspace_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKPT as SLDWORKS.SKETCHPOINT _
) AS SLDWORKS.MATHPOINT
Dim Swmathutil As SLDWorks.mathutility
DIM SWXFORM As SLDWORKS.MATHTRANSFORM
DIM NPT (2) As Double
DIM VPT AS VARIANT
Dim Swmathpt as SLDWORKS.MATHPOINT
NPT (0) = swskpt.x: npt (1) = swskpt.y: npt (2) = swskpt.z
VPT = NPT
Set swamathutil = swapp.getMathutility
Set swxform = swsketch.modeltosketchtransform
Set swxform = swxform.inverse
Set swamathpt = swmathutil.createpoint ((vpt))
Set swimathpt = swmathpt.multiplyTransform (swxform)
Set TransformsketchPointTomodelSpace = SWMATHPT
END FUNCTION
Sub ProcessSketchline_ (_
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKLINE AS SLDWORKS.SKETCHLINE
)
DIM SWSTARTPT As SLDWORKS.SKETCHPOINT
DIM SWENDPT as SLDWORKS.SKETCHPOINT
DIM SWSTARTMODPT As SLDWORKS.MATHPOINT
DIM SWENDMODPT as SLDWORKS.MATHPOINT
Set swStartpt = swskline.getStartPoint2
Set swndpt = swskline.getendpoint2
Set SWSTARTMODPT = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swstartpt)
Set SwendModpt = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swndpt)
Debug.print "START (Sketch) = (" & SWStartPt.x * 1000 # & "," & SWStartpt.y * 1000 # & "," & SWStartpt.z * 1000 # & ") mm"
Debug.print "Start (Model) = (" & SWSTARTMODPT.ArrayData (0) * 1000 # & "," & SWSTARTMODPT.ArrayData (1) * 1000 # & "," & SWSTARTMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "End (" & SWENDPT.X * 1000 # & "," & SWENDPT.Y * 1000 # & "," & SWENDPT.Z * 1000 # & ") MM"
Debug.print "End (" & SWENDMODPT.ARRAYDATA (0) * 1000 # & "," & SWENDMODPT.ArrayData (1) * 1000 # & "," & SWENDMODPT.ArrayData (2) * 1000 # & ") mm"
End Sub
Sub ProcesssketCharc_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKARC AS SLDWORKS.SKETCHARC _
)
DIM SWSTARTPT As SLDWORKS.SKETCHPOINT
DIM SWENDPT AS SLDWORKS.SKETCHPOINTDIM SWCTRPT AS SLDWORKS.SKETCHPOINT
DIM Vnormal As Variant
DIM SWSTARTMODPT As SLDWORKS.MATHPOINT
DIM SWENDMODPT as SLDWORKS.MATHPOINT
DIM SWCTRMODPT As SLDWORKS.MATHPOINT
Set swStartpt = SWSKARC.GETSTARTPOINT2
Set SwendPt = SWSKARC.GETENDPOINT2
Set swCtrpt = SWSKARC.GETCENTERPOINT2
Set SWSTARTMODPT = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swstartpt)
Set SwendModpt = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swndpt)
Set swCtrModpt = TransformsketchPointTomodelSpace (swapp, swmodel, swsketch, swCtrpt)
vnormal = swskarc.getnormalvector
Debug.print "START (Sketch) = (" & SWStartPt.x * 1000 # & "," & SWStartpt.y * 1000 # & "," & SWStartpt.z * 1000 # & ") mm"
Debug.print "Start (Model) = (" & SWSTARTMODPT.ArrayData (0) * 1000 # & "," & SWSTARTMODPT.ArrayData (1) * 1000 # & "," & SWSTARTMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "End (" & SWENDPT.X * 1000 # & "," & SWENDPT.Y * 1000 # & "," & SWENDPT.Z * 1000 # & ") MM"
Debug.print "End (" & SWENDMODPT.ARRAYDATA (0) * 1000 # & "," & SWENDMODPT.ArrayData (1) * 1000 # & "," & SWENDMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "Center (" & SWCTRPT.X * 1000 # & "," & SWCTRPT.Y * 1000 # & "," & SWCTRPT.Z * 1000 # & ") mm" debug.print "Center ("& SWCTRMODPT.ArrayData (0) * 1000 # &", "& SWCTRMODPT.ArrayData (1) * 1000 # &", "& SWCTRMODPT.ARRAYDATA (2) * 1000 # &") mm "
Debug.print "Radius =" & SWSKARC.GETRADIUS * 1000 # & "mm"
Debug.print "iscircle =" & cbool (swskarc.iscircle)
Debug.print "Rot Dirn =" & SWSKARC.GETROTIONDIR
End Sub
Sub ProcessSketchellipse_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKELLIPSE AS SLDWORKS.STCHELLIPSE _
)
DIM SWSTARTPT As SLDWORKS.SKETCHPOINT
DIM SWENDPT as SLDWORKS.SKETCHPOINT
DIM SWCTRPT As SLDWORKS.SKETCHPOINT
DIM SWMAJPT As SLDWORKS.SKETCHPOINT
DIM SWMINPT As SLDWORKS.SKETCHPOINT
DIM SWSTARTMODPT As SLDWORKS.MATHPOINT
DIM SWENDMODPT as SLDWORKS.MATHPOINT
DIM SWCTRMODPT As SLDWORKS.MATHPOINT
DIM SWMAJMODPT as SLDWORKS.MATHPOINT
DIM SWMINMODPT as SLDWORKS.MATHPOINT
Set swastpt = swskellipse.getStartPoint2
Set SwendPt = SWSKELLIPSE.GETENDPOINT2
Set swCtrpt = SWSKELLIPSE.GETCENTERPOINT2
Set swamajpt = swskellipse.getmajorpoint2
Set swimpt = swskellipse.getminorpoint2
Set SWSTARTMODPT = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swstartpt)
Set SwendModpt = TransformsketchPointTomodelSpace (swapp, swmodel, swsketch, swndpt) set swCTRMODPT = TransformsketchPointTomodelSpace (swapp, swmodel, swsketch, switrpt)
Set swamajmodpt = transformsketchpointTomodelSpace (swap, swmodel, swsketch, swamajpt)
Set swimmodpt = transformsketchpointTomodelSpace (swapp, swmodel, swsketch, swminpt)
Debug.print "START (Sketch) = (" & SWStartPt.x * 1000 # & "," & SWStartpt.y * 1000 # & "," & SWStartpt.z * 1000 # & ") mm"
Debug.print "Start (Model) = (" & SWSTARTMODPT.ArrayData (0) * 1000 # & "," & SWSTARTMODPT.ArrayData (1) * 1000 # & "," & SWSTARTMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "End (" & SWENDPT.X * 1000 # & "," & SWENDPT.Y * 1000 # & "," & SWENDPT.Z * 1000 # & ") MM"
Debug.print "End (" & SWENDMODPT.ARRAYDATA (0) * 1000 # & "," & SWENDMODPT.ArrayData (1) * 1000 # & "," & SWENDMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "Center (Sketch) = (" & SWCTRPT.X * 1000 # & "," & SWCTRPT.Y * 1000 # & "," & SWCTRPT.Z * 1000 # & ") mm"
Debug.print "Center (" & SWCTRMODPT.ArrayData (0) * 1000 # & "," & SWCTRMODPT.ARRAYDATA (1) * 1000 # & "," & SWCTRMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "Major (Sketch) = (" & SWMajpt.x * 1000 # & "," & SWMAJPT.Y * 1000 # & "," & SWMAJPT.Z * 1000 # & ") MM" Debug.print "Major ("& SWMAJMODPT.ARRAYDATA (0) * 1000 # &", "& SWMAJMODPT.ArrayData (1) * 1000 # &", "& SWMAJMODPT.ArrayData (2) * 1000 # &") mm "
Debug.print "Minor (Sketch) = (" & SWMINPT.X * 1000 # & "," & SWMINPT.Y * 1000 # & "," & SWMINPT.Z * 1000 # & ") mm"
Debug.print "Minor (" & SWMINMODPT.ARRAYDATA (0) * 1000 # & "," & SWMINMODPT.ArrayData (1) * 1000 # & "," & SWMINMODPT.ArrayData (2) * 1000 # & ") mm"
End Sub
Sub ProcesSketchspline_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKSPLINE AS SLDWORKS.SKETCHSPLINE _
)
DIM VSPLinePTARR As Variant
DIM vsplinept as variant
DIM SWSPLINEPT AS SLDWORKS.SKETCHPOINT
DIM SWSPLINEMODPT As SLDWORKS.MATHPOINT
vsplineptarr = swskspline.getpoints2
For Each VSPlinept in vsplineptarr
Set swsplinept = vsplinept
Set SWSPLINEMODPT = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swsplinept)
Debug.print "spline (" & swsplinept.x * 1000 # & "," & swsplinept.y * 1000 # & "," & swsplinept.z * 1000 # & ") mm"
Debug.print "Spline (" & SWSPLINEMODPT.ArrayData (0) * 1000 # & "," & SWSPLINEMODPT.ArrayData (1) * 1000 # & "," & SWSPLINEMODPT.ArrayData (2) * 1000 # & ") MM" Next vsplinept
End Sub
Sub ProcesSketchText_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKText As SLDWORKS.SKETCHTEXT _
)
DIM VCOORDPT AS VARIANT
Dim Swmathutil As SLDWorks.mathutility
DIM SWXFORM As SLDWORKS.MATHTRANSFORM
DIM SWCOORDMODPT As SLDWORKS.MATHPOINT
vcoordpt = swsktext.getcoordinates
Set swamathutil = swapp.getMathutility
Set swxform = swsketch.modeltosketchtransform
Set swxform = swxform.inverse
SET SWCOORDMODPT = SWMATHUTIL.CREATEPOINT (VCOORDPT))
Set swagordmodpt = swcoordmodpt.multiplyTransform (SWXFORM)
Debug.print "Coords (" & vcoordPt (0) * 1000 # & "," & VcoordPT (1) * 1000 # & "," & VcoordPt (2) * 1000 # & ") mm"
Debug.print "Coords (Model) = (" & SWCoRDModpt.ArrayData (0) * 1000 # & "," & SWCoRDModpt.ArrayData (1) * 1000 # & "," & SWCoRDModpt.ArrayData (2) * 1000 # & ") mm"
Debug.print "Use Doc FMT =" & SWSKText.getuseDOCTextFormat
Debug.print "Text =" & swsktext.text
ProcessTextFormat Swapp, SWModel, SWSKText.getTextFormat
End Sub
Sub ProcessSketchparabola_
(_ _
Swapp as flterworks.sldworks, _
SWModel As SLDWORKS.MODELDOC2, _
SWSKETCH AS SLDWORKS.SKETCH, _
SWSKParabola as SLDWORKS.SKETCHPARABOLA _
)
DIM SWAPEXPT AS SLDWORKS.SKETCHPOINTDIM SWSTARTPT AS SLDWORKS.SKETCHPOINT
DIM SWENDPT as SLDWORKS.SKETCHPOINT
DIM SWFOCALPT As SLDWORKS.SKETCHPOINT
DIM SWAPEXMODPT as SLDWORKS.MATHPOINT
DIM SWSTARTMODPT As SLDWORKS.MATHPOINT
DIM SWENDMODPT as SLDWORKS.MATHPOINT
DIM SWFOCALMODPT as SLDWORKS.MATHPOINT
Set swapExpt = swskparabola.getapexpoint2
Set swStartpt = swskparabola.getStartPoint2
Set swndpt = swskparabola.getendpoint2
Set swfocalpt = swskparabola.getfocalpoint2
Set swapexmodpt = transformsketchpointTomodelSpace (swapp, swyxpt)
Set SWSTARTMODPT = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swstartpt)
Set SwendModpt = TransformsketchPointTomodelSpace (swapp, swydel, swsketch, swndpt)
Set swfocalmodpt = transformsketchpointTomodelSpace (swapp, swydel, swsketch, swfocalpt)
Debug.print "Apex (" & SWAPEXPT.X * 1000 # & "," & SWAPEXPT.Y * 1000 # & "," & SWAPEXPT.Z * 1000 # & ") mm"
Debug.print "Apex (" & swapExmodpt.ArrayData (0) * 1000 # & "," & SWAPEXMODPT.ARRAYDATA (1) * 1000 # & "," & SWAPEXMODPT.ARRAYDATA (2) * 1000 # & ") mm"
Debug.print "START (Sketch) = (" & SWStartPt.x * 1000 # & "," & SWStartpt.y * 1000 # & "," & SWStartpt.z * 1000 # & ") mm"
Debug.print "Start (Model) = (" & SWSTARTMODPT.ArrayData (0) * 1000 # & "," & SWSTARTMODPT.ArrayData (1) * 1000 # & "," & SWSTARTMODPT.ArrayData (2) * 1000 # & ") MM" debug.print "end (" & swendpt.x * 1000 # & "," & swndpt.y * 1000 # & "," & swndpt.z * 1000 # & ") mm"
Debug.print "End (" & SWENDMODPT.ARRAYDATA (0) * 1000 # & "," & SWENDMODPT.ArrayData (1) * 1000 # & "," & SWENDMODPT.ArrayData (2) * 1000 # & ") mm"
Debug.print "Focal (Sketch) = (" & SWFOCALPT.X * 1000 # & "," & SWFOCALPT.Y * 1000 # & "," & SWFOCALPT.Z * 1000 # & ") mm"
Debug.print "Focal (Model) = (" & SwfocalModpt.ArrayData (0) * 1000 # & "," & SWFOCalModpt.ArrayData (1) * 1000 # & "," & SWFOCalModpt.ArrayData (2) * 1000 # & ") mm"
End Sub
Sub main ()
DIM SSKSEGMENTSNAME (5) AS STRING
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 vsksegarr as variant
DIM vskseg as variant
DIM SWSKSEG As SLDWORKS.SKETCHSEGMENT
Dim Swskline as SLDWORKS.SKETCHLINE
Dim Swskarc as SLDWORKS.SKETCHARC
DIM SWSKELLIPSE As SLDWORKS.SKETCHELLIPSE
DIM SWSKSPLINE As SLDWORKS.SKETCHSPLINE
Dim Swsktext As SLDWORKS.SKETCHTEXTDIM SWSKPARABOLA AS SLDWORKS.SKETCHPARABOLA
DIM VID AS VARIANT
DIM I as long
Dim Bret As Boolean
SSKSEGMENTSNAME (SWSKETCHLINE) = "SWSKETCHLINE"
SSKSEGMENTSNAME (SWSTCHARC) = "SWSTCHARC"
SSKSEGMENTSNAME (SWSKETCHELLIPSE) = "swsketchellipse"
SSKSEGMENTSNAME (SWSKETCHSPLINE) = "swsketchspline"
SSKSEGMENTSNAME (SWSTCHTEXT) = "SWSTCHTEXT"
SSKSEGMENTSNAME (SWSKETCHPARABOLA) = "SWSTCHPARABOLA"
Set swapp = application.sldworks
Set swymodel = swapp.activedoc
Set swselmgr = swmodel.selectionManager
Set swfeat = swselmgr.getSelectedObject5 (1)
Set swsketch = swfeat.getspecificfeature
Debug.print "feature =" & swfeat.name & "[" & swsketch.is3d & "]"
Debug.print "Sketch Segments:"
vsksegarr = swsketch.getsketchsegments
For Each vskseg in vsksegarr
Set swskseg = vskseg
VID = swskseg.getID
Debug.print "ID = [" & VID (0) & "," & VID (1) & "]"
Debug.print "Type =" & SSKSEGmentsName (swskseg.gettype)
Debug.print "constgeom =" & swskseg.constructiongeometry
Select Case Swskseg.gettype
Case swsketchline
Set swskline = swskseg
Processsketchline Swapp, SWModel, SWSKETCH, SWSKLINE
Case swsketcharc
Set swskarc = swskseg
ProcessSketcharc Swapp, Swmodel, SWSKETCH, SWSKARC
Case swsketchellipse
Set swskellipse = swskseg
ProcessSketchellipse Swapp, Swmodel, SWSKETCH, SWSKELLIPSE
Case swsketchspline
Set swskspline = swsksegprocesssketchspline swapp, swydel, swsketch, swskspline
Case swsketchtext
Set swsktext = swskseg
ProcesssketchText Swapp, SWModel, SWSKETCH, SWSKText
Case Swsketchparabola
Set swskparabola = swskseg
ProcessSketchparabola Swapp, SWModel, Swsketch, Swskparabola
Case Default
Debug.Assert False
End SELECT
NEXT VSKSEG
End Sub
'---------------------------------------------