找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
楼主: 李邈

SW宏-零件数量自动写入自定义属性表

  [复制链接]
 楼主| 发表于 2024-9-7 09:38:50 | 显示全部楼层
本帖最后由 李邈 于 2024-9-7 09:41 编辑
; x- b9 S2 v; w3 S9 j( n$ \# I& b
sycfj 发表于 2024-9-7 09:27* W, Y" _) H" f! h( y
能详细说明怎么使用吗?我在装配体中使用这个宏以后,工程图中不会出现数量

5 R. @4 ]2 _, }- c8 |5 y* zhttps://baijiahao.baidu.com/s?id=1796657905732909661&wfr=spider&for=pc
  Q! w' N$ F6 H按照这个方法设置,他链接的是仓库 你连接数量那一栏就行了5 f- Q( l6 {8 P
设置好之后,把工程图文件另存为工程图模板,删掉上面的视图,下次新建就会直接出现  h# M5 p' y2 P% U" Z" W% S1 N
以前出的工程图,用批量替换模板的工具,全部替换成新的工程图模板就好啦
6 E; U& e5 Z) D4 g. l
 楼主| 发表于 2024-9-7 09:40:02 | 显示全部楼层
sycfj 发表于 2024-9-7 09:363 {2 p6 R' I1 E4 L: ^: D
也就是说零件模板中要自定义属性 数量
2 C7 b; Y  E$ A% X3 ?) \& N
这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了6 D5 F( ]' @" v6 }
你只要工程图去连接就行了6 V, _0 [/ y3 O& L5 v1 N, ~
发表于 2024-9-7 14:50:39 | 显示全部楼层
李邈 发表于 2024-9-7 09:25% E- _& b/ V( S- s. ?. k3 m
装配体中使用

6 f  n0 g" s) ~0 d* V% h7 D' f: t还是不会用,加个微sycfj2: }7 D- Z$ ]9 A3 p+ N0 C

点评

加了  发表于 2024-9-7 14:51
 楼主| 发表于 2024-9-7 15:22:48 | 显示全部楼层
Type BomPosition
* [0 ^. ~2 F! Q" T  l- U( `    model As SldWorks.ModelDoc2: b2 i; @% d3 R4 x
    Configuration As String. k& F3 f1 L* K% p
    Quantity As Double
' c& `/ R  H2 c3 lEnd Type- [7 v7 l$ \. b

9 L  Y& H" F( p$ ZConst PRP_NAME As String = "数量"
3 r! q) q& S( g. _* ?4 PConst MERGE_CONFIGURATIONS As Boolean = True
# K9 c+ v  d7 x) {. @" A- A! |Const INCLUDE_BOM_EXCLUDED As Boolean = False
6 n# j' B: }8 T. C
9 j0 J4 I; q" V# {8 i6 {( [9 Z8 K; VDim swApp As SldWorks.SldWorks
# ^" ~& K" l% a& }1 I* s, P7 B/ iSub main()
" P1 }2 ^8 W: G3 M    Set swApp = Application.SldWorks
) k0 d& \" l1 O. ~$ w- Gtry_:
1 ~1 b2 w5 V4 ?  c    On Error GoTo catch_) D" _' G, d2 G0 P& w
    Dim swAssy As SldWorks.AssemblyDoc
7 J3 x4 r. M7 Q; D, K0 n( X5 p4 G; ^    Set swAssy = swApp.ActiveDoc$ ?6 H) f* U8 O% H, f, [
    If swAssy Is Nothing Then
0 }! h( @" x% s5 W" \( T3 H        Err.Raise vbError, "", "Assembly is not opened"
+ d. `$ ~4 x0 _, i    End If& K8 G* C$ ?* ~. ^( X( r0 y
    swAssy.ResolveAllLightWeightComponents True' O: U" b4 b5 o2 c4 w2 Q
    Dim swConf As SldWorks.Configuration
5 n" M4 j  M# z9 e# y    Set swConf = swAssy.ConfigurationManager.ActiveConfiguration* d& u& N1 }& a7 d4 x5 c% n
    Dim bom() As BomPosition
; x6 ^$ g6 H+ k  S- `2 }. C0 R    ComposeFlatBom swConf.GetRootComponent3(True), bom
2 h3 _" c' m3 {/ k7 _5 z) n    If (Not bom) <> -1 Then- Z1 u# ^7 A# k$ r0 S
        WriteBomQuantities bom
0 |* L3 k# h3 u" @5 q& s    End If
: A: `$ Q: r% y* U" ?0 A: i9 q3 ~    GoTo finally_
+ O( z4 p' L6 u/ X: ncatch_:
2 ^! n4 l  Z6 B$ y( m5 q2 ^! n    MsgBox Err.Description, vbCritical, "Count Components"
1 J- W- p/ I2 \2 ?' {' C. ]& ^2 afinally_:
/ c4 j' F& A+ i- \End Sub) C7 H0 Z/ }7 X, B
9 [* C1 V5 B0 L0 e# k3 O
Sub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition)
1 J, n) p7 h3 o' ^# ~+ [' `    Dim vComps As Variant- K* Z) B' k  v5 B
    vComps = swParentComp.GetChildren; N+ b; S4 [* d3 E' ?; G3 m
    If Not IsEmpty(vComps) Then
5 J% a& k. E; K7 Z$ G- b- r( |        Dim i As Integer9 f2 _- v7 F6 P9 s4 H% s: Y
        For i = 0 To UBound(vComps)
4 u; I3 O& T1 `- n1 K- T' ?! [" |            Dim swComp As SldWorks.Component2% k$ b6 n0 B; v( r
            Set swComp = vComps(i), I# U6 [1 b2 X0 z0 Q
            If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then- A+ y8 k0 t! t5 O; ^9 U
                Dim swRefModel As SldWorks.ModelDoc2) F8 U% a: u% x* S' B$ s
                Set swRefModel = swComp.GetModelDoc2()
/ C6 U: `7 G) T5 s                If swRefModel Is Nothing Then; h6 P0 r7 J! N. A
                    Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded"+ R9 I. c: w3 T, Q2 Q
                End If% _- c; D' l9 ]# T1 D% u. U
                Dim swRefConf As SldWorks.Configuration
+ Y; v- f) D9 S4 i  x                Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)
1 `2 Z7 I2 a( H8 }9 }. ?8 K                Dim bomChildType As Integer+ p8 R. z* N8 X* B
                bomChildType = swRefConf.ChildComponentDisplayInBOM
' N1 \( Z5 t4 N2 a: N( g* {                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then  x/ X: ?# V  H4 u7 w7 H& f  M
                    Dim bomPos As Integer
* n% b9 A! B" C0 F7 |                    bomPos = FindBomPosition(bom, swComp)" b9 y2 P1 j' q
                    If bomPos = -1 Then
7 {4 X" ^4 y5 }$ d7 B3 v4 {/ q) c  N                        If (Not bom) = -1 Then0 x9 P! @: M( J# E
                            ReDim bom(0)
5 ]" q  K1 n: l3 M: ]" S                        Else
! m) B) B& M4 ~2 G% w                            ReDim Preserve bom(UBound(bom) + 1)
1 N/ c4 P3 Z5 F: J! i9 I2 T7 |& A( k$ ~. R2 c                        End If% O: l& y7 `0 M- l* t, [* i3 G
                        bomPos = UBound(bom)
% w1 U- X. d3 _3 w2 E                        Dim refConfName As String: s' R; M3 b2 D
                        If MERGE_CONFIGURATIONS Then
5 Q) m6 K5 d, I5 {) d2 ~                            refConfName = ""
, \6 c7 j- Q$ V: `6 H) O$ t                        Else
% k$ A: Z- u2 ]/ }                            refConfName = swComp.ReferencedConfiguration
/ n$ [+ l% g) E/ h4 e( @0 v2 d                        End If3 l, d  z% P, `. ^1 S$ `; T
                        Set bom(bomPos).model = swRefModel4 ]+ Z2 U/ L) q5 j
                        bom(bomPos).Configuration = refConfName
# D  O8 |. z( j& w; o4 e                        bom(bomPos).Quantity = GetQuantity(swComp)
6 \. q2 a* }8 K* }  D, x; [                    Else
% k; N' |& S+ h* f                        bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)
: @1 ^6 R+ y0 w6 w8 U; K                    End If2 V4 \8 q* }- g, x+ C; h( o
                End If1 ~$ ~' H/ N( e% \0 G
                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then5 o- }4 @5 n( m6 ]8 t4 D$ F
                    ComposeFlatBom swComp, bom0 ]" [( J- n3 w$ w7 I2 x* u& C
                End If
/ V) ~/ O7 L4 n2 F8 P' V: K            End If
$ D! P% h+ M3 b/ G+ d- Q        Next. X0 [7 l7 R3 j( B; h& S( @
    End If& o1 o: b+ B" k: ?, k, ~6 z* }
End Sub, F0 \% U9 n- M- A
3 {3 |- D' v0 E3 b1 L
Function FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer6 `+ }/ ?# L% _; J
    FindBomPosition = -1/ U) x$ p7 B3 J: i
    Dim i As Integer% i# ]( ?3 I  b8 W6 U" N
    If (Not bom) <> -1 Then7 `& f- E' O/ e) A( j9 M
        Dim refConfName As String  U% {7 y  m; g; J1 ^
        If MERGE_CONFIGURATIONS Then, }& r& F2 c- t6 E0 b
            refConfName = ""- W' R' P5 C+ b
        Else# }) ]" \/ p5 p+ \$ Z5 U8 t2 U+ O
            refConfName = comp.ReferencedConfiguration% q3 |, F$ `4 H" n2 r. g
        End If
) C; w7 D! v9 y        For i = 0 To UBound(bom)
6 E7 r1 u" v7 G5 R4 K            If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then! T. s7 q" V' o# n- m- U: v
                FindBomPosition = i1 y; \5 E% P6 J; j; f
                Exit Function
7 \. b' i- g5 n( r6 M            End If
7 B- H/ I7 p  B6 M: I+ I# T        Next8 z1 ?9 T* C9 J% e& n
    End If( e4 E: |% J+ G1 |
End Function
3 y( W/ P: X3 f4 ^2 @; Z. k8 P7 G" Q, W5 }3 j
Function GetQuantity(comp As SldWorks.Component2) As Double
/ _9 q" E; C- @& AOn Error GoTo err_
+ l; O9 @7 W* i) B/ j. _    Dim refModel As SldWorks.ModelDoc24 ]3 x/ V& P& r* d0 n
    Set refModel = comp.GetModelDoc23 ?. j: _* x5 {' g, I" [# o  Q
    Dim qtyPrpName As String/ x; I9 m" `2 q1 v6 |  q* W% C
    qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE")
2 L5 r, E; C2 l& v* x( ^+ O+ k/ b    If qtyPrpName <> "" Then* }( `! n. s6 V
        GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
. ^6 J% w! c4 ?3 }( B    Else
! \; W1 s7 l/ N: s; l+ l        GetQuantity = 1
& q0 w& ]' X& g$ i% e    End If6 c. `! ?7 \- O  c: G+ @
    Exit Function- I- P6 V$ f, A: N' B4 _; J# Z
err_:
7 u) |3 _$ K& K* O2 O' T1 p% K; i    Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description
5 e  M$ b5 z! n. l1 p" b8 l; U1 A; S$ J5 d    GetQuantity = 1
4 H* V% H& b$ t# p5 S" \6 jEnd Function, b1 a/ Q1 @4 X- _' o# I$ F

* H8 ~& s7 T) MFunction GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String2 H/ x) ]$ x$ k( ?( g: b# Y
    Dim confSpecPrpMgr As SldWorks.CustomPropertyManager
! \" l. g  c' a, c9 Z, Y  Q" G2 @+ H    Dim genPrpMgr As SldWorks.CustomPropertyManager9 q. z& r* C! q4 h
    Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)
1 G8 l1 Q0 W0 h9 [! d3 |7 ?) `1 N; s    Set genPrpMgr = model.Extension.CustomPropertyManager("")+ a0 m9 k" Z% a5 w  K9 |
    Dim prpResVal As String& a( ?8 y2 p+ ?0 x' V
    confSpecPrpMgr.Get3 prpName, False, "", prpResVal" V$ {8 l& y0 D" ~6 x+ c4 _
    If prpResVal = "" Then5 y! A2 ^: F* v# m1 G* @% a( A
        genPrpMgr.Get3 prpName, False, "", prpResVal( G. m% v$ F* I
    End If" d# J( Z% |0 I3 j# t) v7 x/ B
    GetPropertyValue = prpResVal
2 T* l6 {/ Z7 B) t) `End Function7 k- a8 _2 _4 d! p" A
7 H8 f6 H* s; ~% D
Sub WriteBomQuantities(bom() As BomPosition)
& b" A' e2 l# w; ?+ ~1 j    Dim i As Integer- j/ f, j$ r: k% E9 ~5 j9 `
    If (Not bom) <> -1 Then6 b. P' ~7 e* A5 o6 p
        For i = 0 To UBound(bom)
8 T8 ~' h/ r9 y7 k. [' Q( [0 x5 W            Dim refConfName As String
5 z) Q+ y2 f) l% k- y; m" j/ v            Dim swRefModel As SldWorks.ModelDoc2% z- a+ P% m$ f- W- f. }6 G* \
            Set swRefModel = bom(i).model
/ s6 Y+ ?2 l: u* ^$ e            If MERGE_CONFIGURATIONS Then
8 x. N% Y3 E5 u& `                refConfName = ""
3 M: y- {2 ~/ g, ?            Else
% ~) ?0 m* i* v* |, I- t                refConfName = bom(i).Configuration4 H" k/ R) P) z
                If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then" e+ b! k$ ~! b8 L! L0 U
                    Dim swConf As SldWorks.Configuration  S+ ]" e+ H0 B; W9 D4 w, l
                    Set swConf = swRefModel.GetConfigurationByName(refConfName)
5 n5 E! H' n  `5 G                    Dim vChildConfs As Variant
4 {  D; b+ z: u                    vChildConfs = swConf.GetChildren()
7 v$ ]' C7 c- [- j                    If Not IsEmpty(vChildConfs) Then* a7 {* w3 ~" L, ~
                        Dim j As Integer
8 K( h0 ~0 A3 B  O                        For j = 0 To UBound(vChildConfs)! Y' Q0 u0 [2 I0 s! Y' B
                            Dim swChildConf As SldWorks.Configuration
$ [5 s; P* L( P# |+ f                            Set swChildConf = vChildConfs(j)
+ k) Y  D1 {( u8 t: g, w' z6 v2 Z- i8 J) D                            If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then
& D9 I/ p/ _" Y% w( M                                SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity
" P2 ?7 p9 @/ Z  z7 O1 O                            End If& o6 i1 P; H$ @2 G
                        Next
) G3 O6 F% j5 X1 g; L                    End If
/ C3 Q9 K6 I! u2 a7 ]                End If
4 n' ~! }- P! o            End If
* u% d0 {$ f! y- z( B: D' r            SetQuantity swRefModel, refConfName, bom(i).Quantity
& b/ W4 Q4 b- E        Next
! W& h# F* E% Z    End If1 E( v! J7 u$ f9 M; O
End Sub  \% |# q3 O) b5 \$ f" p* x# r
. q9 P) `% l- J$ h5 L% ?( F6 @
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)
$ P8 q( W5 X' S7 ?/ @    Dim swCustPrpsMgr As SldWorks.CustomPropertyManager) ^- ?5 I' }, v! W+ @5 D9 x
    Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)  o$ f$ M" V: u; f* r" c( b. Y
    swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
7 D) D# r8 M- P  Z8 Z5 m# S5 m    swCustPrpsMgr.Set2 PRP_NAME, qty5 B! D/ w0 k" W0 a# u1 a( C
End Sub
' j* B+ \5 [5 a' ^& o
发表于 2024-9-14 12:31:09 | 显示全部楼层
感谢楼主的分享 找了好久了
发表于 2024-10-7 16:38:14 | 显示全部楼层
楼主,能求个宏吗?不会搞
. H/ f, _) ~1 n+ V2 y
发表于 2024-11-21 16:35:10 | 显示全部楼层
看前方 发表于 2024-8-14 19:12
4 U; S4 u" H, P+ z. \1 N一直都是用凯元,自带了这个功能

% t. m# J2 S. h* D$ i在开元没有看到这个功能啊& E) i7 ]* B4 G# h
' ]+ B0 j, \" X5 l
发表于 2024-11-21 16:35:50 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36
( e  P" K' q  y) G也就是说零件模板中要自定义属性 数量
) }9 `! }! S0 c; w, u6 ?+ K# l
刷个威望下附件+ N* |' |/ A$ @- w/ s
发表于 2024-11-21 16:36:08 | 显示全部楼层
李邈 发表于 2024-9-7 09:40
  [3 U- i5 m5 M$ l% R这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了! `9 e& V% i0 `5 C# f1 T
你只要工程图去连接就行了

" C& l+ T4 Z% M, O刷个威望下附件
! I# b* [/ D, ~& L
发表于 2024-11-21 16:53:04 | 显示全部楼层
sycfj 发表于 2024-9-7 09:24
1 H$ K/ D) a0 ~/ V  F' `4 {  [' @  Y这个在工程图,装配体,零件哪个环境中使用
  Z) a- W# ~0 q2 F( w
装配体环境中使用
  @& B! i' L0 d
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Archiver|手机版|小黑屋|机械社区 ( 京ICP备10217105号-1,京ICP证050210号,浙公网安备33038202004372号 )

GMT+8, 2025-10-23 05:21 , Processed in 0.066560 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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