Selection list of which model state to export into stp

Selection list of which model state to export into stp

manuel_mair
Participant Participant
572 Views
2 Replies
Message 1 of 3

Selection list of which model state to export into stp

manuel_mair
Participant
Participant

Hello together,
We are currently already using iLogic rules for automatic stp export of components.
Recently we have been working more and more with the model states for different machining states of a part.
It would be cool if we could have a selection window appear in the stp export rule with the model states present in the part, where you can select the state to export.

Thanks and best regards

Accepted solutions (1)
573 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @manuel_mair.  I don't know what your current code may look like, but here is an example of some iLogic code that can be used to present the user with a list of the available ModelStates to choose from, then gets the ModelState object associated with the chosen name, then makes sure that ModelState is activated, then gets the Document associated with that ModelState for you to export in your process.

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
Dim ChosenMSName As String = InputListBox("", oMSNames, oActiveMSName, "Model State Names", "Choose A ModelState To Use")
If ChosenMSName = "" Then Exit Sub
Dim ChosenMS As ModelState = oMSs.Item(ChosenMSName)
If ChosenMSName <> oActiveMSName Then ChosenMS.Activate
Dim oMSDoc As Document = ChosenMS.Document
'now use this oMSDoc variable as the document object you are exporting

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

manuel_mair
Participant
Participant

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