My friends from the DevTech group, which support the members of the Autodesk Developer Network (ADN), are helping me out while I’m buried with project work and are providing some content for blog posts. This one is from Xiaodong Liang who works in the Beijing China office.
There are two basic mechanisms for importing non-native surface or solid bodies into Inventor. The first involves using the TransientBrep.ReadFromFile method to read a surface body in directly. The second mechanism involves using a suitable translator add-in to read the data in. In both cases the surface/solid data can then be added as a Part feature via the NonParametricFeatures.Add method.
This first code sample illustrates how to use the TransientBrep.ReadFromFile mechanism to read the model data from a SAT file:-
Sub importByBrep()
' Get the active part document
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
' Get transient Brep
Dim oBrep As TransientBRep
Set oBrep = ThisApplication.TransientBRep
' Get the surfacebodies
Dim oSBs As SurfaceBodies
Set oSBs = oBrep.ReadFromFile("c:\temp\test.sat")
' Get definition of current document
Dim oActiveDocDef As PartComponentDefinition
Set oActiveDocDef = oDoc.ComponentDefinition
' Add each surfacebody of imported file to
' NonParametricBaseFeatures
Dim oSB As SurfaceBody
For Each oSB In oSBs
oActiveDocDef.Features.NonParametricBaseFeatures.Add oSB
Next
oDoc.Update
End Sub
This next code sample illustrates how to use one of the translator add-ins to read the relevant data in, again from a SAT file.
Sub ImportByTanstorAddin()
' Get the active part document
Dim oActiveDoc As PartDocument
Set oActiveDoc = ThisApplication.ActiveDocument
' Get definition of active part document
Dim oActiveDocDef As PartComponentDefinition
Set oActiveDocDef = oActiveDoc.ComponentDefinition
' Get SAT TranslatorAddIn
Dim oAddin As TranslatorAddIn
Set oAddin = ThisApplication.ApplicationAddIns.ItemById( _
"{89162634-02B6-11D5-8E80-0010B541CD80}")
' Create context for open
Dim oTransObjs As TransientObjects
Set oTransObjs = ThisApplication.TransientObjects
Dim oContext As TranslationContext
Set oContext = oTransObjs.CreateTranslationContext
oContext.Type = kDataDropIOMechanism
' Create option for open
Dim oOptions As NameValueMap
Set oOptions = oTransObjs.CreateNameValueMap
' Create DataMedium for open
Dim oDataMedium As DataMedium
Set oDataMedium = oTransObjs.CreateDataMedium
Call oOptions.Add("ImportSolid", True)
oDataMedium.fileName = "c:\temp\test.sat"
' Open the SAT file
Dim oNewDoc As PartDocument
Call oAddin.Open(oDataMedium, oContext, oOptions, oNewDoc)
Dim oNewDocDef As PartComponentDefinition
Set oNewDocDef = oNewDoc.ComponentDefinition
' Read the surfacebodies to the current document
Dim oSB As SurfaceBody
For Each oSB In oNewDocDef.SurfaceBodies
oActiveDocDef.Features.NonParametricBaseFeatures.Add oSB
Next
' Close the temp doc without saving
oNewDoc.Close True
End Sub
What can be confusing about this is that reading multiple bodies from different files this way yields a solid for the first import, but surfaces for any further imports to the same Part document. A solid is created if there are no other solids in the Part and a surface is created if there is at least one solid in the Part already. This was the behavior in Inventor when importing bodies interactively until the recent addition of support for multiple solid bodies in a single part. The behavior of the method NonParametricBaseFeatures.Add mimics the old behavior.
There is a way to get the new behavior though. We can use a definition object to stipulate exactly what kind of model is imported (Solid or Surface). The CreateDefinition and AddByDefinition methods of the NonParametricBaseFeatures object were added in Inventor 2011 and support additional flexibility that the Add method doesn’t. CreateDefinition is used to create the relevant definition object, which then specifies the output type of the NonParametricBaseFeature (surface, solid, multiple solids or composite type). The code below illustrates how this can be used to import solids:-
Sub BatchImport()
ImportEachFile "C:\temp\test.sat", 1
ImportEachFile "C:\temp\test2.sat", 2
End Sub
Sub ImportEachFile(fileName As String, index As Integer)
' Get the active part document
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
' Get transient Brep
Dim oBrep As TransientBRep
Set oBrep = ThisApplication.TransientBRep
' Get the surfacebodies
Dim oSBs As SurfaceBodies
Set oSBs = oBrep.ReadFromFile(fileName)
' To transform the body
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix()
' Move the body arbitrarily
oMatrix.SetTranslation oTG.CreateVector( _
index * 10, index * 10, 0)
Dim oTransObjs As TransientObjects
Set oTransObjs = ThisApplication.TransientObjects
Dim oObjColl As ObjectCollection
Set oObjColl = oTransObjs.CreateObjectCollection()
' Add each surfacebody of imported file to ObjectCollection
Dim oSB As SurfaceBody
For Each oSB In oSBs
oBrep.Transform oSB, oMatrix
oObjColl.Add oSB
Next
' Get definition of current document
Dim oPartDef As PartComponentDefinition
Set oPartDef = oDoc.ComponentDefinition
Dim oFeatures As PartFeatures
Set oFeatures = oPartDef.Features
Dim oNPFD As NonParametricBaseFeatureDefinition
Set oNPFD = oFeatures.NonParametricBaseFeatures.CreateDefinition
' Assign the ObjectCollection to the definition.
oNPFD.BRepEntities = oObjColl
' Set the output type
oNPFD.OutputType = kSolidOutputType
' Create the NonParametricBaseFeature using the definition.
oPartDef.Features.NonParametricBaseFeatures.AddByDefinition oNPFD
End Sub
And there we have it! I hope this answers a few questions about importing solids into Inventor.