Issue
I want to load iges or parasolid files into the active document of Inventor via the api. I want this to work in the same way as the [Import] command in Inventor. The code below works but does not load the iges file into the active document, it creates its own file.
Solution
The argument “TargetObject” of TranslatorAddIn.Open has to use the new document. There's an easy way to do this with Inventor 2011. Use NonParametricBaseFeatures.Add to create a base feature in the desired part file. But it is as designed that NonParametricBaseFeatures.Add should create a solid if there are no other solids in the part and a surface if there is at least one solid in the part. So if batching importing, the next file will be imported as surface. To have all as solid, you could use CreateDefinition and AddByDefinition methods of the NonParametricBaseFeatures object which were added in Inventor 2011 and support additional capabilities that the Add method doesn’t. The code below imports ttwo Parasolid files to the current part.
Sub testImport()
' import file 1
ImportXB ("C:\Temp\1.X_B")
' import file 2
ImportXB ("C:\Temp\2.X_B" )
End Sub
' import function
Sub ImportXB(fileName As String)
' assume we have got Inventor application
' get active document
Dim oActiveDoc As PartDocument = _
_InvApplication.ActiveDocument
' get ComponentDefinition
Dim oActiveDocDef As PartComponentDefinition = _
oActiveDoc.ComponentDefinition
' get the TranslatorAddIn
Dim oAddin As TranslatorAddIn = _
_InvApplication.ApplicationAddIns. _
ItemById("{A8F8F8E5-BBAB-4F74-8B1B-AC011251F8AC}")
' prepare the arguments for TranslatorAddIn
Dim oContext As TranslationContext = _
_InvApplication.TransientObjects. _
CreateTranslationContext
oContext.Type = _
IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = _
_InvApplication.TransientObjects. _
CreateNameValueMap
Dim oDataMedium As DataMedium = _
_InvApplication.TransientObjects.CreateDataMedium
' set the option ImportSolid = true
oOptions.Add("ImportSolid", True)
oDataMedium.fileName = fileName
' import the file to one new part document
Dim oNewDoc As PartDocument
oAddin.Open(oDataMedium, oContext, oOptions, oNewDoc)
oNewDoc.Views.Add
' get PartComponentDefinition of the new part document
Dim oNewDocDef As PartComponentDefinition = _
oNewDoc.ComponentDefinition
' create one Definition of NonParametricBaseFeature
Dim oNPFD As NonParametricBaseFeatureDefinition = _
oActiveDocDef.Features.NonParametricBaseFeatures. _
CreateDefinition
Dim oObjColl As ObjectCollection = _
_InvApplication.TransientObjects.CreateObjectCollection()
''check the solids
Dim oSB As SurfaceBody
For Each oSB In oNewDocDef.SurfaceBodies
oObjColl.Add (oSB)
Next
oNPFD.BRepEntities = oObjColl
oNPFD.OutputType =BaseFeatureOutputTypeEnum.kSolidOutputType
' add the bodies of the new part document to the
' active document
oActiveDocDef.Features.NonParametricBaseFeatures. _
AddByDefinition( oNPFD )
'close the temp doc without saving
' because we do not need it any more
oNewDoc.Close( True)
End Sub