Fast Booleans Using the API

The Inventor API supports some functionality that’s relatively new (since Inventor 2009) and is not very well known.  It’s called Transient B-Rep and is accessed through the TransientBRep property on the Application object.  It supports some relatively simple functionality and also some of the more complicated capabilities provided by the API.  Luckily you don’t have to understand the complicated in order to use the simple. 

There was a question on the Inventor Customization Forum recently that asked about being able to quickly perform many Boolean operations on a body by repeatedly moving another body and doing a difference operation.  They didn’t need the resulting model to have any intelligence (i.e. be parametric) but just needed accurate geometry.  The TransientBRep object supports functionality that makes this possible.

The Inventor API uses the term Transient to mean data that is not seen or saved in the document.  It’s data that exists outside of the model.  The TransientBRep object provides functions that are a very light wrapper over the Autodesk Shape Manager (ASM) modeling core.  When you create a feature in Inventor, it’s also using ASM to do the actual model computations but it is also doing a lot more work to remember all of the feature input, adding additional information to the resulting model to allow for associativity, creating data in the browser to represent the feature, and updating the graphics to display the modified model.  Although a key part of the process, the actual modeling is just a piece of all of the work that’s done.  Using the TransientBRep functionality allows you to do just the modeling work without all of the overhead of creating a standard feature.

I’ve created a small sample to demonstrate the concept and illustrate how to use the API.  I have a part model that contains two bodies.  The yellow body will be used as the base or main part material.  The red part is used as the tool.  In this case it can be thought of as a punch that will be used to remove material from the base part.  The punch has been built relative to the model origin, as can be seen by the base work planes.

TransBRep1

In addition to the two bodies you can also see several work points that have been created. They’re normal work points except they’ve been named “Punch#”. as shown below. The points are used to specify locations where the tool is to be subtracted from the base.

TransBRep2

When the macro is run it prompts you to select the base and tool bodies and then uses the TransientBrep functionality to create transient copies of those and then performs multiple Boolean operations by moving the tool and subtracting it from the base.  Finally it creates a base feature using the modified base body and turns off the display of the tool and the base body so the result can be seen, which is shown below.

TransBRep3

Below is the sample macro and here is the sample part that I used to test it.  The part is an Inventor 2015 model, but this macro will work in previous versions of Inventor too.

Public Sub SampleBoolean()     Dim partDoc As PartDocument     Set partDoc = ThisApplication.ActiveDocument     Dim partDef As PartComponentDefinition     Set partDef = partDoc.ComponentDefinition         Dim tg As TransientGeometry     Set tg = ThisApplication.TransientGeometry     Dim tb As TransientBRep     Set tb = ThisApplication.TransientBRep     Dim tObjs As TransientObjects     Set tObjs = ThisApplication.TransientObjects          ' Have the bodies selected.     Dim baseBody As SurfaceBody     Set baseBody = ThisApplication.CommandManager.Pick( _                         kPartBodyFilter, _                         "Select the base body")         Dim toolBody As SurfaceBody     Set toolBody = ThisApplication.CommandManager.Pick( _                         kPartBodyFilter, _                         "Select the tool body")                             ' Copy the two bodies to create transient copies.     Dim transBase As SurfaceBody     Set transBase = tb.Copy(baseBody)     Dim transTool As SurfaceBody     Set transTool = tb.Copy(toolBody)         ' Create a matrix and a point to use in positioning     ' the punch.  The matrix is initialized to an identity     ' matrix the point is (0,0,0).     Dim trans As Matrix     Set trans = tg.CreateMatrix     Dim lastPosition As Point     Set lastPosition = tg.CreatePoint         ' Process each work point whose name starts with "Punch"     ' by performing a boolean with the tool at that location.     Dim wp As WorkPoint     For Each wp In partDef.WorkPoints         If Left$(UCase(wp.Name), 5) = "PUNCH" Then             ' Transform the tool body to the position of the x and             ' y coordinates work point, leaving the z as-is.  The              ' punch is at the last punch so the transform defines             ' the difference between the last and the current.             trans.Cell(1, 4) = wp.Point.X - lastPosition.X             trans.Cell(2, 4) = wp.Point.Y - lastPosition.Y             Call tb.Transform(transTool, trans)                         ' Do the boolean operation.             Call tb.DoBoolean(transBase, transTool, _                               kBooleanTypeDifference)                     ' Save the last position.             Set lastPosition = wp.Point         End If     Next         ' Create a base body feature of the result.     Dim nonParamFeatures As NonParametricBaseFeatures     Set nonParamFeatures = partDef.Features.NonParametricBaseFeatures     Dim nonParamDef As NonParametricBaseFeatureDefinition     Set nonParamDef = nonParamFeatures.CreateDefinition         Dim objs As ObjectCollection     Set objs = tObjs.CreateObjectCollection     Call objs.Add(transBase)     nonParamDef.BRepEntities = objs     nonParamDef.OutputType = kSolidOutputType         Call nonParamFeatures.AddByDefinition(nonParamDef)         ' Turn off the display of the original two     ' features to see the result.     baseBody.Visible = False     toolBody.Visible = False         ' Force a refresh of the view.     ThisApplication.ActiveView.Update End Sub

-Brian


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading