Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've created a rule that creates a bounding box for each component in an assembly and puts the dimensions inside of custom iProperties.
It currently causes an error when I run it with content center components like bolts and nuts in the assembly but the rule runs normally for the rest of the assembly+sub-assemblies.
How do I fix this ?
' iLogic Rule to calculate and store maximum dimensions in separate custom iProperties (in mm) Sub Main() Dim oAsmDoc As Document = ThisApplication.ActiveDocument ' Check if the active document is an assembly If oAsmDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim oAsmDef As AssemblyComponentDefinition = CType(oAsmDoc.ComponentDefinition, AssemblyComponentDefinition) ' Iterate through all occurrences in the assembly For Each oOccurrence As ComponentOccurrence In oAsmDef.Occurrences ProcessComponent(oOccurrence) Next ' Update the assembly to reflect changes oAsmDoc.Update() Else MessageBox.Show("This rule is intended for Assembly documents only.", "Rule Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub ' Function to add or update a custom property with a given name and value Sub AddOrUpdateCustomProperty(oDoc As Document, propertyName As String, propertyValue As Object) Try Dim customPropertiesSet As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties") Dim existingProperty As Inventor.Property = customPropertiesSet.Item(propertyName) ' If the property exists, update its value existingProperty.Value = propertyValue Catch ex As Exception ' If the property doesn't exist, create it with a default value "" Dim newProperty As Inventor.Property = oDoc.PropertySets.Item("Inventor User Defined Properties").Add("", propertyName) newProperty.Value = propertyValue End Try End Sub ' Recursive function to process components and sub-assemblies Sub ProcessComponent(oOccurrence As ComponentOccurrence) Try If oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then ' If the occurrence is a part Dim oPartDoc As PartDocument = TryCast(oOccurrence.Definition.Document, PartDocument) If oPartDoc IsNot Nothing Then ' Get the bounding box of the component Dim oBoundingBox As Box = oOccurrence.RangeBox ' Calculate dimensions in mm Dim componentWidth As Double = (oBoundingBox.MaxPoint.X - oBoundingBox.MinPoint.X) * 10 ' Convert to mm Dim componentLength As Double = (oBoundingBox.MaxPoint.Y - oBoundingBox.MinPoint.Y) * 10 ' Convert to mm Dim componentHeight As Double = (oBoundingBox.MaxPoint.Z - oBoundingBox.MinPoint.Z) * 10 ' Convert to mm ' Store dimensions in separate custom iProperties with default value "" AddOrUpdateCustomProperty(oPartDoc, "MaxWidth", componentWidth) AddOrUpdateCustomProperty(oPartDoc, "MaxLength", componentLength) AddOrUpdateCustomProperty(oPartDoc, "MaxHeight", componentHeight) End If ElseIf oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then ' If the occurrence is a sub-assembly Dim oSubAsmDoc As AssemblyDocument = TryCast(oOccurrence.Definition.Document, AssemblyDocument) If oSubAsmDoc IsNot Nothing Then ' Create a bounding box for the sub-assembly Dim oSubAsmBoundingBox As Box = oOccurrence.RangeBox ' Calculate dimensions in mm Dim subAsmWidth As Double = (oSubAsmBoundingBox.MaxPoint.X - oSubAsmBoundingBox.MinPoint.X) * 10 ' Convert to mm Dim subAsmLength As Double = (oSubAsmBoundingBox.MaxPoint.Y - oSubAsmBoundingBox.MinPoint.Y) * 10 ' Convert to mm Dim subAsmHeight As Double = (oSubAsmBoundingBox.MaxPoint.Z - oSubAsmBoundingBox.MinPoint.Z) * 10 ' Convert to mm ' Store dimensions in separate custom iProperties with default value "" AddOrUpdateCustomProperty(oSubAsmDoc, "MaxWidth", subAsmWidth) AddOrUpdateCustomProperty(oSubAsmDoc, "MaxLength", subAsmLength) AddOrUpdateCustomProperty(oSubAsmDoc, "MaxHeight", subAsmHeight) ' Iterate through all occurrences in the sub-assembly For Each oSubOccurrence As ComponentOccurrence In oOccurrence.SubOccurrences ProcessComponent(oSubOccurrence) Next End If End If Catch ex As Exception MessageBox.Show("Error processing component: " & ex.Message, "Rule Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
Solved! Go to Solution.