Add custom iproperties to virtual components with ilogic

Add custom iproperties to virtual components with ilogic

pmonteiro
Enthusiast Enthusiast
428 Views
4 Replies
Message 1 of 5

Add custom iproperties to virtual components with ilogic

pmonteiro
Enthusiast
Enthusiast

Good afternoon.

I'm managing to create a virtual component with ilogic with the same parnumber as the active assembly.

The problem is that I need to create custom properties that are the same as the assembly's. The code generates a response saying that the component was created successfully and the properties.

Yes, the component was created, but not the properties.

Can anyone help?

 

Thank you

 

0 Likes
Accepted solutions (1)
429 Views
4 Replies
Replies (4)
Message 2 of 5

jnowel
Advocate
Advocate
Accepted solution

Something like this should do to add a custom iProperty for the virtual component

oCompDef.PropertySets.Item("User Defined Properties").Add("Property Value","Property Name")


From the Inventor BOM API Samples
You can check the Component Definition (Starts at Line 57)

Public Sub BOMQuery()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim FirstLevelOnly As Boolean
    If MsgBox("First level only?", vbYesNo) = vbYes Then
        FirstLevelOnly = True
    Else
        FirstLevelOnly = False
    End If
    
    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = oDoc.ComponentDefinition.BOM
    
    ' Set whether first level only or all levels.
    If FirstLevelOnly Then
        oBOM.StructuredViewFirstLevelOnly = True
    Else
        oBOM.StructuredViewFirstLevelOnly = False
    End If
    
    ' Make sure that the structured view is enabled.
    oBOM.StructuredViewEnabled = True
    
    'Set a reference to the "Structured" BOMView
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Structured")
        
    Debug.Print "Item"; Tab(15); "Quantity"; Tab(30); "Part Number"; Tab(70); "Description"
    Debug.Print "----------------------------------------------------------------------------------"

    'Initialize the tab for ItemNumber
    Dim ItemTab As Long
    ItemTab = -3
    Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
    ItemTab = ItemTab + 3
    ' Iterate through the contents of the BOM Rows.
    Dim i As Long
    For i = 1 To oBOMRows.Count
        ' Get the current row.
        Dim oRow As BOMRow
        Set oRow = oBOMRows.Item(i)

        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

        Dim oPartNumProperty As Property
        Dim oDescripProperty As Property

        If Typeof oCompDef Is VirtualComponentDefinition Then
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the virtual component definition
            Set oPartNumProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Description")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); oDescripProperty.Value
        Else
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Set oPartNumProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Description")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); oDescripProperty.Value
            
            'Recursively iterate child rows if present.
            If Not oRow.ChildRows Is Nothing Then
                Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
            End If
        End If
    Next
    ItemTab = ItemTab - 3
End Sub

 

Message 3 of 5

pmonteiro
Enthusiast
Enthusiast

Yes, that seems to be the way to go.

It worked, I just had to adapt it a bit to make the name the same as the active assembly.

Now I have another problem:

If the virtual component already exists, don't create the virtual component, but update the custom properties.

I keep getting errors.

Can you help ?

0 Likes
Message 4 of 5

pmonteiro
Enthusiast
Enthusiast

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

0 Likes
Message 5 of 5

jnowel
Advocate
Advocate

Glad to know that you have resolved it.
Anyway, below is what I had

 

Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument

Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

Dim oVirtCompList As New List(Of String)
oVirtCompList.Add("Virtual Component - Oil")
oVirtCompList.Add("Virtual Component - Paint")
oVirtCompList.Add("Other Stuff")

'Create a Dictionary of Occurrences
Dim oOccDict As New Dictionary(Of String, ComponentOccurrence)
For Each oCompOcc As ComponentOccurrence In oAsmCompDef.Occurrences
	If Not oOccDict.ContainsKey(oCompOcc.Name) Then
		oOccDict.Add(oCompOcc.Name, oCompOcc)
	End If
Next

'Update/Create Virtual Components
For Each item In oVirtCompList
	
	Dim oVirtualComp As  ComponentOccurrence = Nothing
	Dim oMatrix = ThisApplication.TransientGeometry.CreateMatrix

	If oOccDict.ContainsKey(item) Then
		'Check if Virtual Component
		If TypeOf oOccDict.Item(item).Definition Is VirtualComponentDefinition Then
			oVirtualComp = oOccDict.Item(item)
		Else
			MessageBox.Show("Skipped Existing Component Occurrence: " & item & " is not a Virtual Component")
			Continue For
		End If
	Else
		oVirtualComp = oAsmCompDef.Occurrences.AddVirtual(item, oMatrix)
		oVirtualComp.Name = item
	End If
	
	Dim oVirtualCompDef As VirtualComponentDefinition = oVirtualComp.Definition
	Dim DesignProp As Inventor.PropertySet =  oVirtualCompDef.PropertySets("Design Tracking Properties")
	Dim CustomProp As Inventor.PropertySet = oVirtualCompDef.PropertySets("User Defined Properties")
	
	DesignProp.Item("Part Number").Value = oAsmDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value
	DesignProp.Item("Description").Value = oAsmDoc.PropertySets("Design Tracking Properties").Item("Description").Value
	
	Try : CustomProp.Item("Custom Property").Value = "Test Value"
	Catch : CustomProp.Add("Test Value", "Custom Property")
	End Try

Next