找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
楼主: 李邈

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

  [复制链接]
 楼主| 发表于 2024-9-7 09:38:50 | 显示全部楼层
本帖最后由 李邈 于 2024-9-7 09:41 编辑 & r& I2 o- r% ~( d
sycfj 发表于 2024-9-7 09:27
+ }2 Z4 l! v- @* K; h- n能详细说明怎么使用吗?我在装配体中使用这个宏以后,工程图中不会出现数量

, w( x' g4 l1 O4 x4 ehttps://baijiahao.baidu.com/s?id=1796657905732909661&wfr=spider&for=pc1 O2 ], c( Y$ H5 H0 L. W% q
按照这个方法设置,他链接的是仓库 你连接数量那一栏就行了) a7 F) f( y( O& \
设置好之后,把工程图文件另存为工程图模板,删掉上面的视图,下次新建就会直接出现
" [; {" V# o$ w以前出的工程图,用批量替换模板的工具,全部替换成新的工程图模板就好啦
, ^3 j; T/ G5 p) J: W
 楼主| 发表于 2024-9-7 09:40:02 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36/ O. [9 M- s1 S6 |: P- {  M
也就是说零件模板中要自定义属性 数量

9 r* i: S5 B0 u( ]7 j; W& s这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了
# f  H$ P; O; f! O你只要工程图去连接就行了6 w9 q. e2 f3 y. E$ y# [+ l
发表于 2024-9-7 14:50:39 | 显示全部楼层
李邈 发表于 2024-9-7 09:25/ @8 T/ h. _& T8 J  B' d) d
装配体中使用

! H$ B) r3 U$ x  d4 B还是不会用,加个微sycfj29 v: L! p0 i* h6 T! K

点评

加了  发表于 2024-9-7 14:51
 楼主| 发表于 2024-9-7 15:22:48 | 显示全部楼层
Type BomPosition3 S1 M% M2 w% L) n
    model As SldWorks.ModelDoc20 r$ q6 J8 {+ Q+ H3 s
    Configuration As String
% Q5 O# U) T% E3 ]  \2 U0 g+ o6 s% O    Quantity As Double
/ a  r* n4 A& x) A. IEnd Type
2 w9 r3 w2 M* E, h/ P
6 |5 t( w7 \2 gConst PRP_NAME As String = "数量"
  U, }1 F& k$ k: c% SConst MERGE_CONFIGURATIONS As Boolean = True
" B# s4 i$ C% kConst INCLUDE_BOM_EXCLUDED As Boolean = False
, K. l2 ~3 e, v: @, y1 X2 [/ z9 j. j. b0 W
Dim swApp As SldWorks.SldWorks1 @& `6 h8 V  ^& r! R+ t& i' {# g" G
Sub main()7 c9 ?. O! f* K  o" x1 Q2 Q; b
    Set swApp = Application.SldWorks. O4 R9 B* t8 W6 ~8 G- s3 H1 G, y6 n
try_:0 D) ^6 B$ i- \  `/ x* G  A: y
    On Error GoTo catch_
* D: x- ]) ?" r  P7 `    Dim swAssy As SldWorks.AssemblyDoc
  J' ~. j6 @* f* P3 A    Set swAssy = swApp.ActiveDoc& s0 T) v7 a, W7 A
    If swAssy Is Nothing Then
' w, `; g% d, i0 Q( M. f        Err.Raise vbError, "", "Assembly is not opened"( q' A: t' F" N4 }2 ^3 X# i2 j: ]
    End If
1 `2 b  ]6 w2 q' X3 [, ^    swAssy.ResolveAllLightWeightComponents True
; M8 c9 i# I1 g. s    Dim swConf As SldWorks.Configuration
0 v) h! Y  X$ m7 a4 I& V& o    Set swConf = swAssy.ConfigurationManager.ActiveConfiguration% U0 P# R, [( D6 s
    Dim bom() As BomPosition3 t* J8 J/ Y4 l
    ComposeFlatBom swConf.GetRootComponent3(True), bom8 [) E5 Q! u0 x! o7 O
    If (Not bom) <> -1 Then
5 P; `3 Y8 q' T& V8 R, \9 F' g" z        WriteBomQuantities bom( @6 w( i! n" \; N! S
    End If. k' }, X! G! c0 d
    GoTo finally_0 g& J3 M- t$ m
catch_:3 ?. v1 C& {3 d$ X. X$ y
    MsgBox Err.Description, vbCritical, "Count Components"
' m# M- ]7 R% Sfinally_:% q: S4 _  v0 X; e
End Sub+ @/ {. a) S/ P7 K  }. h- d
$ e7 s( y3 C; ]% J7 J, s! _* c
Sub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition)
4 {8 |( j% {8 n. o5 \    Dim vComps As Variant
! b' ~+ }0 Q' z" ~8 I* g    vComps = swParentComp.GetChildren
2 c: H# W- [7 H8 N( M4 I5 j    If Not IsEmpty(vComps) Then
4 n5 l4 f: z) Q- M        Dim i As Integer
* p( ]8 s- n% @) D        For i = 0 To UBound(vComps)' v% R* {- S' D  p8 m6 x1 ~
            Dim swComp As SldWorks.Component2/ r3 ^# }1 C/ m4 g" u5 Q# O4 u0 E
            Set swComp = vComps(i)$ e$ l7 P/ ]- U# Q2 d
            If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then# ?5 W; @' |8 C& f+ u2 d. Q+ v% j5 u/ q
                Dim swRefModel As SldWorks.ModelDoc2
$ S8 S) r& j% E1 q  P, h1 D                Set swRefModel = swComp.GetModelDoc2()" c  z5 h. {, w; {* U, Y! S
                If swRefModel Is Nothing Then5 W# l1 u0 `& ~% s/ K7 P
                    Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded": A/ v, D4 s9 U2 V8 _
                End If
% j( _6 C+ k7 R  L) Q, J                Dim swRefConf As SldWorks.Configuration5 x8 G7 r( L0 L' g: `
                Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)
% J1 I: `/ X3 p' P6 D9 \, ]& \                Dim bomChildType As Integer
) b8 E) L) M8 x                bomChildType = swRefConf.ChildComponentDisplayInBOM$ d  Q5 m# Q. [
                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then& S( ]- r/ I/ s9 r4 a
                    Dim bomPos As Integer
- |7 O' Y7 l! X' S5 y                    bomPos = FindBomPosition(bom, swComp)& X* s  P! Z- x- V. l& O
                    If bomPos = -1 Then
# F1 @7 L1 X7 p% G                        If (Not bom) = -1 Then; V) N# Q* _- E- v
                            ReDim bom(0)% a2 y, n" W6 ~; k
                        Else7 ~3 {# q9 R+ _7 P" N! Z
                            ReDim Preserve bom(UBound(bom) + 1)* b+ `  A- h/ c" k5 }  `6 y
                        End If) O% u8 Y: v$ u
                        bomPos = UBound(bom)0 x3 \* z: d" |- ]
                        Dim refConfName As String
* u. r2 v% @4 [- j+ a2 m& ^                        If MERGE_CONFIGURATIONS Then" ^+ P3 [1 c8 }
                            refConfName = ""
0 F& @5 f, }7 S0 ^/ o( d; z                        Else
6 }& t6 q, @* _  e3 G                            refConfName = swComp.ReferencedConfiguration9 Y4 b( U7 _$ t2 L4 x
                        End If  U  y% F" H( p! m
                        Set bom(bomPos).model = swRefModel5 V2 b- p2 E  y- H  r5 r
                        bom(bomPos).Configuration = refConfName
) E. g% D2 X- G& m3 @                        bom(bomPos).Quantity = GetQuantity(swComp)0 V3 m$ c7 ?5 O2 W1 p. U& j5 X
                    Else: a* @8 {( ~) Z' y" f7 {
                        bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)  ~3 u( N8 @1 t3 \+ P1 y. \
                    End If+ [6 p2 f1 p2 W6 U
                End If
3 ?+ V& y# o4 i( C6 u                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then
$ w2 N* |4 x5 C; g8 u                    ComposeFlatBom swComp, bom
) r% N7 p0 `: {3 m5 a. Q                End If
/ T9 o7 T, n5 \4 I            End If- @' S, p9 A6 l0 D5 {. B; u0 k4 L/ f5 ^
        Next
" P; L6 R3 q6 x9 ?: h5 w    End If
! n6 `/ Z* o- f) j) D. IEnd Sub0 e  K: ]% n2 ^- K

8 C, g+ P% t$ M0 {  x! ^0 ?Function FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer
( b+ S1 ~5 H0 a# j/ Q- n8 W    FindBomPosition = -1
3 f; v8 C& [/ n2 ?7 b    Dim i As Integer
1 t4 T$ O) t9 y0 m* @5 [$ q! r% P    If (Not bom) <> -1 Then5 f' j. G2 X! Y- N
        Dim refConfName As String# h! D7 g1 ?/ j
        If MERGE_CONFIGURATIONS Then/ O+ M9 P: Q& `' C/ j
            refConfName = ""
! b: R' o6 Q! g/ K        Else1 I! E2 X+ t7 |) X
            refConfName = comp.ReferencedConfiguration
/ M/ S$ J# e1 S& _  M2 L        End If
0 i$ g) G* G8 Q) Q* v. S        For i = 0 To UBound(bom)( c- ?: J- S, j: o0 c1 @
            If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then, D8 d2 T1 {4 a2 q: c7 W2 W9 T6 T$ C
                FindBomPosition = i
  E2 Z1 Z. q! i: {" ]; O% g' ?                Exit Function
9 @& g7 r/ f: ]3 T/ w            End If
/ S, T6 P5 k. q; l        Next8 m, c/ H1 `5 A5 B* m' X
    End If
4 ^" l5 D" O9 A# ]End Function
- p+ O4 i% g- J& u5 E* j- x2 w5 I9 ~/ r% }0 s# g0 K  @8 g: ]7 m
Function GetQuantity(comp As SldWorks.Component2) As Double% q8 }1 |' ], b% n7 L% c7 p
On Error GoTo err_- o9 B5 w/ _' r0 P, D6 o
    Dim refModel As SldWorks.ModelDoc2( P; Q* B" @4 [( r  p+ v
    Set refModel = comp.GetModelDoc2
+ t& A5 R% x( f  c9 d$ I8 r    Dim qtyPrpName As String, ~1 d8 f4 T- u: q2 o
    qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE")% P8 u) }2 B% [0 l) ~
    If qtyPrpName <> "" Then
! `0 @( N$ L$ b) G/ ?8 Q. t        GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
0 K% l4 |. m: ~- R    Else
' v- e0 U6 q) h. l+ j        GetQuantity = 1
( H" U( x1 l# S) O& G& R, ]    End If& Y6 S6 J9 |5 y# k) y
    Exit Function
- H2 h! O0 f# L3 b0 x6 Ferr_:" W- h. {$ A# h7 I6 n* S" y
    Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description/ J, e4 L2 d- W0 y* {3 t
    GetQuantity = 1. _$ Q( E# y" T
End Function2 ]# F* a6 l7 l, F- a, a

* k( J$ R2 ~% v- l, KFunction GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String  W! }# g6 r2 V& `6 g7 n: \3 K) R
    Dim confSpecPrpMgr As SldWorks.CustomPropertyManager( E0 i8 N4 o2 l5 _+ ^0 ~5 m
    Dim genPrpMgr As SldWorks.CustomPropertyManager
& Z  D) p$ x0 o& V/ [; P/ S1 }    Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)2 h' U: B2 ?' z, c8 c
    Set genPrpMgr = model.Extension.CustomPropertyManager("")! H8 ^# w' b/ t) i
    Dim prpResVal As String0 b0 U, B+ P7 {
    confSpecPrpMgr.Get3 prpName, False, "", prpResVal) Z- S. _* H6 `
    If prpResVal = "" Then! V9 p3 P, q5 E0 m* P  W: v; P3 l! S, I
        genPrpMgr.Get3 prpName, False, "", prpResVal
( p1 [) y0 G2 J  Y    End If
- x. p7 y1 `2 j$ b0 v+ l2 h    GetPropertyValue = prpResVal! Q: m0 }/ R, Y  e: ?* p; u, O0 k
End Function7 q7 f* H, s7 Z! N. d$ @' R4 X

, ]/ i) l; ?3 I, O- w! M# Q% rSub WriteBomQuantities(bom() As BomPosition)
, q0 h+ W" w7 |) ^    Dim i As Integer
: N6 j" A. h& K/ F! Q    If (Not bom) <> -1 Then
" v0 b; y$ t$ O: [& Y        For i = 0 To UBound(bom)
9 W9 n# K+ M1 a9 ~  g( c5 z            Dim refConfName As String; b5 R3 q  F% N" n4 z/ Z' E
            Dim swRefModel As SldWorks.ModelDoc2+ n! P. {6 P+ K1 K4 c
            Set swRefModel = bom(i).model( f! K' b* b! y
            If MERGE_CONFIGURATIONS Then
" t6 E+ t. J+ H                refConfName = ""1 ^2 w3 }3 ?3 z* E
            Else8 ?" K) @" l$ `& s& Z9 Z
                refConfName = bom(i).Configuration: r7 P& `  B: _2 {+ I) C0 b
                If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then
' u/ n1 N; B* _1 \                    Dim swConf As SldWorks.Configuration
3 A! O. C& t$ V, n* U3 x. ^                    Set swConf = swRefModel.GetConfigurationByName(refConfName)
  L2 o) I+ y4 {, w$ Z                    Dim vChildConfs As Variant
) V/ t3 o5 g" `) n2 s7 P                    vChildConfs = swConf.GetChildren()+ X, g) u: @; S- h! [, S
                    If Not IsEmpty(vChildConfs) Then
0 E5 F! d1 `/ X+ A0 ]) `                        Dim j As Integer
; u  o8 [& x; _" S: ]                        For j = 0 To UBound(vChildConfs)
$ h# l2 B$ ?! p1 i9 @- ?) w# O9 b                            Dim swChildConf As SldWorks.Configuration3 q$ f  P; k3 y) ]# g$ l, Z1 a0 s
                            Set swChildConf = vChildConfs(j)
; Y4 l( E2 Z' m7 y/ x' r                            If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then
- P1 f3 P  T. C* f6 c. R  ]* P                                SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity
3 `: b9 R7 Z$ [$ p% f                            End If7 c5 J+ Z& e% e. P6 G) X
                        Next
( g5 H; K" K0 Q" Y9 ~$ S                    End If8 q: E2 O' M1 r+ ]
                End If4 i- e, ^+ ?, ?- ~
            End If
6 O' E% b  S, I3 k+ E            SetQuantity swRefModel, refConfName, bom(i).Quantity
) \- X$ S5 I# r8 x0 ~        Next
5 G& y2 B$ m" w6 v" z4 ~( }2 {' e    End If9 _# q. N0 Y* t. a
End Sub5 C3 d% g0 [& D
/ `0 S" l7 |  g/ ~9 Y. G: h
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)9 e! b% l# |) w  |4 g: |
    Dim swCustPrpsMgr As SldWorks.CustomPropertyManager4 k; J7 C8 I. m4 W, Q* N* `0 t
    Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)  [4 s, Y3 h+ |* f  G
    swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
/ W6 f0 I. M; [- q# R! }/ u, x    swCustPrpsMgr.Set2 PRP_NAME, qty
( r& }" y# h6 R" EEnd Sub
* R) W, `+ a' U9 J, x7 f- C
发表于 2024-9-14 12:31:09 | 显示全部楼层
感谢楼主的分享 找了好久了
发表于 2024-10-7 16:38:14 | 显示全部楼层
楼主,能求个宏吗?不会搞0 d8 h& [+ J; g# U: q% a
发表于 2024-11-21 16:35:10 | 显示全部楼层
看前方 发表于 2024-8-14 19:124 n/ \7 `" H6 k" M, I. ?
一直都是用凯元,自带了这个功能
9 n, [0 B7 u" ~$ x
在开元没有看到这个功能啊
6 Q/ ~. o6 _: u: Z9 R7 ?, D
  Q3 F9 u1 C9 h/ ?$ D0 o( O/ B
发表于 2024-11-21 16:35:50 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36
( F. L. w3 s: u- Y' n也就是说零件模板中要自定义属性 数量
9 q3 [6 F! j- o7 s% c4 ~
刷个威望下附件4 d" ]6 E; a: x: Y  B6 Z' W# \6 L
发表于 2024-11-21 16:36:08 | 显示全部楼层
李邈 发表于 2024-9-7 09:40
2 s9 [9 `7 b) W! h这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了
: d8 h. q3 {) A& d* W/ i7 n* y0 x你只要工程图去连接就行了

9 o7 G. y+ C0 h刷个威望下附件
4 w8 m( W2 {- Q3 \
发表于 2024-11-21 16:53:04 | 显示全部楼层
sycfj 发表于 2024-9-7 09:24( z; V1 C/ a0 j; J' D* [: a/ q
这个在工程图,装配体,零件哪个环境中使用
$ f! J. o. D$ M
装配体环境中使用
: D2 N$ ^0 J3 F2 J. N6 s2 s/ Q
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

GMT+8, 2025-9-16 14:50 , Processed in 0.087363 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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