找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
楼主: 李邈

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

[复制链接]
 楼主| 发表于 2024-9-7 09:38:50 | 显示全部楼层
本帖最后由 李邈 于 2024-9-7 09:41 编辑 8 v( A5 s/ \. b0 g9 F
sycfj 发表于 2024-9-7 09:27! c7 _. t) o7 ]7 d2 w' F1 q( g
能详细说明怎么使用吗?我在装配体中使用这个宏以后,工程图中不会出现数量
. T/ G# Y& q2 J2 w" i8 ^
https://baijiahao.baidu.com/s?id=1796657905732909661&wfr=spider&for=pc
) s: F8 R! B! u0 h/ \按照这个方法设置,他链接的是仓库 你连接数量那一栏就行了
4 Z- z1 K( o1 e设置好之后,把工程图文件另存为工程图模板,删掉上面的视图,下次新建就会直接出现
; b* T+ r$ p/ ~  O# L6 p8 f以前出的工程图,用批量替换模板的工具,全部替换成新的工程图模板就好啦4 ]; G  W  |. a0 y- H6 ]
 楼主| 发表于 2024-9-7 09:40:02 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36; Z# n1 l8 b; L1 p3 \" ]
也就是说零件模板中要自定义属性 数量

# a  R( Q( s! _7 J/ a0 X4 B, D& j5 E这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了, V; O) ]+ v0 c0 O; O: t: W4 G
你只要工程图去连接就行了
) O4 C/ A1 U/ J4 H4 p' Z" ~
发表于 2024-9-7 14:50:39 | 显示全部楼层
李邈 发表于 2024-9-7 09:25
3 W, D1 ]% y) `; o装配体中使用

3 u9 R7 J/ K, s4 [) D5 |: U3 }$ b5 \还是不会用,加个微sycfj2
1 Z0 l( G9 J; E5 K# S; `

点评

加了  发表于 2024-9-7 14:51
 楼主| 发表于 2024-9-7 15:22:48 | 显示全部楼层
Type BomPosition
, p4 R" d" @' {6 z  t& t    model As SldWorks.ModelDoc2
/ B+ x& J0 m0 z& V5 f3 ]  M    Configuration As String* d, T9 U8 O1 q$ w. O
    Quantity As Double0 a( j% C+ n8 T/ ~' ]; W) l
End Type
9 Z. o" c+ A8 f' w% U! @: Y4 S+ A* @" w
Const PRP_NAME As String = "数量"  ?6 P" Y0 i1 ]  W
Const MERGE_CONFIGURATIONS As Boolean = True
4 c. ?! [9 X5 g7 yConst INCLUDE_BOM_EXCLUDED As Boolean = False6 x6 ~+ I# Q- [9 B1 k
  G; b( x# y9 k
Dim swApp As SldWorks.SldWorks
4 Z7 y+ M( I4 X0 ~+ ~/ KSub main()
7 f  G( Y" ^, _% H    Set swApp = Application.SldWorks+ p# [9 k- x& i+ r/ c
try_:1 c3 Z. t* h  b, ~: g2 S  p& D5 i
    On Error GoTo catch_9 d" a7 ^$ S8 c: C
    Dim swAssy As SldWorks.AssemblyDoc
$ J5 y: R& T  ?& c9 n    Set swAssy = swApp.ActiveDoc
; A8 s2 a9 r  m    If swAssy Is Nothing Then
/ h; [0 c' P, n, Z$ N) q        Err.Raise vbError, "", "Assembly is not opened"
/ `. e8 V* M) n0 X0 A    End If
7 F+ A  y/ X! o3 i    swAssy.ResolveAllLightWeightComponents True
' c, t# Q7 T& z$ H# g% s1 T" ?    Dim swConf As SldWorks.Configuration
# d8 Z+ k3 n" Q    Set swConf = swAssy.ConfigurationManager.ActiveConfiguration
% n! Y1 B+ @' f$ G( m0 w( |    Dim bom() As BomPosition. U0 `9 ~. P" e5 [6 K$ I
    ComposeFlatBom swConf.GetRootComponent3(True), bom
3 |2 U0 ^6 B" d4 @    If (Not bom) <> -1 Then4 C% Q' b) i5 B  r
        WriteBomQuantities bom
! C. K6 M9 w# y    End If/ h; k" K8 W# p: l8 ^; W5 y6 j
    GoTo finally_
% U* z1 m5 E; M( `catch_:; T1 v6 H" ]" ^% b/ ~% H% w6 ^" q
    MsgBox Err.Description, vbCritical, "Count Components"8 W8 [/ K- e8 ^. b4 A% H/ I! K0 H; C
finally_:: q. H) [9 i" z# g+ p
End Sub
, V/ m1 y1 n/ t! O" }& K4 V/ V6 M- ?6 U' N, l/ @) d- P3 L! S/ Z1 [
Sub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition)! z# d0 z( Z  U- q. W. ]' ]
    Dim vComps As Variant
  l2 E: g) a% m' {; T6 P+ B: B    vComps = swParentComp.GetChildren
/ g9 G8 K+ t0 L/ l2 v8 d    If Not IsEmpty(vComps) Then
# m6 Q8 L6 _7 E) e        Dim i As Integer
, k  e' ^5 I; }, h4 Z        For i = 0 To UBound(vComps)+ o" w) P, D& Z# J6 f3 c. N. C+ y
            Dim swComp As SldWorks.Component2
) l2 I- u0 s' B. l1 F4 E8 V) J% B( y            Set swComp = vComps(i); I: Y) Q! H; X9 ?
            If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then
' T4 q# n7 F- G/ c  U                Dim swRefModel As SldWorks.ModelDoc2& h0 c. p* o+ \
                Set swRefModel = swComp.GetModelDoc2()
, \. ]3 {7 E, @) [                If swRefModel Is Nothing Then1 X8 ~0 c- C; H
                    Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded"" z: }2 d7 G! M4 K7 ~2 ~7 y5 K! w
                End If
1 i9 w; H/ r" H/ Y4 b% L) e                Dim swRefConf As SldWorks.Configuration
9 |! {  C$ H5 P" }0 O$ w1 Q                Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)
# R( d/ e4 V2 _" d2 A# v5 P                Dim bomChildType As Integer
  ]2 [2 x9 ?8 `                bomChildType = swRefConf.ChildComponentDisplayInBOM- e5 y) Q/ r: D' y  c2 v
                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then
' h* \1 }+ X/ ?8 ^) Q  i: A                    Dim bomPos As Integer
) b9 t/ V: I, \+ {                    bomPos = FindBomPosition(bom, swComp)+ d3 F, ~  B7 M
                    If bomPos = -1 Then
! `! {$ {  O' F; l4 I. ?) ^/ \                        If (Not bom) = -1 Then9 y* z1 d) A0 }% ]
                            ReDim bom(0)9 E# i- }, Q( P, R  H" ~
                        Else
9 ]! _' j0 @; |# D  C9 X' U                            ReDim Preserve bom(UBound(bom) + 1)6 J$ Z, H- u0 i' j9 h& B  I$ e: ?
                        End If3 _0 J. U0 M8 M  \3 W  B5 d
                        bomPos = UBound(bom)% i4 b$ g/ p, F8 _& e
                        Dim refConfName As String; R9 ~6 V% G$ J: z( A. W" u
                        If MERGE_CONFIGURATIONS Then
2 y, u6 _3 h' [7 U( ?. P6 F) ^                            refConfName = ""
  c% E( T, d+ ]$ ?% j                        Else
& j* J  P* s1 G, c                            refConfName = swComp.ReferencedConfiguration! e- |' u: O& l
                        End If
. J- i0 O% D9 J, B8 `                        Set bom(bomPos).model = swRefModel
: J6 F9 d2 S9 x1 f; D) S0 _4 K  |                        bom(bomPos).Configuration = refConfName9 z8 z" x0 C% z- M
                        bom(bomPos).Quantity = GetQuantity(swComp)
, Z3 T0 e. M' ~: D                    Else
9 E1 @5 n" h3 C& |6 y! i: \                        bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)5 }. R: M5 M% t/ Y( w
                    End If
* q1 S6 P( K2 H4 U                End If+ o, |' [. s5 _
                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then. g. e' j! `9 e% q. g7 U
                    ComposeFlatBom swComp, bom
- g! g! U5 C, h: O                End If( [- A( j. B+ b1 m% G0 n# ?
            End If: Z4 o; J, A" p1 h% l
        Next
6 Q' c) E: ^% p. W7 [5 @( M; [2 i& U    End If9 W1 z8 k9 w" y# U
End Sub  g: y: p! d" O6 @

; y. A0 H' e! v; RFunction FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer2 A9 `! d* Y/ i
    FindBomPosition = -1% `* x7 b1 e+ n  N. q  X
    Dim i As Integer8 j. P. S( O1 {4 d
    If (Not bom) <> -1 Then
' q& |+ q+ H+ B( ?9 g1 p0 }$ ^. \! Y        Dim refConfName As String
$ O2 O- Y. o! }9 K6 V! I: h        If MERGE_CONFIGURATIONS Then
* P) d9 p2 x0 D  [) T" s. Q            refConfName = ""
' g- L8 b7 v6 z# n7 ?: ~        Else
! K+ f! ]% N7 q8 v8 ^- S6 M( c            refConfName = comp.ReferencedConfiguration+ U8 S3 T: T' B8 D0 f
        End If
3 t  ]! s5 `. g6 u! @4 J        For i = 0 To UBound(bom)
3 y0 z( r5 z' f5 B0 H            If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then* H" n1 G  t7 X
                FindBomPosition = i
/ F* E4 N! `: D. t/ p, l                Exit Function/ A# U/ c0 F7 H0 r6 h7 |
            End If
; V# h, {; F* R" o# k- S7 t" {2 `        Next
3 E) p" @8 O4 q: w; N0 y/ S    End If
5 p& G1 R2 f& {) v. C1 m9 q6 ?End Function
" z9 ?0 p. P" G# p6 I3 P: [3 l
8 t( f8 T) }, M( Y* L. J# WFunction GetQuantity(comp As SldWorks.Component2) As Double
: j" m) G% ~+ W  lOn Error GoTo err_
) G" t& T& V6 O4 y/ ~- E. U3 P; H    Dim refModel As SldWorks.ModelDoc2
& W% [5 H) Y6 h0 b1 R    Set refModel = comp.GetModelDoc27 n9 y- l$ X, c- q- f
    Dim qtyPrpName As String
7 v' `$ @+ K: z$ j    qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE")  f! A2 r9 O# i6 u4 r
    If qtyPrpName <> "" Then
* I: ]6 a& N2 R7 \        GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
) e# H: ~. I, {; H" |- q    Else' M4 S8 D  b* @& a7 O; R) G0 `$ u
        GetQuantity = 1& ^$ y, i0 y; K+ ]
    End If& w* h1 Z/ L  z1 I1 ~7 R8 H
    Exit Function
" I; h# \% Y, Terr_:: ^3 B* Y3 d/ S* w9 l( q
    Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description* n  T, h, g& g6 `9 O
    GetQuantity = 1
: b; w* Z; e' y6 LEnd Function
2 J4 o; K5 P/ s( N6 j) u0 A8 h' k" V0 ?* A
Function GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String
- ?5 ~: b* \9 Z& W% M6 U    Dim confSpecPrpMgr As SldWorks.CustomPropertyManager: E" E- t% _  x
    Dim genPrpMgr As SldWorks.CustomPropertyManager
" x, l4 X2 [; L    Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)
( G" d; R) D- e' p    Set genPrpMgr = model.Extension.CustomPropertyManager("")$ K  Q) g! [5 s) b% z- p
    Dim prpResVal As String- Q! V$ ^0 Z! W
    confSpecPrpMgr.Get3 prpName, False, "", prpResVal5 g: Y2 \) t1 O$ B+ l) O
    If prpResVal = "" Then, I- }1 i/ w, v9 h3 b1 @# o0 h3 Z4 @
        genPrpMgr.Get3 prpName, False, "", prpResVal+ h- Q% G  g9 ]
    End If
2 \* M0 B" M/ d) X# T' B) U& D! |0 r3 \    GetPropertyValue = prpResVal
  c0 O) c& L: E$ i" KEnd Function9 d2 P" _7 z7 c& h

( v4 l7 k, Z- l& ]; @! zSub WriteBomQuantities(bom() As BomPosition)2 ~4 A" A  C5 G9 Y& u) A
    Dim i As Integer
' k( ?2 h4 i  w    If (Not bom) <> -1 Then
) F, d9 M7 i# r$ k0 B        For i = 0 To UBound(bom)
. h  b7 c2 {; Z' m            Dim refConfName As String
. W: G% H) {6 ~: K/ w+ ]6 x            Dim swRefModel As SldWorks.ModelDoc2
9 V/ ?5 a9 I& h( }            Set swRefModel = bom(i).model
, o5 Y% \$ ]1 `4 y6 v: d/ }            If MERGE_CONFIGURATIONS Then
9 a, V" ]! I3 g( h  ?" P$ A; Z. U0 P                refConfName = "", O% e, F  D1 s/ f+ A( p
            Else
1 k9 b5 P% e- K) f  q, A% q                refConfName = bom(i).Configuration
6 P  W- R$ k* N$ W; n                If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then: k% n1 W3 P& ^0 k- e5 U7 _/ p
                    Dim swConf As SldWorks.Configuration
0 c6 D% }7 @; ?; T8 |                    Set swConf = swRefModel.GetConfigurationByName(refConfName)
+ n4 Y: N* A( X, a, W. }! C                    Dim vChildConfs As Variant
! y4 Y# i! }' J: r( ^  {                    vChildConfs = swConf.GetChildren()
6 L8 H  _; n* M2 S+ X+ l5 e: _                    If Not IsEmpty(vChildConfs) Then4 A$ r# y7 I' o8 @: U
                        Dim j As Integer& N: }% B6 L  M: X- o$ B
                        For j = 0 To UBound(vChildConfs)
2 I2 w& R, F% U                            Dim swChildConf As SldWorks.Configuration  s2 L/ _+ E3 ~, L: B: k( ^; f
                            Set swChildConf = vChildConfs(j)
$ Y: R) U/ d$ _+ l1 P& h                            If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then! g! [: ^( f( `, A4 t+ ~2 S: D
                                SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity* |7 k% s2 T- L& O/ I. S3 x
                            End If$ E1 o% I; Z/ W- W8 b3 k
                        Next7 n" M0 ]. i2 @- g. j
                    End If
8 Q1 @) [( [( Q  j) }* t8 j                End If
0 [5 y; h' o! A4 X' r            End If
/ n0 v# y/ |* W& |# r; u6 n) V5 b            SetQuantity swRefModel, refConfName, bom(i).Quantity! b2 g  B2 O3 C; {- b
        Next
/ e/ `5 o" S3 K- F& V    End If  @) I' W% T' o0 Q4 q/ J+ J
End Sub
# K2 c% i# T* G7 r. r( |, r7 a3 i# P# h; l
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)& p; @4 y1 X/ \5 a# U2 g
    Dim swCustPrpsMgr As SldWorks.CustomPropertyManager% r! `! I" T8 P( q* X3 K* ^8 L
    Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)1 C5 t' ~8 K6 f" m
    swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue5 R# s( F7 i% z, Q9 P
    swCustPrpsMgr.Set2 PRP_NAME, qty
7 Y, O+ f/ s) g5 KEnd Sub! W3 [# ~4 n+ J9 a( ^7 n' B
发表于 2024-9-14 12:31:09 | 显示全部楼层
感谢楼主的分享 找了好久了
发表于 2024-10-7 16:38:14 | 显示全部楼层
楼主,能求个宏吗?不会搞
, D8 E, s2 c5 n9 S
发表于 2024-11-21 16:35:10 | 显示全部楼层
看前方 发表于 2024-8-14 19:12. F2 ]/ ?0 J2 p# G7 O, _- Z
一直都是用凯元,自带了这个功能

8 K+ C, Q, w8 a# \! E在开元没有看到这个功能啊
7 v  s3 _: B5 N7 t
' F5 x8 {7 h7 L
发表于 2024-11-21 16:35:50 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36
' q7 @8 o$ J& z, Z; I" Y: Q+ d也就是说零件模板中要自定义属性 数量
. I7 t  d8 ^2 e
刷个威望下附件
* _! o- l  k: o2 W9 ~% _; `
发表于 2024-11-21 16:36:08 | 显示全部楼层
李邈 发表于 2024-9-7 09:40
. A3 D) E: X, _$ O9 Y这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了
: B, e: `5 e9 u; ?你只要工程图去连接就行了
/ ~( @6 {) b0 i! x
刷个威望下附件. o8 s  \  B0 C5 l1 \4 l& a& U
发表于 2024-11-21 16:53:04 | 显示全部楼层
sycfj 发表于 2024-9-7 09:248 S6 y5 v$ k2 \" Z- V: k3 V$ S
这个在工程图,装配体,零件哪个环境中使用

. @' C, K$ P7 B6 J8 `6 w. M3 e装配体环境中使用4 b, a% T$ b0 C' b9 [% ^$ ~
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

GMT+8, 2025-7-13 17:13 , Processed in 0.088028 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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