软件小白一枚,因为公司需要统一零件模板,并且和我们搭建的PDM关联,需要将文件属性写入配置待定里面。之前用图号分离宏只能把属性写入自定义信息中,配置待定中只有空的标题,分离后的内容不能读取。 $ G$ a. R; c/ Y# V$ W# ^6 q下面是我在网上找的说是可以将图号分离后属性写到配置待定的代码,实际调试有错误,见红色代码。 7 f5 y( j: M* ^, t. Z3 {; T---# ?# J7 A9 y+ q2 Y+ k0 Z Q: `2 ^! J1 ] Dim a As Integer 0 N2 ^: }8 Z" I" T2 m1 N. H( u7 r3 m8 S5 t Dim b As String * F Z! W% H# A+ _4 v1 k j( S$ }/ X# h' H% ? Dim m As String6 o. u4 I( U0 a/ p! e 8 N) f7 V8 d1 l- a C/ ~( x Dim e As String }$ p3 K- O. a) d" q! a9 h3 f/ ]8 l( O { Dim k As String% l0 r# S! ]! ]6 g
2 t: p+ q# d2 W! X) K% BDim t As String1 l. l* Z: U0 L0 H( I& u 0 I4 T0 V0 K; f! ?5 e* h, p8 v Dim c As String & N- e% [& M! g0 P5 x2 \2 [/ M/ R 1 ^* V* V: q$ c. n# q% v: N. tDim j As Integer & v2 E! _5 \1 b( r0 T+ D : |# W! ]3 {, z+ e( N& hDim strmat As String; |/ y ?5 i" o# q! ~! k 2 b: Z1 v% s+ X( F, d4 [ Dim tempvalue As String* n7 y8 y9 K0 b# r. m0 T" |1 D
& G" T" H2 D2 H* BDim Part As Object / t) e( e @: _ H. S' G3 H+ M/ B- h% ?. V( a Dim swApp As SldWorks.SldWorks; r$ z" A! u4 m/ y7 m9 X2 \
. i/ ~/ ?5 Z4 }5 h) iDim swModelDoc As SldWorks.ModelDoc2 , d0 _. \* Q% L7 u* ]7 ^% x- S" F/ C% p! _ Dim swConfig As SldWorks.Configuration + U5 m: d! X9 Z3 b+ Z ' I# h$ B' X/ H% a0 p; q, H4 t1 B# oDim swModel As SldWorks.ModelDoc2P \' G0 a/ D5 J- S
2 E! N$ [% g; G3 X( GSub main()0 e* D/ }8 a4 ^( r9 b
+ B2 X- A0 K8 X. A- p1 x! VSet swApp = Application.SldWorks) _* o$ o' Z. h/ i& P8 }
, ?+ X: F4 k0 T5 ^Set swModelDoc = swApp.ActiveDoc) n+ L% @. K8 g 3 J; L5 Z* c0 Q Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration 9 V: l0 h. s6 \) |# [% y5 d5 y; X/ Y, p8 `( d2 N; l# J Set swModel = swApp.ActiveDoc# a1 `$ i, H, G* B8 v2 O$ h" A! D 1 `9 L. }' n, W. {! @ Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguratio.Name) '配置特定延伸9 ]! h- X- i+ Z5 K/ B& g$ Q: J
2 G$ s* _; h" c) L$ l, {+ ]. b* B'设定变量 6 N: q# `+ U' K. A2 r. C2 o: q5 u$ `0 F- Y# i c = swApp.ActiveDoc.GetTitle() '零件名' w4 w9 T. ]& ~ : W/ I) `; V; N5 k% M0 @ strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) # s' n% h v1 _2 X! R4 ? ' x; C3 q6 s/ X$ e6 V2 p& Da = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格,也可换成其他符号$ S2 K* X6 `7 _( @( e
* ]7 D! v7 j' i( B3 x: PIf a > 0 Then ' j F; I9 i/ n 6 Q4 \2 K+ M: m' {. Yk = Left(c, a) 9 s* }! J! k7 X2 W / j: k. z! Y5 B0 g3 Y7 kt = Left(LTrim(e), 3)1 B: j' p! O7 C: T4 k) o , @/ H* @+ }$ ]/ F4 T If t = "GBT" Then) K2 v' G( H9 C$ c4 T+ m ! J7 Q; \, H$ f, Z e = "GB/T" + Mid(k, 4)3 |% A2 e0 A, i% m/ |, d0 w
W/ x+ s/ F3 n6 S: nElse3 o! P' R- I1 }: F1 T5 B: S
+ Z) `# v5 X6 te = k ; s# w( A; n) v# y _$ d i, f& W' i N0 N: eEnd If & C1 }, n$ {) ]' S% b* y A3 @9 x9 R ^* p. F+ x! A* p0 n) C" u b = Mid(c, a + 2) 6 }3 S1 @ E p+ b8 L3 A0 d1 Y; x$ \) B7 j1 A* L& I t = Right(c, 7) 5 }3 S! q8 \% q1 ` m$ a6 U4 @. E/ {% G7 {' Y- Z2 K* O If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm" Then, I' e4 [8 J, ^, E # t# I. [9 V4 |: t j = Len(b) - 7 '消除后缀(区分大小写,即含4种) - a; Q) d8 w! S; I+ K% B# G* c ~ # V# m% q* e- k" l6 NElse; M3 X o. H, G6 L
9 D' `1 V5 u/ r- C$ p' z- Q2 Q9 Qj = Len(b) 4 X6 ~5 Q6 \- Y& Q! m( @* w: G7 o& {* q8 N, s End If ( k6 }; {! w* K s9 `5 O ! T: H; j; ]; u+ p, a) ~# Dm = Left(b, j)- s! Y: b, z9 o3 h5 g- u3 n$ P + H: O7 P( l6 F9 Z; T+ j. ]+ U End If - E) F4 T' [7 L3 [& ~& W5 \( q2 n# E( n& ^9 E% R. g '删除栏/ V( v/ a* s4 o1 X
/ N8 n' d* H6 \6 ^* G) oCustPropMgr.Delete ("图样代号") . B- ^1 [* |+ C4 G# A+ v; _7 B$ I0 `( o# i8 c CustPropMgr.Delete ("图样名称")8 q1 Y- N( p8 v$ M" t' J
% B" a2 M) @! }4 h+ z- z# o8 L5 I5 KCustPropMgr.Delete ("材料") 3 S$ P% y& D, r, L 9 {! s' r5 J3 t G" ^+ i* K'新增 2 U" }8 ?1 a2 }: J, N & Q) k. Q' O: }9 f# U: o* HCustPropMgr.Add2 "图样代号", swCustomInfoText, e# Z9 p% M r$ G; _" K7 j ! X& {" ~8 t6 C& A3 h CustPropMgr.Add2 "图样名称", swCustomInfoText, m" k: f/ M$ x% V" V! P
# x+ d e, p, ]% C/ dCustPropMgr.Add2 "数量", swCustomInfoText, ""7 z: M% G, R/ U9 K
+ P# J) a/ s5 h% `- vCustPropMgr.Add2 "材料", swCustomInfoText, strmat, F; g8 {; j( }& o" L0 j5 J , u; U% k4 D* l; W CustPropMgr.Add2 "单重", swCustomInfoText, ""' @3 U0 @& j% [! F1 \4 i6 @
4 T0 [" [4 J2 A w; kCustPropMgr.Add2 "总重", swCustomInfoText, "" ) y1 `2 R! ~2 Z& U$ _/ R( J- a: Y: K* O: Z$ @+ f, D5 ]7 g6 Z CustPropMgr.Add2 "备注", swCustomInfoText, ""6 d6 C/ g! {0 l: m2 V ' M/ ^ i0 A( z End Sub + T: z4 V0 n# ~7 R: [& h5 {--- 0 L: _+ c7 l b+ V. Z3 k想请教下大拿们,这个代码要怎么修改才能管用。之前的图号分离宏,可以让所有属性出现在自定义里。或者有没有办法加一段自动复制自定义里面属性到配置待定里去的代码?$ e9 d+ X' S/ L% S : @1 {% P0 Z& @5 T* c 在线等大神解惑j% O2 @6 y/ R& b 3 `- t1 i1 a! k! d0 ~% z 4 A' H0 s, b+ i( I4 N* s7 {
|