![]() |
【转帖】components on layers
components on layers
our company has to export drawings to acad for customers. we want these drawings to be on layers. currently notes, etc. are on layers, but we want individual components in assemblies to be on separate layers. yesterday i found a macro in the sw api help files that says it will do this, but i can't figure out how. i saved it as a macro, assigned it to an icon, select the view, and hit the icon. nothing seems to happen though. can anyone tell me what is wrong, or how to do this? (i am trying to learn vbscript, but am still very new) thank you! here's the script i'm working w/: (can someone tell me how i can copy/paste the script into here w/o it screwing the format? any easy way for me to show you what i'm working w/ would be great.) edited: 04/20/2007 at 04:06 pm by steven wood steven, how are you copying the code? are you using vbscript or vba? i do not know how vbscript and sw get along. if you have the code in the form of a swp file. put it in a zip file and upload that. you could also tell us where in api you found the example. also what version of solidworks and sp are you using? sa edited: 04/20/2007 at 06:23 pm by solidair oh, so a zip file will upload, okay. i tried it w/ a text file but that wouldn't. in the sw api help you can find it under: "put assemblyfff"> components in drawing view on different layersfff"> example (vb) i tried to copy/paste (ctrl-c/ctrl-v) the test straight from there, from the swp file, and from a text file into this thread, but none of those format it correctly and it looks like one big paragraph. i am also attaching a zip file. i copied the code exactly from the help file, and it says that is vb. that's all i know about whether the macro is one or another. (i'm new to all of this) i'm using sw 2007 sp3.1. thanks, steven edited: 04/23/2007 at 09:18 am by steven wood steven, you are not alone in having trouble pasting code directly into posts. several others have had the same problem. i wish i could be there when you were doing it so i could see what is going on. as you can see, at the end of this post, i took the macro from your zip file, made a few adjustments and pasted it into this reply. and, except for losing the indentations, it looks pretty good. the problem with some of solidworks examples is that sw tries hard to format the code so you can just paste it into a macro, but to make it more readable they double space the lines. the vba editor gets confused with this when underscores are used for line continuation. when you pasted the example into the vba editor, there were three different parts where the text turned red. red text always means there is an error in the code format (unless you changed it to another color in the options dialog) and it has to be corrected before it will run the code. for example, if you look at this code in your macro: private sub changecomponentlayer _ () ( _ swapp as sldworks.sldworks, _ swdraw as sldworks.drawingdoc, _ swdrawcomp as sldworks.drawingcomponent, _ slayername as string _ ) lines 3, 4, 5, 6 and 8 were red. to fix this, i had to get rid of double spacing (and the extra parenthesises) like this private sub changecomponentlayer( _ swapp as sldworks.sldworks, _ swdraw as sldworks.drawingdoc, _ swdrawcomp as sldworks.drawingcomponent, _ slayername as string _ ) to get the code to turn black. try pasting the code below into a macro and running it (i tried it before posting and it worked for me). sa '------------------- ' ' preconditions: ' (1) drawing document is open. ' (2) drawing view of an assembly is selected. ' ' postconditions: ' (1) new drawing layer is created for each component. ' (2) each part is put on its own layer. ' ' note: illegal characters are replaced with legal characters when creating a new ' drawing layer. ' '------------------- option explicit public enum swlinestyles_e swlinecontinuous = 0 swlinehidden = 1 swlinephantom = 2 swlinechain = 3 swlinecenter = 4 swlinestitch = 5 swlinechainthick = 6 end enum public enum swlineweights_e swlw_none = -1 swlw_thin = 0 swlw_normal = 1 swlw_thick = 2 swlw_thick2 = 3 swlw_thick3 = 4 swlw_thick4 = 5 swlw_thick5 = 6 swlw_thick6 = 7 swlw_number = 8 swlw_layer = 9 end enum private sub changecomponentlayer( _ swapp as sldworks.sldworks, _ swdraw as sldworks.drawingdoc, _ swdrawcomp as sldworks.drawingcomponent, _ slayername as string _ ) dim bret as boolean ' form a legal layer name by replacing backslash (/) and at sign (@) symbols ' with underscores slayername = replace(slayername, "/", "_") slayername = replace(slayername, "@", "_") bret = swdraw.createlayer( _ slayername, _ "layer for " & slayername, _ 0, swlinecontinuous, swlw_normal, true): debug.assert bret swdrawcomp.layer = slayername end sub sub processdrawingcomponent _ ( _ swapp as sldworks.sldworks, _ swdraw as sldworks.drawingdoc, _ swdrawcomp as sldworks.drawingcomponent, _ spadstr as string _ ) dim vdrawcompchildarr as variant dim vdrawcompchild as variant dim swdrawcompchild as sldworks.drawingcomponent debug.print spadstr & swdrawcomp.name changecomponentlayer swapp, swdraw, swdrawcomp, swdrawcomp.name vdrawcompchildarr = swdrawcomp.getchildren if not isempty(vdrawcompchildarr) then for each vdrawcompchild in vdrawcompchildarr set swdrawcompchild = vdrawcompchild processdrawingcomponent swapp, swdraw, swdrawcompchild, spadstr + " " next end if end sub sub main() dim swapp as sldworks.sldworks dim swmodel as sldworks.modeldoc2 dim swdraw as sldworks.drawingdoc dim swselmgr as sldworks.selectionmgr dim swview as sldworks.view dim swdrawcomp as sldworks.drawingcomponent dim bret as boolean set swapp = application.sldworks set swmodel = swapp.activedoc set swdraw = swmodel set swselmgr = swmodel.selectionmanager set swview = swselmgr.getselectedobject5(1) set swdrawcomp = swview.rootdrawingcomponent debug.print "file = " & swmodel.getpathname debug.print " " & swview.name & " [" & swview.type & "]" processdrawingcomponent swapp, swdraw, swdrawcomp, " " end sub thank you! and dang am i an idiot!!! i knew that... i knew to fix that, and completely missed it. this script should be a big help to us, so this is a huge break through! thanks again, and i'm sure y'all will be seeing a lot more of me. steven i know this is an old topic. but, i was hoping to find a version of this program with some more features... - automatically assign random colors to each of the layers. - option to treat sub assemblies as one part rather than always putting each individual part on a separate layer. - the layer names are too long, and the layer management window will not expand large enough to see the layer name. joe dunfee sw 2008 sp4 sw 2009 sp3.0 windows xp hp compaq dc5800 microtower nvidia quadro fx 3700 quick |
所有的时间均为北京时间。 现在的时间是 11:50 AM. |