|
.stl export of multibody part
hi all,
i'm writing a small vb6.0 app that saves multibody part to a number of stl's along with a file that's used as command/preprocessor batch file for our casting analysis software (magmasoft).
app checks body folders (as defined by designer), selects body by body in certain order (as required by magma modeling principle) and spits them out as stl, while appending certain info to an ascii file.
problem is in stl triangulation time. at complex models (hydrodynamic shapes, many bodies), solidworks triangulates all bodies first, then asks do i want to save all bodies or only a selected one (it does this for each body again and again). while i can save all these bodies in separate files in one hit, it be much better option for me to be able to do it via code.
so, my questions are:
1. is there a way to make sw triangulate and save *only* selected body, without triangulating "kitchen and the sink" first?
2. short of workable answer to q1, what is the best way to delete a body using vb6.0?
cheers and thanks,
mladen
edited: 01/18/2009 at 10:44 pm by mladen meduric
is your code something like this?
option explicit
private sub command1_click()
dim swapp as sldworks.sldworks
dim swmodel1 as sldworks.modeldoc2
dim swpart1 as sldworks.partdoc
dim swbody1 as sldworks.body2
dim vswbody1 as variant
dim vbodyresarr1 as variant
dim bret as boolean
dim i as integer
set swapp = getobject(, "sldworks.application")
set swmodel1 = swapp.activedoc
set swpart1 = swmodel1
vbodyresarr1 = swpart1.getbodies2(swsolidbody, true)
for each vswbody1 in vbodyresarr1
swmodel1.clearselection
set swbody1 = vswbody1
swbody1.select false, -1
set swpart1 = swapp.activedoc
bret = swpart1.savetofile2("d:\x" & i & ".stl", 0, 0, 0): debug.assert bret
set swpart1 = swapp.activedoc
swapp.closedoc swpart1.gettitle
i = i + 1
next vswbody1
end sub
i would recommend traversing the bodies twice. the first time through do nothing but hide all the bodies. the next time through, show each body, export, and re-hide. i guess you could traverse a third time and re-show all the bodies. this should run pretty quickly. to further improve performance, you might want to try setting modelview::enablegraphicsupdate to false. if you do this, though, you must remember to set it back to true, and you must have an error handling routine to set it back to true. otherwise if your macro ends due to an error, there is no way to reset enablegraphicsupdate through the user interface.
i'll get you eh steve, if it's the last thing i dooooo!
thanks for your replies.
ivana, yes, in essence, that's what i currently use. but i got to it entirely accidentally, thanks to forgetting to remove ".stl" in swpart.savetofile2 syntax (copy'n'paste, you know the drill...) .
my problem and question posted was/is with swpart.saveas4 command as an "official" way of dealing with conversions. in a year or few time, if i need to maintain the code, i'll likely forget that swpart.savetofile2 was used as a trick (api manual says it's for part saving as in *.sldprt !!!). so, i anticipate i'd search the manual and wander why on earth did i use a command that's not even mentioning stl..... it's about a clean code.
anyway, i've sent an enhancement request, so let's see what comes up as swpart.saveas4 is currently unusable for my type of issue.
cheers,
mladen
edited: 01/19/2009 at 05:38 pm by mladen meduric
why is it unusable for your type issue? why can't you hide bodies then save?
i'll get you eh steve, if it's the last thing i dooooo!
i haven't tried your suggestion because i got working code already.
but, speaking about it, why would i need to hide/unhide bodies? i want it working without "trickery" or trial and error, so when my designers want to do their bit of job at hand, it works without any preconditions... and i don't need to write any "manuals" ;-).
as said above, it's about clean code that's easy to maintain in time to come, not about tricks and preconditions. sure, i'd do it if that's the only way, but it's not.
it's not a "trick" or a precondition. you do it all in code. easy as the proverbial pie. you asked about deleting bodies. that would certainly be a bad way to do it. hiding bodies will "hide" them from export. they won't get triangulated.
i'll get you eh steve, if it's the last thing i dooooo!
this code is a slight modification of ivana's code. it uses saveas4 to silently save all bodies to .stl in the same folder as the original document. the stl file has the name of the original document followed by the name of the body from the design tree. it incorporates enabling/disabling of graphics updating. no preconditions, no "tricks". the code is easy to follow later, even if you don't use any comments.
option explicit
private sub savebodiestostl()
dim swapp as sldworks.sldworks
dim swmodel1 as sldworks.modeldoc2
dim swpart1 as sldworks.partdoc
dim swbody1 as sldworks.body2
dim vswbody1 as variant
dim vbodyresarr1 as variant
dim bret as boolean
dim i as integer
dim save2name as string
set swapp = getobject(, "sldworks.application")
set swmodel1 = swapp.activedoc
set swpart1 = swmodel1
vbodyresarr1 = swpart1.getbodies2(swsolidbody, true)
on error goto graphicsrestore
swmodel1.activeview.enablegraphicsupdate = false
for each vswbody1 in vbodyresarr1
swmodel1.clearselection
set swbody1 = vswbody1
swbody1.select false, -1 'select the body of interest
swmodel1.extension.runcommand swcommands_invertselection, "" 'invert selection to select all other bodies
swmodel1.hidesolidbody 'hide the selected bodies
save2name = left(swmodel1.getpathname, len(swmodel1.getpathname) - 7) & " - " & swbody1.name & ".stl"
bret = swmodel1.saveas4(save2name, 0, swsaveasoptions_silent + swsaveasoptions_copy, empty, empty)
swbody1.select false, -1 're-select the body of interest
swmodel1.extension.runcommand swcommands_invertselection, "" 'select all the hidden bodies
swmodel1.showsolidbody 'show all the selected hidden bodies
i = i + 1
next vswbody1
graphicsrestore:
swmodel1.activeview.enablegraphicsupdate = true
msgbox "finished"
end sub
i'll get you eh steve, if it's the last thing i dooooo!
edited: 01/21/2009 at 09:06 am by josh brady
here is a more elegant, easy to maintain method for doing the same as above. and it runs about 10 times faster. rather than using the runcommand function (which basically mimics user actions and is very slow), it operates directly on the bodies.
option explicit
private sub fastsavebodiestostl()
dim swapp as sldworks.sldworks
dim swmodel1 as sldworks.modeldoc2
dim swbody1 as sldworks.body2
dim swpart1 as sldworks.partdoc
dim vbodyresarr1 as variant
dim bret as boolean
dim i as integer
dim save2name as string
set swapp = getobject(, "sldworks.application")
set swmodel1 = swapp.activedoc
set swpart1 = swmodel1
vbodyresarr1 = swpart1.getbodies2(swsolidbody, true) 'array of visible bodies
on error goto graphicsrestore
swmodel1.activeview.enablegraphicsupdate = false 'disable graphics for performance
swmodel1.clearselection2 true
'hide all the visible bodies
for i = 0 to ubound(vbodyresarr1)
set swbody1 = vbodyresarr1(i)
swbody1.hidebody true
next i
'show and save bodies to .stl one by one
for i = 0 to ubound(vbodyresarr1)
set swbody1 = vbodyresarr1(i)
swbody1.hidebody false
save2name = left(swmodel1.getpathname, len(swmodel1.getpathname) - 7) & " - " & swbody1.name & ".stl"
bret = swmodel1.saveas4(save2name, 0, swsaveasoptions_silent + swsaveasoptions_copy, empty, empty)
swbody1.hidebody true
next i
're-show all the bodies hidden earlier
for i = 0 to ubound(vbodyresarr1)
set swbody1 = vbodyresarr1(i)
swbody1.hidebody false
next i
graphicsrestore:
swmodel1.activeview.enablegraphicsupdate = true
if err.number <> 0 then
msgbox "finished with errors"
else
msgbox "finished"
end if
end sub
i'll get you eh steve, if it's the last thing i dooooo!
josh, thanks for the effort.
i get your point: it can be done if hide/unhide used, but i'd say you didn't get mine. perhaps i should have been clearer: i meant why do i need to hide bodies in order to export visible one(s)? why not just select and export selected? my take is sw needs to enable this option.
not everybody knows nor it should know about effect of hide/unhide at export (i didn't), hence calling it a "trick"...
...not in the manual - it's a "trick" (or experience).
as one of the colleagues at ansys forum said it, just paraphrased for solidworks:
"overcoming solidworks deficiencies since 1996"
cheers,
mladen
quick |
|