Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
WCrihfield
in reply to: m.rymut

This is just another version that I was working on for you at the same time.  It just puts the code for the folder dialog out into a separate function to keep the main code cleaner.

Sub Main()
	Dim ass As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oSelSet As SelectSet = ass.SelectSet
	If oSelSet.Count = 0 Then
		MessageBox.Show("You must select files to export before running rule", "Error")
		Exit Sub
	End If
	'this runs the custom Function defined below to return a selected folder path
	oFolder = GetFolder
	'make sure a folder was selected, if not exit rule
	If String.IsNullOrEmpty(oFolder) Then Exit Sub
	Dim i As Integer = 0
	Dim result As Boolean
	Dim Occ As ComponentOccurrence
	For Each Occ In oSelSet
		Dim OccDoc As Document = Occ.Definition.Document
		'get file name (without path & without file extension)
		oName = System.IO.Path.GetFileNameWithoutExtension(OccDoc.FullFileName)
		'put new STEP file name together
		oNewName = oFolder & "\" & oName & ".stp"
		result = ExportToSTEP(OccDoc, oNewName)
		If result = True Then
			i = i + 1
		End If
	Next
	MessageBox.Show(i & " files successfully exported", "result")
End Sub

'Here's the subroutine.
Function ExportToSTEP(doc As Document, NewFileName As String) As Boolean
	' Get the STEP translator Add-In.
	Dim oSTEPTranslator As TranslatorAddIn
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	If oSTEPTranslator Is Nothing Then
		MsgBox("Could not access STEP translator.")
		ExportToSTEP = False
		Exit Function
	End If

	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oData = ThisApplication.TransientObjects.CreateDataMedium
	oData.FileName = NewFileName
	
	If oSTEPTranslator.HasSaveCopyAsOptions(doc, oContext, oOptions) Then
		' Set application protocol.
		' 2 = AP 203 - Configuration Controlled Design
		' 3 = AP 214 - Automotive Design
		oOptions.Value("ApplicationProtocolType") = 3
		'oOptions.Value("Author") = ""
		'oOptions.Value("Authorization") = ""
		'oOptions.Value("Description") = ""
		'oOptions.Value("Organization") = ""

		oSTEPTranslator.SaveCopyAs(doc, oContext, oOptions, oData)
		ExportToSTEP = True
    End If
End Function

Function GetFolder() As String
	'Imports System.Windows.Forms
	Dim oSFolder As String
	Dim oFDialog As New System.Windows.Forms.FolderBrowserDialog
	oFDialog.Description = "SELECT DIRECTORY TO SAVE STEP FILES IN."
	oFDialog.RootFolder = System.Environment.SpecialFolder.MyComputer
	Dim oResult As DialogResult = oFDialog.ShowDialog()
	If oResult = DialogResult.OK Then
		oSFolder = oFDialog.SelectedPath
	Else
		Return String.Empty
	End If
	Return oSFolder
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)