Run Rule for all Subbassebly Files in Inventor

Run Rule for all Subbassebly Files in Inventor

vkulikajevas
Advocate Advocate
544 Views
2 Replies
Message 1 of 3

Run Rule for all Subbassebly Files in Inventor

vkulikajevas
Advocate
Advocate

Hello, I am trying to make a BoundingBox Rule for all subassembly files. It takes extentions and places them in order of Length, Width and Thickness. But I want to run this rule for all subassembly files (Assemblies and Parts). I added rules for that, but so far, they still only work for current Assembly or part. Can you help me make this rule work?

The main rule starts at "Sub BoundingBox". I tried messing up later with different rules, but so far no success.

Maybe the code should open part and run the BoundingBox rule from within to work? and then close it and repeat for other parts?

 

Sub Main
	
	If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub
	
	Dim parentDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	'Do anything unique to main assembly

	Call AssemblyRunner(parentDoc)
	
End Sub

Sub AssemblyRunner(aDoc As AssemblyDocument)
	
	'Do things to the assembly file if any
	'MessageBox.Show(aDoc.DisplayName, "Assembly:")
	
        'Start Looping
	For Each oOcc As ComponentOccurrence In aDoc.ComponentDefinition.Occurrences
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject
			Call AssemblyRunner(oOcc.Definition.Document)
			Call BoundingBox
		Else If oOcc.DefinitionDocumentType = kPartDocumentObject
			Call PartRunner(oOcc.Definition.Document, aDoc)
		End If
	Next
	
End Sub

Sub PartRunner(pDoc As PartDocument, parent As AssemblyDocument)
	
	'Do Part things
	'MessageBox.Show(pDoc.DisplayName & vbCrLf & vbCrLf & _
	'				"Found in Assembly: " & parent.DisplayName, "Part:")
	Call BoundingBox
End Sub

Sub BoundingBox
	
'Get Component bounding box measurements
Model_X=Round(Measure.ExtentsLength) 'X-axis BB length
Model_Y=Round(Measure.ExtentsWidth)  'Y-axis BB length
Model_Z=Round(Measure.ExtentsHeight) 'Z-axis BB length'Add / Update Bounding Box measurements to model's Custom Properties

If Model_X >= Model_Y And Model_Y >= Model_Z Then 
iProperties.Value("Custom", "DIM") = Model_X & "x" & Model_Y & "x" & Model_Z
iProperties.Value("Custom", "Ilgis")=Model_X
iProperties.Value("Custom", "Plotis")=Model_Y
iProperties.Value("Custom", "Storis")=Model_Z

ElseIf Model_X >= Model_Z And Model_Z >= Model_Y Then 
iProperties.Value("Custom", "DIM") = Model_X & "x" & Model_Z & "x" & Model_Y
iProperties.Value("Custom", "Ilgis")=Model_X
iProperties.Value("Custom", "Plotis")=Model_Z
iProperties.Value("Custom", "Storis")=Model_Y

ElseIf Model_Y >= Model_X And Model_X >= Model_Z Then
iProperties.Value("Custom", "DIM") = Model_Y & "x" & Model_X & "x" & Model_Z
iProperties.Value("Custom", "Ilgis")=Model_Y
iProperties.Value("Custom", "Plotis")=Model_X
iProperties.Value("Custom", "Storis")=Model_Z

ElseIf Model_Y >= Model_Z And Model_Z >= Model_X Then
iProperties.Value("Custom", "DIM") = Model_Y & "x" & Model_Z & "x" & Model_X
iProperties.Value("Custom", "Ilgis")=Model_Y
iProperties.Value("Custom", "Plotis")=Model_Z
iProperties.Value("Custom", "Storis")=Model_X

ElseIf Model_Z >= Model_X And Model_X >= Model_Y Then
iProperties.Value("Custom", "DIM") = Model_Z & "x" & Model_X & "x" & Model_Y
iProperties.Value("Custom", "Ilgis")=Model_Z
iProperties.Value("Custom", "Plotis")=Model_X
iProperties.Value("Custom", "Storis")=Model_Y

ElseIf Model_Z >= Model_Y And Model_Y >= Model_X Then
iProperties.Value("Custom", "DIM") = Model_Z & "x" & Model_Y & "x" & Model_X
iProperties.Value("Custom", "Ilgis")=Model_Z
iProperties.Value("Custom", "Plotis")=Model_Y
iProperties.Value("Custom", "Storis")=Model_X

End If


'ThisDoc.Save
'ThisDoc.Document.Close(True)

End Sub

 

0 Likes
545 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

Hello @vkulikajevas . Try this code, just update your "if" statements.

Please let me know if it works for you.

Regards.

 

Sub Main ()

Dim oCompDef As AssemblyComponentDefinition

For Each oDoc As Document In ThisApplication.ActiveDocument.ReferencedDocuments
	Dim oFileName As String = oDoc.FullFileName
	oRefDoc = ThisApplication.Documents.Open(oFileName,True)
	Call BoundingBox()
	oRefDoc.Close	
Next

iLogicVb.UpdateWhenDone = True
MsgBox("Done!")

End Sub


Sub BoundingBox()
Dim oDoc As Document = ThisApplication.ActiveDocument	
Dim oCompDef As ComponentDefinition = oDoc.ComponentDefinition


'[ ADD CUSTOM PROPERTIES

customPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Try
      pDIM = customPropertySet.Item("DIM")
Catch
      ' Assume error means not found
      customPropertySet.Add("", "DIM")
End Try


Try
      pIlgis = customPropertySet.Item("Ilgis")
Catch
      ' Assume error means not found
      customPropertySet.Add("", "Ilgis")
End Try


Try
      pPlotis = customPropertySet.Item("Plotis")
Catch
      ' Assume error means not found
      customPropertySet.Add("", "Plotis")
End Try


Try
      pStoris = customPropertySet.Item("Storis")
Catch
      ' Assume error means not found
      customPropertySet.Add("", "Storis")
End Try
']




Dim minPoint As Point = oCompDef.RangeBox.MinPoint
Dim maxPoint As Point = oCompDef.RangeBox.MaxPoint

Dim uom As UnitsOfMeasure = oDoc.UnitsOfMeasure
Dim Units As String = uom.GetStringFromType(uom.LengthUnits)

Dim X As Double = uom.ConvertUnits ((maxPoint.X - minPoint.X), "cm", uom.LengthUnits)
Dim Y As Double = uom.ConvertUnits ((maxPoint.Y - minPoint.Y), "cm", uom.LengthUnits)
Dim Z As Double = uom.ConvertUnits ((maxPoint.Z - minPoint.Z), "cm", uom.LengthUnits)

Length = Round(MaxOfMany (X,Y,Z),2)
Width = Round( X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z),2)
Thickness = Round(MinOfMany (X,Y,Z),2)
	
'Get Component bounding box measurements
'Please double check if these equevalents are correct. Model_X = Length 'Round(Measure.ExtentsLength) 'X-axis BB length Model_Y= Width 'Round(Measure.ExtentsWidth) 'Y-axis BB length Model_Z = Thickness 'Round(Measure.ExtentsHeight) 'Z-axis BB length'Add / Update Bounding Box measurements to model's Custom Properties If Model_X >= Model_Y And Model_Y >= Model_Z Then customPropertySet.Item("DIM").Value = Model_X & "x" & Model_Y & "x" & Model_Z customPropertySet.Item("Ilgis").Value = Model_X customPropertySet.Item("Plotis").Value = Model_Y customPropertySet.Item("Storis").Value = Model_Z 'ElseIf Model_X >= Model_Z And Model_Z >= Model_Y Then 'iProperties.Value("Custom", "DIM") = Model_X & "x" & Model_Z & "x" & Model_Y 'iProperties.Value("Custom", "Ilgis")=Model_X 'iProperties.Value("Custom", "Plotis")=Model_Z 'iProperties.Value("Custom", "Storis")=Model_Y 'ElseIf Model_Y >= Model_X And Model_X >= Model_Z Then 'iProperties.Value("Custom", "DIM") = Model_Y & "x" & Model_X & "x" & Model_Z 'iProperties.Value("Custom", "Ilgis")=Model_Y 'iProperties.Value("Custom", "Plotis")=Model_X 'iProperties.Value("Custom", "Storis")=Model_Z 'ElseIf Model_Y >= Model_Z And Model_Z >= Model_X Then 'iProperties.Value("Custom", "DIM") = Model_Y & "x" & Model_Z & "x" & Model_X 'iProperties.Value("Custom", "Ilgis")=Model_Y 'iProperties.Value("Custom", "Plotis")=Model_Z 'iProperties.Value("Custom", "Storis")=Model_X 'ElseIf Model_Z >= Model_X And Model_X >= Model_Y Then 'iProperties.Value("Custom", "DIM") = Model_Z & "x" & Model_X & "x" & Model_Y 'iProperties.Value("Custom", "Ilgis")=Model_Z 'iProperties.Value("Custom", "Plotis")=Model_X 'iProperties.Value("Custom", "Storis")=Model_Y 'ElseIf Model_Z >= Model_Y And Model_Y >= Model_X Then 'iProperties.Value("Custom", "DIM") = Model_Z & "x" & Model_Y & "x" & Model_X 'iProperties.Value("Custom", "Ilgis")=Model_Z 'iProperties.Value("Custom", "Plotis")=Model_Y 'iProperties.Value("Custom", "Storis")=Model_X End If End Sub

 

0 Likes
Message 3 of 3

vkulikajevas
Advocate
Advocate

Thank you, it works. 

0 Likes