找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
楼主: 李邈

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

  [复制链接]
 楼主| 发表于 2024-9-7 09:38:50 | 显示全部楼层
本帖最后由 李邈 于 2024-9-7 09:41 编辑
" ]1 n1 [% Z+ b7 |
sycfj 发表于 2024-9-7 09:27, A7 r* C3 ?1 l1 d  @- r
能详细说明怎么使用吗?我在装配体中使用这个宏以后,工程图中不会出现数量

2 ]0 ^; b6 V9 m! i. O; E1 Ohttps://baijiahao.baidu.com/s?id=1796657905732909661&wfr=spider&for=pc
5 u, M7 E4 U) O1 [) V: I按照这个方法设置,他链接的是仓库 你连接数量那一栏就行了
. ^9 @3 _: o5 B设置好之后,把工程图文件另存为工程图模板,删掉上面的视图,下次新建就会直接出现$ A* E4 @# ^1 I5 W! ]$ G' f& b) J
以前出的工程图,用批量替换模板的工具,全部替换成新的工程图模板就好啦, H1 [  k5 j$ B. ?
 楼主| 发表于 2024-9-7 09:40:02 | 显示全部楼层
sycfj 发表于 2024-9-7 09:363 ~1 {: }" J0 f2 }+ b( f
也就是说零件模板中要自定义属性 数量

3 ?2 j2 U' @' h这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了
, r/ q; O6 F: {- t' X, K你只要工程图去连接就行了" k* e& V8 x7 ?# x
发表于 2024-9-7 14:50:39 | 显示全部楼层
李邈 发表于 2024-9-7 09:25
1 ?4 P; s& Q6 q& B7 B装配体中使用
' k2 H) G9 p$ _7 f+ l: f, _
还是不会用,加个微sycfj2
1 c! v2 d0 y# `2 L2 F

点评

加了  发表于 2024-9-7 14:51
 楼主| 发表于 2024-9-7 15:22:48 | 显示全部楼层
Type BomPosition
1 ^0 z8 k9 J, z% S- Z: ~/ a) c    model As SldWorks.ModelDoc2
7 X5 @% X: }1 a1 h0 P# Z6 j2 l1 H    Configuration As String8 N( ~- z; H2 T: h
    Quantity As Double
) T) w8 E& l8 c5 R! @# YEnd Type
+ L; N1 l$ l+ n6 k6 E3 Y' F  f5 ~2 ?" E% l! h
Const PRP_NAME As String = "数量"  _. a+ S4 |  d# l1 {
Const MERGE_CONFIGURATIONS As Boolean = True
/ @- u# L% ]" ~Const INCLUDE_BOM_EXCLUDED As Boolean = False; t; V. {& F0 i3 @. u! E" Z! Y1 i$ M

/ w: Y9 I; J9 M. H9 IDim swApp As SldWorks.SldWorks, J) \5 p2 L. Z
Sub main()3 ]! H  z7 A% q+ i. {/ d2 U7 {% u
    Set swApp = Application.SldWorks
9 R+ s( E7 B0 @/ j' Jtry_:
' D, X6 M- x& g. w5 k% r7 S    On Error GoTo catch_
6 \# Q# e& x: a( `0 f1 u% I- V    Dim swAssy As SldWorks.AssemblyDoc. e  h  m0 ?# {% W" ^' I3 ]
    Set swAssy = swApp.ActiveDoc  K: i, Q* C) G/ v# i' t
    If swAssy Is Nothing Then
4 p4 X2 s; [' H# h8 T  S: P3 R        Err.Raise vbError, "", "Assembly is not opened"
3 U) e% E+ X5 S# }: D6 E4 b    End If' s) W  q: G; x8 N! [( B
    swAssy.ResolveAllLightWeightComponents True, D( T4 ]/ V5 Z7 p% \
    Dim swConf As SldWorks.Configuration
: {, q: L& A  ?; g8 @  t1 N* j    Set swConf = swAssy.ConfigurationManager.ActiveConfiguration, @3 D. P  o- o. ^$ ?7 p
    Dim bom() As BomPosition  T* o( g: t( o# s
    ComposeFlatBom swConf.GetRootComponent3(True), bom
2 L- |7 h& u$ _" a    If (Not bom) <> -1 Then5 E; P, g; X' Y- ~: z% W
        WriteBomQuantities bom, h) |1 K* H3 j
    End If7 l. j4 |% Z4 [. T8 w
    GoTo finally_
+ L0 Z( ^+ \( x7 a' w7 s$ Scatch_:
& ^6 q5 V& g" I1 z1 @2 h    MsgBox Err.Description, vbCritical, "Count Components"( b& ^' N- }, v9 H" @- b
finally_:
+ h3 E4 E; |/ IEnd Sub5 z' G% ?. P6 U  ~: |: C) \

0 s6 h& ]/ |1 G% \6 [Sub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition)
0 @: l, ]* ^: Z. L, z  @% [    Dim vComps As Variant
" R6 [! C& ~7 _$ m    vComps = swParentComp.GetChildren3 Z& _0 S( R) H- u* E, g8 ]
    If Not IsEmpty(vComps) Then4 G# \0 B' K: L. ]3 k: j8 o
        Dim i As Integer
# G" N8 W2 C, V/ i8 r& }. K        For i = 0 To UBound(vComps), H1 P( \, v! _8 U* f  G& h
            Dim swComp As SldWorks.Component27 t/ V  _/ X  r- E# f" L
            Set swComp = vComps(i)
5 Q0 u% L* Q0 G! @" g            If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then
- q+ ~& m1 A! G+ S0 F- L: N* U                Dim swRefModel As SldWorks.ModelDoc2  G3 B3 P0 O7 Z9 c3 Z# d! }* F  G1 e
                Set swRefModel = swComp.GetModelDoc2(), C' Z+ @* x9 m6 l; W" T6 G
                If swRefModel Is Nothing Then
+ N1 g) C: d4 q% {3 p6 N                    Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded"
4 [& x% s  J5 N% F- E/ l                End If
' f: R! O% L2 ~9 y                Dim swRefConf As SldWorks.Configuration
7 c. g7 p: o0 {& }: W                Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)+ ~. {% d/ c* [5 x7 p/ T- {
                Dim bomChildType As Integer7 K' T3 M4 X' |! M: e7 }
                bomChildType = swRefConf.ChildComponentDisplayInBOM
; G3 C' h0 I" E: R% i# o                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then0 @3 l# Y# `6 R+ M
                    Dim bomPos As Integer
! v. A& [1 O& _5 D                    bomPos = FindBomPosition(bom, swComp)
" c$ H! I4 @, R+ I                    If bomPos = -1 Then, }* Z; @+ E+ B
                        If (Not bom) = -1 Then
' V0 g0 A$ p% W+ {                            ReDim bom(0)6 v1 Q0 f4 q* a: _1 }- w5 e
                        Else
, j: N& r$ ]% j" c0 |- m% L# Z3 T" D                            ReDim Preserve bom(UBound(bom) + 1)
3 f% t3 p% Y* T+ w                        End If" b# |0 X8 F" D9 y6 }: _; t  l! _
                        bomPos = UBound(bom)
0 g  Y6 x' H0 i1 }                        Dim refConfName As String# L6 ]# ^  p* x) _, J6 `3 l
                        If MERGE_CONFIGURATIONS Then8 c  i0 c4 _% C+ ]
                            refConfName = "", h9 e+ W, Y" d1 i% e
                        Else0 E( k% \, h/ W7 z+ ~% r  K, D
                            refConfName = swComp.ReferencedConfiguration
: e, r& t. [1 u/ S0 R4 d                        End If
- n; h! L# A" r( P) A; \; D7 K                        Set bom(bomPos).model = swRefModel4 r- g5 K; E: p
                        bom(bomPos).Configuration = refConfName
: Z& z- x/ ]2 [6 p5 t0 [) D                        bom(bomPos).Quantity = GetQuantity(swComp)2 b, P% _( I7 X. E
                    Else  c; r+ r& U, G. q/ U" v
                        bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)) t2 q- t& K# Q: D7 }; l+ B5 W
                    End If/ u3 p8 o5 C. p5 s# B
                End If1 J) X$ P' `# m; a1 o  f
                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then2 N( M& T7 q" |/ q, a/ }
                    ComposeFlatBom swComp, bom
: ~7 y! ?" J) Q1 Z                End If
3 |# K. J4 ~1 v5 e7 E, I4 \! n            End If
& b; p4 I& h' z& ]        Next3 z1 c9 [5 l% V$ a) j2 [1 x) D
    End If
" ^7 F" v: P! s4 ]End Sub( I3 T6 @4 w9 p, O& \# T

) @' R+ ~' _9 [% }Function FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer6 z  }" r& r8 @
    FindBomPosition = -1
* e3 O% l+ @8 l    Dim i As Integer
7 z4 [7 `* Q6 t% d- P5 Y: N0 k    If (Not bom) <> -1 Then
9 a# U; S4 a8 I2 _        Dim refConfName As String5 i' [3 t1 L" I$ N; ?
        If MERGE_CONFIGURATIONS Then
, M: A* _" F% P1 T            refConfName = "") U1 }8 f7 d0 ~% K4 ?2 e
        Else
: Y+ n* Q4 @$ t- O1 C$ D            refConfName = comp.ReferencedConfiguration  y) j" j- R/ R  ~' T/ p! t
        End If
$ [2 B* F0 G, i) m        For i = 0 To UBound(bom)
: n6 \1 U3 `; D            If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then
$ A7 o3 Q: d2 Z$ T7 f                FindBomPosition = i
4 A7 ]0 k) @: B                Exit Function& \4 I$ y$ P- f/ Y( P0 Z$ G5 `' v: k
            End If
; h/ Z! O" j0 M* g) R        Next$ A5 P9 T! F. |
    End If- D7 t6 I; B6 e: P0 V: X; x/ J, J# ~
End Function
8 L8 a0 [* J1 G# P8 E/ R4 _* @% r1 v) b6 I8 k
Function GetQuantity(comp As SldWorks.Component2) As Double
! m( g, Y5 Q$ @/ oOn Error GoTo err_& A( V- k( c# R
    Dim refModel As SldWorks.ModelDoc2
2 B$ U9 n0 v6 M$ ~- G9 L    Set refModel = comp.GetModelDoc2
' S3 C1 O- x8 j" o) z    Dim qtyPrpName As String
/ o6 q8 Q' E! Q# ~3 b# ?    qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE"); ~: P- j- x! G  V/ f. j) q
    If qtyPrpName <> "" Then
4 _! v; q, G; `+ x5 [' F        GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
: G7 Y- i8 k2 \! w- X    Else" J/ I' N  l0 e, b4 E" p# L9 R' E% d
        GetQuantity = 1& x( q9 ~4 x* k# H2 C" r( `) m
    End If
% ?* k( m+ l  l9 x    Exit Function
' g8 U7 @1 d. G4 o* j. aerr_:
7 c4 x* j; Z! t3 F) m    Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description
/ x( k# O2 L0 d' Y7 O2 B/ C+ w7 q    GetQuantity = 1
0 M5 S  D' ?  ~2 n. yEnd Function' z1 [2 ?5 F! u' J  S

2 h7 r4 `5 u) w3 ^1 S- Y% lFunction GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String7 V3 r" Z8 Z1 ^' E/ W7 Z
    Dim confSpecPrpMgr As SldWorks.CustomPropertyManager# l( X6 Y/ K# W' w* H4 {
    Dim genPrpMgr As SldWorks.CustomPropertyManager
, P' |& ^+ Q5 Y9 P# I    Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)" D; R" n* r9 M+ y5 Y
    Set genPrpMgr = model.Extension.CustomPropertyManager("")
( Q: }; d' `& C4 Y9 `- C    Dim prpResVal As String
% p4 F- B3 u5 E4 J0 l2 }1 F9 N0 B    confSpecPrpMgr.Get3 prpName, False, "", prpResVal
/ |' L# i# g) ?& k$ k/ m% V: h    If prpResVal = "" Then/ i4 A! }! ]! l( p- P
        genPrpMgr.Get3 prpName, False, "", prpResVal
3 P7 |6 t! f$ w    End If
. v$ q" G" w% J! d. e# E    GetPropertyValue = prpResVal
: R$ r6 _" S3 ~1 G+ nEnd Function- o. k# f0 c0 l! M' v* `  P9 C
1 t' J5 ?- t) ^" |& h8 I
Sub WriteBomQuantities(bom() As BomPosition)
- R7 A$ H3 p7 B' C4 }1 F    Dim i As Integer
( e8 V) D7 K1 W0 L$ b, l+ F+ F: a    If (Not bom) <> -1 Then! c. J+ M) ~' \6 L) k4 h2 s
        For i = 0 To UBound(bom)/ C; r# R+ t/ e( N
            Dim refConfName As String
) n, j5 k9 v$ N# d: ~            Dim swRefModel As SldWorks.ModelDoc21 \- c: g% S) R: \0 ^
            Set swRefModel = bom(i).model
9 M) i1 Q% W/ W1 Q            If MERGE_CONFIGURATIONS Then
5 g5 L/ @% S& L# k* I6 d. }                refConfName = ""/ u& q) f( k2 k' B9 ^0 i0 C# V
            Else
5 X, W: h9 W/ Y& G- Q8 Y                refConfName = bom(i).Configuration' a( Y# e+ G/ t* F
                If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then7 \* f& X" F; b# w1 {
                    Dim swConf As SldWorks.Configuration( [/ K1 W4 w& k4 E+ O
                    Set swConf = swRefModel.GetConfigurationByName(refConfName): L- X  O. K1 R) ^, g
                    Dim vChildConfs As Variant
( j% j% b6 V3 ]. K9 s                    vChildConfs = swConf.GetChildren()+ c9 f8 K. @" k& b
                    If Not IsEmpty(vChildConfs) Then
/ x( k) u" ~$ }, F                        Dim j As Integer
; L8 Z+ h3 b1 S+ ]% l  `0 g                        For j = 0 To UBound(vChildConfs)& ?, B1 M) e# J/ N' b& x
                            Dim swChildConf As SldWorks.Configuration
5 o) T9 ~7 F% D% ]$ P+ T* k% {                            Set swChildConf = vChildConfs(j)
7 W; x  S  s+ Y, h; i8 g                            If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then$ p5 k5 c4 u6 ^$ a7 t
                                SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity
1 G; `4 T* W& E3 i                            End If2 J" z5 j2 Y% P4 i" d
                        Next) F  E& Q# j1 j8 \
                    End If& L7 S( o- p" N  P+ a, p6 z
                End If
9 q# i; `4 k% c  o5 s9 V, p            End If+ c4 I6 q* K1 @- n* N9 `  h
            SetQuantity swRefModel, refConfName, bom(i).Quantity
+ m$ }0 s  R: z# d        Next- R* }) s2 `* U9 p
    End If& N2 i/ R7 \  E' U3 ?
End Sub, w8 _+ A1 i0 z  S# X0 R4 \
  o3 p2 f, [8 F" q1 s$ Z, a: e3 @4 w
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)
0 [6 g/ c- n# k9 S6 X9 ?& W    Dim swCustPrpsMgr As SldWorks.CustomPropertyManager
* D4 G+ n5 i8 `. A' q1 X, {    Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)
! u' P+ |5 j# V+ L! l2 g+ J    swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue* M6 T" _5 d& u2 G' Q" g- \
    swCustPrpsMgr.Set2 PRP_NAME, qty
( e% n0 j' f2 L) z9 A9 Y  A' WEnd Sub
9 H: v4 ]% b2 z! Y
发表于 2024-9-14 12:31:09 | 显示全部楼层
感谢楼主的分享 找了好久了
发表于 2024-10-7 16:38:14 | 显示全部楼层
楼主,能求个宏吗?不会搞3 O- C1 h- i/ @0 Q; x+ x
发表于 2024-11-21 16:35:10 | 显示全部楼层
看前方 发表于 2024-8-14 19:12  w: O; c* D+ ^
一直都是用凯元,自带了这个功能

' B: n+ ^1 Y3 O) y* Y# L在开元没有看到这个功能啊: y2 [- n' ?% m# D4 q+ q9 S+ g8 I) P

; W4 b( ]2 Q- \2 _5 [  I' i
发表于 2024-11-21 16:35:50 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36
5 `( r- i1 a/ B. @# Q( o$ o! u) P也就是说零件模板中要自定义属性 数量

  M$ @0 Q/ M- x1 ~刷个威望下附件
9 a) q# r, L1 q5 x4 \; B' Z# B
发表于 2024-11-21 16:36:08 | 显示全部楼层
李邈 发表于 2024-9-7 09:40- x, t2 \) j3 M1 c) L
这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了
3 N5 h* t4 I- \: m你只要工程图去连接就行了

; o# z! l; T1 ]: ~; a+ K" \. l' j刷个威望下附件( }: s* D7 Y3 X, O! T& G+ y
发表于 2024-11-21 16:53:04 | 显示全部楼层
sycfj 发表于 2024-9-7 09:24) `- Z& k" ]4 ?- Z6 q8 a
这个在工程图,装配体,零件哪个环境中使用
1 }% m( j. h# x4 z0 a
装配体环境中使用
4 p. |+ ?6 f& h+ u5 X" X3 W
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

GMT+8, 2025-9-17 09:05 , Processed in 0.074089 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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