Run the same Ilogic script from assembly level

Run the same Ilogic script from assembly level

hamid_fibo
Observer Observer
965 Views
4 Replies
Message 1 of 5

Run the same Ilogic script from assembly level

hamid_fibo
Observer
Observer

Hello, dear community,
The attached script works as fine as needed at the part level, it's used to get parts bounding boxes' dimensions and push data to iproperties. What I want is that I would be able to run this same script from the assembly level for all assembly parts at once. 
Any help would be appreciated. 
Thanks.

 

' Get the current Part document.
Dim partDoc As PartDocument = ThisDoc.Document

' Get the TransientBRep and TransientGeometry objects.
Dim transBRep As TransientBRep = ThisApplication.TransientBRep
Dim transGeom As TransientGeometry = ThisApplication.TransientGeometry

' Combine all bodies in Part into a single transient Surface Body.
Dim combinedBodies As SurfaceBody = Nothing
For Each surfBody As SurfaceBody In partDoc.ComponentDefinition.SurfaceBodies
	If combinedBodies Is Nothing Then
		combinedBodies = transBRep.Copy(surfBody)
	Else
		transBRep.DoBoolean(combinedBodies, surfBody, BooleanTypeEnum.kBooleanTypeUnion)
	End If
Next

' Get the oriented mininum range box of all bodies in Part.
' NOTE: "OrientedMinimumRangeBox" was added in Inventor 2020.3/2021.
Dim minBox As OrientedBox = combinedBodies.OrientedMinimumRangeBox

' Get length of each side of mininum range box.
Dim dir1 As Double = minBox.DirectionOne.Length
Dim dir2 As Double = minBox.DirectionTwo.Length
Dim dir3 As Double = minBox.DirectionThree.Length

' Convert lengths to document's length units.
Dim uom As UnitsOfMeasure = partDoc.UnitsOfMeasure

dir1 = uom.ConvertUnits(dir1, "cm", uom.LengthUnits)
dir2 = uom.ConvertUnits(dir2, "cm", uom.LengthUnits)
dir3 = uom.ConvertUnits(dir3, "cm", uom.LengthUnits)

' Sort lengths from smallest to largest.
Dim lengths As New List(Of Double) From {dir1, dir2, dir3 }
lengths.Sort

Dim minLength As Double = lengths(0)
Dim midLength As Double = lengths(1)
Dim maxLength As Double = lengths(2)

iProperties.Value("Custom", "ModelX")= minLength
iProperties.Value("Custom", "ModelY")= midLength
iProperties.Value("Custom", "ModelZ")= maxLength

' Create surface to represent oriented range box...

' Create starting box with dimensions of oriented rangebox.
Dim startBox As Box = transGeom.CreateBox()
Dim boxMaxPoint As Point = transGeom.CreatePoint(minBox.DirectionOne.Length, minBox.DirectionTwo.Length, minBox.DirectionThree.Length)
startBox.Extend(boxMaxPoint)

' Create transformation matrix to move box to correct location/orientation.
Dim transMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
transMatrix.SetCoordinateSystem(minBox.CornerPoint, minBox.DirectionOne.AsUnitVector.AsVector, minBox.DirectionTwo.AsUnitVector.AsVector, minBox.DirectionThree.AsUnitVector.AsVector)

' Create surface body for the range box.
Dim minBoxSurface As SurfaceBody = ThisApplication.TransientBRep.CreateSolidBlock(startBox)

' Transform range box surface body to the correct location/orientation.
ThisApplication.TransientBRep.Transform(minBoxSurface, transMatrix)

' Display range box on screen.
Dim transac As Transaction = ThisApplication.TransactionManager.StartTransaction(partDoc, "DisplayRangeBox")

Dim cGraphics As ClientGraphics

Try
	cGraphics = partDoc.ComponentDefinition.ClientGraphicsCollection.Add("OrientedRangeBox")
	Dim surfacesNode As GraphicsNode = cGraphics.AddNode(1)
	Dim surfGraphics As SurfaceGraphics = surfacesNode.AddSurfaceGraphics(minBoxSurface)
	Dim targetAppearance As Asset
	
	Try
		targetAppearance = partDoc.Assets.Item("Clear - Blue")
	Catch
		Dim sourceAppearance As Asset = ThisApplication.AssetLibraries.Item("314DE259-5443-4621-BFBD-1730C6CC9AE9").AppearanceAssets.Item("InvGen-001-1-2") ' "Clear - Blue"
		targetAppearance = sourceAppearance.CopyTo(partDoc)
	End Try
	
	surfacesNode.Appearance = targetAppearance

	ThisApplication.ActiveView.Update

	' Display message with minimum rangebox size.
	MessageBox.Show("Oriented Minimum Rangebox Size: " &
	minLength.ToString("#.###") & " x " & midLength.ToString("#.###") & " x " & maxLength.ToString("#.###"),
	"Oriented Minimum Rangebox", MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
	' Remove range box from screen.
	transac.Abort
	ThisApplication.ActiveView.Update
End Try

 

 

0 Likes
966 Views
4 Replies
Replies (4)
Message 2 of 5

Ralf_Krieg
Advisor
Advisor

Hello

 

Then you need to traverse through the referenced documents and proceed all part files.

Dim oAssDoc As AssemblyDocument = ThisDoc.Document

Dim oDoc As Document
For Each oDoc In oAssDoc.AllReferencedDocuments
	If Not oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For
		
	' Get the current Part document.
	Dim partDoc As PartDocument = oDoc 'ThisDoc.Document
	
	' Get the TransientBRep and TransientGeometry objects.
	Dim transBRep As TransientBRep = ThisApplication.TransientBRep
	Dim transGeom As TransientGeometry = ThisApplication.TransientGeometry
	
	' Combine all bodies in Part into a single transient Surface Body.
	Dim combinedBodies As SurfaceBody = Nothing
	For Each surfBody As SurfaceBody In partDoc.ComponentDefinition.SurfaceBodies
		If combinedBodies Is Nothing Then
			combinedBodies = transBRep.Copy(surfBody)
		Else
			transBRep.DoBoolean(combinedBodies, surfBody, BooleanTypeEnum.kBooleanTypeUnion)
		End If
	Next
	
	' Get the oriented mininum range box of all bodies in Part.
	' NOTE: "OrientedMinimumRangeBox" was added in Inventor 2020.3/2021.
	Dim minBox As OrientedBox = combinedBodies.OrientedMinimumRangeBox
	
	' Get length of each side of mininum range box.
	Dim dir1 As Double = minBox.DirectionOne.Length
	Dim dir2 As Double = minBox.DirectionTwo.Length
	Dim dir3 As Double = minBox.DirectionThree.Length
	
	' Convert lengths to document's length units.
	Dim uom As UnitsOfMeasure = partDoc.UnitsOfMeasure
	
	dir1 = uom.ConvertUnits(dir1, "cm", uom.LengthUnits)
	dir2 = uom.ConvertUnits(dir2, "cm", uom.LengthUnits)
	dir3 = uom.ConvertUnits(dir3, "cm", uom.LengthUnits)
	
	' Sort lengths from smallest to largest.
	Dim lengths As New List(Of Double) From {dir1, dir2, dir3 }
	lengths.Sort
	
	Dim minLength As Double = lengths(0)
	Dim midLength As Double = lengths(1)
	Dim maxLength As Double = lengths(2)
	
	iProperties.Value("Custom", "ModelX")= minLength
	iProperties.Value("Custom", "ModelY")= midLength
	iProperties.Value("Custom", "ModelZ")= maxLength
	
	' Create surface to represent oriented range box...
	
	' Create starting box with dimensions of oriented rangebox.
	Dim startBox As Box = transGeom.CreateBox()
	Dim boxMaxPoint As Point = transGeom.CreatePoint(minBox.DirectionOne.Length, minBox.DirectionTwo.Length, minBox.DirectionThree.Length)
	startBox.Extend(boxMaxPoint)
	
	' Create transformation matrix to move box to correct location/orientation.
	Dim transMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
	transMatrix.SetCoordinateSystem(minBox.CornerPoint, minBox.DirectionOne.AsUnitVector.AsVector, minBox.DirectionTwo.AsUnitVector.AsVector, minBox.DirectionThree.AsUnitVector.AsVector)
	
	' Create surface body for the range box.
	Dim minBoxSurface As SurfaceBody = ThisApplication.TransientBRep.CreateSolidBlock(startBox)
	
	' Transform range box surface body to the correct location/orientation.
	ThisApplication.TransientBRep.Transform(minBoxSurface, transMatrix)
	
	' Display range box on screen.
	Dim transac As Transaction = ThisApplication.TransactionManager.StartTransaction(partDoc, "DisplayRangeBox")
	
	Dim cGraphics As ClientGraphics
	
	Try
		cGraphics = partDoc.ComponentDefinition.ClientGraphicsCollection.Add("OrientedRangeBox")
		Dim surfacesNode As GraphicsNode = cGraphics.AddNode(1)
		Dim surfGraphics As SurfaceGraphics = surfacesNode.AddSurfaceGraphics(minBoxSurface)
		Dim targetAppearance As Asset
		
		Try
			targetAppearance = partDoc.Assets.Item("Clear - Blue")
		Catch
			Dim sourceAppearance As Asset = ThisApplication.AssetLibraries.Item("314DE259-5443-4621-BFBD-1730C6CC9AE9").AppearanceAssets.Item("InvGen-001-1-2") ' "Clear - Blue"
			targetAppearance = sourceAppearance.CopyTo(partDoc)
		End Try
		
		surfacesNode.Appearance = targetAppearance
	
		ThisApplication.ActiveView.Update
	
		' Display message with minimum rangebox size.
		MessageBox.Show("Oriented Minimum Rangebox Size: " &
		minLength.ToString("#.###") & " x " & midLength.ToString("#.###") & " x " & maxLength.ToString("#.###"),
		"Oriented Minimum Rangebox", MessageBoxButtons.OK, MessageBoxIcon.Information)
	Finally
		' Remove range box from screen.
		transac.Abort
		ThisApplication.ActiveView.Update
	End Try
	
Next

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 3 of 5

hilde.vanderhaeghe
Participant
Participant

This code gives the bbox for each part in the assy.

I would like the bounding box for the total assembly.

Any idea?

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Hi @hilde.vanderhaeghe.  The main assembly itself does not have an OrientedMinimumRangeBox property, just the regular RangeBox property, so my example below is just using that regular RangeBox property, and reporting its findings in a MsgBox.  I am also a few lines of code to convert the default 'database units' (centimeters) to inches, but you can change that to whatever units you want, if those are not the units you prefer.

 

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oBox As Box = oADef.RangeBox
Dim oMinP As Point = oBox.MinPoint
Dim oMaxP As Point = oBox.MaxPoint
Dim dX As Double = oMaxP.X - oMinP.X
Dim dY As Double = oMaxP.Y - oMinP.Y
Dim dZ As Double = oMaxP.Z - oMinP.Z
'convert units from database units (centimeters) to inches
UOM = oADoc.UnitsOfMeasure
dX = UOM.ConvertUnits(dX, "cm", "in")
dY = UOM.ConvertUnits(dY, "cm", "in")
dZ = UOM.ConvertUnits(dZ, "cm", "in")
Dim sBoxSize As String = "Bounding Box Size Of Assembly:" & vbCrLf & _
"X = " & dX & vbCrLf & _
"Y = " & dY & vbCrLf & _
"Z = " & dZ
MsgBox(sBoxSize, vbInformation, "")

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

hilde.vanderhaeghe
Participant
Participant

Thanks for the code but it is not what we are looking for. We produce hoppers, at times divided, and we want to know if they fit into our paintline. Below you see the BBOX from a section (subsitute IPT from an IAM)

hildevanderhaeghe_1-1676474926909.png

 

 

0 Likes