Virtual Parts - Looping to add iProperty AFTER Virtual Parts has been created

Virtual Parts - Looping to add iProperty AFTER Virtual Parts has been created

ABoaro
Enthusiast Enthusiast
655 Views
6 Replies
Message 1 of 7

Virtual Parts - Looping to add iProperty AFTER Virtual Parts has been created

ABoaro
Enthusiast
Enthusiast

I am trying to figure out a method of adding a new iProperty to a virtual part AFTER it has already been created in an assembly.

The following code runs without errors but it unsuccessfully adds the new iProperty. I tried going line by line to debug but I cannot figure out why when looping through virtual parts it doesn't actually change the iProperty but still displays the correct name of the virtual part.

Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAssDef As AssemblyComponentDefinition = oAssDoc.ComponentDefinition

Dim oOccurrence As ComponentOccurrence
	For Each oOccurrence In oAssDef.Occurrences
		If TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
				' Define Comp Def
				Dim oVirt As VirtualComponentDefinition 
				oVirt = oOccurrence.Definition
				'oVirt.PropertySets.Add("Priority", "1")
				iProperties.Value(oVirt.DisplayName, "Custom", "Priority") = 1
		End If
	Next

ABoaro_0-1594309638490.png

^ This is what the custom panel looks like for iProperty. Eventually I want to expand the code to detect the routing of a virtual part and then assign an associated priority. As it stands, nothing I have tried with accessing the iProperties for the virtual part has worked.

 

0 Likes
Accepted solutions (3)
656 Views
6 Replies
Replies (6)
Message 2 of 7

ckeveryga
Advocate
Advocate

I'm not super familiar with virtual parts, but I think this code will work the same way as it does with a standard part. 

oCustomPropSet = oOcc.Definition.Document.PropertySets("Inventor User Defined Properties")
Dim intPri As Integer
intPri = 0
Try						
	intPri = oCustomPropSet.Item("Priority").Value
Catch ex As Exception									
	oCustomPropSet.Add(1, "Priority")
End Try

You will need to use a try, catch statement because there is no elegant way (that I know of)  to test if a custom property exists. 

0 Likes
Message 3 of 7

ABoaro
Enthusiast
Enthusiast
Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAssDef As AssemblyComponentDefinition = oAssDoc.ComponentDefinition

Dim oOcc As ComponentOccurrence
For Each oOcc In oAssDef.Occurrences
	If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
		oCustomPropSet = oOcc.Definition.Document.PropertySets("Inventor User Defined Properties")
		Dim intPri As Integer
		intPri = 0
		Try						
			intPri = oCustomPropSet.Item("Priority").Value
		Catch ex As Exception									
			oCustomPropSet.Add(1, "Priority")
			MsgBox("Success")
		End Try
	End If
Next

 I went ahead and used your suggestion. I got the same issue where the code runs fine, but it doesn't actually update the virtual parts iProperties. I am wondering if there is a different niche way to interact with a VirtualComponent definition versus an actual ComponentDefinition 

0 Likes
Message 4 of 7

Anonymous
Not applicable
Accepted solution

You were correct in assuming this is a niche interaction. Accessing the Document from the VirtualComponentDefinition actually references the assembly document, since Virtual Parts are built into the assembly. So you were actually adding a custom property to your assembly. The Virtual Parts iproperties  are actually referenced directly from the definition so just remove the .document from

oCustomPropSet = oOcc.Definition.Document.PropertySets("Inventor User Defined Properties")

So you get

oCustomPropSet = oOcc.Definition.PropertySets("Inventor User Defined Properties")

 and it should work

Message 5 of 7

ckeveryga
Advocate
Advocate
Accepted solution

Got it! Just need to remove "document" from the line that defines the property set. 

 

Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAssDef As AssemblyComponentDefinition = oAssDoc.ComponentDefinition

Dim oOcc As ComponentOccurrence
For Each oOcc In oAssDef.Occurrences
	If TypeOf oOcc.Definition Is VirtualComponentDefinition Then
		oCustomPropSet = oOcc.Definition.PropertySets("Inventor User Defined Properties")
		Dim intPri As Integer
		intPri = 0
		Try						
			intPri = oCustomPropSet.Item("Priority").Value
		Catch ex As Exception									
			oCustomPropSet.Add(1, "Priority")
			MsgBox("Success")
		End Try
	End If
Next

 

Message 6 of 7

ABoaro
Enthusiast
Enthusiast

The fix worked! Thank you for also confirming my belief that virtual parts behave differently.

 

So by extension, I should be able to access say the "Routing" property from a virtual part via a niche interaction?

 

I have tried just doing:

 

Dim oRouting As String = iProperties.Value(oOcc.Definition.DisplayName,"Custom","Routing")

This didn't seem to work. I presume I need to access the virtual part properties in another way.

0 Likes
Message 7 of 7

ckeveryga
Advocate
Advocate
Accepted solution

You cannot access the properties like you would from a document level using iProperties, you need to access them using the occurrence definition. Project properties are defined like so:

oDesignTrackPropSet = oOcc.Definition.PropertySets("Design Tracking Properties")

 then accessed the same way as the custom iProperties. 

Summary information like so:

oInventorSummaryInfo = oOcc.Definition.PropertySets("Inventor Summary Information")

 

0 Likes