iLogic to create custom iProperty for certain parts of assembly

iLogic to create custom iProperty for certain parts of assembly

jishee
Enthusiast Enthusiast
2,511 Views
4 Replies
Message 1 of 5

iLogic to create custom iProperty for certain parts of assembly

jishee
Enthusiast
Enthusiast

I want to create a custom iProperty for parts that have an already defined custom iProperty called "Routing". Below is the code I have so far. Every time I run it I get an error message that says "Cannot find a property named "Routing." I know that there are parts in the assembly with the "Routing" property in them. Any suggestions would be appreciated.

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Conversion factor cm to in
Dim oCF As Double = 0.393701

Dim oX As Double = 0
Dim oZ As Double = 0

'Iterate through all Of the occurrences
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef) 
	
	'Check for infills and skip virtual components
	If iProperties.Value(oOcc.Name, "Custom", "Routing") = "_I" And Not TypeOf oOcc.Definition Is VirtualComponentDefinition Then
			
	  	oX = (oOcc.Definition.RangeBox.MaxPoint.X - oOcc.Definition.RangeBox.MinPoint.X)*oCF
		oZ = (oOcc.Definition.RangeBox.MaxPoint.Z - oOcc.Definition.RangeBox.MinPoint.Z) * oCF
		
		Try 'write to component iprops	
			iProperties.Value(oOcc.Name, "Custom", "Width") = iProperties.Value("Custom", "Width")
			iProperties.Value(oOcc.Name, "Custom", "Height") = iProperties.Value("Custom", "Height")
		Catch 'catch errors
			'create iprop with default value
			iProperties.Value("Custom", "Width") = Round(oZ, 3)
			iProperties.Value("Custom", "Height") = Round(oX, 3)
			'write to component iprops
			iProperties.Value(oOcc.Name, "Custom", "Width") = iProperties.Value("Custom", "Width")
			iProperties.Value(oOcc.Name, "Custom", "Height") = iProperties.Value("Custom", "Height")
		End Try
	End If
Next

 

 

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

WCrihfield
Mentor
Mentor

Try it this way, and see if it works.  I put an extra loop in there to check if the custom iProperty exists or not.  If you're just creating a new custom iProperty, or are just changing the value of an existing custom iProperty, using the iProperties.Value() method is fine, but if you're just trying to check the value of a custom iProperty that may not be there, you either need to use a Try...Catch statement around it, or search for it first, so it won't throw an error when not found.

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Conversion factor cm to in
Dim oCF As Double = 0.393701

Dim oX As Double = 0
Dim oZ As Double = 0

'Iterate through all Of the occurrences
Dim oOcc As ComponentOccurrence
Dim oOccDoc As Document
Dim oPropSet As PropertySet
Dim oProp As [Property]
For Each oOcc In oAsmCompDef.Occurrences
	'Check for infills and skip virtual components
	If oOcc.Definition.Type <> ObjectTypeEnum.kVirtualComponentDefinitionObject Then
		oOccDoc = oOcc.Definition.Document
		oPropSet = oOccDoc.PropertySets.Item("Inventor User Defined Properties")
		For Each oProp In oPropSet
			If oProp.Name = "Routing" Then
				If oProp.Value = "_I" Then
					oX = (oOcc.Definition.RangeBox.MaxPoint.X - oOcc.Definition.RangeBox.MinPoint.X) * oCF
					oZ = (oOcc.Definition.RangeBox.MaxPoint.Z - oOcc.Definition.RangeBox.MinPoint.Z) * oCF
					Try 'write to component iprops	
						iProperties.Value(oOcc.Name, "Custom", "Width") = iProperties.Value("Custom", "Width")
						iProperties.Value(oOcc.Name, "Custom", "Height") = iProperties.Value("Custom", "Height")
					Catch 'catch errors
						'create iprop with default value
						iProperties.Value("Custom", "Width") = Round(oZ, 3)
						iProperties.Value("Custom", "Height") = Round(oX, 3)
						'write to component iprops
						iProperties.Value(oOcc.Name, "Custom", "Width") = iProperties.Value("Custom", "Width")
						iProperties.Value(oOcc.Name, "Custom", "Height") = iProperties.Value("Custom", "Height")
					End Try
				End If
			End If
		Next
	End If
Next

Also, not sure if this will work for this situation or not, but it may be faster or more efficiant to loop through Each oRefDoc As Document In oAssemblyDoc.AllReferencedDocuments to do this.  It doesn't accound for VirtualComponents, but they probably don't have externally saved files.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

jishee
Enthusiast
Enthusiast

@WCrihfield  thanks for your help. The fixes you made run without error and add the width and height properties like expected but do no fill in any values. I am missing something with the range box? I just want the dimensions in the X and Z directions for the parts.

0 Likes
Message 4 of 5

marcin_otręba
Advisor
Advisor
Accepted solution

try this code:

 

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Conversion factor cm to in
Dim oCF As Double = 0.393701

Dim oX As Double = 0
Dim oZ As Double = 0

'Iterate through all Of the occurrences
Dim oOcc As ComponentOccurrence
Dim oOccDoc As Document
Dim oPropSet As PropertySet
Dim oProp As [Property]
For Each oOcc In oAsmCompDef.Occurrences
	'Check for infills and skip virtual components
	If oOcc.Definition.Type <> ObjectTypeEnum.kVirtualComponentDefinitionObject Then
		oOccDoc = oOcc.Definition.Document
		oPropSet = oOccDoc.PropertySets.Item("Inventor User Defined Properties")
		For Each oProp In oPropSet
			If oProp.Name = "Routing" Then
				If oProp.Value = "_I" Then
					oX = (oOcc.Definition.RangeBox.MaxPoint.X - oOcc.Definition.RangeBox.MinPoint.X) * oCF
					oZ = (oOcc.Definition.RangeBox.MaxPoint.Z - oOcc.Definition.RangeBox.MinPoint.Z) * oCF
					Try 'write to component iprops	
						oPropSet.item("Width1").Value = Round(oZ, 3)
						oPropSet.item("Height1").Value= Round(oX, 3)
					Catch 'catch errors
						'create iprop with default value
						      oPropSet.Add(Round(oZ, 3),  "Width1") 
						      oPropSet.Add(Round(oX, 3),  "Height1")  

					End Try
				End If
			End If
		Next
	End If
Next

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 5 of 5

jishee
Enthusiast
Enthusiast

That works as expected. Thank you for your help @WCrihfield  and @marcin_otręba !

0 Likes