|
| 1 | +Imports System.Windows.Forms |
| 2 | +Imports System.Drawing |
| 3 | +Public Class RunMyForm |
| 4 | + |
| 5 | + Private Sub Main |
| 6 | + |
| 7 | + Dim strAppName As String = "Set Part Colours" |
| 8 | + |
| 9 | + ' Check a document is open |
| 10 | + If ThisApplication.Documents.Count = 0 Then |
| 11 | + MessageBox.Show("No Documents Are Open",strAppName) |
| 12 | + Exit Sub |
| 13 | + End If |
| 14 | + |
| 15 | + ' Get current document |
| 16 | + Dim oDoc As Inventor.Document = ThisApplication.ActiveEditDocument |
| 17 | + |
| 18 | + ' Check document type |
| 19 | + If oDoc.DocumentType <> Inventor.DocumentTypeEnum.kPartDocumentObject And oDoc.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then |
| 20 | + MessageBox.Show("An assembly or part document must be open",strAppName) |
| 21 | + Exit Sub |
| 22 | + End If |
| 23 | + |
| 24 | + ' Get list of active library appearances |
| 25 | + Dim appearanceList As New List(Of String) |
| 26 | + |
| 27 | + ' Iterate through all active render styles |
| 28 | + For Each oRS As RenderStyle In oDoc.RenderStyles |
| 29 | + ' Add render style name to list |
| 30 | + appearanceList.Add(oRS.Name) |
| 31 | + Next |
| 32 | + |
| 33 | + ' Create new instance of the custom form (sends appearance list to form) |
| 34 | + Dim myfrm As New CustomForm(appearanceList) |
| 35 | + |
| 36 | + Dim strAppearance As String = "" |
| 37 | + Dim bClearOverride As Boolean = True |
| 38 | + |
| 39 | + ' Display the custom form result |
| 40 | + If (myfrm.ShowDialog() = DialogResult.OK) Then |
| 41 | + ' Use ShowDialog so it waits for user / form close before continuing |
| 42 | + strAppearance = myfrm.myResult |
| 43 | + bClearOverride = myfrm.bOverride |
| 44 | + Else |
| 45 | + ' Do nothing if dialog was cancelled |
| 46 | + Exit Sub |
| 47 | + End If |
| 48 | + |
| 49 | + |
| 50 | + ' Create new transaction |
| 51 | + Dim oTx As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, strAppName) |
| 52 | + |
| 53 | + ' Get active document directory |
| 54 | + Dim strParentDirectory As String = System.IO.Path.GetDirectoryName(oDoc.FullFileName) |
| 55 | + |
| 56 | + Dim iCount As Integer = oDoc.AllReferencedDocuments.Count |
| 57 | + Dim x As Integer = 0 |
| 58 | + |
| 59 | + ' Check active document type |
| 60 | + If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then |
| 61 | + ' Iterate through all referenced documents |
| 62 | + For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments |
| 63 | + |
| 64 | + ' Increase count + update status |
| 65 | + x += 1 |
| 66 | + ThisApplication.StatusBarText = "Processing part " & x & " of " & iCount |
| 67 | + |
| 68 | + ' Skip Master Files |
| 69 | + If oRefDoc.FullFileName.ToLower.Contains("master") Then Continue For |
| 70 | + |
| 71 | + ' Check file is modifiable |
| 72 | + If Not oRefDoc.IsModifiable Then Continue For ' This check not really necessary |
| 73 | + |
| 74 | + ' Only do files in same directory |
| 75 | + If oRefDoc.FullFileName.Contains(strParentDirectory) Then ChangeColour(oRefDoc,strAppearance,bClearOverride) |
| 76 | + Next |
| 77 | + ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then |
| 78 | + ChangeColour(oDoc,strAppearance,bClearOverride) |
| 79 | + End If |
| 80 | + |
| 81 | + ' End transaction |
| 82 | + oTx.End |
| 83 | + |
| 84 | + ' Update Assembly Document |
| 85 | + oDoc.Update() |
| 86 | + |
| 87 | + ' Update status |
| 88 | + ThisApplication.StatusBarText = "Processing complete" |
| 89 | + |
| 90 | + End Sub |
| 91 | + |
| 92 | + Sub ChangeColour(ByVal oDoc As Inventor.Document,ByVal strAppearance As String,ByVal bClearOverride As Boolean) |
| 93 | + |
| 94 | + Try |
| 95 | + ' Check document is a part document |
| 96 | + If oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Exit Sub |
| 97 | + |
| 98 | + ' Redefine as part document |
| 99 | + Dim oPartDoc As PartDocument = oDoc |
| 100 | + |
| 101 | + ' Check if part is derived and if derived colour override is wanted |
| 102 | + If oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Count > 0 And bClearOverride Then |
| 103 | + ' Iterate through each derived component |
| 104 | + For Each oDComp As Inventor.DerivedPartComponent In oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents |
| 105 | + ' Get the definition |
| 106 | + Dim oDDef As Inventor.DerivedPartDefinition = oDComp.Definition |
| 107 | + ' Change derived settings |
| 108 | + oDDef.UseColorOverridesFromSource = False |
| 109 | + ' Push changes back to derived part |
| 110 | + oDComp.Definition = oDDef |
| 111 | + Next |
| 112 | + |
| 113 | + End If |
| 114 | + |
| 115 | + ' Create new instance of render style |
| 116 | + Dim oNewColor As RenderStyle = oPartDoc.RenderStyles.Item(strAppearance) |
| 117 | + |
| 118 | + ' Change render style if it doesn't match |
| 119 | + If Not oPartDoc.ActiveRenderStyle Is oNewColor Then |
| 120 | + oPartDoc.ActiveRenderStyle = oNewColor |
| 121 | + End If |
| 122 | + |
| 123 | + Catch Ex As Exception |
| 124 | + MessageBox.Show(Ex.Message & System.Environment.NewLine & "An unexpected error occurred" _ |
| 125 | + & System.Environment.NewLine & oDoc.FullFileName, "Change Colour") |
| 126 | + End Try |
| 127 | + |
| 128 | + End Sub |
| 129 | +End Class |
| 130 | + |
| 131 | +Public Class CustomForm |
| 132 | + Inherits System.Windows.Forms.Form |
| 133 | + |
| 134 | + Public myResult As String = "Not Set" |
| 135 | + Public bOverride As Boolean = True |
| 136 | + |
| 137 | + Public Sub New(ByRef appearanceList As List(Of String)) |
| 138 | + |
| 139 | + ' This is run when a new instance of the form is created |
| 140 | + oForm = Me |
| 141 | + |
| 142 | + With oForm |
| 143 | + ' Set up form |
| 144 | + .FormBorderStyle = FormBorderStyle.FixedToolWindow 'Fixed3D / FixedDialog / FixedSingle / FixedToolWindow / None / Sizable / SizableToolWindow |
| 145 | + '.MaximizeBox = False |
| 146 | + '.MinimizeBox = False |
| 147 | + .StartPosition = FormStartPosition.CenterScreen |
| 148 | + .Width = 300 |
| 149 | + .Height = 150 |
| 150 | + .TopMost = True |
| 151 | + .Text = "Set Part Colours" |
| 152 | + .Name = "Set Part Colours" |
| 153 | + |
| 154 | + End With |
| 155 | + |
| 156 | + Dim Button1 As New Button() |
| 157 | + With Button1 |
| 158 | + '.Font = myfnt |
| 159 | + .Text = "Start" |
| 160 | + .Width = 100 |
| 161 | + .Height = 30 |
| 162 | + '.Top = 60 |
| 163 | + '.Left = oForm.Width/2 - 100/2 |
| 164 | + .Enabled = True |
| 165 | + .Dock = DockStyle.Bottom |
| 166 | + End With |
| 167 | + |
| 168 | + ' Create a selection box and load list |
| 169 | + Dim ComboBox1 As New ComboBox() |
| 170 | + With ComboBox1 |
| 171 | + .Name = "cb_appearances" |
| 172 | + .Items.Clear() |
| 173 | + .Items.AddRange(appearanceList.ToArray) |
| 174 | + .SelectedIndex = 1 |
| 175 | + .Top = 25 |
| 176 | + .Left = 25 |
| 177 | + .DropDownWidth = 300 |
| 178 | + .Width = 225 |
| 179 | + ' Users can select from list / start typing to search |
| 180 | + '.DropDownStyle = Dropdownlist |
| 181 | + ' This will prevent invalid typing / not matching selections |
| 182 | + .AutoCompleteMode = Append |
| 183 | + End With |
| 184 | + |
| 185 | + Dim CheckBox1 As New CheckBox() |
| 186 | + With CheckBox1 |
| 187 | + .Name = "check_colour" |
| 188 | + .Text = "Remove derived colour override" |
| 189 | + .Width = 225 |
| 190 | + .Top = 50 |
| 191 | + .Left = 25 |
| 192 | + .Checked = True |
| 193 | + End With |
| 194 | + |
| 195 | + 'Add your Event handler |
| 196 | + AddHandler Button1.Click, AddressOf Button1_Click |
| 197 | + AddHandler ComboBox1.KeyPress, AddressOf ComboBox1_KeyPress |
| 198 | + AddHandler CheckBox1.CheckedChanged, AddressOf CheckBox1_CheckedChanged |
| 199 | + |
| 200 | + 'Add controls to form |
| 201 | + oForm.Controls.Add(Button1) |
| 202 | + oForm.Controls.Add(ComboBox1) |
| 203 | + oForm.Controls.Add(CheckBox1) |
| 204 | + |
| 205 | + ' Set default dialog result |
| 206 | + Me.DialogResult = System.Windows.Forms.DialogResult.None |
| 207 | + |
| 208 | + End Sub |
| 209 | + |
| 210 | + Private Sub CustomForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing |
| 211 | + ' Only run if dialog cance "x" button is clicked |
| 212 | + If Me.DialogResult = DialogResult.Cancel Then |
| 213 | + If MessageBox.Show("Are you sure to close this application?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then |
| 214 | + |
| 215 | + Else |
| 216 | + e.Cancel = True |
| 217 | + End If |
| 218 | + End If |
| 219 | + End Sub |
| 220 | + |
| 221 | + Private Sub Button1_Click(ByVal sender As System.Object, ByVal E As System.EventArgs) |
| 222 | + |
| 223 | + Dim oCB As ComboBox = Me.Controls.Item("cb_appearances") |
| 224 | + myResult = oCB.Text |
| 225 | + |
| 226 | + ' Validate combo box text |
| 227 | + If oCB.Items.Contains(oCB.Text) = False Then |
| 228 | + MessageBox.Show("Selection is not in the list") |
| 229 | + Exit Sub |
| 230 | + End If |
| 231 | + 'myResult = Me.Controls.Item("cb_appearances").Text |
| 232 | + |
| 233 | + Me.DialogResult = System.Windows.Forms.DialogResult.OK |
| 234 | + Me.Close |
| 235 | + End Sub |
| 236 | + |
| 237 | + Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal E As EventArgs) |
| 238 | + Dim check1 As CheckBox = Me.Controls.Item("check_colour") |
| 239 | + bOverride = check1.Checked |
| 240 | + End Sub |
| 241 | + |
| 242 | + Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) |
| 243 | + ' Limit key press entries via string matching the combo box list |
| 244 | + If e.KeyChar = ControlChars.Back AndAlso e.KeyChar = ControlChars.Back Then |
| 245 | + Return |
| 246 | + End If |
| 247 | + |
| 248 | + Dim t As String = sender.Text |
| 249 | + Dim typedT As String = t.Substring(0, sender.SelectionStart) |
| 250 | + Dim newT As String = typedT + e.KeyChar |
| 251 | + |
| 252 | + Dim i As Integer = sender.FindString(newT) |
| 253 | + If i = -1 Then |
| 254 | + e.Handled = True |
| 255 | + End If |
| 256 | +End Sub |
| 257 | + |
| 258 | +End Class |
| 259 | + |
| 260 | + |
0 commit comments