Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to run an off-the-shelf addin from API?

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
atsushi_kuno
266 Views, 5 Replies

How to run an off-the-shelf addin from API?

Hi, everyone

 

I am trying to write an addin to export an .obj file after the assembly is translated such that its range box is centered at the origin of the global coordinates. At first, I coded the addin to translate the asm, and then save it as .obj file as instructed here (while I rewrote it in C#). It works fine but I thought that runing the `Translator: OBJ Export` addin would be better because the GUI of the addin allows uses to set export options as shown on this page. I changed my addin code to call the `Translator: OBJ Export` addin like

 

objExporeterAddin.Activate()

 

But no GUI appeared. 

 

So, my question is: 1) is it possible to run an off-the-shelf addin using Inventor API? and, if so, 2) How can I do that?

 

Thanks in advance!

5 REPLIES 5
Message 2 of 6
WCrihfield
in reply to: atsushi_kuno

Hi @atsushi_kuno.  I am not experienced in creating add-ins for Inventor, but what you seem to be asking about may not actually be about creating an add-in, but simply how to use other built-in add-ins by code.  Since you seem to be focused on the TranslatorAddIn sub type of Inventor add-ins, there is actually a couple methods for visibly 'showing' them in the user interface, for manual user interaction.

Below are links to the online help documentation about those two methods:

TranslatorAddIn.ShowOpenOptions 

TranslatorAddIn.ShowSaveCopyAsOptions 

As far as I have ever seen them used, they are primarily used to allow the user to manually review and/or change any 'Options' that you may normally be able to see when you would have normally clicked on the Options button within the Open or Save file dialog screens while opening or saving a file, but initiated by code, so that the resulting settings can be captured within the code.  Most import/export code examples do not use this, because we usually just specify the settings/options within the code, using a NameValueMap.  But sometimes export options need to change often enough that hard-coding into the import/export block of code is not efficient or effective enough.  In cases like that, the options dialog can be shown to the user, allowing them to manually interact with it, then once they click OK (or otherwise exit that dialog), control returns to the code process.  Showing the dialog does not actually open or export anything, it only captures settings/options, which can then be read from within the 'ChosenOptions' (NameValueMap).

 

If those two methods are not what you were looking for, then your next options may be to execute a command to import/export a file, but then all control will be given to the user interface tools, and you will most likely not be able to interact with them by code at all.  We usually have very little code based control over any of the dialogs that get shown by Inventor...unless maybe you are including a lot of really advanced code for finding and interacting with foreign dialogs (dialogs that you did not fully design and implement yourself), and have done a ton of exploring those specific dialogs, to be able to find specific stuff within them, and know how to interact with them by code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

@atsushi_kuno here are a couple of quick VB.net examples of what @WCrihfield mentioned, in case it is of help.


Export an OBJ file

' Get the OBJ translator Add-In.
Dim OBJAddIn As TranslatorAddIn
OBJAddIn = ThisApplication.ApplicationAddIns.ItemById("{F539FB09-FC01-4260-A429-1818B14D6BAC}")

'a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

''set option defaults
''see link: https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-25D24E28-7517-4903-8AEE-123F8C71A89E&v=2018
oOptions.Value("ExportUnits") = 0
oOptions.Value("Resolution") = 3
oOptions.Value("SurfaceDeviation") = 5
oOptions.Value("NormalDeviation") = 10
oOptions.Value("MaxEdgeLength") = 10
oOptions.Value("AspectRatio") = 10
oOptions.Value("ExportFileStructure") = 10

'use this for exporting/saving out a file
OBJAddIn.ShowSaveCopyAsOptions(oDocument, oContext, oOptions)

'the destination file name
oDataMedium.FileName = "c:\temp\test.OBJ"

'Publish document.
Call OBJAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

 

Import an OBJ file

' Get the OBJ translator Add-In.
Dim OBJAddIn As TranslatorAddIn
OBJAddIn = ThisApplication.ApplicationAddIns.ItemById("{F539FB09-FC01-4260-A429-1818B14D6BAC}")

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kDataDropIOMechanism

' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

Dim oDocument As Document 

''set option defaults
''see link: https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-25D24E28-7517-4903-8AEE-123F8C71A89E&v=2018
oOptions.Value("SaveComponentDuringLoad ") = False
oOptions.Value("SaveLocationIndex") = 2
oOptions.Value("EmbedInDocument ") = True
oOptions.Value("SaveToDisk") = False
oOptions.Value("ImportUnit") = 0
oOptions.Value("SplitGroup") = True

'use this if importing/opening a file
OBJAddIn.ShowOpenOptions(oDataMedium, oContext, oOptions)

'the destination file name
oDataMedium.FileName = "c:\temp\test.OBJ"

'Publish document.
Call OBJAddIn.Open(oDataMedium, oContext, oOptions, oDocument)

oDocument.SaveAs("c:\temp\test99.ipt",False)

 

Message 4 of 6
atsushi_kuno
in reply to: atsushi_kuno

Hello, @WCrihfield and @Curtis_Waguespack ,

 

I really appreciate your suggestions. The code @Curtis_Waguespack provided worked pretty nicely.

 

Actually, I have another question now, although it might be better to make another post. But, let me ask it here at first. Is it possible to export only selected component occurrences? I tried to do so like

objExportAddin.ShowSaveCopyAsOptions(asmDoc.SelectSet, context, opts)
objExportAddin.SaveCopyAs(asmDoc.SelectSet, context, opts, dataMedium)

The first line did not raise any errors but after I click the 'OK' on the form of 'ShowSaveCopyAsOptions', Inventor crushed. I believe that 'SelectSet' object is not an expected object passed to the methods, but what is the expected object then? Do you have any other idea to export only selected occurrences as an .obj file?

 

Thank in advance!

Message 5 of 6

@atsushi_kuno , I think something like this will work

 

' Get the OBJ translator Add-In.
Dim OBJAddIn As TranslatorAddIn
OBJAddIn = ThisApplication.ApplicationAddIns.ItemById("{F539FB09-FC01-4260-A429-1818B14D6BAC}")

'a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

''set option defaults
''see link: https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-25D24E28-7517-4903-8AEE-123F8C71A89E&v=2018
oOptions.Value("ExportUnits") = 0
oOptions.Value("Resolution") = 3
oOptions.Value("SurfaceDeviation") = 5
oOptions.Value("NormalDeviation") = 10
oOptions.Value("MaxEdgeLength") = 10
oOptions.Value("AspectRatio") = 10
oOptions.Value("ExportFileStructure") = 10

'use this for exporting/saving out a file
OBJAddIn.ShowSaveCopyAsOptions(oDocument, oContext, oOptions)

Dim oSelectSet As SelectSet = oDocument.SelectSet
For Each oOcc As ComponentOccurrence In oSelectSet
	oDoc = oOcc.Definition.Document

	'the destination file name
	oDataMedium.FileName = "c:\temp\" & IO.Path.GetFileNameWithoutExtension(oDoc.fullfilename) & ".OBJ"

	'Publish document.
	Call OBJAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)
Next

 

Message 6 of 6

Hi @Curtis_Waguespack,

 

Thanks for your quick response! Unfortunately, the solution I am looking for is to export a single .obj file which contains only selected occurrences, which is the requirement of a simulator to which I pass the .obj file. I believe that your code generates an .obj file per occurrence.

 

Investigating all the lines of separate .obj files would work. But manually reconciling occurrences' IDs or something sounds tedious and error-prone. I want to avoid that

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report