-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPretty Colors.iLogicVB
More file actions
177 lines (122 loc) · 4.78 KB
/
Pretty Colors.iLogicVB
File metadata and controls
177 lines (122 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'Author: Cody Redding
'contact: codered741@gmail.com
'Runs in an Assembly to give all first level components a random color.
'Procedure:
' Get all first level components
' Create/Switch to new view rep ("Pretty Colors")
' iterate through the components
' select all instances
' generate a random color
' assign color to instances
Sub Main()
SharedVariable("LogVar") = "Pretty Colors"
iLogicVB.RunExternalRule("Write SV to Log.iLogicVB")
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
Dim dViewRepMgr As RepresentationsManager = oAsmDef.RepresentationsManager
Dim dViewReps As DesignViewRepresentations = dViewRepMgr.DesignViewRepresentations
Dim dMasterViewRep As DesignViewRepresentation = dViewReps.Item("Default")
Dim vRepName As String = "Pretty Colors" 'name for the new view rep
Dim dPCViewRep As DesignViewRepresentation
Dim oOccs as ComponentOccurrencesEnumerator
Dim PCVRepOn as Boolean = False
Dim UsedColors as New List(of Integer)
Dim BreakLoop as Integer ' used to prevent possible infinite loop
Dim ColorsCount as Integer = 194
Try
dPCViewRep = dViewReps.Item(vRepName) 'look for the view rep
Catch 'assume error means view rep does not exist, and
dMasterViewRep.Activate()
dViewReps.Add(vRepName)
PCVRepOn = True
End Try
IF PCVRepOn = False Then
dPCViewRep.Activate()
End If
' Initialize the random number generator and get
' the number of render styles defined.
Randomize
Dim iColorCount As Long
iColorCount = oAsmDoc.RenderStyles.Count
Dim BOMDocs As New List(of String)
'get list of part numbers from Structured BOM
GetDocsFromBOM(ThisApplication.ActiveDocument, BOMDocs)
If BOMDocs.Count > 50 Then
uCont = MessageBox.Show("This assembly has a large number of parts. This operation may take some time. Are you sure you want to continue?", "iLogic", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
If uCont = vbCancel Then
Exit Sub
End If
End If
If BOMDocs.Count > ColorsCount Then '194 RAL colors
uCont = MessageBox.Show("This assembly has more parts than available distinct colors. The operation will be canceled. ", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
For Each BOMDoc in BOMDocs
refDoc = ThisApplication.Documents.ItemByName(BOMDoc)
'If refDoc.DocumentType = kPartDocumentObject Then
oOccs = oAsmDef.Occurrences.AllReferencedOccurrences(refDoc)
BreakLoop = 0 ' reset for each components
loop1:
pColor = Int((iColorCount * Rnd) + 1)
If oAsmDoc.RenderStyles.Item(pColor).Name.Contains("RAL") AndAlso Not UsedColors.Contains(pColor) Then
'Do nothing
Else
BreakLoop += 1
If BreakLoop > ColorsCount Then
MessageBox.Show("An error occurred, please check to see that the RAL colors Library is installed. ", "iLogic Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
GoTo loop1 'pick a new color, until an unused RAL color is selected.
End If
For Each oOcc In oOccs
oOcc.SetRenderStyle(kOverrideRenderStyle, oAsmDoc.RenderStyles.Item(pColor))
Next
UsedColors.Add(pColor)
'End If
Next
End Sub 'Main
Sub GetDocsFromBOM(oDoc as Document, ByRef BOMDocs as List(of String))
If oDoc.DocumentType <> kAssemblyDocumentObject Then
MsgBox("This rule can only be run from an assembly")
Exit Sub
End If
Dim oDocBOM As BOM = oDoc.ComponentDefinition.BOM
oDocBOM.StructuredViewEnabled = True
oDocBOM.StructuredViewFirstLevelOnly = False
oDocBOM.PartsOnlyViewEnabled = False
Dim oBOMView as BOMView = oDocBOM.BOMViews.Item("Structured")
Dim oBOMRows As BOMRowsEnumerator = oBOMView.BOMRows
Dim oCompDef As ComponentDefinition
Dim oPNProp as Inventor.Property
For Each oRow as BOMRow in oBOMRows
oCompDef = oRow.ComponentDefinitions.Item(1)
'GetBOMRowDocFile(oRow, BOMDocs)
If oCompDef.Type = 100675072 Then 'exclude Virtual Components
'do nothing
Else
DocName = oCompDef.Document.FullFileName
If Not BOMDocs.Contains(DocName) Then
BOMDocs.Add(DocName)
End If
End If
Next
'BOMDocs.Sort()
End Sub
' Sub GetBOMRowDocFile(oRow as BOMRow, ByRef BOMDocs as List(of String))
' Dim oCompDef As ComponentDefinition
' 'Add the part number of the current row, for parts that have children
' oCompDef = oRow.ComponentDefinitions.Item(1)
' If oCompDef.Type = 100675072 Then 'exclude Virtual Components
' 'do nothing
' Else
' DocName = oCompDef.Document.FullFileName
' If Not BOMDocs.Contains(DocName) Then
' BOMDocs.Add(DocName)
' End If
' End If
' If Not oRow.ChildRows Is Nothing Then
' For Each oChildRow as BOMRow in oRow.ChildRows
' GetBOMRowDocFile(oChildRow, BOMDocs)
' Next
' End If
' End Sub