Type BomPosition
2 h% }' R5 N3 o% x model As SldWorks.ModelDoc25 E# Z5 W. u$ b, W; ]6 G# M( n
Configuration As String
$ p0 Q' u6 b, K; E8 P% ` Quantity As Double3 ` T7 T7 I; T% r! [
End Type
* P: @$ y( r3 r6 X, U l9 F, _+ h( ^& A6 x
Const PRP_NAME As String = "數量") v# }) Z w4 J# E
Const MERGE_CONFIGURATIONS As Boolean = True
7 b9 A* l( q& x: ?! z3 QConst INCLUDE_BOM_EXCLUDED As Boolean = False
& g0 {. o# \2 Z" O3 P4 i' j" k( _0 W% R% q. E/ e) j5 }
Dim swApp As SldWorks.SldWorks1 [% A: p$ C5 X0 U4 ^/ H
Sub main()
# j' C% s+ S7 r9 D) Y9 U Set swApp = Application.SldWorks
) A/ U5 Z+ g/ b" w6 ?* |- vtry_:
( W0 [% y# W& z7 C# y; w On Error GoTo catch_% H8 X8 |$ c8 p, L9 v/ j8 a
Dim swAssy As SldWorks.AssemblyDoc
( y# ~. h4 P! t( p& {7 F$ p- j: R/ d0 Y) a Set swAssy = swApp.ActiveDoc
/ ?0 d+ M6 G; `8 I5 W3 r If swAssy Is Nothing Then% V% @* |( _& U: L. q% K
Err.Raise vbError, "", "Assembly is not opened"
' y& l; m6 G6 O9 G; ]( v% E/ L End If. \9 g" X5 f3 C& s: e, P6 u
swAssy.ResolveAllLightWeightComponents True
2 V3 @/ {: Z0 ~$ _! @0 N& s) { Dim swConf As SldWorks.Configuration2 e" f2 H* | \3 T$ S
Set swConf = swAssy.ConfigurationManager.ActiveConfiguration
+ l5 w0 {2 M. Q6 G; |# _, J! } Dim bom() As BomPosition/ V5 C0 l4 l3 k' T/ s& ]
ComposeFlatBom swConf.GetRootComponent3(True), bom
' Z, y y$ C. v0 n g4 U% M If (Not bom) <> -1 Then9 t1 J: I* S- L$ P& [
WriteBomQuantities bom8 ]6 ^: [; q8 s# C+ ?6 q
End If
1 Z' v4 J2 `0 U6 [4 {9 E GoTo finally_8 R" g: B' K$ W1 Y
catch_:
- O# E+ H5 M; P9 T3 v: m6 D8 w MsgBox Err.Description, vbCritical, "Count Components"
: _0 l$ G ^ [9 X' T; ~/ M xfinally_:
8 U: x% ]5 {, d( p/ gEnd Sub
1 O! A q; {$ K4 w% k$ e5 |5 X
! ]" H! _, g/ i; o5 E& zSub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition). g' \: E# W$ e+ l6 r9 u
Dim vComps As Variant. O3 M7 q. g6 V
vComps = swParentComp.GetChildren
5 _: L' C$ q J3 o! ` If Not IsEmpty(vComps) Then
( _$ l; L. `( h- ^! C! U) ]4 C' o Dim i As Integer( {, {: V9 _2 s$ Z
For i = 0 To UBound(vComps)3 g& }. A' | j
Dim swComp As SldWorks.Component20 j, c( G/ I t
Set swComp = vComps(i)
K; g) M. W! j& U1 W3 K- U, V7 o( A If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then, [' t* w1 q: Z) V
Dim swRefModel As SldWorks.ModelDoc2
, G& P. s5 m5 u4 j' {& N$ H5 b a Set swRefModel = swComp.GetModelDoc2()8 x8 x3 h& c) X1 ?3 s$ A
If swRefModel Is Nothing Then2 u$ F6 g7 L* {% I* e' }) q* {
Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded"# l8 [' {% Q3 Y' z B
End If# z: v8 P! e- C3 g, R2 V; ]0 o
Dim swRefConf As SldWorks.Configuration
# E# |! B7 q) x* ^ Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)7 l6 b0 b5 V+ Y( @9 k# S
Dim bomChildType As Integer
' m: c1 U4 U! _9 X3 R" y bomChildType = swRefConf.ChildComponentDisplayInBOM; l- |1 L# Y% L" N
If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then
9 c2 Z$ r! x9 x" E! J9 k( O Dim bomPos As Integer$ y( }; q% ?4 e) }7 [9 l- ]
bomPos = FindBomPosition(bom, swComp)
8 j$ d6 ]& a' j* L i) f& a If bomPos = -1 Then
v* q* Y: \( S8 J If (Not bom) = -1 Then
2 H$ e8 i7 r4 P' `$ E ReDim bom(0)
1 X5 p7 M: T8 F Q' i+ S/ x. Y Else
+ l* C% i7 ~& @$ o7 [- o' n ReDim Preserve bom(UBound(bom) + 1)" [$ G( h" p6 m/ b. G/ y: R4 ]$ w
End If# ^$ s' W" j1 Y
bomPos = UBound(bom)
* X8 I! b0 f4 } Dim refConfName As String4 e4 N3 G) e6 b0 |, U" ]. i
If MERGE_CONFIGURATIONS Then* T. v' b0 `# c9 B% T+ C7 Z( r
refConfName = ""
' s4 }* Q- a0 W4 v Else' a' H$ J( t8 ~. _- o
refConfName = swComp.ReferencedConfiguration$ p" U o) D `/ S+ E1 n; _# c
End If
% U( R/ ?3 J& H7 u3 Q, o Set bom(bomPos).model = swRefModel
1 d5 A$ U* G* V bom(bomPos).Configuration = refConfName1 E* y' p/ u2 S2 s) z
bom(bomPos).Quantity = GetQuantity(swComp)5 \8 s& X: g# @" x
Else
. F& j2 m# f% T6 Q8 k( k bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)8 d1 R0 M/ g3 m) O3 x
End If
F9 Q% v" e" u End If
) S8 ~! \& ~0 c' W1 b7 u/ V If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then
3 z3 b7 g7 L' U/ O. r! `( z- Z ComposeFlatBom swComp, bom0 c3 x0 z+ [5 x9 H/ m: Y0 r
End If
$ z% P9 N3 `2 u8 o2 h' U0 K: f4 d End If
7 M! O8 K' A: @2 I" Z6 q! @ Next. ~3 v- l: x2 R7 c) u
End If: x5 h8 q% N7 ]
End Sub
9 L) W: Q/ u% V3 O9 C( W! ^' R2 T! {7 p7 ?* `
Function FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer6 S/ d& l2 F" c2 T3 d
FindBomPosition = -1+ I% G9 m6 e: H* o8 C
Dim i As Integer% W' Y3 }% j' L0 m9 b( I9 R/ k! l
If (Not bom) <> -1 Then% Z0 \0 |$ z! w0 J) J: ?
Dim refConfName As String! w- T i; |' c# j
If MERGE_CONFIGURATIONS Then& ^& y6 t$ L" e$ R# f
refConfName = ""( [: X! a6 U$ e! k5 {9 N
Else
6 u a( H! J. _& K refConfName = comp.ReferencedConfiguration# T' _- m2 S) i9 R& f& X* w2 K
End If+ @0 Z! P1 j5 v% [0 b9 I0 s. N L
For i = 0 To UBound(bom)* k4 U$ h3 ?; b. H4 q
If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then
0 u$ I2 v( U9 O6 x FindBomPosition = i
3 \) K* Q- r( e& q1 T+ H0 G3 N( Z$ P Exit Function
, J, y% y9 U. F1 w3 T End If5 o4 r) H2 i0 O0 T: l6 d
Next2 `' f1 ~; G" L2 ?5 P. _
End If; Q6 H+ p& E& B- {. c
End Function. [- e, c# D! d7 q4 |
0 n/ `% Z& b8 T1 l
Function GetQuantity(comp As SldWorks.Component2) As Double7 M4 a& z6 U/ C
On Error GoTo err_: Z2 I! D; s! g+ v! N& v9 s8 u
Dim refModel As SldWorks.ModelDoc2
( Y. C. S9 g7 |0 ]1 z' s+ x" w Set refModel = comp.GetModelDoc20 O, B7 {3 s: ?4 H: n: s) l) P
Dim qtyPrpName As String
- \/ b* @9 L0 K qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE")
7 Z+ ?1 M2 d5 t* K+ E6 \2 H3 b$ I If qtyPrpName <> "" Then
4 b" b5 b; F: T" s/ H* O0 G9 G GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
4 N& G0 a3 q7 z Else) R8 K) p/ {2 Q9 `1 o Y
GetQuantity = 1 ?; p( d# m4 }9 M4 T/ G
End If- Y1 [, d5 @3 t
Exit Function- P ~6 u" E+ ?9 [
err_:# |$ z/ c# d4 n( d9 N* }6 ^
Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description
; e: l) A) @* s GetQuantity = 1- ^4 C; h, { `+ J2 ], }
End Function
" [* T7 r' |- G- G6 d2 d& H
9 A7 t- l$ p. D! W6 T4 J) WFunction GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String
- O# s# z+ c0 V) `( j Dim confSpecPrpMgr As SldWorks.CustomPropertyManager
: d9 `1 |2 k& @ j+ y) ` Dim genPrpMgr As SldWorks.CustomPropertyManager, a3 z" k; R7 g. z% Q+ `. b2 Y& }
Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)5 N) \7 s3 y! M8 I9 I6 Z4 n" m4 O& i5 `+ G
Set genPrpMgr = model.Extension.CustomPropertyManager("")& t* M' ]1 O+ V+ ^4 A u" Y; T
Dim prpResVal As String1 s) q2 Z5 D# w) T& e& m- @) o. G2 C
confSpecPrpMgr.Get3 prpName, False, "", prpResVal
1 V) U' _9 _4 R If prpResVal = "" Then
/ i6 n9 d& \1 r. A% J genPrpMgr.Get3 prpName, False, "", prpResVal
, k0 u3 l1 ^( K$ Q8 A4 |' x End If6 Y/ q/ }" `9 Q$ K8 a5 _4 C0 a
GetPropertyValue = prpResVal
; n9 b' q1 I8 }$ lEnd Function8 F. ~: a0 s+ [* N# j; X( [
, ^. H6 G+ Q5 Q6 s
Sub WriteBomQuantities(bom() As BomPosition)# H( m7 t9 f1 D
Dim i As Integer
8 h) D4 H0 D$ F( J5 {/ R. l( t If (Not bom) <> -1 Then
" G9 P! |5 X, e+ z, P# L0 j! r For i = 0 To UBound(bom)+ p9 A% a4 z3 L1 [
Dim refConfName As String! H6 n w9 n6 M& r- I
Dim swRefModel As SldWorks.ModelDoc2
5 M: h6 t* Q/ L* r Set swRefModel = bom(i).model4 O2 w, Z5 O' G7 l
If MERGE_CONFIGURATIONS Then
8 E% t5 K' N5 K3 }5 A/ K refConfName = ""
+ C2 _0 z+ i% f& X# ~& C, h Else
5 U' R; R7 f; y3 J refConfName = bom(i).Configuration3 ?2 Q6 I! y. Z2 ?% o$ I% ~3 C+ J: E
If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then4 z9 a' n$ i+ u& Q( `
Dim swConf As SldWorks.Configuration$ t1 n1 M' d- c0 G
Set swConf = swRefModel.GetConfigurationByName(refConfName)
7 j3 C2 X2 T# f. ~) P9 m: N% O Dim vChildConfs As Variant
1 F+ \$ ?% d! n; H8 C2 d! s: v vChildConfs = swConf.GetChildren()7 }7 V- r, W# D1 x) n/ }
If Not IsEmpty(vChildConfs) Then) U/ ]3 Y$ o9 G! g3 U! j! X% }
Dim j As Integer# [. v E9 U/ g, e" o
For j = 0 To UBound(vChildConfs), c2 V: @4 w. e9 g7 |- ?% l" b
Dim swChildConf As SldWorks.Configuration2 x) A! c3 |" g6 N' R
Set swChildConf = vChildConfs(j)6 O5 J* f' |5 Z! V/ c
If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then1 m _$ G% w0 h* u+ Q
SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity
$ C% w1 @) |( |6 c- B6 T End If4 F4 R$ z9 y" d+ j0 y5 P
Next
& S" w' _, o! \# ~$ d. k End If; G# @5 m) X9 A
End If
, g; p v! l0 _9 g' N End If
6 q( J) Q( ^0 B2 k% E SetQuantity swRefModel, refConfName, bom(i).Quantity( @2 N- l( S, F+ q
Next
$ ^. D, k# z- ~# q5 E# _" e6 Z+ y End If& `% @. I; ~6 h5 Y7 F
End Sub( A; x$ D+ d) \5 d/ P, ^7 u
' o1 H9 e: ?. q2 j( [( U- J
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)
/ ?) _/ a* A9 L, C- \# o Dim swCustPrpsMgr As SldWorks.CustomPropertyManager# S& C. |& d- J2 t
Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)6 c$ v/ x& _" o! l) t; K4 D- V
swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
$ @+ Z# A8 n5 P) B swCustPrpsMgr.Set2 PRP_NAME, qty( E) |; x s5 f' H# _) v% w
End Sub4 r1 K9 `' N! b& B2 T5 L! Q) O l
|