oSTEPTranslator

oSTEPTranslator

jostroopers
Collaborator Collaborator
402 Views
2 Replies
Message 1 of 3

oSTEPTranslator

jostroopers
Collaborator
Collaborator

I have a code where I make an export from a main assembly.
This is a .step export of all underlying parts, so no assys.
A model state is activated.
These step files are written to a new location.
The part number becomes the file name.
Now we also want to use the oSTEPTranslator to write the step as: 5 = AP 242 - Managed Model Based 3D Engineering
We also want to define other options.
If we add the default snippet it doesn't go well.
The parts are no longer written to the created folder.
Can anyone help me how to insert or apply the oSTEPTranslator?

My code:

 'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = ThisDoc.FileName(False) 'without extension

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If
'get user input
RUsure = MessageBox.Show ("This will create a STEP file for all components.")
If RUsure = vbNo Then
    Return
Else
End If
'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = "Z:\Werkvoorbereiding\Step files"
'get STEP target folder path
oFolder = oPath & "\" & Werkorder & "-"  & " STEP Profielen"
'oFolder = oPath & "\" & oAsmName & " STEP Files"


'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
System.IO.Directory.CreateDirectory(oFolder)
End If

ThisDoc.ActiveModelState = "CNC Profielen"

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
		Dim oCurFileName = oCurFile.FullFileName
		Dim ShortName = IO.Path.GetFileNameWithoutExtension(oCurFileName)
		Dim oPN As String = oCurFile.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
		'might want to ckeck if oPN = "" or not
		Try
							' Get the STEP translator Add-In.
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(ThisApplication.ActiveDocument, oContext, oOptions) Then
'    ' Set application protocol.
'    ' 2 = AP 203 - Configuration Controlled Design
'    ' 3 = AP 214 - Automotive Design
'	' 5 = AP 242 - Managed Model Based 3D Engineering
'    oOptions.Value("ApplicationProtocolType") = 5
'    ' Other options...
'    oOptions.Value("Author") = iProperties.Value("Project", "Designer")
'    'oOptions.Value("Authorization") = ""
'    oOptions.Value("Description") = ""
'    oOptions.Value("Organization") = iProperties.Value("Summary", "Company")
'    oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
'    Dim oData As DataMedium
'    oData = ThisApplication.TransientObjects.CreateDataMedium
'    oData.FileName = oFolder & "\" & oCurFileName & ".stp"
'oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
'	End If
			oCurFile.SaveAs(oFolder & "\" & oPN & ".stp", True)
		Catch
			MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		End Try
		oCurFile.Close()
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder, vbNormalFocus)

 

jostroopers_0-1659963501015.png

Thanks.

 

Mvg Jos
Youre drawings are as good as the symbols that compleet them.....
0 Likes
Accepted solutions (1)
403 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @jostroopers.  I'm not sure I have the full file name of the exported STEP file set-up correct, due to seeing multiple pre-existing name specifications, but I think I may have fixed some of the other possible issues you may have been experiencing.  You will see that I moved the document type check up to before your document variable's value is set, because the way you had it could potentially result in an error.  I also eliminated some of the code that might have been pointing to a different source document.  (When using 'ThisApplication.ActiveDocument' to specify the main target document to work on, do not then use the term 'ThisDoc' in an attempt to access its properties, because that term might be pointing to a different document.  Use your original document variable instead.)  I moved the main portion of the STEP translator stuff up to before the main loop, because most of that stuff only needs to be established once.  The only setting that needed to change for each loop was the oData.FileName value.  See if the following edited version of your code will work better for you.

If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
    Exit Sub
End If
'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
'get user input
RUsure = MsgBox("This will create a STEP file for all Part type components.", vbYesNo+vbQuestion,"Export All Parts As STEP?")
If RUsure = vbNo Then Return
'- - - - - - - - - - - - -STEP setup - - - - - - - - - - - -
oPath = "Z:\Werkvoorbereiding\Step files"
'get STEP target folder path
oFolder = oPath & "\" & Werkorder & "-"  & " STEP Profielen"
'oFolder = oPath & "\" & oAsmName & " STEP Files"

'Check for the step folder and create it if it does not exist
If Not System.IO.Directory.Exists(oFolder) Then
	System.IO.Directory.CreateDirectory(oFolder)
End If

oAsmDoc.ComponentDefinition.ModelStates.Item("CNC Profielen").Activate

'you do not want to find the STEP translator and set its options within each loop, just do it once before loop
' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Set STEP Export options here one time, then set the oData.FileName down within the 'loop'
'If oSTEPTranslator.HasSaveCopyAsOptions(oRefDoc, oContext, oOptions) Then
'Set application protocol.
'2 = AP 203 - Configuration Controlled Design
'3 = AP 214 - Automotive Design
'5 = AP 242 - Managed Model Based 3D Engineering
oOptions.Value("ApplicationProtocolType") = 5
'Other options...
'oOptions.Value("Author") = iProperties.Value("Project", "Designer")
'oOptions.Value("Authorization") = ""
'oOptions.Value("Description") = ""
'oOptions.Value("Organization") = iProperties.Value("Summary", "Company")

Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
For Each oRefDoc As Document In oRefDocs
		'if is not a Part, skip to next referenced document
		If oRefDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Continue For
		'no need to open it, because it is already open in the background
		'Dim oCurFile As Document = ThisApplication.Documents.Open(oRefDoc.FullFileName, True)
		Dim oCurFileName = oRefDoc.FullFileName
		Dim ShortName = System.IO.Path.GetFileNameWithoutExtension(oCurFileName)
		Dim oPN As String = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
		'might want to ckeck if oPN = "" or not
		'oData.FileName = oFolder & "\" & oCurFileName & ".stp"
		oData.FileName = oFolder & "\" & oPN & ".stp"
		Try
			oSTEPTranslator.SaveCopyAs(oRefDoc, oContext, oOptions, oData)
			'oRefDoc.SaveAs(oFolder & "\" & oPN & ".stp", True)
		Catch
			MessageBox.Show("Error processing " & oCurFileName, "ilogic")
		End Try
Next
'- - - - - - - - - - - - -
MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new files are saved
Shell("explorer.exe " & oFolder, vbNormalFocus)

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

jostroopers
Collaborator
Collaborator

Hi Wesley @WCrihfield 

 

Super thanks for your response.
The correct data for the step file is now being written.
But also generating the step files is super fast.
Previously with my code each part was first opened visibly, which was time consuming.
Now, after activating the code, the model state becomes visible and the step files are generated in the background.
Super fast.

 

THANKS.

Mvg Jos
Youre drawings are as good as the symbols that compleet them.....
0 Likes