软件小白一枚,因为公司需要统一零件模板,并且和我们搭建的PDM关联,需要将文件属性写入配置待定里面。之前用图号分离宏只能把属性写入自定义信息中,配置待定中只有空的标题,分离后的内容不能读取。' I' Z6 Y. x5 u _
下面是我在网上找的说是可以将图号分离后属性写到配置待定的代码,实际调试有错误,见红色代码。, `# M4 A* O1 j. {$ s4 k$ \
---) f% V& e& v2 Q% ^9 R- F; D
Dim a As Integer* G! i9 W* u- |+ h! N' k: ?
; N1 B6 T% o* V+ T$ r6 q) y1 Q
Dim b As String D9 F" M# x& Q3 D2 Y/ }6 X
: w& w8 _# Q8 ?: n/ QDim m As String E7 H3 T5 ` ?
1 P* \ `( t3 u4 c8 k$ l
Dim e As String! a( t" X" D# {: m/ l
/ r" i- z# m) V& \% N1 [3 ~Dim k As String% u8 }1 f7 N% P) ?
4 s% T% J( L% S7 ~Dim t As String
" w/ u6 F8 H7 v
8 n) h3 C4 a4 h P& h% J" SDim c As String
0 I' k' f+ X8 \& W* q0 F
, Y6 H2 t0 g# Z, K& DDim j As Integer
" b# F F3 E# }! |( b# b+ i% t1 o( s9 p# b' w' z' x% E
Dim strmat As String
& u: A$ f$ h7 l' X j- O! i, a
; m3 V7 \ @& C# `9 G9 zDim tempvalue As String4 O! v9 h. L- ^3 P
5 ?. s2 c( \/ R) wDim Part As Object9 H" p5 `* j) k! W
2 x9 ~8 O3 M" bDim swApp As SldWorks.SldWorks5 f7 ~9 S6 c# N" D( o, i# ^8 H( z
T9 p O1 {! e
Dim swModelDoc As SldWorks.ModelDoc25 V# b0 w5 U4 ^! J& X
8 Q" @) O: C, m( P6 |4 x, GDim swConfig As SldWorks.Configuration
0 g/ P2 d1 r2 Z. W& W$ [
: I: z# T, H+ o9 j* ^Dim swModel As SldWorks.ModelDoc2
7 A- q. ~! a8 V
6 \! x0 J, G2 |Sub main()
. ]1 b( h5 w6 D! O2 |4 h8 |
( N' P! V% G. t# }9 s4 _0 J8 m6 mSet swApp = Application.SldWorks+ T8 k; J4 s @4 ]. i1 L' V
2 Q. t7 P2 J8 Y8 `, w- }Set swModelDoc = swApp.ActiveDoc
. f: |) v0 X. ]* O" Z, ~; X- t: y4 H+ K7 V$ @# R
Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration
" V4 e( s: A2 y* Z8 b0 n
! f' T9 T. d. P: t/ CSet swModel = swApp.ActiveDoc
( f8 p$ Z _. s* i, v; |5 `# w7 D/ s A+ x: {
Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguratio.Name) '配置特定延伸
6 |6 ]( l" J0 ?+ X; L/ V0 x' d0 q+ e v, A
'设定变量
a, H( a; l" z4 Z' V
" w j9 ?& a( U3 rc = swApp.ActiveDoc.GetTitle() '零件名; H" S, U @* |. h# e6 C- W
9 l$ ~) [$ a9 d+ T
strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34)
; e, J/ J8 P8 o2 |9 o
4 N! l4 F3 p! l# \9 n! ca = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格,也可换成其他符号
5 ?4 m0 u# D1 r' X' g
% D, T# g% Y, zIf a > 0 Then
% l9 I" b' I$ H0 m; v1 M6 H. T+ D% K7 Q1 b# d7 m# K; r
k = Left(c, a)* R/ }) {9 C# |9 o
$ s3 [9 ]4 v4 Y/ [! `t = Left(LTrim(e), 3)
0 u" F+ e+ a; A/ Z
6 J9 m- d7 k. qIf t = "GBT" Then4 _ V6 J4 Q6 D4 p0 t9 j
' C4 ^. ]) |7 V9 R" A# d6 H, f
e = "GB/T" + Mid(k, 4)6 |6 V3 r& p9 M4 O
# `2 r) B( Y* p: p7 u+ \4 }
Else6 I" K: {& B1 b; ~0 F$ B4 P: g
& W5 B: Z. |% `+ M* Pe = k
) `, B- g* h! z8 G+ b& G$ \
! y4 ~$ E6 M- _ I3 z, aEnd If
8 \3 v$ I7 ^1 M+ }6 x
/ u z5 |3 @: l7 e, h1 kb = Mid(c, a + 2)6 \- y) B+ C: G$ q4 t: c k/ P0 v: s
: m% T3 F+ A& R2 n( ^6 t4 It = Right(c, 7)
, z) R* Z1 g* u' X) m, {* n3 ?- C" S; H6 a& ]4 ?( k: q7 p) r7 X' M
If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm" Then
9 N/ w' n5 y2 N0 @- W# K- f
' T5 S3 w0 G; x$ J7 u: M" Bj = Len(b) - 7 '消除后缀(区分大小写,即含4种)
0 K# F/ g# o7 d5 {" h7 B, j
( a( x6 a4 g9 b+ aElse- _2 `# ?" t5 @5 D* u! h
2 N6 C% f9 U1 F/ o
j = Len(b)
/ L4 f9 m0 ^7 E: ?/ V. w+ W( P* p: @5 N0 ^- ~
End If
9 ~; T+ Y# j' n( B, M( R6 W5 ^# n& U1 Q9 V8 V* a
m = Left(b, j)9 l) @) _9 F1 ~9 Q T% Z7 b' y
: y& Y/ n# B3 Q3 {$ }8 r( ^End If- b8 @, u8 X" R H9 I2 [( s
4 V. h; P4 O7 }'删除栏
' E# u( [4 d. Y4 y: V# v; K) ^* M3 E7 A
CustPropMgr.Delete ("图样代号")
$ o. |1 Y: `3 G' p" l' K; u' S6 K% Q
CustPropMgr.Delete ("图样名称")
$ P% a5 C, F& Q
0 z6 }# y& X3 s1 C, S# _CustPropMgr.Delete ("材料")
2 l8 W' ] b2 v& \" Z- A
, j, X$ B! g1 V+ T+ _5 T; L'新增
+ V ^0 a S9 r/ k9 s$ ~1 S4 @5 g0 J9 \' S" F, a
CustPropMgr.Add2 "图样代号", swCustomInfoText, e
, G$ V: q0 K, I9 X# ?- v% A3 h. D
CustPropMgr.Add2 "图样名称", swCustomInfoText, m
' g" e" ^8 ?3 K# ^
5 P% H" N: g& q$ f/ ICustPropMgr.Add2 "数量", swCustomInfoText, ""
, v& n* l9 M% E* b3 s: ^7 S
& n3 X0 \3 P7 o! G. v9 cCustPropMgr.Add2 "材料", swCustomInfoText, strmat7 R9 d* j8 h Y/ B3 n$ Y" v5 k6 R
& U9 E, I7 A0 C9 l
CustPropMgr.Add2 "单重", swCustomInfoText, ""
5 s% G' I' y0 q3 e1 [2 o! k
$ d$ V! s% ~5 R; q- NCustPropMgr.Add2 "总重", swCustomInfoText, "") c: A7 Y# x% G' _9 D' {9 c7 u
, O2 l; Y/ L/ P. D0 Z4 LCustPropMgr.Add2 "备注", swCustomInfoText, ""
+ D+ ]* I& d% S* B9 ]
2 g, ^1 I: r& o& h' | X( I; kEnd Sub
% P( R" Y9 i1 c( S M* b; A1 N---# _# ]- e, |& C( N8 x% U9 \/ c
想请教下大拿们,这个代码要怎么修改才能管用。之前的图号分离宏,可以让所有属性出现在自定义里。或者有没有办法加一段自动复制自定义里面属性到配置待定里去的代码?
$ j# S) s) V1 [, ^5 W
2 Z/ @, ~" I, ~ d- j4 _3 H在线等大神解惑
- y3 G% h, r6 c: x7 {7 f3 N" m. f) j) E3 `2 d( ^
E2 B, T1 d2 Q- C! k/ i; W! X |