solidworks二次开发—07—控制草图对象
get all elements of sketch example (vb)
solidwork中对草图的控制,下面的例子很详细。特征下的草图在solidwork中其实是特征的子特征,我们可以对特征进行getfirstsubfeature、及getnextsubfeature得到。
如果有需要大家可以从中找到对直线、弧线、圆等对象的操作。代码是solidworks的示例文件,里面充斥了debug.print,只是向用户显示程序执行的结果。
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 explicit
public enum swsksegments_e
swsketchline = 0
swsketcharc = 1
swsketchellipse = 2
swsketchspline = 3
swsketchtext = 4
swsketchparabola = 5
end enum
sub processtextformat _
( _
swapp as sldworks.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.escapement
debug.print " isheightspecifiedinpts = " & swtextformat.isheightspecifiedinpts
debug.print " italic = " & swtextformat.italic
debug.print " linelength = " & swtextformat.linelength
debug.print " linespacing = " & swtextformat.linespacing
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 sldworks.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 swmathutil = swapp.getmathutility
set swxform = swsketch.modeltosketchtransform
set swxform = swxform.inverse
set swmathpt = swmathutil.createpoint((vpt))
set swmathpt = swmathpt.multiplytransform(swxform)
set transformsketchpointtomodelspace = swmathpt
end function
sub processsketchline _
( _
swapp as sldworks.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 swendpt = swskline.getendpoint2
set swstartmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swstartpt)
set swendmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swendpt)
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 (sketch) = (" & swendpt.x * 1000# & ", " & swendpt.y * 1000# & ", " & swendpt.z * 1000# & ") mm"
debug.print " end (model ) = (" & swendmodpt.arraydata(0) * 1000# & ", " & swendmodpt.arraydata(1) * 1000# & ", " & swendmodpt.arraydata(2) * 1000# & ") mm"
end sub
sub processsketcharc _
( _
swapp as sldworks.sldworks, _
swmodel as sldworks.modeldoc2, _
swsketch as sldworks.sketch, _
swskarc as sldworks.sketcharc _
)
dim swstartpt as sldworks.sketchpoint
dim swendpt as sldworks.sketchpoint
dim 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, swmodel, swsketch, swstartpt)
set swendmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swendpt)
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 (sketch) = (" & swendpt.x * 1000# & ", " & swendpt.y * 1000# & ", " & swendpt.z * 1000# & ") mm"
debug.print " end (model ) = (" & 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(model ) = (" & 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.getrotationdir
end sub
sub processsketchellipse _
( _
swapp as sldworks.sldworks, _
swmodel as sldworks.modeldoc2, _
swsketch as sldworks.sketch, _
swskellipse as sldworks.sketchellipse _
)
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 swstartpt = swskellipse.getstartpoint2
set swendpt = swskellipse.getendpoint2
set swctrpt = swskellipse.getcenterpoint2
set swmajpt = swskellipse.getmajorpoint2
set swminpt = swskellipse.getminorpoint2
set swstartmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swstartpt)
set swendmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swendpt)
set swctrmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swctrpt)
set swmajmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swmajpt)
set swminmodpt = 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 (sketch) = (" & swendpt.x * 1000# & ", " & swendpt.y * 1000# & ", " & swendpt.z * 1000# & ") mm"
debug.print " end (model ) = (" & 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(model ) = (" & 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 (model ) = (" & 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 (model ) = (" & swminmodpt.arraydata(0) * 1000# & ", " & swminmodpt.arraydata(1) * 1000# & ", " & swminmodpt.arraydata(2) * 1000# & ") mm"
end sub
sub processsketchspline _
( _
swapp as sldworks.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, swmodel, swsketch, swsplinept)
debug.print " spline (sketch) = (" & swsplinept.x * 1000# & ", " & swsplinept.y * 1000# & ", " & swsplinept.z * 1000# & ") mm"
debug.print " spline (model ) = (" & swsplinemodpt.arraydata(0) * 1000# & ", " & swsplinemodpt.arraydata(1) * 1000# & ", " & swsplinemodpt.arraydata(2) * 1000# & ") mm"
next vsplinept
end sub
sub processsketchtext _
( _
swapp as sldworks.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 swmathutil = swapp.getmathutility
set swxform = swsketch.modeltosketchtransform
set swxform = swxform.inverse
set swcoordmodpt = swmathutil.createpoint((vcoordpt))
set swcoordmodpt = swcoordmodpt.multiplytransform(swxform)
debug.print " coords (sketch) = (" & vcoordpt(0) * 1000# & ", " & vcoordpt(1) * 1000# & ", " & vcoordpt(2) * 1000# & ") mm"
debug.print " coords (model ) = (" & swcoordmodpt.arraydata(0) * 1000# & ", " & swcoordmodpt.arraydata(1) * 1000# & ", " & swcoordmodpt.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 sldworks.sldworks, _
swmodel as sldworks.modeldoc2, _
swsketch as sldworks.sketch, _
swskparabola as sldworks.sketchparabola _
)
dim swapexpt as sldworks.sketchpoint
dim 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 swendpt = swskparabola.getendpoint2
set swfocalpt = swskparabola.getfocalpoint2
set swapexmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swapexpt)
set swstartmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swstartpt)
set swendmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swendpt)
set swfocalmodpt = transformsketchpointtomodelspace(swapp, swmodel, swsketch, swfocalpt)
debug.print " apex (sketch) = (" & swapexpt.x * 1000# & ", " & swapexpt.y * 1000# & ", " & swapexpt.z * 1000# & ") mm"
debug.print " apex (model ) = (" & 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 (sketch) = (" & swendpt.x * 1000# & ", " & swendpt.y * 1000# & ", " & swendpt.z * 1000# & ") mm"
debug.print " end (model ) = (" & 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.sketchtext
dim swskparabola as sldworks.sketchparabola
dim vid as variant
dim i as long
dim bret as boolean
ssksegmentsname(swsketchline) = "swsketchline"
ssksegmentsname(swsketcharc) = "swsketcharc"
ssksegmentsname(swsketchellipse) = "swsketchellipse"
ssksegmentsname(swsketchspline) = "swsketchspline"
ssksegmentsname(swsketchtext) = "swsketchtext"
ssksegmentsname(swsketchparabola) = "swsketchparabola"
set swapp = application.sldworks
set swmodel = 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 = swskseg
processsketchspline swapp, swmodel, 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
———————————————