Hello.
I've managed to achieve my goal and get the illogic code working as I needed it to.
Thanks for the initial push.
- This code first checks if a virtual component already exists in the assembly. If it does, it updates its Part Number, Description, and User Defined Properties.
- If the virtual component doesn't exist, it creates a new one using the Part Number, Description, and custom properties from the active assembly.
' Get the active document (must be an assembly)
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
' Check if the active document is really an assembly
If oDoc.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("The active document is not an assembly.")
Exit Sub
End If
' Get the definition of the active assembly component
Dim oCompDef As AssemblyComponentDefinition
oCompDef = oDoc.ComponentDefinition
' Get the Part Number and Description of the active assembly
Dim oPartNumber As String
oPartNumber = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
Dim oDescription As String
oDescription = oCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Description").Value
' Get the name of the active assembly to rename the virtual component
Dim oAssemblyName As String
oAssemblyName = oDoc.DisplayName ' DisplayName includes the file extension
' Remove the extension (.iam) from the file name to use as the virtual component name
oAssemblyName = Left(oAssemblyName, InStrRev(oAssemblyName, ".") - 1)
' Create a default transformation (position) matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix()
' Search directly for virtual components, ignoring names
Dim oVirtualOccurrence As ComponentOccurrence = Nothing
Dim oOccurrence As ComponentOccurrence
Dim found As Boolean = False
' Iterate through the occurrences in the assembly
For Each oOccurrence In oCompDef.Occurrences
' Check if the occurrence is a virtual component
If TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
' Virtual component found
oVirtualOccurrence = oOccurrence
found = True
Exit For
End If
Next
' If the virtual component already exists, update its properties
If found Then
' Update the properties of the existing virtual component
Dim oVirtualCompDef As VirtualComponentDefinition
oVirtualCompDef = oVirtualOccurrence.Definition
' Update the Part Number
oVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = oPartNumber
' Update the Description
oVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Description").Value = oDescription
' Get the User Defined Properties of the active assembly
Dim oUserDefinedProps As PropertySet
oUserDefinedProps = oCompDef.Document.PropertySets.Item("User Defined Properties")
' Iterate through the User Defined Properties of the active assembly and copy them to the virtual component
Dim oProp As Inventor.Property
For Each oProp In oUserDefinedProps
Try
' Try to access and replace the property if it exists
oVirtualCompDef.PropertySets.Item("User Defined Properties").Item(oProp.Name).Value = oProp.Value
Catch ex As Exception
Try
' If the property doesn't exist, add it
oVirtualCompDef.PropertySets.Item("User Defined Properties").Add(oProp.Value, oProp.Name)
Catch addEx As Exception
' Handle exceptions when adding the property
MessageBox.Show("Error adding the property: " & oProp.Name & " - " & addEx.Message)
End Try
End Try
Next
' Display a message indicating that the virtual component has been successfully updated
MessageBox.Show("The virtual component's custom properties and description have been successfully updated!")
Else
' If the virtual component does not exist, create a new virtual component
Try
oVirtualOccurrence = oCompDef.Occurrences.AddVirtual(oAssemblyName, oMatrix) ' Use the assembly name here
Dim oVirtualCompDef As VirtualComponentDefinition
oVirtualCompDef = oVirtualOccurrence.Definition
' Assign the Part Number and Description to the new virtual component
oVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = oPartNumber
oVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Description").Value = oDescription
' Get the User Defined Properties of the active assembly
Dim oUserDefinedProps As PropertySet
oUserDefinedProps = oCompDef.Document.PropertySets.Item("User Defined Properties")
' Iterate through the User Defined Properties of the active assembly and copy them to the virtual component
Dim oProp As Inventor.Property
For Each oProp In oUserDefinedProps
oVirtualCompDef.PropertySets.Item("User Defined Properties").Add(oProp.Value, oProp.Name)
Next
' Display a message indicating that the virtual component has been successfully created
MessageBox.Show("Virtual component created and renamed to: " & oAssemblyName & " with the same Part Number, description, and custom properties as the active assembly!")
Catch ex As Exception
MessageBox.Show("Error creating the virtual component: " & ex.Message)
Exit Sub
End Try
End If