几何尺寸与公差论坛

 找回密码
 注册
查看: 612|回复: 0

【转帖】custom properties on several files

[复制链接]
发表于 2009-4-13 10:09:01 | 显示全部楼层 |阅读模式
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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-24 09:32 , Processed in 0.036604 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表