When exporting an Assembly file within Inventor it can produce a STL for each occurrence (filename suffixed as per the DisplayName) with its Origin in the Master Assembly preserved (such that re-importing it all the parts are correctly positioned) when the 'One File per Part Instance' option is selected. Using the Translator via the API on a part file is fine but I cannot seem to get the Translator to replicate the behaviour when running the 'One File per Part Instance' command on an assembly using the API. As there is an option for this in the API translator I thought it might do this when it was enabled and offered an assembly but I cant get it work and how would it deal with the filenames?
If I recursively traverse the the assembly and all sub assemblies and export them individually the Origin position of each part within the assembly is lost.
There's lots of API information on using the STL Translator on Parts but I can find no examples on an IAM file - can anyone please point me in the right direction or provide a code snippet ?
Dim oDoc As Document = oInv.ActiveDocument
Dim oContext As TranslationContext = oInv.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap = oInv.TransientObjects.CreateNameValueMap
' Save Copy As Options:
' Name Value Map:
' ExportUnits = 4 , 5 = Millimeter
' Resolution = 1
' AllowMoveMeshNode = False
' SurfaceDeviation = 60
' NormalDeviation = 14
' MaxEdgeLength = 100
' AspectRatio = 40
' ExportFileStructure = 0
' OutputFileType = 0
' ExportColor = True
If oSTLTranslator.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("Resolution") = ComboSTLRes.SelectedIndex ' Set accuracy.0 = High, 1 = Medium, 2 = Low
oOptions.Value("OutputFileType") = 0 ' Set output file type:0 - binary, 1 - ASCII
oOptions.Value("ExportFileStructure") = ComboSTLStructure.SelectedIndex 'One part or multi ?
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oData As DataMedium = oInv.TransientObjects.CreateDataMedium
oData.FileName = STLPath & "\" & STLFileName & ".STL"
Call oSTLTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
SBar1.Text = "Exported " & oData.FileName
oDoc.Close(True)
End If
Solved! Go to Solution.
Solved by Ralf_Krieg. Go to Solution.
Hello
You don't have to care of the file naming. The names are generated by the Addin.
I've edited your code to make it able to run stand alone. The rule creates a STL file for the assembly and one for each part instance with respect to the main assembly origin. All Subassemblies are skipped. I can not see a different behaviour to the GUI version. 😔
Dim oInv As Inventor.Application = ThisApplication
Dim strCLSID As String = "{81CA7D27-2DBE-4058-8188-9136F85FC859}"
Dim STLPath As String = ThisDoc.Path
Dim STLFileName As String = ThisDoc.FileName(False)
Dim oAddIns As ApplicationAddIns = ThisApplication.ApplicationAddIns
Dim oSTLTranslator As TranslatorAddIn = oAddIns.ItemById(strCLSID)
oSTLTranslator.Activate
Dim oDoc As Document = oInv.ActiveDocument
Dim oContext As TranslationContext = oInv.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap = oInv.TransientObjects.CreateNameValueMap
' Save Copy As Options:
' Name Value Map:
' ExportUnits = 4 , 5 = Millimeter
' Resolution = 1
' AllowMoveMeshNode = False
' SurfaceDeviation = 60
' NormalDeviation = 14
' MaxEdgeLength = 100
' AspectRatio = 40
' ExportFileStructure = 0
' OutputFileType = 0
' ExportColor = True
If oSTLTranslator.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("Resolution") = 0 'ComboSTLRes.SelectedIndex ' Set accuracy.0 = High, 1 = Medium, 2 = Low
oOptions.Value("OutputFileType") = 0 ' Set output file type:0 - binary, 1 - ASCII
oOptions.Value("ExportFileStructure") = 1 'ComboSTLStructure.SelectedIndex 'One part or multi ?
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oData As DataMedium = oInv.TransientObjects.CreateDataMedium
oData.FileName = STLPath & "\" & STLFileName & ".STL"
Call oSTLTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
'SBar1.Text = "Exported " & oData.FileName
'oDoc.Close(True)
End If
Many thanks for replying @Ralf_Krieg but I'm still having the same problem. The code will:
1) Produce a single part STL from an IPT when oOptions.Value("ExportFileStructure") =0
2) Create a single part STL from an IAM when oOptions.Value("ExportFileStructure") =0
3) Create a single part STL from an IPT when oOptions.Value("ExportFileStructure") =1
But it will not create multiple STLs from an IAM when oOptions.Value("ExportFileStructure") =1
If I dont pass anything in oData.FileName (for example "C:\Temp\TEST.STL") it throws an exception at the line Call oSTLTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData) so it seems it needs something here.
I cant find any reference material on the STL Translator in the API documentation either.
Any thoughts?
Private Sub MakeSTL()
oInv = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
Dim oFileManager As FileManager = oInv.FileManager ' Set a reference to the FileManager object.
Dim strFullDocumentName As String = oFileManager.GetFullDocumentName("C:\Temp\Test.iam", "Test LoD") ' Use the full file name and LOD name
Dim oDocOpenOptions As NameValueMap = oInv.TransientObjects.CreateNameValueMap ' Create a new NameValueMap object
IVDoc = oInv.Documents.OpenWithOptions(strFullDocumentName, oDocOpenOptions) 'Open the document in a LoD state
Dim strCLSID As String = "{81CA7D27-2DBE-4058-8188-9136F85FC859}" 'This seems to be a different GUID
Dim oAddIns As ApplicationAddIns = oInv.ApplicationAddIns
Dim oSTLTranslator As TranslatorAddIn = oAddIns.ItemById(strCLSID)
oSTLTranslator.Activate()
Dim oDoc As Document = oInv.ActiveDocument
Dim STLPath As String = IO.Path.GetDirectoryName(oDoc.FullFileName) & "\STLs\" & IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
If Not IO.Directory.Exists(STLPath) Then IO.Directory.CreateDirectory(STLPath)
Dim STLFileName As String = IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName) & ".STL"
Dim STLFullFileName As String = STLPath & "\" & STLFileName
Dim oContext As TranslationContext = oInv.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap = oInv.TransientObjects.CreateNameValueMap
If oSTLTranslator.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("Resolution") = 2
oOptions.Value("OutputFileType") = 0
oOptions.Value("ExportFileStructure") = 1
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oData As DataMedium = oInv.TransientObjects.CreateDataMedium
oData.FileName = STLFullFileName
Call oSTLTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
oDoc.Close(True)
End If
End Sub
Hello
Can you try without closing the source document at end?
Thanks @Ralf_Krieg ,
Unfortunately even if dont close the ActiveDocument I still get same the result. Single Part STLs from either a selected IPT or an IAM are created fine, but nothing is created when I ask for a multipart STL from an IAM.
I wonder if it implemented at all? Its a pity there doesn't seem to be any documentation in the API Reference Manual for the STL Translator.
Hello
There's not much further documentation needed. The translator Addins are all mostly identic. Reading your code, you are running a standalone exe right?You grep a running Inventor instance in your first line. Are you running two or more Inventor instances in parallel while trying? Is there a Try-Catch or On Error resume next statement somewhere in the code around, that prevents displaying error messages and just silently abort? Are you using a program like VisualStudio for developing? Have you tried to step line by line through your code in debugging mode and checked that all variables hold the value you expected they do?
Have you tried my code in an iLogic rule, just to see if it's work that way? What version of Inventor are you using? ServicePack level?
Is there no way to transpose coordinate systems in every file relative to the assembly origin? Maybe that's what will solve this. I had a need to do this too, but quit trying b/c it's WAY over my paygrade and I have better things to do than fiddle around w/the one-time thing. Anyway, that's what I would've tried to do to get this solved, that is, IF I knew what I was doing.
Hello
None of my questions answered. How do you expect to get help???
The example version I gave you runs fine in Inventor 2021 and 2022 with latest updates installed. If it's not working with your data, we would need a demo assembly trying to reproduce.
If you only need the position, create a ComponentOccurrenceProxy of tha part in assembly context. This will give you the origin coordinates of the part occurrence in assembly context. This will not givw you any information about rotation angles of the part occurrence in assembly space. And take a look at the TranformationMatrix object of the component occurrence. This can give you further information about part position.
Thanks - sorry I got hit by THE bug and have been laid up ill for a few days, not 100% but I'm back at my PC now.
Yes, I'm using Visual Studio and writing a standalone VB Net application calling on the Inventor 2019 Build 330 Release 2019.4.8
My App does several other things (some using Apprentice) so I think I'll pull out just the code for the STL export and try that on its own to make sure I'm not running two instances and also try it in iLogic.
I did just notice that when starting the Inventor application from the desktop it required me to login to the Autodesk server so maybe that was preventing the API from starting.
Steve,
'the bug' has a name:
"FLURONA"
Talk about mocking us while seeding society with weaponized microbes that the DoD and USDA invented in their filthy, criminal infested germ warfare labs around the world!
Thanks for your help, I've installed 2021 Inventor Pro on my PC and tried the code in iLogic inside Inventor and (as you said) it works, creating both single and multipart files and taking care of the filenames for multi-parts, but it still does not create multi parts when called using the interop DLL reference (for either 2019 or 2021 versions) using the 2021 API in a standalone app created in Visual Studio, only creating single parts. I've tried taking all the Try/Catches out hoping for an exception to be thrown and also tried stepping through on debug but there are no clues and no errors are generated during the process so I'm no further to understanding why its not working. I suspect that it may be something to do with the TranslationContext and/or NameValueMap parameters not being passed to the API or maybe them not being implemented in the API. Without any documentation on how to call the STL translator correctly it is hard to know for sure.
Hello
Sorry to hear that this special case is not working. I've added a button (Button1) in one of my standalone exe applications and assigned the code below to the button. The application has a reference to the Autodesk.Inventor.Interop.dll version 25.2.0.0 (Inventor 2021). Running this application while the assembly document is open in Inventor, it creates multiple STL files as expected.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oInv As Inventor.Application = TryCast(Marshal.GetActiveObject("Inventor.Application"), Inventor.Application)
Dim strCLSID As String = "{81CA7D27-2DBE-4058-8188-9136F85FC859}"
Dim STLPath As String = System.IO.Path.GetDirectoryName(oInv.ActiveDocument.FullFileName) 'ThisDoc.Path
Dim STLFileName As String = System.IO.Path.GetFileNameWithoutExtension(oInv.ActiveDocument.FullFileName) 'ThisDoc.FileName(False)
Dim oAddIns As ApplicationAddIns = oInv.ApplicationAddIns 'ThisApplication.ApplicationAddIns
Dim oSTLTranslator As TranslatorAddIn = DirectCast(oAddIns.ItemById(strCLSID), TranslatorAddIn)
oSTLTranslator.Activate()
Dim oDoc As Document = oInv.ActiveDocument
Dim oContext As TranslationContext = oInv.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap = oInv.TransientObjects.CreateNameValueMap
' Save Copy As Options:
' Name Value Map:
' ExportUnits = 4 , 5 = Millimeter
' Resolution = 1
' AllowMoveMeshNode = False
' SurfaceDeviation = 60
' NormalDeviation = 14
' MaxEdgeLength = 100
' AspectRatio = 40
' ExportFileStructure = 0
' OutputFileType = 0
' ExportColor = True
If oSTLTranslator.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
oOptions.Value("Resolution") = 0 'ComboSTLRes.SelectedIndex ' Set accuracy.0 = High, 1 = Medium, 2 = Low
oOptions.Value("OutputFileType") = 0 ' Set output file type:0 - binary, 1 - ASCII
oOptions.Value("ExportFileStructure") = 1 'ComboSTLStructure.SelectedIndex 'One part or multi ?
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oData As DataMedium = oInv.TransientObjects.CreateDataMedium
oData.FileName = STLPath & "\" & STLFileName & ".STL"
Call oSTLTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
'SBar1.Text = "Exported " & oData.FileName
'oDoc.Close(True)
End If
End Sub
Thanks for your reply once again. I've finally got it working - I had problems with the line :
Dim oInv As Inventor.Application = TryCast(Marshal.GetActiveObject("Inventor.Application"), Inventor.Application)
which threw an exception, but replacing this with:
Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
oInv = System.Activator.CreateInstance(inventorAppType)
oInv.Visible = False 'Show/Hide it
It now works and exports multi-part STLs but I don't understand why.
I've attached a copy of my Visual Studio vb.net code for information for anyone. This opens inventor in the background, allows a user to select an IAM in any selected LoD state and export the parts within as an binary STL(s) with selected resolution and either multipart or single part option.
Its only has minimal error checking.
@Ralf_Krieg - thank you so much for your help, its very much appreciated.
Hello
Glad to hear it work now. 👍
My code snippet assumes an Inventor instance is already running. That's why it throws an error.
Let your users find the things to check and prevent. It will be an endless list. There's nothing a user don't do. 😁
Just a closing note on this for anyone else finding that the STL translator wont export multiple STL parts - the 2019 API wouldn't do this for me (although it did export single parts) but the 2021 API does export multiple parts using the same code.
Can't find what you're looking for? Ask the community or share your knowledge.