Export all Model States to individual step files

Export all Model States to individual step files

HSI-Autodesk
Participant Participant
697 Views
3 Replies
Message 1 of 4

Export all Model States to individual step files

HSI-Autodesk
Participant
Participant

We have part files with multiple model states. Like 20 each. Eacch time we update something across all states, i must regenerate the step files for each state. Instead of doing it manually, How can I go about using ilogic to make run a script to export each individual model state as a step file with the filename being the model state name to a chosen folder.

 

I looked though all the inventor add-ons, and couldn't find one that would do this, and looked searched this forum to see if anyone had done this before. 

 

I found this forum where someone posted how to get a list of the model states and pick one to export, but I haven't been able to modify it to do my use case yet.

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/selection-list-of-which-model-state-to-ex...

 

Any help is appreciated. 

 

Thank you. 

0 Likes
Accepted solutions (1)
698 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor
Accepted solution

Try this:

Dim oSTEPTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition

Dim folderDialog As New System.Windows.Forms.FolderBrowserDialog()
folderDialog.SelectedPath = System.IO.Path.GetDirectoryName(doc.FullFileName) ' "c:\temp\"
folderDialog.ShowDialog()
Dim folder = folderDialog.SelectedPath

For Each modelState As ModelState In def.ModelStates
    ModelState.Activate()

    ' Get the STEP translator Add-In.
    Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
    Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

    If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
        ' Set application protocol.
        ' 2 = AP 203 - Configuration Controlled Design
        ' 3 = AP 214 - Automotive Design
        oOptions.Value("ApplicationProtocolType") = 3
        ' Other options...
        'oOptions.Value("Author") = ""
        'oOptions.Value("Authorization") = ""
        'oOptions.Value("Description") = ""
        'oOptions.Value("Organization") = ""
        oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

        Dim oData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
        oData.FileName = System.IO.Path.Combine(folder, ModelState.Name & ".stp")

        oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
    End If
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 4

Johnnytk36
Community Visitor
Community Visitor

I was wondering why I couldn't accept your solution. I was logged into my personal autodesk account and not the company. haha

 

Thanks again.

0 Likes
Message 4 of 4

HSI-Autodesk
Participant
Participant

Thank You! This works perfect!

 

I didn't know where to start on this, and you gave me exactly what I wanted.

 

I made two changes, I am going to post here for other people.

 

I added the base document name to the exported filename path, and I added a line to got back to the primary model state after this export is done.

 

Dim oSTEPTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim doc As PartDocument = ThisDoc.Document
Dim def As PartComponentDefinition = doc.ComponentDefinition

Dim folderDialog As New System.Windows.Forms.FolderBrowserDialog()
folderDialog.SelectedPath = System.IO.Path.GetDirectoryName(doc.FullFileName) ' "c:\temp\"
folderDialog.ShowDialog()
Dim folder = folderDialog.SelectedPath

For Each modelState As ModelState In def.ModelStates
    ModelState.Activate()

    ' Get the STEP translator Add-In.
    Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
    Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

    If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
        ' Set application protocol.
        ' 2 = AP 203 - Configuration Controlled Design
        ' 3 = AP 214 - Automotive Design
        oOptions.Value("ApplicationProtocolType") = 3
        ' Other options...
        'oOptions.Value("Author") = ""
        'oOptions.Value("Authorization") = ""
        'oOptions.Value("Description") = ""
        'oOptions.Value("Organization") = ""
        oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

        Dim oData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
        oData.FileName = System.IO.Path.Combine(folder, ThisDoc.FileName(False) & " - " & ModelState.Name & ".stp")
	
		

        oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
    End If
Next

ThisDoc.ActiveModelState = ThisServer.LanguageTools.CurrentPrimaryModelStateString
0 Likes