Skip to content

Commit 090e68e

Browse files
committed
Updated the load properties from text macro to process all components
Added icons to multi extrude boss-base and set watermark macros
1 parent 0e927a9 commit 090e68e

File tree

8 files changed

+157
-36
lines changed

8 files changed

+157
-36
lines changed

solidworks-api/data-storage/custom-properties/link-to-file/Macro.vba

Lines changed: 116 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
1+
Type RefCompModel
2+
RefModel As SldWorks.ModelDoc2
3+
RefConf As String
4+
End Type
5+
16
#Const ARGS = True 'True to use arguments from Toolbar+ or Batch+ instead of the constant
27

38
Const CLEAR_PROPERTIES As Boolean = False
9+
Const ALL_COMPONENTS As Boolean = False
410

511
Sub main()
612

713
Dim swApp As SldWorks.SldWorks
814
Set swApp = Application.SldWorks
915

1016
Dim swModel As SldWorks.ModelDoc2
11-
12-
try_:
13-
On Error GoTo catch_
1417

1518
Dim csvFilePath As String
1619
Dim confSpecific As Boolean
1720

1821
If GetParameters(swApp, swModel, csvFilePath, confSpecific) Then
1922

2023
If Not swModel Is Nothing Then
21-
WritePropertiesFromFile swModel, csvFilePath, IIf(CBool(confSpecific), swModel.ConfigurationManager.ActiveConfiguration, Nothing)
24+
25+
Dim vTable As Variant
26+
vTable = GetArrayFromCsv(csvFilePath)
27+
28+
Dim swRefConf As SldWorks.Configuration
29+
Set swRefConf = swModel.ConfigurationManager.ActiveConfiguration
30+
31+
WritePropertiesFromTable swModel, vTable, IIf(CBool(confSpecific), swRefConf.Name, ""), CLEAR_PROPERTIES
32+
33+
If ALL_COMPONENTS Then
34+
35+
Dim refCompModels() As RefCompModel
36+
refCompModels = CollectUniqueComponents(swRefConf, confSpecific)
37+
38+
If (Not refCompModels) <> -1 Then
39+
40+
Dim i As Integer
41+
42+
For i = 0 To UBound(refCompModels)
43+
WritePropertiesFromTable refCompModels(i).RefModel, vTable, refCompModels(i).RefConf, CBool(clearPrps)
44+
Next
45+
46+
End If
47+
48+
End If
49+
50+
'WritePropertiesFromFile swModel, csvFilePath, IIf(CBool(confSpecific), swModel.ConfigurationManager.ActiveConfiguration, Nothing)
2251
Else
2352
Err.Raise vbError, "", "Please open model"
2453
End If
2554

2655
End If
2756

28-
GoTo finally_
29-
catch_:
30-
swmRebuild = Err.Description
31-
finally_:
32-
3357
End Sub
3458

3559
Function GetParameters(app As SldWorks.SldWorks, ByRef model As SldWorks.ModelDoc2, ByRef csvFilePath As String, ByRef confSpecific As Boolean) As Boolean
@@ -144,44 +168,25 @@ Function GetArrayFromCsv(filePath As String) As Variant
144168

145169
End Function
146170

147-
Sub WritePropertiesFromFile(model As SldWorks.ModelDoc2, csvFilePath As String, conf As SldWorks.Configuration)
148-
149-
If Dir(csvFilePath) = "" Then
150-
Err.Raise "Linked CSV file is missing: " & csvFilePath
151-
End If
152-
153-
Dim vTable As Variant
154-
vTable = GetArrayFromCsv(csvFilePath)
171+
Sub WritePropertiesFromTable(model As SldWorks.ModelDoc2, table As Variant, confName As String, clearPrps As Boolean)
155172

156173
Dim i As Integer
157174

158-
Dim confName As String
159-
160-
If conf Is Nothing Then
161-
confName = ""
162-
Else
163-
confName = conf.Name
164-
End If
165-
166175
Dim swCustPrpMgr As SldWorks.CustomPropertyManager
167176

168177
Set swCustPrpMgr = model.Extension.CustomPropertyManager(confName)
169178

170-
If UBound(vTable, 2) <> 1 Then
171-
Err.Raise vbError, "", "There must be only 2 columns in the CSV file"
172-
End If
173-
174-
If CLEAR_PROPERTIES Then
179+
If clearPrps Then
175180
ClearProperties swCustPrpMgr
176181
End If
177182

178-
For i = 0 To UBound(vTable, 1)
183+
For i = 0 To UBound(table, 1)
179184

180185
Dim prpName As String
181-
prpName = CStr(vTable(i, 0))
186+
prpName = CStr(table(i, 0))
182187

183188
Dim prpVal As String
184-
prpVal = CStr(vTable(i, 1))
189+
prpVal = CStr(table(i, 1))
185190

186191
If swCustPrpMgr.Add3(prpName, swCustomInfoType_e.swCustomInfoText, prpVal, swCustomPropertyAddOption_e.swCustomPropertyReplaceValue) <> swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged Then
187192
Err.Raise vbError, "", "Failed to add property '" & prpName & "'"
@@ -206,4 +211,81 @@ Sub ClearProperties(custPrpMgr As SldWorks.CustomPropertyManager)
206211

207212
End If
208213

209-
End Sub
214+
End Sub
215+
216+
Function CollectUniqueComponents(assmConf As SldWorks.Configuration, confSpecific As Boolean) As RefCompModel()
217+
218+
Dim swRootComp As SldWorks.Component2
219+
Set swRootComp = assmConf.GetRootComponent3(False)
220+
221+
Dim refCompModels() As RefCompModel
222+
223+
ProcessComponents swRootComp.GetChildren(), confSpecific, refCompModels
224+
225+
CollectUniqueComponents = refCompModels
226+
227+
End Function
228+
229+
Sub ProcessComponents(vComps As Variant, confSpecific As Boolean, refCompModels() As RefCompModel)
230+
231+
If Not IsEmpty(vComps) Then
232+
233+
Dim i As Integer
234+
235+
For i = 0 To UBound(vComps)
236+
237+
Dim swComp As SldWorks.Component2
238+
Set swComp = vComps(i)
239+
240+
Dim swRefModel As SldWorks.ModelDoc2
241+
Set swRefModel = swComp.GetModelDoc2
242+
243+
If Not swRefModel Is Nothing Then
244+
245+
Dim refConfName As String
246+
247+
refConfName = IIf(confSpecific, swComp.ReferencedConfiguration, "")
248+
249+
If Not Contains(refCompModels, swRefModel, refConfName) Then
250+
251+
If (Not refCompModels) = -1 Then
252+
ReDim refCompModels(0)
253+
Else
254+
ReDim Preserve refCompModels(UBound(refCompModels) + 1)
255+
End If
256+
257+
Set refCompModels(UBound(refCompModels)).RefModel = swRefModel
258+
refCompModels(UBound(refCompModels)).RefConf = refConfName
259+
260+
End If
261+
262+
ProcessComponents swComp.GetChildren(), confSpecific, refCompModels
263+
264+
End If
265+
266+
Next
267+
268+
End If
269+
270+
End Sub
271+
272+
Function Contains(refCompModels() As RefCompModel, model As SldWorks.ModelDoc2, conf As String) As Boolean
273+
274+
Contains = False
275+
276+
If (Not refCompModels) <> -1 Then
277+
278+
Dim i As Integer
279+
280+
For i = 0 To UBound(refCompModels)
281+
282+
If refCompModels(i).RefModel Is model And LCase(refCompModels(i).RefConf) = LCase(conf) Then
283+
Contains = True
284+
Exit Function
285+
End If
286+
287+
Next
288+
289+
End If
290+
291+
End Function

solidworks-api/data-storage/custom-properties/link-to-file/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ Mass,"""SW-Mass"""
2424
2525
Set the value of the **CLEAR_PROPERTIES** constant to **True** or **False** to configure if existing properties need to be deleted before updating.
2626

27+
Set **ALL_COMPONENTS** to **True** to process all components of the assembly
28+
2729
~~~ vb
2830
Const CLEAR_PROPERTIES As Boolean = False
31+
Const ALL_COMPONENTS As Boolean = True
2932
~~~
3033

3134
{% code-snippet { file-name: Macro.vba } %}
7.94 KB
Binary file not shown.

solidworks-api/document/macro-feature/add-watermark/InsertWatermark.vba

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,27 @@ Sub main()
2525
vMethods(3) = watermarkMacroPath: vMethods(4) = moduleName: vMethods(5) = "swmEditDefinition"
2626
vMethods(6) = watermarkMacroPath: vMethods(7) = moduleName: vMethods(8) = "swmSecurity"
2727

28+
Dim iconsDir As String
29+
iconsDir = swApp.GetCurrentMacroPathFolder() & "\Icons\"
30+
31+
Dim icons(8) As String
32+
icons(0) = iconsDir & "watermark_20x20.bmp"
33+
icons(1) = iconsDir & "watermark-suppressed_20x20.bmp"
34+
icons(2) = iconsDir & "watermark_20x20.bmp"
35+
36+
icons(3) = iconsDir & "watermark_32x32.bmp"
37+
icons(4) = iconsDir & "watermark-suppressed_32x32.bmp"
38+
icons(5) = iconsDir & "watermark_32x32.bmp"
39+
40+
icons(6) = iconsDir & "watermark_40x40.bmp"
41+
icons(7) = iconsDir & "watermark-suppressed_40x40.bmp"
42+
icons(8) = iconsDir & "watermark_40x40.bmp"
43+
2844
Dim swFeat As SldWorks.Feature
2945

3046
Set swFeat = swModel.FeatureManager.InsertMacroFeature3(BASE_NAME, "", vMethods, _
3147
Empty, Empty, Empty, Empty, Empty, Empty, _
32-
Empty, swMacroFeatureOptions_e.swMacroFeatureEmbedMacroFile + swMacroFeatureOptions_e.swMacroFeatureAlwaysAtEnd)
48+
icons, swMacroFeatureOptions_e.swMacroFeatureEmbedMacroFile + swMacroFeatureOptions_e.swMacroFeatureAlwaysAtEnd)
3349

3450
If Not swFeat Is Nothing Then
3551
Dim swSecNote As SldWorks.note

solidworks-api/document/macro-feature/add-watermark/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ The solution consists of 2 parts:
3030

3131
{% code-snippet { file-name: InsertWatermark.vba } %}
3232

33+
In order to add custom icons, download the [Icons](Icons.zip) file and unzip into the **Icons** sub-folder next to the macro feature file
34+
3335
## Setting up watermark macro
3436

3537
* Create another new macro

solidworks-api/document/macro-feature/multi-extrude/Controller.vba

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ Sub InsertMacroFeature(vSketches As Variant, vDepths As Variant)
7575
vMethods(3) = curMacroPath: vMethods(4) = MACRO_FEATURE_MODULE_NAME: vMethods(5) = "swmEditDefinition"
7676
vMethods(6) = curMacroPath: vMethods(7) = MACRO_FEATURE_MODULE_NAME: vMethods(8) = "swmSecurity"
7777

78+
Dim iconsDir As String
79+
iconsDir = swApp.GetCurrentMacroPathFolder() & "\Icons\"
80+
81+
Dim icons(8) As String
82+
icons(0) = iconsDir & "extrude_20x20.bmp"
83+
icons(1) = iconsDir & "extrude-suppressed_20x20.bmp"
84+
icons(2) = iconsDir & "extrude_20x20.bmp"
85+
86+
icons(3) = iconsDir & "extrude_32x32.bmp"
87+
icons(4) = iconsDir & "extrude-suppressed_32x32.bmp"
88+
icons(5) = iconsDir & "extrude_32x32.bmp"
89+
90+
icons(6) = iconsDir & "extrude_40x40.bmp"
91+
icons(7) = iconsDir & "extrude-suppressed_40x40.bmp"
92+
icons(8) = iconsDir & "extrude_40x40.bmp"
93+
7894
Dim vParamNames As Variant
7995
Dim vParamTypes As Variant
8096
Dim vParamValues As Variant
@@ -84,7 +100,7 @@ Sub InsertMacroFeature(vSketches As Variant, vDepths As Variant)
84100
Dim swFeat As SldWorks.Feature
85101
Set swFeat = swModel.FeatureManager.InsertMacroFeature3(BASE_NAME, "", vMethods, _
86102
vParamNames, vParamTypes, vParamValues, Empty, Empty, Empty, _
87-
Empty, swMacroFeatureOptions_e.swMacroFeatureEmbedMacroFile)
103+
icons, swMacroFeatureOptions_e.swMacroFeatureEmbedMacroFile)
88104

89105
If swFeat Is Nothing Then
90106
MsgBox "Failed to create feature"
11.3 KB
Binary file not shown.

solidworks-api/document/macro-feature/multi-extrude/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Property Manager pages are defined in the **SolidWorks {{Version}} exposed type
2020

2121
![VBA macro references](macro-references.png)
2222

23+
In order to add custom icons, download the [Icons](Icons.zip) file and unzip into the **Icons** sub-folder next to the macro feature file
24+
2325
## Macro Module
2426

2527
Entry point of the macro. Use this to insert new macro feature.

0 commit comments

Comments
 (0)