软件小白一枚,因为公司需要统一零件模板,并且和我们搭建的PDM关联,需要将文件属性写入配置待定里面。之前用图号分离宏只能把属性写入自定义信息中,配置待定中只有空的标题,分离后的内容不能读取。6 Y" n& e: u, P% c; b0 v
下面是我在网上找的说是可以将图号分离后属性写到配置待定的代码,实际调试有错误,见红色代码。
7 K" Z# }2 T6 w" c---8 i0 J3 U6 d# Z
Dim a As Integer
9 @9 P% U$ S F- O9 p4 l" u% F3 N' i: p6 F- |( g2 S1 w, j
Dim b As String
w3 I; x& w! [& z! k: ?. m6 o1 \+ g; [8 d
Dim m As String
, [# |( d1 g; k7 j' B) A r/ J( K- Z" y8 K3 _7 k3 k
Dim e As String, R% T: Q% v8 A7 y B$ B* `0 n
$ P* k' E4 {. n ]# f- |. G% _
Dim k As String
( d* ^6 K! x1 s& \# g! w; Z% a9 @# y8 w! d. p* G
Dim t As String
3 p; v ^( ^4 r- @4 C& G/ T- l) T b3 }8 q, g+ V: z
Dim c As String
; F! q8 O# g G$ L( E. y8 H" S x c+ Z5 {
Dim j As Integer. H3 Y1 }/ K9 S7 ]
0 g! ^. ?: v K! J. b9 e0 G- E! C. fDim strmat As String
$ Q/ K: p# N( \) G7 P8 W
, s5 A, K& P/ `% H, yDim tempvalue As String
3 ^( p, F1 Y( i( K
7 ~, e# K' N6 w) {Dim Part As Object
7 y! \* E$ V6 ]) y/ W( ^7 q6 D$ S% Q2 J: `+ z. Y$ Z- [
Dim swApp As SldWorks.SldWorks
& q7 }9 B5 J7 t! T. a
4 ?: P; @* w b3 N9 c1 ]" ZDim swModelDoc As SldWorks.ModelDoc2
3 c+ Q) k( U; B. M. } g) {& z" L& t3 m, v( F/ `* [
Dim swConfig As SldWorks.Configuration2 N: ^0 ?! R, ]3 _# e& j- w5 u& a, Z7 a5 a
, c2 O8 J4 t3 ]$ t1 `0 Q+ KDim swModel As SldWorks.ModelDoc2, o. y0 f3 ]5 @8 T: R6 p' u
/ k. r% a+ u' G y% k0 U; X! U. WSub main()
1 x+ |3 Q: |' |# ]( X* \# F" z* Y6 T: v! L/ K
Set swApp = Application.SldWorks+ l' e" \2 M& K- n i; @& v/ N
. @3 \' ]) C z. p
Set swModelDoc = swApp.ActiveDoc5 m9 D! e" b E& w, F0 w
3 P& s( O6 Z7 M1 R+ {Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration% c5 B6 d# Y* S4 b% D
% Z+ j8 m9 u0 B; K& {7 y& B8 nSet swModel = swApp.ActiveDoc9 W6 D: k9 D/ ?' h. p6 i% Y
* d' T) f0 ^3 A/ N* E9 R6 O3 K& }! {
Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguratio.Name) '配置特定延伸! m/ J9 h* O% f5 Q( ]3 @3 `
& [0 @# V6 Q* H' h$ @' ~& M% G'设定变量
7 ? N6 A! h/ c/ P" x6 z" y' z& o' j1 V
c = swApp.ActiveDoc.GetTitle() '零件名
0 D! ]1 P8 B* h) G
2 X. a' y! b$ a' q0 Tstrmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34): ~! i! @9 _. X T& N9 T) `$ q; p
8 y3 W0 ~7 `; ^9 x. N+ ?# J
a = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格,也可换成其他符号( b, ~/ @2 o9 E8 K( k
) Y6 d" j- |3 K! CIf a > 0 Then% Z8 N/ s% B) g' E/ e7 |8 ^
( `! f) |, c" [0 w; A
k = Left(c, a)
" N' ]9 B' O- U; M% M0 |( A5 T% ^$ ]& R3 g. N
t = Left(LTrim(e), 3)( H0 b1 T2 L4 t& [3 h" E+ Q
2 d4 R' j9 B1 E- C) U& G# ]If t = "GBT" Then
( Q" B5 W- E* k8 F3 V( V* ]- Q9 l Y1 A% r7 v8 f' z
e = "GB/T" + Mid(k, 4)3 i# u8 j: ]6 I9 X& p( S
6 j$ g! @% C9 j3 H& ]
Else
8 W, o' \- ^8 X5 w9 {% |8 }, [: R! ~. k+ p, y* p% g
e = k2 |) ]; {% h3 h( v+ g
& q6 Q: r/ ^+ m2 z, W% M# hEnd If
, {' d4 `9 G8 l
: F. O5 S1 @& n, V$ Ob = Mid(c, a + 2)
( C9 F" R& w2 \" h Y# I' m& _0 D$ m3 C$ o; m3 N
t = Right(c, 7)$ v4 F5 U- p5 C3 i- z' I4 _
+ _7 Q: ^! L) d- I6 ~- Q
If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm" Then
! p3 q9 W6 {/ k1 u, ]( h2 }
# M7 J, x2 B/ d% d3 L( `j = Len(b) - 7 '消除后缀(区分大小写,即含4种)! v. L" i3 R. B7 |3 J3 M! M
( m" c4 W l' Y x; TElse9 C9 L( A; L9 r9 ^' l$ ]
0 j- k6 q. f$ {9 V1 l
j = Len(b)3 E0 q+ e+ z" p6 [$ I" x( q
7 |* ~7 p, m# X' t5 _3 }End If
# A" E- `; h9 b* T: K: y. U7 Y% [2 G; P" p; m( j4 e
m = Left(b, j)
3 Z6 B- W; X$ [2 R0 m
2 F3 e4 X4 ?" D0 D0 h+ c6 UEnd If6 q9 j) A2 D! i
. s% Y. d0 w7 w Q% w/ ^, f# J'删除栏 `7 j2 z+ X3 n* W6 z
" x" |0 y( U7 q9 \) r4 U4 g8 qCustPropMgr.Delete ("图样代号")
9 c% m4 F4 o! ?( B9 _- p+ n8 y, \: H/ V8 m: D9 `/ p
CustPropMgr.Delete ("图样名称")
. b7 f9 S; r6 x1 p2 |
8 {- I+ h1 m+ m2 g- ACustPropMgr.Delete ("材料")4 `: g0 a: y" B- }" ]8 y. L
3 X' H& R* u+ u d
'新增
7 O; y2 j0 Q$ u7 A h- n8 x) z2 _
) V# c3 L" E2 V* |2 WCustPropMgr.Add2 "图样代号", swCustomInfoText, e
" r8 \+ l$ }( L2 W& M* N) Q6 x* Z$ i6 V# {1 D
CustPropMgr.Add2 "图样名称", swCustomInfoText, m
* e7 F( E# N$ V: U P4 r/ J0 w% U. p& s! T
CustPropMgr.Add2 "数量", swCustomInfoText, ""
+ M9 j3 L: ~' X+ n, U/ {
$ B9 s. v. q/ A6 D2 O6 ?/ G/ ^CustPropMgr.Add2 "材料", swCustomInfoText, strmat
8 I; \6 z) V. p9 _% ^" A. H1 T* j1 W" u# w# O4 f6 y
CustPropMgr.Add2 "单重", swCustomInfoText, """ I* A, S$ j2 A6 s& E
' |6 V* h- j4 ] u9 H; F
CustPropMgr.Add2 "总重", swCustomInfoText, ""
- A# N3 ]* e" N2 T7 `' E( i0 O( ]+ p2 q4 n) n5 F* S8 R
CustPropMgr.Add2 "备注", swCustomInfoText, ""
3 h, o8 ^: L3 M. S7 J
- l, ~$ A0 \/ u/ h; A4 m/ u$ CEnd Sub
7 `2 j7 Q1 g& b% p: H---
) O+ d. W+ e, E7 O& Y想请教下大拿们,这个代码要怎么修改才能管用。之前的图号分离宏,可以让所有属性出现在自定义里。或者有没有办法加一段自动复制自定义里面属性到配置待定里去的代码?& K$ _+ _: O, E) J( Y1 p$ o, H) h
) W* }* P/ M3 {1 H
在线等大神解惑
% q9 F N0 ^4 Q9 s6 J( z8 B2 X4 U" j3 \% O& J7 E
6 S9 A4 s" P5 J) M7 o5 U |