![]() |
【转帖】custom properties on several files
custom properties on several files
hey, here i am with another stupid question i have an array which hold 2 things: a list of the full paths of a part and the quantity of the part. eg: customprop(0)= "c:\test.sldprt" and customprop(1)="1" what i want is a macro who does the following: open the first part in the list add or change the custom property "quantity" of the part to the value from the array close the first part open the second part add or change .... i've been trying all day now, but i just can't get it to work. could someone plz help me... tnx a lot guys steven, here's some code posted below. might need some error handling to make it more robust... could also handle any open/save errors or warnings that get loaded into the long variables to increase robustness. change out the array ary() with your own array. it's working here, but with sw you never know. let me know how it goes. dim swapp as object dim part as object sub main() set swapp = application.sldworks 'build test array dim ary(5) as string ary(0) = "c:\documents and settings\master bennett\my documents\part1.sldprt" ary(1) = "11" ary(2) = "c:\documents and settings\master bennett\my documents\part2.sldprt" ary(3) = "111" ary(4) = "c:\documents and settings\master bennett\my documents\part4.sldprt" ary(5) = "11111" dim i as integer dim s as string dim swopenerrors as long dim swopenwarnings as long 'loop thru array for i = lbound(ary) to ubound(ary) step 2 'determine doc type of filename from array dim swdoctype as swconst.swdocumenttypes_e dim extension as string extension = ucase(strings.right(ary(i), strings.len(ary(i)) - strings.instrrev(ary(i), "."))) select case extension case "sldasm" swdoctype = swdocumenttypes_e.swdocassembly case "sldprt" swdoctype = swdocumenttypes_e.swdocpart case "slddrw" swdoctype = swdocumenttypes_e.swdocdrawing end select 'open document set part = swapp.opendoc6(ary(i), swdoctype, swopendocoptions_silent, "", swopenerrors, swopenwarnings) set swmodeldoc = swapp.activedoc dim custompropmgr as sldworks.custompropertymanager set custompropmgr = part.extension.custompropertymanager("") dim custompropnames as variant dim propalreadyexists as boolean 'see if custom property quantity already exists in the file custompropnames = custompropmgr.getnames if isempty(custompropnames) = false then for j = lbound(custompropnames) to ubound(custompropnames) if custompropnames(j) = "quantity" then propalreadyexists = true exit for end if next end if s = s + "quantity of " + ary(i) + " set to '" + ary(i + 1) + "'....." 'set custom property 'quantity'... 2 different methods depending on whether it already exists or not dim retval as long if propalreadyexists = true then retval = custompropmgr.set("quantity", ary(i + 1)) if retval = 0 then s = s + "success!" else s = s + "failed!" end if else retval = custompropmgr.add2("quantity", swcustominfotext, ary(i + 1)) if retval = 1 then s = s + "success!" else s = s + "failed!" end if end if 'save and close dim retbool as boolean retbool = part.save3(swsaveasoptions_silent, swopenerrors, swopenwarnings) swapp.closealldocuments (false) set part = nothing set swmodeldoc = nothing set custompropmgr = nothing s = s + vbcrlf next 'display results msgbox s end sub kevin bennett kevinsbennett@navitech.us |
所有的时间均为北京时间。 现在的时间是 09:26 PM. |