Skip to content

Commit ec95260

Browse files
authored
Merge branch 'xarial:master' into master
2 parents 4b82291 + b569f62 commit ec95260

File tree

3 files changed

+159
-9
lines changed

3 files changed

+159
-9
lines changed

solidworks-api/document/assembly/components/write-quantities/Macro.vba

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,56 @@ Sub WriteBomQuantities(bom() As BomPosition)
198198
For i = 0 To UBound(bom)
199199

200200
Dim refConfName As String
201-
201+
202+
Dim swRefModel As SldWorks.ModelDoc2
203+
Set swRefModel = bom(i).model
204+
202205
If MERGE_CONFIGURATIONS Then
203206
refConfName = ""
204207
Else
205208
refConfName = bom(i).Configuration
209+
210+
If swRefModel.GetBendState() <> swSMBendState_e.swSMBendStateNone Then
211+
212+
Dim swConf As SldWorks.Configuration
213+
Set swConf = swRefModel.GetConfigurationByName(refConfName)
214+
215+
Dim vChildConfs As Variant
216+
vChildConfs = swConf.GetChildren()
217+
218+
If Not IsEmpty(vChildConfs) Then
219+
Dim j As Integer
220+
221+
For j = 0 To UBound(vChildConfs)
222+
223+
Dim swChildConf As SldWorks.Configuration
224+
Set swChildConf = vChildConfs(j)
225+
226+
If swChildConf.Type = swConfigurationType_e.swConfiguration_SheetMetal Then
227+
SetQuantity swRefModel, swChildConf.Name, bom(i).Quantity
228+
End If
229+
230+
Next
231+
232+
End If
233+
234+
End If
235+
206236
End If
207237

208-
Dim swRefModel As SldWorks.ModelDoc2
209-
Set swRefModel = bom(i).model
210-
211-
Dim swCustPrpsMgr As SldWorks.CustomPropertyManager
212-
Set swCustPrpsMgr = swRefModel.Extension.CustomPropertyManager(refConfName)
213-
214-
swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, bom(i).Quantity, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
215-
swCustPrpsMgr.Set2 PRP_NAME, bom(i).Quantity
238+
SetQuantity swRefModel, refConfName, bom(i).Quantity
216239

217240
Next
218241
End If
219242

243+
End Sub
244+
245+
Sub SetQuantity(model As SldWorks.ModelDoc2, confName As String, qty As Double)
246+
247+
Dim swCustPrpsMgr As SldWorks.CustomPropertyManager
248+
Set swCustPrpsMgr = model.Extension.CustomPropertyManager(confName)
249+
250+
swCustPrpsMgr.Add3 PRP_NAME, swCustomInfoType_e.swCustomInfoText, qty, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
251+
swCustPrpsMgr.Set2 PRP_NAME, qty
252+
220253
End Sub
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
Const SKIP_ASSIGNED As Boolean = True
2+
Const UNABSORBED_ONLY As Boolean = True
3+
4+
Dim swApp As SldWorks.SldWorks
5+
Dim swModel As SldWorks.ModelDoc2
6+
7+
Sub main()
8+
9+
Set swApp = Application.SldWorks
10+
11+
Set swModel = swApp.ActiveDoc
12+
13+
Dim vFeats As Variant
14+
vFeats = CollectAllSketchFeatures(swModel.FirstFeature)
15+
16+
If Not IsEmpty(vFeats) Then
17+
18+
Dim i As Integer
19+
20+
For i = 0 To UBound(vFeats)
21+
22+
Dim swFeat As SldWorks.Feature
23+
Set swFeat = vFeats(i)
24+
25+
If Not SKIP_ASSIGNED Or Not HasAppearence(swFeat) Then
26+
27+
If Not UNABSORBED_ONLY Or Not IsAbsorbed(swFeat) Then
28+
29+
Dim dMatPrps(8) As Double
30+
dMatPrps(0) = Rnd(): dMatPrps(1) = Rnd(): dMatPrps(2) = Rnd()
31+
dMatPrps(3) = 1: dMatPrps(4) = 1: dMatPrps(5) = 0.5
32+
dMatPrps(6) = 0.4: dMatPrps(7) = 0: dMatPrps(8) = 0
33+
34+
Debug.Print "Assigning color " & dMatPrps(0) * 255 & ";" & dMatPrps(1) * 255 & ";" & dMatPrps(2) * 255 & " to " & swFeat.Name
35+
36+
swFeat.SetMaterialPropertyValues2 dMatPrps, swInConfigurationOpts_e.swThisConfiguration, Empty
37+
38+
End If
39+
40+
End If
41+
42+
Next
43+
44+
End If
45+
46+
End Sub
47+
48+
Function IsAbsorbed(feat As SldWorks.Feature) As Boolean
49+
50+
Dim vFeatChildren As Variant
51+
vFeatChildren = feat.GetChildren()
52+
53+
IsAbsorbed = Not IsEmpty(vFeatChildren)
54+
55+
End Function
56+
57+
Function HasAppearence(feat As SldWorks.Feature) As Boolean
58+
59+
Dim vMatPrpVals As Variant
60+
vMatPrpVals = feat.GetMaterialPropertyValues2(swInConfigurationOpts_e.swThisConfiguration, Empty)
61+
62+
HasAppearence = vMatPrpVals(0) <> -1 And vMatPrpVals(1) <> -1 And vMatPrpVals(2) <> -1
63+
64+
End Function
65+
66+
Function CollectAllSketchFeatures(firstFeat As SldWorks.Feature) As Variant
67+
68+
Const SKETCH_FEAT_TYPE_NAME As String = "ProfileFeature"
69+
Const SKETCH_3D_FEAT_TYPE_NAME As String = "3DProfileFeature"
70+
71+
Dim swFeats() As SldWorks.Feature
72+
73+
Dim swFeat As SldWorks.Feature
74+
Set swFeat = firstFeat
75+
76+
While Not swFeat Is Nothing
77+
78+
If swFeat.GetTypeName2 = SKETCH_FEAT_TYPE_NAME Or _
79+
swFeat.GetTypeName2 = SKETCH_3D_FEAT_TYPE_NAME Then
80+
81+
If (Not swFeats) = -1 Then
82+
ReDim swFeats(0)
83+
Else
84+
ReDim Preserve swFeats(UBound(swFeats) + 1)
85+
End If
86+
87+
Set swFeats(UBound(swFeats)) = swFeat
88+
89+
End If
90+
91+
Set swFeat = swFeat.GetNextFeature
92+
93+
Wend
94+
95+
If (Not swFeats) = -1 Then
96+
CollectAllSketchFeatures = Empty
97+
Else
98+
CollectAllSketchFeatures = swFeats
99+
End If
100+
101+
End Function
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
caption: Assign Random Color To Sketches
3+
title: Macro to assign random color to sketches in the document
4+
description: VBA macro assigns random color to sketches in SOLIDWORKS parts or assemblies with an option to skip already assigned sketches and unabsorbed sketches
5+
---
6+
7+
This VBA macro assigns the random color to all sketches of active parts or assemblies.
8+
9+
Macro can be configured to skip sketches with already assigned colors and select only unabsorbed sketches (e.g. sketches which are not used in other features)
10+
11+
~~~vb
12+
Const SKIP_ASSIGNED As Boolean = False 'Processes all sketches (including the sketches with assigned colors)
13+
Const UNABSORBED_ONLY As Boolean = False 'Process all sketches (absorbed and unabsorbed)
14+
~~~
15+
16+
{% code-snippet { file-name: Macro.vba } %}

0 commit comments

Comments
 (0)