' Start of iLogic Code ===========================================================
' This code 2017 - Luke Davenport @ Excitech
Sub Main
'Check whether open document is an assembly and exit rule if not
Dim oDoc As AssemblyDocument = Nothing
Try
oDoc = ThisApplication.ActiveEditDocument
Catch
MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "Excitech iLogic")
Return
End Try
'Define assembly
Dim oDef As AssemblyComponentDefinition = ThisApplication.ActiveEditDocument.ComponentDefinition
Dim oOccs As ComponentOccurrences = oDef.Occurrences
Dim oSelectedDocs As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim oSelectedOcc As Object
Dim Title As String = "Change Sheet Metal Rule for Selected Components"
' Create arraylist to hold all the available sheet metal styles
Dim SheetStylesArray As New ArrayList
Dim oSheetDoc As PartDocument = Nothing
Dim oSheetCompDef As SheetMetalComponentDefinition = Nothing
Do
While True
oSelectedOcc = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter,
"Select component(s) to apply new sheet metal rule to - (" & oSelectedDocs.Count & " selected) - Hit ESC when done")
' If nothing gets selected then we're done
If IsNothing(oSelectedOcc) Then
Exit Do
Else
' A part occurrence was selected - is it a sheet metal occurrence?
Try
oSheetDoc = oSelectedOcc.Definition.Document
oSheetCompDef = oSheetDoc.ComponentDefinition
' If we're still here then the selected part is a sheet metal part
' Turn off all the occurrences of the selected part
Call EnableComps(oOccs, oSheetDoc, False)
' Add the document represented by the selected occurrence to the collection
oSelectedDocs.Add(oSheetDoc)
Catch
' Selected part is not sheet metal - loop
Dim Ques As MsgBoxResult = MessageBox.Show("Please select a sheet metal component!", Title,
MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
If Ques = MsgBoxResult.Cancel Then
' User has cancelled
' Need to turn back on all the selected occurrences
For Each oD As PartDocument In oSelectedDocs
' Turn on all the occurrences of the selected part
Call EnableComps(oOccs, oD, True)
Next
Exit Sub
Else
' Loop
End If
End Try
End If
End While
If oSelectedDocs.Count = 0 Then
Dim Ques As MsgBoxResult = MessageBox.Show("Please select components", Title,
MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
If Ques = MsgBoxResult.Cancel Then
Exit Sub
Else
' Loop
End If
Else
Exit Do
End If
Loop
If oSelectedDocs.Count = 0 Then
' User hit escape with no components selected - exit
Exit Sub
End If
Dim ErrorList As New List(Of String)
' Get the sheet metal rules from the first document selected
oSheetDoc = oSelectedDocs.Item(1)
oSheetCompDef = oSheetDoc.ComponentDefinition
Try
' Get the sheet metal styles that are available in the document
Dim oStyle As SheetMetalStyle
For Each oStyle In oSheetCompDef.SheetMetalStyles
' Check that the style exists in the library - this means it'll be available to all the sheet metal documents that have been selected...
If oStyle.StyleLocation = StyleLocationEnum.kBothStyleLocation OrElse oStyle.StyleLocation = StyleLocationEnum.kLibraryStyleLocation Then
' Write sheet metal style name to arraylist
SheetStylesArray.Add(oStyle.Name)
End If
Next
Catch
' Do nothing
End Try
' Error checking - no sheet metal rules in library
If SheetStylesArray.Count = 0 Then
MsgBox("No sheet metal rules were found in the styles library. Please populate the library before trying again", MsgBoxStyle.OkOnly, Title)
' Need to turn back on all the selected occurrences
For Each oD As PartDocument In oSelectedDocs
' Turn on all the occurrences of the selected part
Call EnableComps(oOccs, oD, True)
Next
Exit Sub
End If
' Sort the array alphabetically
SheetStylesArray.Sort()
Dim SheetRuleSelection As String = ""
' Attempt to set the value of a parameter containing the list of sheet metal style names
Try
MultiValue.List("ActiveRule") = SheetStylesArray
Catch
' parameter doesn't exist - create it
Dim oParam As Parameter = oDef.Parameters.UserParameters.AddByValue("ActiveRule", SheetStylesArray(0), UnitsTypeEnum.kTextUnits)
MultiValue.List("ActiveRule") = SheetStylesArray
End Try
' Ask user to pick what sheet metal rule they would like to apply (using the values in 'ActiveRule' parameter)...
Do
SheetRuleSelection = InputListBox("Specify sheet metal rule for selected part(s)", MultiValue.List("ActiveRule"), "None", Title:=Title, ListName:="Please choose")
If SheetRuleSelection = "" Then ' Ask again!
Dim Ques As MsgBoxResult = MessageBox.Show("Please select a choice!", Title, MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
If Ques = MsgBoxResult.Cancel Then
' User has cancelled
'Need to turn back on all the selected occurrences
For Each oD As PartDocument In oSelectedDocs
' Turn on all the occurrences of the selected part
Call EnableComps(oOccs, oD, True)
Next
Exit Sub
Else
' Loop
End If
Else
Exit Do
End If
Loop
ThisApplication.UserInterfaceManager.UserInteractionDisabled = True
Dim SuccessCount As Integer = 0
' Apply the sheet metal rule to the selected parts
For Each oSheetDoc In oSelectedDocs
'change active sheet metal rule
Try
oSheetDoc.ComponentDefinition.SheetMetalStyles.Item(SheetRuleSelection).Activate
SuccessCount += 1
Catch
'error checking
ErrorList.Add(oSheetDoc.FullFileName)
End Try
' Turn on all the occurrences of the selected part
Call EnableComps(oOccs, oSheetDoc, True)
Next
ThisApplication.UserInterfaceManager.UserInteractionDisabled = False
'Inform user which parts have failed to update if there are any errors
Dim ErrorListNew As String = String.Join(vbLf, ErrorList)
If Not String.IsNullOrEmpty(ErrorListNew) Then
MessageBox.Show("Failed to update the following sheet metal parts without errors: " & vbLf & vbLf & ErrorListNew & "." & vbLf & vbLf &
"1) Please check there is a sheet metal rule called '" & SheetRuleSelection & "' available in these parts" &
" and spelled correctly!" & vbLf & _
"2) Please check that the sheet metal rule can be applied manually without an error occurring", SuccessCount & " Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
Else
ThisApplication.StatusBarText = oSelectedDocs.Count & " Documents - Sheet Metal Rule Successfully Changed..."
End If
oDoc.Update()
ThisApplication.ActiveView.Update()
End Sub
''' <summary>
''' This sub finds all the occurrences that refer to the supplied part document and either enables or unenables them
''' ' depending on the supplied 'Enable' boolean
''' </summary>
Sub EnableComps(ByRef oOccs As ComponentOccurrences,
ByVal oSheetDoc As PartDocument,
ByVal Enable As Boolean)
Dim oOccEnum As ComponentOccurrencesEnumerator = Nothing
' Make occurrences that refer to this document 'enabled' again
oOccEnum = oOccs.AllReferencedOccurrences(oSheetDoc)
For Each oOcc In oOccEnum
' Enable it
oOcc.Enabled = Enable
Next
End Sub
' End of iLogic Code ===========================================================