Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: tfrohe_LSI

Hi @tfrohe_LSI.  That is certainly a long and involved rule template you have there, and it even seems to be referencing two other external iLogic rules for extra functionality.  It does seem like a bit of overkill to me, but if that system is working OK for you guys, then who am I to judge.  I also occasionally use references to external rules with extra resources in them from time to time.  There are several ways that this situation could be handled differently, but I guess it all comes down to personal preference and what you ideally want to see happen (how you want it to work).  Just attempting to maintain the existing integers at the end of the component name may not be enough in a situation like this, but there is only one way to find out...testing.  The code you are using to extract the digits after the colon is a fairly advanced technique that I do not see that often, so I have not used it for that exact purpose before.  I generally just first make sure that the component name 'Contains' the ":" (colon) character, then use the 'Split' function to split the name into the portion before that character, and the portion after that character.  Of course, if for some reason three is more than one colon character in its name, that would normally cause problems.

I created an alternate iLogic rule of my own, just as something to try out or compare with the functionality of your current solution.  It does utilize the Logger functionality to log some useful information to the iLogic Log window, but you can comment those lines out, if you do not want to pay attention to that as a resource.  This code also keeps track of all components that encountered issues with a 'Dictionary' type collection.  Each entry would potentially contain the component object as its 'Key', and a note/message about what was wrong with that component as the value of the dictionary entry.  Then, if any problem components were found, it loops back through those problem components, informing you about the current name of each one, and what the problem was.  As I said, there are many directions and different actions that could be done here, so this is just one possibility among many.  I could have also added the problem components to a HighlightSet, as an additional way to point them out, but that is often just a fleeting moment that they remain highlighted, so often not super helpful.

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Return
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	If oOccs.Count = 0 Then Return
	Dim oOccsWithProblems As New Dictionary(Of Inventor.ComponentOccurrence, String)
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.Name.Contains(":") Then
			'assumes that there is just one instance of the ":" string present
			'if more than that, that would mess this process up
			Dim oSections() As String = oOcc.Name.Split(":") 'split the name into multiple Strings devided by the ":" character
			If oSections.Length <> 2 Then
				'the split resulted in more or less than 2 Strings
				'code here for what to do about that situation
				oOccsWithProblems.Add(oOcc, "More Or Less Than 2 Sections In Name When Split By Colon")
				Continue For 'skip to next component
			End If
			Dim sName As String = oSections.First 'the portion of the name before the ":" character
			Dim sInstance As String = oSections.Last 'the portion of the name after the ":" character
			Dim sPN As String = iProperties.Value(oOcc.Name, "Project", "Part Number")
			If sPN = "" Then
				'what to do when the Part Number iProperty value is empty
				Logger.Debug("Component named:  " & oOcc.Name & vbCrLf & _
				"...had empty Part Number value.")
				sPN = "▲▲▲ Missing Part Number ▲▲▲"
				oOccsWithProblems.Add(oOcc, "Empty Part Number")
				'Continue For 'skip to next component
			End If
			Dim sDesc As String = iProperties.Value(oOcc.Name, "Project", "Description")
			If sDesc = "" Then
				'what to do when the Description iProperty value is empty
				Logger.Debug("Component named:  " & oOcc.Name & vbCrLf & _
				"...had empty Description value.")
				sDesc = "▲▲▲ Missing Description ▲▲▲"
				oOccsWithProblems.Add(oOcc, "Empty Description")
				'Continue For 'skip to next component
			End If
			Dim sNewOccName As String = sPN & " ~ " & sDesc & ":" & sInstance
			Try
				oOcc.Name = sNewOccName
			Catch oEx As Exception
				Logger.Error("Error renaming component..." & vbCrLf & _
				"From:  " & oOcc.Name & vbCrLf & _
				"To:  " & sNewOccName)
			End Try
		Else 'component's name did not contain ":"
			Logger.Debug("Component named:  " & oOcc.Name & vbCrLf & _
				"...does not have ':' character in its name.")
			oOccsWithProblems.Add(oOcc, "No Colon")
		End If
	Next
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	'If oADoc.Dirty Then oADoc.Save2(True)
	If oOccsWithProblems.Count > 0 Then
		For Each oEntry As KeyValuePair(Of Inventor.ComponentOccurrence, String) In oOccsWithProblems
			Dim oComp As ComponentOccurrence = oEntry.Key
			Dim sNote As String = oEntry.Value
			'some code here for what to do with each problem component, such as a report or message to the user
			MessageBox.Show("Component named:  " & oComp.Name & vbCrLf & sNote, "Component With Problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
		Next
	End If
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)