a dynamic box?

a dynamic box?

kim.salvesen
Contributor Contributor
2,868 Views
13 Replies
Message 1 of 14

a dynamic box?

kim.salvesen
Contributor
Contributor

I wonder if you can make ilogic/VBA program.
that works similarly to bounding box, you have in CAM. so you can make a solid that represent the stock based on the part.

 

can you have you have the flexibility to increase the size, on the different sides.

and then you will get a cutting info for the material.

0 Likes
Accepted solutions (1)
2,869 Views
13 Replies
Replies (13)
Message 2 of 14

JelteDeJong
Mentor
Mentor

The easiest function (that has no flexibility) looks like this. Creating a visual representation is more difficult and will add depending on what you want features to your model. is that what you want?

Sub Main()

    Dim doc As PartDocument = ThisDoc.Document
    Dim uom = doc.UnitsOfMeasure

    For Each body As SurfaceBody In doc.ComponentDefinition.SurfaceBodies
        Dim box As Box = body.RangeBox
        Dim x As Double = ConvertToDefault(uom, box.MaxPoint.X - box.MinPoint.X)
        Dim y As Double = ConvertToDefault(uom, box.MaxPoint.Y - box.MinPoint.Y)
        Dim z As Double = ConvertToDefault(uom, box.MaxPoint.Z - box.MinPoint.Z)

        Dim msg = String.Format("Body: '{0}': x:{1} y:{2} z:{3}",
                                body.Name, x, y, z)
        MsgBox(msg)
    Next
End Sub

Private Function ConvertToDefault(uom As UnitsOfMeasure, value As Double)
    Return uom.ConvertUnits(value,
            UnitsTypeEnum.kDatabaseLengthUnits,
            UnitsTypeEnum.kDefaultDisplayLengthUnits)
End Function

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 14

kim.salvesen
Contributor
Contributor
Hello thanks for quick reply
 
I shall try to explain my case, we are a small machine shop
we use Fusion 360 on the cam side.
 
when I do quotation to apart
I need to know how big the stock is going to be, to get the price on the stock and the material.
so this is the main purpose on the application.
 
on the stock we need to make it a little bit bigger.
so this is why I want to be able to adjust the size.
 
and I figure I don't need to do the work two times
that's why I want the solid to represent the stock.
so basically I do all the prep in Inventor
 
then I don't need to do the work again Fusion 360 when I pushed apart to Fusion.
 
this is what I come up with so far.
but in the picture you can see the blue but it is only a visual representation and I can't adjust it.
and here is the code
 
' 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 Integer = lengths(0)
Dim midLength As Integer = lengths(1)
Dim maxLength As Integer = lengths(2)

' 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

iProperties.Value("Custom", "Material") = "Z" & minLength.ToString("#.###") + 5 & " Y" & midLength.ToString("#.###") + 5 & " X" & maxLength.ToString("#.###") + 5
iProperties.Value("Custom", "Min Material") = minLength.ToString("#.###") & " x " & midLength.ToString("#.###") & " x " & maxLength.ToString("#.###")


iProperties.Value("Custom", "X") = minLength.ToString("#.###") 
iProperties.Value("Custom", "Y") = midLength.ToString("#.###") 
iProperties.Value("Custom", "Z") = maxLength.ToString("#.###")


 
and I also try something in vba

Private Sub UserForm_Click()
' Get the active part document.
Dim invPartDoc As PartDocument
Set invPartDoc = ThisApplication.ActiveDocument

' Get the custom property set.
Dim invCustomPropertySet As PropertySet
Set invCustomPropertySet = invPartDoc.PropertySets.Item("Inventor User Defined Properties")
'******
Dim length As Property
Dim width As Property
Dim height As Property
'******
Set length = invCustomPropertySet.Item("X")
Set width = invCustomPropertySet.Item("Y")
Set height = invCustomPropertySet.Item("Z")
'******

'MsgBox length.Value
txtW.Text = length.Value
txtH.Text = width.Value
txtD.Text = height.Value

End Sub

 

 

I Am brand new to inventor so I deeply appreciate the help.
 
 
 
 
 
 
 
0 Likes
Message 4 of 14

JelteDeJong
Mentor
Mentor

does this work for you?

 

Sub Main()
    Dim margin1 = 1 ' cm
    Dim margin2 = 1 ' cm
    Dim margin3 = 1 ' cm

    ' 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

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

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

    ' 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)

    'Dim transMatrix2 As Matrix = ThisApplication.TransientGeometry.CreateMatrix
    'transMatrix2.SetTranslation(transGeom.CreateVector(-margin, -margin, -margin))
    'transMatrix.TransformBy(transMatrix2)


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


    Dim cGraphics As ClientGraphics = CreateBox(partDoc, minBoxSurface)


    ' 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, UnitsTypeEnum.kDatabaseLengthUnits, uom.LengthUnits)
    dir2 = uom.ConvertUnits(dir2, UnitsTypeEnum.kDatabaseLengthUnits, uom.LengthUnits)
    dir3 = uom.ConvertUnits(dir3, UnitsTypeEnum.kDatabaseLengthUnits, uom.LengthUnits)

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

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

    ' 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)

    iProperties.Value("Custom", "Material") = "Z" & minLength.ToString("#.###") + 5 & " Y" & midLength.ToString("#.###") + 5 & " X" & maxLength.ToString("#.###") + 5
    iProperties.Value("Custom", "Min Material") = minLength.ToString("#.###") & " x " & midLength.ToString("#.###") & " x " & maxLength.ToString("#.###")


    iProperties.Value("Custom", "X") = minLength.ToString("#.###")
    iProperties.Value("Custom", "Y") = midLength.ToString("#.###")
    iProperties.Value("Custom", "Z") = maxLength.ToString("#.###")

    cGraphics.Delete()

End Sub


Private Function CreateBox(partDoc As Document, surface As Object) As ClientGraphics
    Dim cGraphics As ClientGraphics

    cGraphics = partDoc.ComponentDefinition.ClientGraphicsCollection.Add("OrientedRangeBox")
    Dim surfacesNode As GraphicsNode = cGraphics.AddNode(1)
    Dim surfGraphics As SurfaceGraphics = surfacesNode.AddSurfaceGraphics(surface)
    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()

    Return cGraphics
End Function

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 14

kim.salvesen
Contributor
Contributor

hello and thank you.

 

it is absolutely a step in the right direction.

I need the blue to be a solid not just a presentation.

if I can make a solid Bounding box, then we get big step on the way 

 

0 Likes
Message 6 of 14

JelteDeJong
Mentor
Mentor
Accepted solution

try this:

Dim margin1 = 1 ' cm
Dim margin2 = 1 ' cm
Dim margin3 = 1 ' cm

' 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

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

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

' 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)

'Dim transMatrix2 As Matrix = ThisApplication.TransientGeometry.CreateMatrix
'transMatrix2.SetTranslation(transGeom.CreateVector(-margin, -margin, -margin))
'transMatrix.TransformBy(transMatrix2)


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


' Create a base body feature of the result.
Dim nonParamFeatures As NonParametricBaseFeatures = partDoc.ComponentDefinition.Features.NonParametricBaseFeatures
Dim nonParamDef As NonParametricBaseFeatureDefinition = nonParamFeatures.CreateDefinition
Dim tObjs As TransientObjects = ThisApplication.TransientObjects
Dim objs As ObjectCollection = tObjs.CreateObjectCollection
objs.Add(minBoxSurface)
nonParamDef.BRepEntities = objs
nonParamDef.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType
nonParamFeatures.AddByDefinition(nonParamDef)


' 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, UnitsTypeEnum.kDatabaseLengthUnits, uom.LengthUnits)
dir2 = uom.ConvertUnits(dir2, UnitsTypeEnum.kDatabaseLengthUnits, uom.LengthUnits)
dir3 = uom.ConvertUnits(dir3, UnitsTypeEnum.kDatabaseLengthUnits, uom.LengthUnits)

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

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

' 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)

iProperties.Value("Custom", "Material") = "Z" & minLength.ToString("#.###") + 5 & " Y" & midLength.ToString("#.###") + 5 & " X" & maxLength.ToString("#.###") + 5
iProperties.Value("Custom", "Min Material") = minLength.ToString("#.###") & " x " & midLength.ToString("#.###") & " x " & maxLength.ToString("#.###")


iProperties.Value("Custom", "X") = minLength.ToString("#.###")
iProperties.Value("Custom", "Y") = midLength.ToString("#.###")
iProperties.Value("Custom", "Z") = maxLength.ToString("#.###")

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 7 of 14

kim.salvesen
Contributor
Contributor
Thank you very much it helps so much 😃
0 Likes
Message 8 of 14

Victor.Tuguinay
Enthusiast
Enthusiast

Hi Jelte!

I am trying to use this "dynamic box" rule to get the overall dimensions of an irregular part and it works perfectly for that purpose. In addition to this, I wanted to detail the part and the oriented view shows that the points I arrowed to need to be horizontal so I can get dimensions as calculated by this "dynamic box" rule. 

 

To do that, I came up with this rule that lets the user pick the drawing view and is supposed to let user pick the first and second points and will calculate the angle of rotation for the drawing view. However, it is not letting me pick the points. Can you please evaluate and maybe suggest a fix? Thank you in advance!

 

Sub Main()
    ' Get the active drawing document
    Dim oDrawingDoc As DrawingDocument
    oDrawingDoc = ThisApplication.ActiveDocument
    
    ' Prompt the user to select the drawing view
    Dim oView As DrawingView
    On Error Resume Next
    oView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, "Select the drawing view to rotate")
    On Error GoTo 0
    If oView Is Nothing Then
        MsgBox("Drawing view selection was cancelled or invalid.", vbExclamation)
        Exit Sub
    End If
    
    ' Prompt the user to select the first point
    Dim oPoint1 As Point2d
    On Error Resume Next
    oPoint1 = ThisApplication.CommandManager.Pick(kDrawingPointFilter, "Select the first point")
    On Error GoTo 0
    If oPoint1 Is Nothing Then
        MsgBox("First point selection was cancelled or invalid.", vbExclamation)
        Exit Sub
    End If
    
    ' Prompt the user to select the second point
    Dim oPoint2 As Point2d
    On Error Resume Next
    oPoint2 = ThisApplication.CommandManager.Pick(kDrawingPointFilter, "Select the second point")
    On Error GoTo 0
    If oPoint2 Is Nothing Then
        MsgBox("Second point selection was cancelled or invalid.", vbExclamation)
        Exit Sub
    End If
    
    ' Calculate the angle in radians
    Dim angle As Double
    angle = Math.Atan2(oPoint2.Y - oPoint1.Y, oPoint2.X - oPoint1.X)
    
    ' Convert the angle to degrees
    angle = angle * 180 / Math.PI
    
    ' Rotate the view
    oView.Rotation = angle
End Sub

 

 

VictorTuguinay_0-1725401189082.png

VictorTuguinay_2-1725401393016.png

 

 

 

0 Likes
Message 9 of 14

Victor.Tuguinay
Enthusiast
Enthusiast
@JelteDeJong
Or is there an easier way to rotate the drawing view based on that minimum bounding box? I am looking to orient the view by making the long side of the bounding box horizontal.
0 Likes
Message 10 of 14

WCrihfield
Mentor
Mentor

One small suggestion would be to switch from using 'kDrawingPointFilter' as the selection filter, which does not exist in the SelectionFilterEnum, to SelectionFilterEnum.kAllPointEntities variation.  And you should always include the name of the Enum, before the Enum variation name (SelectionFilterEnum.kAllPointEntities instead of just kAllPointEntities).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 14

442780782
Advocate
Advocate
Hello! In Inventor 2016 API, there is no "OrientedMinimumRangeBox". Can other alternative code be used to achieve this? Thank you!
0 Likes
Message 12 of 14

WCrihfield
Mentor
Mentor

Hi @442780782.  Before the OrientedBox object type (originally available in 2016) and the OrientedMinimimRangeBox property (2021 & 2024) was available, we had to use the regular Box object type and the regular RangeBox property.  That RangeBox property could be found associated with several different types of objects.  It has different properties, so its size is determined differently, but there should actually be a lot more discussions and examples of dealing with RangeBox property and the Box object, and their sizes, than there are for the newer objects / properties, since they were around a lot longer.  As far as I could tell, the only way you encountered an OrientedBox in 2016 is by 'creating' one yourself, using the TransientGeometry.CreateOrientedBox method.  But obtaining the proper and accurate input data for defining it, based on existing geometry in a model, was likely much more challenging in 2016 version of Inventor.  However, when working with a Box, instead of an OrientedBox, there is a much simpler way to generate 'transient' geometry from that using the TransientBRep.CreateSolidBlock method.  It lets us specify a Box as input, and returns a transient SurfaceBody object.  And once you have that, you can use the other simpler route of with the NonParametricBaseFeatures.Add method, to create the visible body representing the RangeBox, if you want to.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 14

442780782
Advocate
Advocate
Thank you for your guidance! Can you provide the complete code that supports Inventor 2016? I would be very grateful, thank you!
0 Likes
Message 14 of 14

WCrihfield
Mentor
Mentor

Hi @442780782.  Below is an example iLogic rule that I think should work OK in 2016 version of Inventor.  It will create a rectangular prism shaped solid body the same size as the bounding box that should fully encompasses all current bodies in a part.  The resulting body is a 'new' solid body that is not 'combined' with all the others, but is independent, and can have its visibility turned on/off, or that feature that created it can be suppressed.  However, I do not believe it will remain accurate (it will not update itself automatically) if you make further changes to the pre-existing bodies, or add more bodies into the part after creating it.  So, if you make changes after that, and need it to be accurate again, then you may need to run this rule (or one like it) again.  The rule assigns the name "Bounding Box" to this feature, so if the rule runs again, it will try to delete an existing feature of that type, with that name, before creating the new one with that same name.  This rule also uses a 'Transaction' to wrap the feature creation and name change into one item in the UNDO list, so it can be undone again in one click on the undo button, if you want.

 

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then Return
	Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
	Dim oMainBox As Inventor.Box = Nothing
	For Each oBody As SurfaceBody In oPDef.SurfaceBodies
		Dim oBodyBox As Inventor.Box = oBody.RangeBox
		If oMainBox Is Nothing Then
			oMainBox = oBodyBox.Copy
		Else
			Try : oMainBox.Extend(oBodyBox.MinPoint) : Catch : End Try
			Try : oMainBox.Extend(oBodyBox.MaxPoint) : Catch : End Try
		End If
	Next oBody
	Dim oTBody As SurfaceBody = oInvApp.TransientBRep.CreateSolidBlock(oMainBox)
	Dim oNPBFs As NonParametricBaseFeatures = oPDef.Features.NonParametricBaseFeatures
	Dim oNPBFDef As NonParametricBaseFeatureDefinition = oNPBFs.CreateDefinition()
	Dim oObjColl As Inventor.ObjectCollection = oInvApp.TransientObjects.CreateObjectCollection
	oObjColl.Add(oTBody)
	oNPBFDef.BRepEntities = oObjColl
	oNPBFDef.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType
	Dim oTrans As Inventor.Transaction = oInvApp.TransactionManager.StartTransaction(oPDoc, "Create Bounding Box - iLogic")
	Try : oNPBFs.Item("Bounding Box").Delete : Catch : End Try
	Dim oNPBF As NonParametricBaseFeature = oNPBFs.AddByDefinition(oNPBFDef)
	oNPBF.Name = "Bounding Box"
	oTrans.End
End Sub

 

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