Type BomPosition
6 Z4 J' H9 p- P model As SldWorks.ModelDoc2
- n( l- S* b, e; L. @ Configuration As String
7 h! {% w8 _' n: C Quantity As Double
2 V2 |' ~1 J5 U) C, g. U: jEnd Type& ~7 M. j% F# O/ [" G; P' d3 W$ _' A
/ M! B8 z- F) J9 ~Const PRP_NAME As String = "數量"
; p: u0 D! s7 V0 y% p8 K1 FConst MERGE_CONFIGURATIONS As Boolean = True. p; j! m8 `2 g( Z/ `; r- z, T Z
Const INCLUDE_BOM_EXCLUDED As Boolean = False: r6 S1 u- z2 {. p. r
0 a6 q) [& I9 r0 X9 z/ L& Y- GDim swApp As SldWorks.SldWorks. ~8 S& @( @5 ~+ X
Sub main(); m1 [/ Z0 g; ]" w' W* s" F1 t
Set swApp = Application.SldWorks
5 M0 R% w, U3 P6 w6 j" ptry_:% m# H4 [% y) e8 y' ~) o) r5 Q
On Error GoTo catch_5 E/ ~! M7 E! U5 L: F d
Dim swAssy As SldWorks.AssemblyDoc
- k) ^0 [( Z# b, C, n9 P& t f& d Set swAssy = swApp.ActiveDoc
6 N) D6 S3 d, n If swAssy Is Nothing Then
4 l( E2 ? ^, L2 d6 S Err.Raise vbError, "", "Assembly is not opened"
" S3 o% E2 Y# Y% q5 ~" ` End If
6 c i6 `3 X+ e swAssy.ResolveAllLightWeightComponents True9 a7 o8 O* {: R& \0 k
Dim swConf As SldWorks.Configuration, ~# A4 T3 g, |/ T2 O
Set swConf = swAssy.ConfigurationManager.ActiveConfiguration5 V, |+ d2 F" I+ N, l3 a
Dim bom() As BomPosition
' L( m7 S9 ~2 g. { ComposeFlatBom swConf.GetRootComponent3(True), bom6 H$ ]6 k) A S3 g
If (Not bom) <> -1 Then! ^) Q2 B1 g4 T
WriteBomQuantities bom; f7 x w/ [" N* ?+ p
End If
5 a, W4 v9 }+ z3 j. @ GoTo finally_; P/ v( A) c' `0 J3 o( _
catch_:
x' l6 {' i1 \) P' ~) V/ ]- r MsgBox Err.Description, vbCritical, "Count Components"
5 g4 x; j0 [& _1 D( _' y3 M) A! i4 sfinally_:
( t5 C+ e' S# [) D) k" H! jEnd Sub; X5 R, b! y+ t- @) p4 y
1 c- Z6 Q" j; u4 _Sub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition) Y I& r |9 b- l: D; d
Dim vComps As Variant' B+ y' e0 e- [' ~
vComps = swParentComp.GetChildren
/ `4 v3 \( G1 v- g, t0 J: O, l If Not IsEmpty(vComps) Then3 |1 }8 B3 k7 v% l
Dim i As Integer
% o1 }! J+ `8 p& U h/ \* ? For i = 0 To UBound(vComps)
' K: ]* W2 o6 C Dim swComp As SldWorks.Component2
. K4 k' W2 b8 g0 G ]3 L Set swComp = vComps(i), t h3 l8 }& G. }6 Q
If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then
( Y2 N8 [+ l& P8 q Dim swRefModel As SldWorks.ModelDoc24 p* h! i( T! d7 J
Set swRefModel = swComp.GetModelDoc2()
$ H" P* E' [# o, S% J! g If swRefModel Is Nothing Then. G& c3 v/ ^ R
Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded"
- U1 D6 J- M6 B1 R End If
L& u6 g( b6 L' K# V- ~ Dim swRefConf As SldWorks.Configuration
) Z- b$ M& G1 p4 U Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)
9 M3 P- K( U. p! j5 a! a$ S( b Dim bomChildType As Integer _% y0 `4 G3 i) d5 T
bomChildType = swRefConf.ChildComponentDisplayInBOM8 x1 U2 G: m; {8 I" ^
If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then8 y& `+ p4 ~* A0 w
Dim bomPos As Integer+ H+ w5 V5 j0 ?9 x; u
bomPos = FindBomPosition(bom, swComp)
/ R" u. t$ e0 j- {) b& H, ? If bomPos = -1 Then0 G) A5 y4 u* C* b) m
If (Not bom) = -1 Then |0 G( @, b) M! e3 C7 r6 K" t) O
ReDim bom(0)
2 H$ x, n) `: Q1 D1 b4 | Else% c- f1 ?5 _$ q& E
ReDim Preserve bom(UBound(bom) + 1)7 x# u! q7 p; X, V6 h: j
End If( `* @% f6 F+ k
bomPos = UBound(bom)2 Q7 C- t2 N4 S2 W
Dim refConfName As String
k+ N$ z% _! V2 \1 r If MERGE_CONFIGURATIONS Then
4 o) ^8 A4 Y& w c3 W refConfName = ""
$ p0 R& K( C, k1 B2 I( Q ^+ q Else5 P. z( c9 }6 T
refConfName = swComp.ReferencedConfiguration8 {. \3 J8 J& f) F3 x: N! z1 I( J
End If
( F! n. a, }1 r Y7 V Set bom(bomPos).model = swRefModel! z% {( f- C- C+ D: {# W* u% B0 [5 j
bom(bomPos).Configuration = refConfName
/ z& ?5 H8 }! T& Y bom(bomPos).Quantity = GetQuantity(swComp)) v5 k1 b: k' m
Else
1 n6 _/ M$ r" K& [9 _ bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)
* u% S7 P! M" R7 E- @0 D End If
5 }; C. b7 G0 H" O: e. n, ~1 S End If# k8 b3 l% F2 x" g) [
If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then
, E) {; M0 c) _& Z* W3 y ComposeFlatBom swComp, bom
/ ~' G9 f$ ]( _5 b! ] End If5 d& a$ U- w' r1 w0 H! {. R
End If
- X8 V% L/ G6 K: T Next
; H$ X; c# E5 @" } End If, x! i# T: ]0 _8 d2 Q1 N
End Sub
- E0 M1 |( x* Y1 A; a
! D* d, i- M6 V: A8 }& J5 jFunction FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer
8 d9 k1 U, L5 O) z4 } FindBomPosition = -1/ q* i9 Q- f9 L4 r
Dim i As Integer& ?1 ]5 T. h }
If (Not bom) <> -1 Then
' C% g# j8 f) f; P. K Dim refConfName As String
# `' J+ E5 q% I( _ If MERGE_CONFIGURATIONS Then
8 g& p' p- {% Q9 v( ?7 r( O refConfName = ""
* O6 I2 O8 C. a; `; V Else: \9 g% e# Z. C* C* u3 i5 L% ]
refConfName = comp.ReferencedConfiguration
1 l' H/ F% p" Z5 ~" k End If0 O X9 q6 m6 C& N) p
For i = 0 To UBound(bom)5 E$ R5 _% c/ F4 B1 I
If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then4 p" o) z$ t, w. n
FindBomPosition = i$ t8 b% n: L: u
Exit Function
( R% t5 m) \% M End If. ^' E# y, t. h3 o/ Q, S' Y8 p
Next
( y- `5 ~+ N. B7 p2 M End If6 i8 o, O/ L* x. ~
End Function
" A$ S. b" _, M( U( _" S+ [8 A
9 v& i; T% {% H4 n& p' H! ^$ r+ s DFunction GetQuantity(comp As SldWorks.Component2) As Double4 M6 S0 L* G( N( Z- t0 j
On Error GoTo err_7 k7 S2 o0 K( {3 Z3 \% v9 K# W
Dim refModel As SldWorks.ModelDoc29 o9 G7 ^" F& j8 I+ E+ l+ H1 j* S
Set refModel = comp.GetModelDoc2) I8 R( K3 i% }+ J* W
Dim qtyPrpName As String, V! ~! W+ t" Z+ h5 l/ r7 M9 Q. d
qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE")
7 _+ p( O9 H( y, D! b) `% S If qtyPrpName <> "" Then2 T% \4 B: @* p7 a
GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
2 j# b- V0 y; T# T' _8 X Else1 H+ k6 K* K9 X( g$ L7 `
GetQuantity = 1' s8 a( l7 P2 D, j. s. [
End If
& L/ x: }, S0 X1 { Exit Function3 _5 w9 K! a' a1 ^$ {
err_:7 w1 _5 z; \* I# d- P; F
Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description* `1 j6 |$ \3 ?$ ^8 m; f7 L
GetQuantity = 1
' P4 g0 u+ [; \1 cEnd Function6 [( R, W- a5 S9 K# q) B1 y9 m
) Y+ y4 m: Y4 H, @ G8 \" ?% DFunction GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String. K' X6 c. E# i3 Z; m: B
Dim confSpecPrpMgr As SldWorks.CustomPropertyManager
; [7 o% m8 k* J Dim genPrpMgr As SldWorks.CustomPropertyManager
/ j1 O- V& [ R9 P% f# Z Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)
2 u$ o) c# E& Q) j6 c Set genPrpMgr = model.Extension.CustomPropertyManager("")
7 E) E4 A6 g6 e ~1 A Dim prpResVal As String7 h1 s9 M" n# w& u, R! m5 p" o: q
confSpecPrpMgr.Get3 prpName, False, "", prpResVal
: P$ L) G4 x- M# r( d9 K! B+ }" G If prpResVal = "" Then6 I& _3 b5 ~* S* a0 D N
genPrpMgr.Get3 prpName, False, "", prpResVal& U9 [3 U6 q( x
End If
5 l) J0 l9 y0 J: L' {5 `- v4 k GetPropertyValue = prpResVal8 D/ w( q7 i r+ u# F
End Function
$ ?+ S: T3 L' m6 F3 a; a2 w9 V, K% H0 B, x$ p* p$ m, S- z
Sub WriteBomQuantities(bom() As BomPosition)
; z( v! U0 b2 a/ O% B Dim i As Integer
. g! @' _ g+ W- g+ K" V4 f: {. C If (Not bom) <> -1 Then7 P9 C G4 k" q; g; R
For i = 0 To UBound(bom)! T1 A- L9 x0 ]; H9 T
Dim refConfName As String
# ^9 v0 U# I4 H6 f h* o1 T Dim swRefModel As SldWorks.ModelDoc24 G6 Q3 e A* U0 {2 X3 `) i
Set swRefModel = bom(i).model
. P, u, x% n) \; B If MERGE_CONFIGURATIONS Then3 f5 F3 s8 U/ P M$ e
refConfName = ""% o. h! g+ m+ S) q& K# E7 [5 F
Else# e# \4 F4 p( A. h
refConfName = bom(i).Configuration
& M- `' i! X& e7 F, b2 ^/ l( @ If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then( s1 @$ @& m8 e; V
Dim swConf As SldWorks.Configuration
& v: M. L5 }0 F; q7 o Set swConf = swRefModel.GetConfigurationByName(refConfName)
$ X* }+ O7 v3 h+ K Dim vChildConfs As Variant/ C0 B, x; ^! L* z
vChildConfs = swConf.GetChildren()7 P0 ^" B0 w% O8 X- y1 q
If Not IsEmpty(vChildConfs) Then
# K& v8 A* C& a U! Z. d# A c5 K5 x Dim j As Integer
* [- t6 w' R8 U6 l- u! V6 g For j = 0 To UBound(vChildConfs)
) m! e/ `2 O8 l. i3 \/ S Dim swChildConf As SldWorks.Configuration
. R! G( s9 a4 O5 [. ]6 x Set swChildConf = vChildConfs(j)6 K! D( w+ X0 W) N% i
If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then8 H6 c9 x! [) S. B; l2 D
SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity
9 J( i0 W [" R0 @ End If
+ W3 \/ ~5 m# a; X6 J Next
8 K* |) R2 F) z End If
7 J" [# R; ]2 ]# }' f End If- L/ S4 ?7 @. X
End If9 r+ _* O* p% y% A
SetQuantity swRefModel, refConfName, bom(i).Quantity
$ K7 ~- ^0 r S/ p1 y# t Next% Z c/ F% ^- K& a# V- r1 H) W! }
End If3 r- V1 O# s+ L
End Sub3 e3 w3 Q) t) P& P) n3 y
3 o& c9 ?) V9 }& E) t$ l" T9 C: m2 q. ASub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)
7 v5 J3 ?& x( B" [% ?& J; R Dim swCustPrpsMgr As SldWorks.CustomPropertyManager
9 G; k* J: ?+ U9 i, y* ] Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)$ Z6 }9 q2 t/ K! |; O/ v( U! G
swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue. Y6 p$ R5 T! P! [2 b* K
swCustPrpsMgr.Set2 PRP_NAME, qty
2 C, [! d# q4 M5 E: {; X7 q4 rEnd Sub
! ~/ U2 l4 o4 f' t5 h7 k% U& @' N |