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
38Const CLEAR_PROPERTIES As Boolean = False
9+ Const ALL_COMPONENTS As Boolean = False
410
511Sub 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-
3357End Sub
3458
3559Function 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
145169End 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
0 commit comments