|
custom properties bom ---> txt
hello fella's,
i've been looking into making a macro for 1 specific task, but i'm finding myself in need of some advice.
to sketch my situation :
i've got 1 assembly, containing several (3-4) subassemblies. these subassemblies each contain vareous parts.
these parts (and only the parts) all have 1 thing in common. they all have a custom property called "bomcode" (meaning bill of material code).
what i'm trying to do is to grab the value of the custom property "bomcode" of each part (unsuppressed and visible), and write these away in an txt file , 1 line at a time.
what i mean with unsuppressed and visible :
like any other bom-table would, only list the parts that aren't suppressed.
there are no fancy configurations envolved.
here is a simplified example of my current layout :
this txt file shows what i'm trying to retrieve; (the lines in the txt file represent the custom property value)
hoping on some good advice,
yours
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
you just want to traverse the assembly tree to get all features, then for each feature check its type and its suppression and visibility, and if it is a part that is visible and unsuppressed, get its model then get its custom property.
a perfect example for you already exists in the api help for 90% of what you want to do. just type in "traverse featuremanager design tree (vb)" in the search
thanks for the quick reply there luke !
i'll give it a try, and keep you updated !
cheers,
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
ok this is what i have thusfar :
i'm able to traverse the feat. tree and pick out all components (parts & assemblies) that are not suppressed, and write these (the component names) away to an external txt file.
what id still would like to add is ;
- have only parts in the list (drop assemblies)
- only parts with the custom property "bomcode" allowed on the list
- only have bomcode.value written into the textfile (instead of component names)
this is how my code looks like (snippets alert ) :
option explicit
global fs, a as object
dim traverselevel as integer
sub writecptext()
dim swapp as sldworks.sldworks
dim modeldoc2 as sldworks.modeldoc2
dim featuremgr as sldworks.featuremanager
dim rootnode as sldworks.treecontrolitem
set swapp = application.sldworks
set modeldoc2 = swapp.activedoc
set featuremgr = modeldoc2.featuremanager
set rootnode = featuremgr.getfeaturetreerootitem()
set fs = createobject("scripting.filesystemobject")
set a = fs.createtextfile("c:\" & plnr & ".txt", true)
if not rootnode is nothing then
debug.print
traverselevel = 0
traverse_node rootnode
end if
a.close
end sub
private sub traverse_node(node as sldworks.treecontrolitem)
dim childnode as sldworks.treecontrolitem
dim featurenode as sldworks.feature
dim componentnode as sldworks.component2
dim nodeobjecttype as long
dim nodeobject as object
dim restofstring as string
dim displaynodeinfo as boolean
dim compname as string
dim componentdoc as object
displaynodeinfo = false
nodeobjecttype = node.objecttype
set nodeobject = node.object
select case nodeobjecttype
case swconst.swtreecontrolitemtype_e.swfeaturemanageritem_component: 'only when nodeobjecttyp is a component (would like to have parts only, filter out assemblies)
if not nodeobject is nothing then
set componentnode = nodeobject
compname = componentnode.name2
set componentdoc = componentnode.getmodeldoc
if componentnode.getsuppression() = swconst.swcomponentsuppressionstate_e.swcomponentfullyresolved then 'is part suppressed ?
'here i'd like to see an additional filter to only let parts trough that have a custom property called "bomcode"
displaynodeinfo = true
end if
restofstring = "[component: " & compname & "]" 'this could/should be value of bombcode (custom property) of the current componetn beeing handled.
else
restofstring = "[component: object null?!]"
end if
case else:
displaynodeinfo = false
if not nodeobject is nothing then
restofstring = "[object type not handled]"
else
restofstring = "[object null?!]"
end if
end select
if (displaynodeinfo) then
a.writeline (node.text & " : " & restofstring) 'here i'd like to write away the value of bomcode (custom property)
end if
traverselevel = traverselevel + 1
set childnode = node.getfirstchild()
while not childnode is nothing
traverse_node childnode
set childnode = childnode.getnext
wend
traverselevel = traverselevel - 1
end sub
i'm sure this might be coded more compactly and efficiently but im not ace in vb .
suggestions are welcome !
so where do i still need help, you ask ?
mainly on the spots with highlighted comments.
1) how to tell the difference between an assembly and part.
2) how can i detect if the current proccesed part has the custom property "bomcode"
3) how do i capture that value of "bomcode" to write away in the textfile
thanks for reading al this and sticking with me !
hoping on some good advice,
regards,
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
answer option explicit
sub main()
dim swapp as sldworks.sldworks
dim swassy as assemblydoc
dim swmod as sldworks.modeldoc2
dim swcustpropmgr as sldworks.custompropertymanager
dim comp as sldworks.component2
dim vs as variant
dim v as variant
dim valout as string
dim revalout as string
dim fs, a as object
dim plnr as string
plnr = "something"
set fs = createobject("scripting.filesystemobject")
set a = fs.createtextfile("c:\" & plnr & ".txt", true)
set swapp = application.sldworks
set swmod = swapp.activedoc
if swmod.gettype = swdocassembly then
set swassy = swmod
vs = swassy.getcomponents(false)
for each v in vs
set comp = v
if comp.getsuppression = swcomponentfullyresolved then
if comp.getmodeldoc.gettype = swdocpart then
set swcustpropmgr = comp.getmodeldoc.extension.custompropertymanager("")
swcustpropmgr.get2 "bomcode", valout, revalout
if valout <> "" then
a.writeline valout 'here i'd like to write away the value of bomcode (custom property)
else
end if
end if
end if
next v
end if
end sub
woohooow ivana !
thats some mean lean mighty code you've pumped in here !!
thanks alot ! this does all the sweetness i ished for !!
humble and kind regards !
bert
solidworks professional 2009 sp2.0
hpxw4400 workstation
intel core2 cpu
6600 @ 2.40ghz
yes, i'm aware i have a funky surname ....
quick |
|