Thanks! That's exatly what I wanted to achiev 😁
Here is my full code, maybe it could be interesting for someone else!
I added an'if statement to check if there's only one model state.
In that case the selection list doesn't appear.
If you see any error or stupidity in it, pls don't hesitate to let me know,
I'm not an iLogic expert. Most of what I do is copy and paste 😅
Note: for the timed message you must add the following line to the header:
Imports System.Threading.Tasks
Sub Main()
' Get the STEP translator Add-In.
Dim Pfad As String = "J:\102 Export Inventor stp\"
'Dim Pfad As String = "c:\temp\"
Dim Pfad_Dateiname As String = Pfad & iProperties.Value("Project", "Part Number") & "_" & iProperties.Value("Project", "Revision number") & ".stp"
If System.IO.File.Exists(Pfad_Dateiname) = True Then
question = MessageBox.Show("Die STP Datei existiert bereits, soll diese überschrieben werden?", "STP Überschreiben?",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
'set condition based on answer
If question = vbYes Then
stp_export(Pfad_Dateiname)
Else
'nicht überschreiben
End If
Else
stp_export(Pfad_Dateiname)
End If
End Sub
Sub stp_export (pfad_Dateiname As String)
'Falls mehrere Modellzustände vorhanden sind, wird abgefragt, welcher exportiert werden soll
Dim oMDoc As Document = ThisDoc.FactoryDocument
If IsNothing(oMDoc) Then Exit Sub
Dim oActiveMSName As String = ThisDoc.ActiveModelState
Dim oMSs As ModelStates = oMDoc.ComponentDefinition.ModelStates
Dim oMSNames As New List(Of String)
For Each oMS As ModelState In oMSs : oMSNames.Add(oMS.Name) : Next
'bei nur EINEM Modellzustand im Dokument wird automatisch dieser als Step exporiert
Dim oMSDoc As Document
If oMSNames.Count > 1 Then
Dim ChosenMSName As String = InputListBox("", oMSNames, oActiveMSName, "Modellzustände im Dokument", "Modellzustand für Export wählen?")
If ChosenMSName = "" Then Exit Sub
Dim ChosenMS As ModelState = oMSs.Item(ChosenMSName)
If ChosenMSName <> oActiveMSName Then ChosenMS.Activate
oMSDoc = ChosenMS.Document
Else
oMSDoc = ThisDoc.Document
End If
'für den Step export nun das Objekt oMSDoc anstatt ThisDoc.Document verwenden
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
'If oSTEPTranslator.HasSaveCopyAsOptions(ThisDoc.Document, oContext, oOptions) Then
If oSTEPTranslator.HasSaveCopyAsOptions(oMSDoc, 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
oData = ThisApplication.TransientObjects.CreateDataMedium
oData.FileName = pfad_Dateiname
Try
oSTEPTranslator.SaveCopyAs(ThisDoc.Document, oContext, oOptions, oData)
'Messagebox anzeigen und nach 0.8 Sekunden automatisch schließen
Dim timeout = 1.5 ' secs
Dim msg As New Form() With { .Enabled = False }
Task.Delay(TimeSpan.FromSeconds(timeout)).ContinueWith(
Sub(t)
msg.Close()
End Sub ,
TaskScheduler.FromCurrentSynchronizationContext())
MessageBox.Show(msg, "Export STP erfolgreich!" & vbCrLf & vbCrLf & "Modellzustand: " & ThisDoc.ActiveModelState, "Info")
Catch
MessageBox.Show("Export STP fehlgeschlagen!", "Fehler")
End Try
End If
End Sub