机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
楼主: 李邈

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

[复制链接]
 楼主| 发表于 2024-9-7 09:38:50 | 显示全部楼层
本帖最后由 李邈 于 2024-9-7 09:41 编辑
+ {6 D: f9 V9 m. x3 s: S2 Q
sycfj 发表于 2024-9-7 09:27
- P1 J' U5 L7 P- |- n' h3 k$ ^能详细说明怎么使用吗?我在装配体中使用这个宏以后,工程图中不会出现数量

7 O# j: D% J  Ihttps://baijiahao.baidu.com/s?id=1796657905732909661&wfr=spider&for=pc
& D' \) p' h) g- B按照这个方法设置,他链接的是仓库 你连接数量那一栏就行了) L5 Z  e6 Y9 _* f
设置好之后,把工程图文件另存为工程图模板,删掉上面的视图,下次新建就会直接出现
. w* L9 J, [# R6 b( x% D% F  @! Y以前出的工程图,用批量替换模板的工具,全部替换成新的工程图模板就好啦
) f' z- r& y* N; N
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2024-9-7 09:40:02 | 显示全部楼层
sycfj 发表于 2024-9-7 09:36+ O5 {4 v1 E6 D9 `
也就是说零件模板中要自定义属性 数量
: ~$ T* }, d$ i- F9 C7 F
这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了; E, g2 U1 ?) K1 }2 R2 ~
你只要工程图去连接就行了; M/ ~# @8 p3 I% I
回复 支持 反对

使用道具 举报

发表于 2024-9-7 14:50:39 | 显示全部楼层
李邈 发表于 2024-9-7 09:25, D* v$ A! f6 }1 F5 z8 k/ E, w
装配体中使用

; F8 h; Y/ l" b( m: l; F还是不会用,加个微sycfj2! u* E% Z3 ^% q3 o' d; c: _# y

点评

加了  发表于 2024-9-7 14:51
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-9-7 15:22:48 | 显示全部楼层
Type BomPosition8 v8 u* ^; c+ @0 b4 j) b
    model As SldWorks.ModelDoc2
0 F. Z( u" j) e+ b0 n; }) U$ a9 ]3 e* i6 z    Configuration As String# b2 A- q$ z% P0 ~  o
    Quantity As Double( _4 Q7 g& H' t' V+ E7 ?" j
End Type
5 a0 J$ [% z3 h( f( e( B% p
& U7 O: ?9 O" r4 m3 S0 oConst PRP_NAME As String = "数量"5 p2 [) ]4 z' F! C/ a- y# M
Const MERGE_CONFIGURATIONS As Boolean = True* h7 z4 V, w; X4 M7 k  e& \
Const INCLUDE_BOM_EXCLUDED As Boolean = False
1 O: S: ~' j. `0 t" e8 y# G, U6 x) B5 ?
Dim swApp As SldWorks.SldWorks
6 d" K; u7 Q. `  cSub main()
$ I0 q8 s; c( c; _+ o- Z9 f7 K    Set swApp = Application.SldWorks
* N( }* Z2 K5 ]' ]9 ]try_:. X5 d; [. T, H" y
    On Error GoTo catch_+ \3 }  }1 I' q% y, F* p
    Dim swAssy As SldWorks.AssemblyDoc, L  {/ y, a; c& j# q, v- h
    Set swAssy = swApp.ActiveDoc
. w1 ]* f9 O- R: W2 Q# l* P    If swAssy Is Nothing Then
9 Z9 k" g: l+ D3 I        Err.Raise vbError, "", "Assembly is not opened"
- H& U/ w, |) }: [+ K    End If3 D4 {* W& j; M, Q
    swAssy.ResolveAllLightWeightComponents True) T. |4 p' _) x5 @, O( z: p
    Dim swConf As SldWorks.Configuration
1 j1 C# r/ ~( Z( T- Z    Set swConf = swAssy.ConfigurationManager.ActiveConfiguration
" _2 {2 @6 F* {! q  E" }5 q    Dim bom() As BomPosition
% Z! O1 B6 v5 w. N, ~    ComposeFlatBom swConf.GetRootComponent3(True), bom
9 H: S! {2 f3 T" G0 b    If (Not bom) <> -1 Then
4 W3 @  i( e% T; `) R& T        WriteBomQuantities bom
, h3 A1 O5 }4 }# O7 X+ `0 S, `    End If
- Y$ b, h* D! W. c    GoTo finally_
( X* y% x: e6 @9 v5 E' {2 d1 _# ncatch_:
0 M( v% Z0 B* t. T% p    MsgBox Err.Description, vbCritical, "Count Components"8 _+ k$ l. Y( J/ P
finally_:
8 c+ D- c) ]- q2 P/ U1 e2 _1 Y2 k  d: iEnd Sub
8 H2 X: i$ i* A- g% h4 G( H0 ?+ {+ X2 }9 S% w3 I, x6 D
Sub ComposeFlatBom(swParentComp As SldWorks.Component2, bom() As BomPosition)9 s8 f5 d' @  F7 x7 `- {7 Y) ~
    Dim vComps As Variant
' ~! g  a9 \8 F. D    vComps = swParentComp.GetChildren8 C1 z8 ^# l0 n# z2 q6 i4 T. r7 j
    If Not IsEmpty(vComps) Then3 ~, s* }$ A( D' {& P% V4 A
        Dim i As Integer0 ^! {* X8 @0 b% y* W' X2 o
        For i = 0 To UBound(vComps)* U0 n' ^. h) Y) R+ Y8 D
            Dim swComp As SldWorks.Component2
! y( l9 C, H( c! L            Set swComp = vComps(i)
/ a% G- W+ |& I' f6 w            If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed And (False = swComp.ExcludeFromBOM Or INCLUDE_BOM_EXCLUDED) Then8 R* n: N& i; L  y1 ?$ i. y* L( ]3 d5 m
                Dim swRefModel As SldWorks.ModelDoc2% R" G0 ~* Z/ E" w* A' A) w
                Set swRefModel = swComp.GetModelDoc2()
  }6 F4 x/ [$ H$ ^                If swRefModel Is Nothing Then
( a: o( B( J  y+ G3 M- V3 N                    Err.Raise vbError, "", swComp.GetPathName() & " model is not loaded"
9 q( L5 V# ^+ U8 ^  \$ {0 Q                End If8 E+ Q4 @; w. e* D$ j
                Dim swRefConf As SldWorks.Configuration% S$ s! i: g9 h7 ?; F
                Set swRefConf = swRefModel.GetConfigurationByName(swComp.ReferencedConfiguration)
$ d) Q/ P9 D( p" D( y                Dim bomChildType As Integer6 R1 \  S9 ^7 V, r) G2 G
                bomChildType = swRefConf.ChildComponentDisplayInBOM  s; v" Q2 ?: X
                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Promote Then* }0 C1 L" ]& T" |
                    Dim bomPos As Integer
7 i% `0 V! V3 K3 E) e                    bomPos = FindBomPosition(bom, swComp)1 W' m2 v3 v- a/ A# v- H$ f
                    If bomPos = -1 Then
7 O8 t* |/ B8 ]$ a  Q" \7 R                        If (Not bom) = -1 Then
. n( K( B' X9 _$ S; Z                            ReDim bom(0), P( H) F' j! r2 ], a& `& _
                        Else
3 b# d; n5 p1 A                            ReDim Preserve bom(UBound(bom) + 1)9 d  c( f3 h4 ^" @2 z2 y
                        End If7 u' H$ t) T1 l! M" h0 s
                        bomPos = UBound(bom)
& b" a3 O: Z2 v: p" u+ K                        Dim refConfName As String
4 U  o1 d  W* Q# j  J                        If MERGE_CONFIGURATIONS Then
! U( B4 t3 n) M                            refConfName = ""! t6 @6 R0 X8 H/ y0 I: M
                        Else+ F0 C) V! C4 z' B$ \/ U
                            refConfName = swComp.ReferencedConfiguration, z$ a8 C" u1 \. ]8 S4 d
                        End If
! l) ~8 Z3 m! u4 x% t; t9 \                        Set bom(bomPos).model = swRefModel9 H3 j; }7 }9 d9 q
                        bom(bomPos).Configuration = refConfName, g- r# p6 q4 e
                        bom(bomPos).Quantity = GetQuantity(swComp)' M) n6 U9 P9 s2 P1 {  t4 n
                    Else! t. ?' H* F2 v, G9 }
                        bom(bomPos).Quantity = bom(bomPos).Quantity + GetQuantity(swComp)" \: Q  w1 O% V7 \/ ~/ m
                    End If' g& D, R  ^' |8 m/ A) t4 C3 Y  X$ D
                End If
$ ]$ {( `3 N8 N. I                If bomChildType <> swChildComponentInBOMOption_e.swChildComponent_Hide Then
' F. a6 ^5 p$ I1 i: Z                    ComposeFlatBom swComp, bom# }, Q& }9 w, b, n0 S
                End If
$ i( ]3 A( Q! A: T4 ?; D  O            End If  m$ D5 c* L0 v/ Y( H2 p' D, g6 \+ Z
        Next# y8 ^9 m: P0 `! g, t/ C1 I
    End If
# i' r6 _6 ]& H2 A! H0 T, L; |End Sub
+ T; I4 L* X6 d4 Y* ]& f& P( N% {  `8 Q6 `1 ?, n2 J
Function FindBomPosition(bom() As BomPosition, comp As SldWorks.Component2) As Integer
; c, c0 t: K. x/ I9 a4 G. {7 Q" J    FindBomPosition = -1
+ j6 j# u* c- Q. d5 |    Dim i As Integer/ }7 g& s0 g! c5 S/ |
    If (Not bom) <> -1 Then$ Q6 t) z" ~2 t
        Dim refConfName As String
  k  ]9 E5 Z0 |: `        If MERGE_CONFIGURATIONS Then
' M2 h* I% |% I8 R$ x: W1 {- P            refConfName = ""! \+ Q" n) E/ b. P2 I$ f0 X
        Else: T9 [. w: a4 s* Q+ M. c) R( @
            refConfName = comp.ReferencedConfiguration9 B- ]8 a0 A1 G; \9 a) L2 G7 ~
        End If. j- v: j3 {1 j: g# }
        For i = 0 To UBound(bom)
( d* V9 @) f6 M$ Q. j            If LCase(bom(i).model.GetPathName()) = LCase(comp.GetPathName()) And LCase(bom(i).Configuration) = LCase(refConfName) Then! @: \5 z! K& D7 r, y( c& D! {& x
                FindBomPosition = i
1 x8 {( h/ i% e2 L2 n% g$ Y% D) s                Exit Function  `$ }: D1 l; W1 ]8 V7 O
            End If
: I' W1 }/ c# s4 l! u& G0 u# c        Next
0 G, A( E2 @9 N+ C    End If3 o# J6 \7 I2 O, C. h, W4 W
End Function
* I, U* J1 B2 P: y. a, T7 ~
1 J: P0 X6 ^/ u4 Y+ ^" XFunction GetQuantity(comp As SldWorks.Component2) As Double
  p9 ?4 \: T4 w& R. G  l* eOn Error GoTo err_  f. M7 m4 @- \: C' ]' e$ X
    Dim refModel As SldWorks.ModelDoc2( D* W7 y% [8 {
    Set refModel = comp.GetModelDoc29 W9 j) q- [  Q# r  K
    Dim qtyPrpName As String7 J/ z& b  ~8 ?5 n# C. ~
    qtyPrpName = GetPropertyValue(refModel, comp.ReferencedConfiguration, "UNIT_OF_MEASURE")0 t* \% s; [4 c( F$ e: @& M
    If qtyPrpName <> "" Then( t& I! y1 [: U( I$ k
        GetQuantity = CDbl(GetPropertyValue(refModel, comp.ReferencedConfiguration, qtyPrpName))
; ~, Q1 {: N2 m    Else2 f: v, i' X4 M- ^1 P  I
        GetQuantity = 1& O# u! `, T. x: C
    End If
+ y( p6 u# m. \0 R" ?; V    Exit Function. V$ s' O% j& F! k
err_:
) Z* W4 Z2 C& ?" @. R    Debug.Print "Failed to extract quantity of " & comp.Name2 & ": " & Err.Description
0 X; ~! |8 u6 g* C6 d0 o6 e  i    GetQuantity = 1
3 [  v' S3 t! ^6 c6 k* i8 n9 m/ HEnd Function
2 z3 H, `4 b( e9 m3 e, r+ o( P2 U. O3 M" ]
Function GetPropertyValue(model As SldWorks.ModelDoc2, conf As String, prpName As String) As String
0 y& ?: l3 V" N; ^; x* a, o$ d: I    Dim confSpecPrpMgr As SldWorks.CustomPropertyManager
. `) e" v* ^) O' J    Dim genPrpMgr As SldWorks.CustomPropertyManager4 X8 [! y3 z2 i1 J/ d% g
    Set confSpecPrpMgr = model.Extension.CustomPropertyManager(conf)
1 R! }' A: k: \4 {  H    Set genPrpMgr = model.Extension.CustomPropertyManager("")
! x# u  C9 N. i3 R# ^7 @    Dim prpResVal As String# h" w) U: O$ x2 b
    confSpecPrpMgr.Get3 prpName, False, "", prpResVal
2 @: G* i( L9 }    If prpResVal = "" Then2 c) t% H5 _4 L4 f8 W! z% B) i8 p
        genPrpMgr.Get3 prpName, False, "", prpResVal
/ x# ?& [0 X& T6 D    End If
+ M" D# ]2 N( Q    GetPropertyValue = prpResVal
5 G; O( k" o" R; ?+ C: bEnd Function
# z9 U1 }" ~% Q1 X) @+ m! x
" b4 u$ R! K) n$ _  p4 w3 pSub WriteBomQuantities(bom() As BomPosition)
. Y: n, C: c) i1 z2 s3 Y* h+ Z; F    Dim i As Integer! f7 h# j0 i; a9 x3 Q$ a
    If (Not bom) <> -1 Then- n3 P+ I0 ]- a$ c$ B
        For i = 0 To UBound(bom)
1 j* z+ _' q  N, O# z6 c            Dim refConfName As String9 m8 v) p8 B/ j  x
            Dim swRefModel As SldWorks.ModelDoc2
' ^: H* Q4 W$ B1 Z            Set swRefModel = bom(i).model2 s6 A8 K' q/ t, A5 i
            If MERGE_CONFIGURATIONS Then, _) w& c0 P$ k+ ^# h+ Z3 O1 e  k
                refConfName = ""
( w" L3 R5 O3 H            Else+ M( u- L9 I0 J7 B
                refConfName = bom(i).Configuration
! f/ g7 Y- G! d) E                If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then1 Q: w) z) E" |& O+ g
                    Dim swConf As SldWorks.Configuration9 z0 p9 i% M8 t4 G
                    Set swConf = swRefModel.GetConfigurationByName(refConfName): H3 ]  @! w- E, k
                    Dim vChildConfs As Variant+ }# z' u* N) H0 i
                    vChildConfs = swConf.GetChildren(), q9 D8 ]$ @% n
                    If Not IsEmpty(vChildConfs) Then
& f' @0 T2 k# n                        Dim j As Integer- r+ B8 d$ d+ L7 I2 k
                        For j = 0 To UBound(vChildConfs)/ ]9 `5 W9 ~3 D7 y
                            Dim swChildConf As SldWorks.Configuration
7 y; V7 ~( s" k8 V1 @5 i9 }# X0 m                            Set swChildConf = vChildConfs(j)0 t  l; ~8 U- J9 Y1 [
                            If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then
! R; i0 w: q3 y9 P- |( e, S4 H8 R                                SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity& P% p& c8 J3 c
                            End If
$ U# p0 A0 V- a: y9 m5 @                        Next
# B+ p1 M- o8 C                    End If
0 u7 M3 m; A; N' t. y                End If6 R, J4 o' ^) S, a
            End If0 T' ^" M3 a3 G6 M
            SetQuantity swRefModel, refConfName, bom(i).Quantity! n2 M- Q  I7 ^) I
        Next0 W6 ^! C- a4 C5 G2 L
    End If# E& v- ^! B* p. Y8 H; M
End Sub
; i/ w* M  l1 v+ @+ T  s6 C- G) {; {. q$ z' D0 l
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)( t5 i$ ?! n- U7 T9 v% r, G0 }
    Dim swCustPrpsMgr As SldWorks.CustomPropertyManager" x& ^: \& y8 ]
    Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)
! P2 \8 J# y% u* }$ E) B2 \    swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue! s5 ~, A* p- S3 [9 w3 X0 ]! o- W& G
    swCustPrpsMgr.Set2 PRP_NAME, qty" Q- V/ f! G! c
End Sub
# U- C$ n+ @" ^. e7 W# {. y2 U
回复 支持 反对

使用道具 举报

发表于 2024-9-14 12:31:09 | 显示全部楼层
感谢楼主的分享 找了好久了
回复 支持 反对

使用道具 举报

发表于 2024-10-7 16:38:14 | 显示全部楼层
楼主,能求个宏吗?不会搞; C: ]2 {! J# p) |
回复 支持 反对

使用道具 举报

发表于 2024-11-21 16:35:10 | 显示全部楼层
看前方 发表于 2024-8-14 19:12$ U! G. `( P! S" H
一直都是用凯元,自带了这个功能

/ U3 f( T: `/ e/ i. S! a! k: j3 q在开元没有看到这个功能啊5 {; ^. t2 O$ U1 ]1 l

7 I7 H3 ~) e; _  d) ]# P- Z, y
回复 支持 反对

使用道具 举报

发表于 2024-11-21 16:35:50 | 显示全部楼层
sycfj 发表于 2024-9-7 09:364 F4 x2 D* S4 `( }. A1 U
也就是说零件模板中要自定义属性 数量

* {4 a8 d) `2 ]刷个威望下附件
/ T8 u1 }& \# }2 F' `% u
回复 支持 反对

使用道具 举报

发表于 2024-11-21 16:36:08 | 显示全部楼层
李邈 发表于 2024-9-7 09:40$ g" V- B6 `; v1 f
这个宏会在每个零件体里添加了自定义属性数量这一栏,并且把数量填好了
5 L  `4 P. f9 T; n你只要工程图去连接就行了

7 s+ E# G0 J' v- `3 S" b0 ^刷个威望下附件
" N  y4 e. i* X2 D/ v: x+ ~* i
回复 支持 反对

使用道具 举报

发表于 2024-11-21 16:53:04 | 显示全部楼层
sycfj 发表于 2024-9-7 09:243 [  \$ c3 G/ y+ X6 n" P% c
这个在工程图,装配体,零件哪个环境中使用

, J' R$ P- o6 L* l+ T& c9 G& X装配体环境中使用
7 f/ {$ n2 w  o5 D) `* @' M
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

GMT+8, 2025-5-1 12:24 , Processed in 0.083616 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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