|
parsing the component2.name2
here is a little function i wrote to use in assemblies that allows one to use the component.name2 raw string to build the correct selectbyid2 string to be assigned to a variable and called when needed. as you can see it could have many uses. let me know what you think. i hope this might be helpful.
get your raw string (example, adding a part to an assembly):
set swcomp = swassy.addcomponent4(partfilename, "", itempoint(0), itempoint(1), itempoint(2))
set swdoc = swapp.activatedoc2(assyfilename, true, swerrors) ' go back to assy
itemstatusb = swcomp.select(false) ' select part
'grab the front plane of that part
assyitempart = "front plane" + "@" + itemselectid((swcomp.name2))
' select the part plane for the mate
itemstatusb = swmodeldocext.selectbyid2(assyitempart, "plane", 0, 0, 0, true, 0, nothing, 0)
'------- start function code --------
' parse string into correct format
private function itemselectid(assyitemloc as string) as string
on error resume next
dim swapp as sldworks.sldworks
dim swdoc as sldworks.modeldoc2
dim swassy as sldworks.assemblydoc
dim ldoctype as long, offset as long
dim assycount as long, assytotal as long, swerrors as long
dim assyroot as string, assyactive as string
dim assyitemname as string, assyitemprefix as string
dim assyactiveshort as string, assyactiveshortsub as string
dim assyitemsplit as variant
set swapp = application.sldworks
set swdoc = swapp.activedoc
set swassy = swdoc
ldoctype = swdoc.gettype
' checks to see if the current document is an assembly
if ldoctype = 0 then ' no file open
msgbox "open or create a new assembly.", vbcritical, "no assembly document open"
exit function
elseif ldoctype = 2 then ' assembly file
' all is good
else ' part and drawing file - not allowed
msgbox "this macro only works on assemblies", vbcritical, "incorrect document type"
exit function
end if
assyroot = swdoc.gettitle ' get assy document name
if assyroot like "*.sldasm" then ' strip off the assyroot extension
assyroot = left$(assyroot, len(assyroot) - 7)
end if
' breakdown assy item location and name
assyitemsplit = split(assyitemloc, "/")
assytotal = ubound(assyitemsplit)
assyactive = assyitemsplit(assytotal - 1)
assyitemname = assyitemsplit(assytotal)
' create short assyactive name
if not assyactive = assyroot then
offset = instrrev(assyactive, chr$(45))
assyactiveshort = left$(assyactive, offset - 1)
else
assyactiveshort = assyroot
end if
assycount = 0 ' start counter at 0
' start parsing for path id
do while assycount <= assytotal
assyitemprefix = assyitemsplit(0) + "@" + assyroot
if assytotal = 0 then ' item located at 1st level assy
itemselectid = assyitemprefix
debug.print "new string: " + itemselectid
exit function
elseif assytotal = 1 and assycount = 0 then ' item located at 2nd level assy
' nothing to do already covered
elseif assytotal > 1 and assycount < (assytotal - 1) then ' item located at 3rd level assy and on
offset = instrrev(assyitemsplit(assycount), chr$(45))
assyactiveshortsub = left$(assyitemsplit(assycount), offset - 1)
itemselectid = itemselectid + "/" + assyitemsplit(assycount + 1) + "@" + assyactiveshortsub
elseif assytotal = assycount then
itemselectid = assyitemprefix + itemselectid + "/" + assyitemname
exit do
end if
assycount = assycount + 1
loop
itemselectid = itemselectid + "@" + assyactiveshort
debug.print "new string: " + itemselectid
end function
'------- end function code --------
cadcam systems analyst
-solidworks office premium 2009 sp3.0
-solidworks simulation premium 2009 sp3.0
-solidworks flow simulation 2009 sp3.0
-2 cpu (fx-62), 2.0 gb of ram
-window xp pro sp2
-nvidia geforce 7950 gx2 (512mb) 6.14.11.6921
edited: 02/01/2009 at 05:14 pm by william crosby
quick |
|