@WCrihfield Thank you for your time. Here is the entire rule. We have a template for rules that this is built using. Should be easy enough to follow. I added all three update lines in and still have the same behavior. If I am approaching this wrong, I am open to any suggestion on how to restructure this. We are attempting to maintain the instance number that was provided by Inventor. Occasionally someone will rename a browser node and remove the :# so our existing script (not written by me) crashes. This is my attempted rewrite into our template with some added functionality to help identify the problem components and avoid the generic exception message.
'Define Imports below
Imports System.Text.RegularExpressions
AddVbFile "LSIiLogicLibrary.VB"
AddVbFile "LSIiLogicDebugger.VB"
Class BrowserNames
' Instantiate the LSI iLogic Function Library
Shared Dim lsiLib As LSIiLogicLibrary = New LSIiLogicLibrary
' Instantiate the LSI Debugging Library and set the debugging level for this rule
' - 0 = None; 1 = Minimal; 2 = Verbose
Shared Dim debug As LSIiLogicDebugger = New LSIiLogicDebugger(0)
' Set the rule name for use later in the rule
Shared Dim ruleName As String = "Browser Names"
Sub Main()
'Rule: BrowserNames
'Purpose: This rule updates the model browser to show PN ~ DESCRIPTION : OCCURRENCE
' If the description is blank, an error will be shown with instructions on how to correct it
'Set initial script references to the Inventor Application and the Document
Dim oApp As Inventor.Application = ThisApplication
Dim doc, oDoc As Document
doc = ThisDoc.Document
oDoc = oApp.ActiveDocument
' Set the type of document this rule is targeted to
' - valid entries are "Drawing", "Part", "Assembly"
Dim ruleTargetType() As String = {"Assembly"}
' Test If we are actually in the document before running rule
' - prevents running rule if triggered from another doc
If Not doc Is oDoc Then
Exit Sub
End If
' Test If we are in the correct document type for the rule
' - prevents running the rule on an incorrect document type
If Not lsiLib.checkDocType(ruleTargetType, oDoc.DocumentType) Then
Dim msgStr = "This is not the correct document type for rule """ & ruleName & """." + vbCrLf + "The correct document types are: "
For i As Integer = 0 To UBound(ruleTargetType)
msgStr = msgStr + vbCrLf + vbTab + ruleTargetType(i)
Next
debug.Print(0, msgStr, ruleName, MessageBoxButtons.OK, MessageBoxicon.Information)
Exit Sub
End If
'Setup TransactionManager so we can undo our changes later
Dim oTransMgr As TransactionManager = oApp.TransactionManager
Dim oTrans As Transaction
Try
debug.Print(1,"Running the rule", ruleName)
'Begin the transaction
oTrans = oTransMgr.StartTransaction(oDoc, ruleName)
' Rule code should be placed in Sub Rule() below
Rule(oApp, oDoc)
'End and commit the transaction for later Undo
If Not oTrans Is Nothing Then
oTrans.End()
End If
Catch ex As Exception
' Abort the transaction so we dont leave it orphaned (avoid possible memory leak)
If Not oTrans Is Nothing Then
oTrans.Abort()
End If
debug.Print(0,"There is an error in the script. Please see the notes below or notIfy the script author." & vbCrLf & "Please provide the drawing/part number, rev and the following error text." & vbCrLf & vbCrLf & ruleName & ": " & ex.Message, ruleName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
'Update the document
InventorVb.DocumentUpdate()
End Sub
'
' Rule(oApp As Inventor.Application, oDoc as Document)
' - This is where the actual rule code lives.
' - We Get a reference To the Application And the Active Document by default
'
Sub Rule(oApp As Inventor.Application, oDoc As Document)
' Comment the following line if using Try...Catch in the rule
' On Error Resume Next
' Cast the current document to the correct type based on what we expect it to be
' as different document types have similar methods that do different things, we
' need to specify the document type and use the correct types in our script
Dim oPartDoc As PartDocument
Dim oAsmDoc As AssemblyDocument
Dim oDrawDoc As DrawingDocument
Select Case oDoc.DocumentType
Case Inventor.DocumentTypeEnum.kDrawingDocumentObject
'Document is a Drawing. Use oDrawDoc as your docuemnt reference in your code
oDrawDoc = DirectCast(oDoc, DrawingDocument)
Case Inventor.DocumentTypeEnum.kAssemblyDocumentObject
'Document is an Assembly. Use oAsmDoc as your docuemnt reference in your code
oAsmDoc = DirectCast(oDoc, AssemblyDocument)
Case Inventor.DocumentTypeEnum.kPartDocumentObject
'Document is a Part. Use oPartDoc as your docuemnt reference in your code
oPartDoc = DirectCast(oDoc, PartDocument)
End Select
debug.Print(2,"Document cast successful!", ruleName) 'This was just a placeholder that was never removed from the template
'Begin code here
Dim badOcc As Boolean = False
Dim oCompDef As Inventor.ComponentDefinition = oAsmDoc.ComponentDefinition
Dim oCompOcc As Inventor.ComponentOccurrence
Dim oPartCompDef As Inventor.PartComponentDefinition
Dim oPane As BrowserPane = oAsmDoc.BrowserPanes.Item("Model")
Dim occNum, sPartNum, sPartDesc As String
For Each oCompOcc In oCompDef.Occurrences
occNum = GetDigitsAfterColon(oCompOcc.Name)
If occNum Is Nothing Then badOcc = True
debug.Print(2, "occNum " & occNum, ruleName)
sPartNum = iProperties.Value(oCompOcc.Name, "Project", "Part Number").Trim()
sPartDesc = iProperties.Value(oCompOcc.Name, "Project", "Description").Trim()
If Len(sPartDesc) = 0 Then badOcc = True
oCompOcc.Name = sPartNum & " ~ " & If(len(sPartDesc) > 0, sPartDesc, "▲▲▲ Missing Description ▲▲▲") & ":" & If(occNum IsNot Nothing, occNum,"▲▲▲ Missing Instance ID ▲▲▲")
Next
oAsmDoc.Update2()
InventorVb.DocumentUpdate()
ThisApplication.ActiveView.Update()
If badOcc Then
debug.Print(0,"You have 1 or more parts with a missing Description or a missing instance ID." & vbCrLf & vbCrLf & "Please edit the part and update the Description iProperty using the Model Properties Form and save the part." & vbCrLf & vbCrLf & "For a missing Instance ID, please edit the description in the assembly model tree and add one. "":1""", ruleName)
End If
End Sub
Function GetDigitsAfterColon(input As String) As String
Dim regexPattern As String = ":(\d+)$" ' colon character followed by one or more digits
Dim match As Match = Regex.Match(input, regexPattern)
If match.Success Then
' Extract and return the digits
Return match.Groups(1).Value
Else
' No match found
Return Nothing
End If
End Function
End Class
Again, I am super grateful for your eyes on this. Please feel free to tear this down. I am just a hobbyist programmer and some of this might be inefficient or just wrong. Thank you.