![]() |
【转帖】mass properties breakdown for assy
mass properties breakdown for assy
i am trying to iterate over a largish (216 parts, 37 assy) assy and get information about each part, specifically i need the parts mass, and the parts center of gravity coordinates in the assy. i have experience with vb and sw but not the api until now. i have disected the assembly transversal example and the mass properties example and frankenstiened them together. i think either my ancient vb experience or my lack of sw api experience is getting in the way having problems addressing my part's model object: set partmodel = partcomp.getmodeldoc2 set swdocext = partmodel.extension i am apparently not using the getmodeldoc2 function correctly, i have attached the whole .bas if someone wants to give it a shot my machine: sw 2009 64 bit, xp 64, 12 gb ram, nvidia quadro fx 3700 -doug edited: 11/07/2008 at 02:23 pm by douglas johnson i'm not using 2009, so i'm not sure if they changed it, but on 2008 the call is 'getmodeldoc' assemblydoc::getcomponents component2::getmodeldoc i have tried it both ways, getmodeldoc and getmodeldoc2 work in parrallel i get the same error either way. see the following code for a running mass total of assembly components. dim swapp as sldworks.sldworks dim swcomps as variant dim swassy as sldworks.assemblydoc dim swpart as sldworks.modeldoc2 dim swcompcount as long dim masstotal(0 to 1) as double dim swdocext as sldworks.modeldocextension dim swmass as sldworks.massproperty dim bret as boolean sub main() set swapp = application.sldworks set swassy = swapp.activedoc bret = swassy.resolvealllightweightcomponents(false) swcompcount = swassy.getcomponentcount(false) swcomps = swassy.getcomponents(false) masstotal(0) = 0 for i = lbound(swcomps) to ubound(swcomps) set swpart = swcomps(i).getmodeldoc if swpart.gettype = swdocpart then set swdocext = swpart.extension set swmass = swdocext.createmassproperty masstotal(1) = masstotal(0) + (swmass.mass * 2.2046226) debug.print "running mass: " & round(masstotal(0), 2) & " lbs. + " & round(swmass.mass * 2.2046226, 2) & " lbs. = " & round(masstotal(1), 2) & " lbs." masstotal(0) = masstotal(1) end if next debug.print "total mass: " & round(masstotal(1), 2) & " lbs. " end sub edited: 11/10/2008 at 02:32 pm by blake dahle so i got the model doc thing working using your code but now i am discovering that all of my cg coordinates aren't in the assembly coordinates system but in the individual part coordinate system, is there some way around this? blake, i was looking for something exactly like the code you posted..here is my dilema...i do not know how to create a macro using this code! i thought i could mearly copy this code create a new macro in sw and paste it and try and run it and voila! but no such luck, i get the following error: run-time error '91': object variable or with block variable not set and then when i click the debug button it highlights in yellow: if swpart.gettype = swdocpart then below is exactly how i have the macro: sub main() set swapp = application.sldworks dim swcomps as variant dim swassy as sldworks.assemblydoc dim swpart as sldworks.modeldoc2 dim swcompcount as long dim masstotal(0 to 1) as double dim swdocext as sldworks.modeldocextension dim swmass as sldworks.massproperty dim bret as boolean set swapp = application.sldworks set swassy = swapp.activedoc bret = swassy.resolvealllightweightcomponents(false) swcompcount = swassy.getcomponentcount(false) swcomps = swassy.getcomponents(false) masstotal(0) = 0 for i = lbound(swcomps) to ubound(swcomps) set swpart = swcomps(i).getmodeldoc if swpart.gettype = swdocpart then set swdocext = swpart.extension set swmass = swdocext.createmassproperty masstotal(1) = masstotal(0) + (swmass.mass * 2.2046226) debug.print "running mass: " & round(masstotal(0), 2) & " lbs. + " & round(swmass.mass * 2.2046226, 2) & " lbs. = " & round(masstotal(1), 2) & " lbs." masstotal(0) = masstotal(1) end if next debug.print "total mass: " & round(masstotal(1), 2) & " lbs. " end sub am i even on the right track here? thanks, rodney peterson if at first you don't suceed, get a big hammer and smash your computer into bits and pieces. i'm not sure why you would be getting that error, on that line. can you send me the files you are using and i will look at it? i was unable to re-create that error with that code. sure, which files are you referring too? the assembly and all the parts or the macro i created? thanks if at first you don't suceed, get a big hammer and smash your computer into bits and pieces. all the referenced files, assy/parts and macro. also what version are you running? ok, i have attached a zip file with everything in it and we are using sw2008 sp4. tanks! if at first you don't suceed, get a big hammer and smash your computer into bits and pieces. the assembly contains toolbox items which pull in the api code as component2 objects, but are not converted to modeldoc2 objects. that is what is causing the error. if you change the code to the following it will ignore the toolbox items, and get all the other components. dim swapp as object sub main() set swapp = application.sldworks dim swcomps as variant dim swassy as sldworks.assemblydoc dim swpart as sldworks.modeldoc2 dim swcompcount as long dim masstotal(0 to 1) as double dim swdocext as sldworks.modeldocextension dim swmass as sldworks.massproperty dim bret as boolean set swapp = application.sldworks set swassy = swapp.activedoc bret = swassy.resolvealllightweightcomponents(false) swcompcount = swassy.getcomponentcount(false) swcomps = swassy.getcomponents(false) masstotal(0) = 0 for i = lbound(swcomps) to ubound(swcomps) set swpart = swcomps(i).getmodeldoc if not swpart is nothing then if swpart.gettype = swdocpart then set swdocext = swpart.extension set swmass = swdocext.createmassproperty masstotal(1) = masstotal(0) + (swmass.mass * 2.2046226) debug.print "running mass: " & round(masstotal(0), 2) & " lbs. + " & round(swmass.mass * 2.2046226, 2) & " lbs. = " & round(masstotal(1), 2) & " lbs." masstotal(0) = masstotal(1) end if end if next debug.print "total mass: " & round(masstotal(1), 2) & " lbs. " end sub |
所有的时间均为北京时间。 现在的时间是 08:16 AM. |