软件小白一枚,因为公司需要统一零件模板,并且和我们搭建的PDM关联,需要将文件属性写入配置待定里面。之前用图号分离宏只能把属性写入自定义信息中,配置待定中只有空的标题,分离后的内容不能读取。
1 U" p9 Y! w& n. B6 K: {下面是我在网上找的说是可以将图号分离后属性写到配置待定的代码,实际调试有错误,见红色代码。! z+ C" ?+ M6 M2 @
---& y+ I7 R, w+ |; P* k* G- _: J
Dim a As Integer; v9 @% `- K' a
5 n! Z$ s/ S1 p
Dim b As String* m B; _' L0 z% y& `
& M" n: p- k7 r3 u: ]Dim m As String
9 g% O) X; ^ T8 F
" ~! t8 h6 X- Y% E! zDim e As String
8 z2 g6 [0 V' W* N) g# S; L, W0 ?9 V" F1 y2 o! D0 P7 }
Dim k As String
8 N! N8 n% q; U/ @
8 v3 O0 b5 _ VDim t As String
d: a9 c& M! d( b5 r
4 R% C* S# }* S" k! h6 i% I" H v7 wDim c As String/ z1 ~5 m7 L: L8 W8 j3 y- I
5 |5 Y* {/ ^* G8 a+ jDim j As Integer
5 H" p/ i' k; i4 c/ ]. i7 [( F, z$ P* ?: N
Dim strmat As String% D/ p) M1 m; O" k8 O% ?' `
' L6 c7 @7 q, |- z J
Dim tempvalue As String: @- {% p. o g W8 J6 p
5 }3 S- }8 ?: R2 S' E7 f
Dim Part As Object
C/ i9 P" B9 x, `( _. ~% A1 F, V, y: f- P. {: W4 g
Dim swApp As SldWorks.SldWorks
2 _$ Z* h6 J0 X
$ s' a6 ^' ]% BDim swModelDoc As SldWorks.ModelDoc24 n1 u% ~. O7 d
& @4 K3 O: N& J# O$ XDim swConfig As SldWorks.Configuration6 H- K/ a2 N1 F$ K x: H' {6 \
7 {1 ^/ c; _7 H, }3 }7 r) U
Dim swModel As SldWorks.ModelDoc2! H0 {2 J* p( i3 E, o3 [0 b, A
. ], ^. q, S! k
Sub main()
* G' W# M H3 Z1 Z3 U+ Y* l C6 w" `" V
Set swApp = Application.SldWorks
8 l- [. x) {! B" y$ r# m) v* W& N: ]: }3 `1 L
Set swModelDoc = swApp.ActiveDoc! c) p) w4 [+ x" ^5 I( S* ~
6 N/ R& l9 B: F9 ^! j r8 Q
Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration& }3 b8 F2 h8 g3 j {
# Q) J9 l9 G2 T* TSet swModel = swApp.ActiveDoc& b8 M( n7 Y3 l v
) y/ m/ z/ ?9 ]( m6 H ESet CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguratio.Name) '配置特定延伸4 {" \, }" |* C! g% |/ z0 {
* u! z' e1 m' b2 e
'设定变量
2 O" s9 B& g3 c9 l. |0 E+ t* [, ^( C+ ~/ ?
c = swApp.ActiveDoc.GetTitle() '零件名3 \' j7 E ~) p
5 L( {( Y/ ^+ N, n" b- K( `
strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34)
+ }$ W& y. s4 d$ W1 f
; Z0 l, z/ |* I4 f' Ia = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格,也可换成其他符号
' i. w8 K; _3 h* N3 S& v7 T
) Y3 Q" }4 X9 V! o1 XIf a > 0 Then, p8 g8 u7 b6 r( b% v5 L5 B: P3 O
3 U/ m: J" H, {. Q& ?; }k = Left(c, a)1 k9 r+ j" \0 M/ x0 R0 [
* Q- r3 q' s. E9 T8 h+ H% K0 Zt = Left(LTrim(e), 3)1 L5 l5 x; T, b, S" J
( Z: h& X! J0 h: ]7 @" gIf t = "GBT" Then
& K+ ^) H; @4 w& z6 q% m
, V( J+ g( D# q, {: r8 O6 H5 I; ie = "GB/T" + Mid(k, 4)
' }% W/ t3 [2 t: I6 V9 s% l2 H
. [8 U+ Q$ _# w4 X) `Else
; F( w/ C- T. j% f7 o5 M) o9 x; x) C4 `" s$ `% v+ Z, j
e = k4 s" A! m% Q N4 i& Z& F% v0 e
+ }! o, \" U$ cEnd If
0 \8 Y# S' w4 ~, |* P0 \
1 L8 m1 O' n4 r1 B0 F+ }6 x: mb = Mid(c, a + 2)
: X! j7 |; L+ x6 i1 ]- ~; f
$ k3 Q; X9 y/ s4 z" y& o! v: mt = Right(c, 7)
3 ]" S/ F- m" @- A
& k0 p5 J" H7 M. |( E( }$ T, ?6 v- ]If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm" Then
1 T$ o, }. V& ^9 H7 F4 r6 Y2 h, b& p
- q1 E+ E5 a r3 |. t! g5 yj = Len(b) - 7 '消除后缀(区分大小写,即含4种)
9 _4 y$ ?* O ~( R- L3 O' |' e$ n+ Q7 s
Else
3 W7 J" Q" ]/ N5 B V1 `2 U% K+ b3 W9 `$ _3 l# ?: D
j = Len(b)
& n& T- N! g; O% h& Q/ g
@/ ?9 G/ W ^1 K# x4 iEnd If; x" U& Y! K- ]# B: N
, G, B9 a% E! a' G2 o0 C& x
m = Left(b, j); z. \( _& i0 z6 C$ M; w; O
+ [; P7 l" r, w5 ^# h) GEnd If4 g3 I/ K2 [; I' J* i9 V* ^# n
5 n( P$ z) Z1 G! S# V o% f
'删除栏4 C, d8 Z2 _8 K
+ H. L6 Q: A4 n/ ]5 U1 d5 @& R; U) X
CustPropMgr.Delete ("图样代号")2 N+ e. `- G& E1 C4 \6 S
4 D6 f- X X; M/ W0 B5 `: ~5 Z
CustPropMgr.Delete ("图样名称")6 z7 n) r% h d3 \4 s
# Y$ p% |6 F* y3 |CustPropMgr.Delete ("材料")
" ^2 S. O9 }8 g& c) [/ _- s& s. v
' V4 y! |0 l) b' e0 o'新增
2 w8 V" k$ h8 O9 v! _1 j
! w: Q% E; B$ ^! J1 I0 c$ qCustPropMgr.Add2 "图样代号", swCustomInfoText, e
$ |% f/ X( m* a/ b- x8 g/ ]. s
0 m# x0 N* x8 @! M. ~CustPropMgr.Add2 "图样名称", swCustomInfoText, m& C i" \9 P8 p2 I. Q
H1 S8 P) r l
CustPropMgr.Add2 "数量", swCustomInfoText, ""
4 M0 }5 Z) K6 M$ x9 B) O6 @+ A
9 c' n, w5 b6 R" i2 x8 P5 OCustPropMgr.Add2 "材料", swCustomInfoText, strmat% r1 f3 h i/ g
: X7 Q& D8 p4 O! ?- b* c' o
CustPropMgr.Add2 "单重", swCustomInfoText, ""
V) s, x6 ~4 J F' ]7 D, o- q) b/ t( Y8 E$ z) F3 I
CustPropMgr.Add2 "总重", swCustomInfoText, ""
5 x) K& Q6 C- z6 S' X3 p; T) [. \4 E$ ?" Z- w* j
CustPropMgr.Add2 "备注", swCustomInfoText, ""
6 n. x9 t) m9 ?2 T2 }/ d# D8 f0 ~$ q3 I* b) t& j/ u7 y/ T/ F
End Sub
, _5 Z' | `" s8 W$ D% h+ ~2 f---) T+ u+ l" Z( g# N
想请教下大拿们,这个代码要怎么修改才能管用。之前的图号分离宏,可以让所有属性出现在自定义里。或者有没有办法加一段自动复制自定义里面属性到配置待定里去的代码?7 z/ |1 A1 n" s, t5 q
4 w# p8 e7 M% d( a' A" ]! O
在线等大神解惑
: O, c( w U A; \
2 K' f: Y' N. L' H+ e& E h+ N$ D* o2 t
|