STP Translator functionality different to DXF & PDF?

STP Translator functionality different to DXF & PDF?

arkelec
Collaborator Collaborator
332 Views
2 Replies
Message 1 of 3

STP Translator functionality different to DXF & PDF?

arkelec
Collaborator
Collaborator

I'm trying to set up iLogic functions to export from a drawing to STP, DXF, DWG (AutoCAD) & PDF.

 

The STP code works well & it allows me to create a new sub-folder, to the folder automatically loaded using the code below:

Imports System.Windows.Forms
'
Dim dialog = New FolderBrowserDialog()
dialog.SelectedPath = ExportPath
If dialog.ShowDialog() = DialogResult.OK Then
'sub called here
End If

 

Here's the full code for the STP export:

Imports System.Windows.Forms

Public Sub Main

	Dim drawingDoc As Inventor.DrawingDocument = ThisDoc.Document
	Dim strSaveAsSuff As String = " Rev " & iProperties.Value("Custom", "c_Revision_Nº")

	' Get current location of this file
	Dim ExportPath As String = ThisDoc.Path
	
	' Check that this file has been saved and actually exists on disk
	If String.IsNullOrEmpty(ExportPath) Then
		MsgBox("This file has not yet been saved and doesn't exist on disk!")
		Return
	End If
	
		' Browse for location to save file
		Dim dialog = New FolderBrowserDialog()
		dialog.SelectedPath = ExportPath
		If dialog.ShowDialog() = DialogResult.OK Then
		
			For Each sht As Sheet In drawingDoc.Sheets
			Dim oView As DrawingView = sht.DrawingViews.Item(1)

				' export part if part, export assembly if assembly
				Dim refDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
					If refDoc.DocumentType = kPartDocumentObject Then
					'ExportToStepFile(refDoc, dialog.SelectedPath)
					ExportToStepFile(refDoc, dialog.SelectedPath, strSaveAsSuff)
				Else If refDoc.DocumentType = kAssemblyDocumentObject Then
					'ExportToStepFile(refDoc, dialog.SelectedPath)
					ExportToStepFile(refDoc, dialog.SelectedPath, strSaveAsSuff)	
				End If	
			Next 'sheet
		End If
End Sub	


Public Sub ExportToStepFile(docToExport As Document, selectedFolderPath As String, selectedSuffix as String)

	' Get the STEP translator Add-In.
	Dim stepTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")
	Dim translatorContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim translatorOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

	
	Dim newFilename as string = doctoexport.displayname.replace(".ipt","").replace(".iam","")
	Dim expPATH as string = selectedFolderPath & "\STP"
	
	newfilename = newfilename & selectedsuffix
	if doctoexport.documenttype = kassemblydocumentobject then newfilename = newfilename &".iam" else newfilename = newfilename & ".ipt"

	If stepTranslator.HasSaveCopyAsOptions(docToExport, translatorContext, translatorOptions) Then
		translatorOptions.Value("ApplicationProtocolType") = 3
		translatorContext.Type = IOMechanismEnum.kFileBrowseIOMechanism

		Dim translatorData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium

		translatorData.FileName = expPATH & "\" & System.IO.Path.ChangeExtension(newFilename, ".stp")
		stepTranslator.SaveCopyAs(docToExport, translatorContext, translatorOptions, translatorData)
	End If 
End Sub

 

Trying to replicate the STP code for DXF or PDF results in an error, seemingly with the syntax in line 59 of the STP code.

translatorData.FileName = expPATH & "\" & System.IO.Path.ChangeExtension(newFilename, ".stp")

 

The original code that I found on this forum was:

translatorData.FileName = selectedFolderPath & "\" & System.IO.Path.ChangeExtension(docToExport.DisplayName, ".stp")

 

Ignoring the differences in the final set of (....), which I don't think have any bearing on the issue, the issue seems to be with "expPATH" vs "selectedFolderPath".

 

As can be seen in the STP code (line 48), I've created "expPATH" to append the desired folder to that returned on line

9, "Dim ExportPath As String = ThisDoc.Path".

 

See also line 19, "dialog.SelectedPath = ExportPath".

 

When I run the DXF code below using "expPATH" - (line 41), it fails with Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).

 

When I run the DXF code below using "selectedFolderPath" - (line 42), it works.

 

DXF Export code:

Imports System.Windows.Forms

Public Sub Main
	
	Dim drawingDoc As Inventor.DrawingDocument = ThisDoc.Document

	' Get current location of this file
	Dim ExportPath As String = ThisDoc.Path

	' Check that this file has been saved and actually exists on disk
	If String.IsNullOrEmpty(ExportPath) Then
		MsgBox("This file has not yet been saved and doesn't exist on disk!")
		Return
	End If

		' Browse for location to save file
		Dim dialog = New FolderBrowserDialog()
		dialog.SelectedPath = ExportPath
		If dialog.ShowDialog() = DialogResult.OK Then
		
			For Each sht As Sheet In drawingDoc.Sheets
			ExportToDXF(drawingDoc, dialog.SelectedPath)
		Next 'sheet
	End If
End Sub	

Public Sub ExportToDXF(docToExport As Document, selectedFolderPath As String)

	' Get the STEP translator Add-In.
	Dim dxfTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
	Dim translatorContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim translatorOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	Dim expPATH as string = selectedFolderPath & "\DXF"
	
	If dxfTranslator.HasSaveCopyAsOptions(docToExport, translatorContext, translatorOptions) Then
		strIniFile = "\\ARKBUILDSERVWSE\Shared Folders\ARK CAD\Customisation\Inventor\Configuration\ARKexportdxf.ini"
		translatorOptions.Value("Export_Acad_IniFile") = strIniFile
		translatorContext.Type = kFileBrowseIOMechanism

		Dim translatorData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
		'translatorData.FileName = expPATH & "\" & System.IO.Path.ChangeExtension(docToExport.DisplayName, ".dxf")
		translatorData.FileName = selectedFolderPath & "\" & System.IO.Path.ChangeExtension(docToExport.DisplayName, ".dxf")

		dxfTranslator.SaveCopyAs(docToExport, translatorContext, translatorOptions, translatorData)
	End If 
End Sub

 

I have the same problem with PDF export.

I appreciate that the STP code is working with model files via drawing views, but I can't see how this would affect the function.

Is this something to do with the way the translators work, or have I missed something else?

0 Likes
333 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor

Hi @arkelec 

The problem looks to be that the folder hasn't been create yet. So therefore it is trying to save a file to a folder that does not exist.  Use this line to create the Directory (Folder).

 

System.IO.Directory.CreateDirectory(expPATH)

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 3

op_thorsager
Enthusiast
Enthusiast

Here's an export sub i like to use, this prompts a dialog to select a location where you want to save your files.

if the necessary folder's aren't there yet, then they will be created.

 

Sub Main()
Export

STPsave

End Sub
Dim Path3D As String
Dim Path2D As String
'Dim PathO As String
Dim TopDoc As Document
Sub STPsave
	' 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
    oOptions.Value("ApplicationProtocolType") = 2
    ' 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 = Path3D & Parameter(FileNaming) & ".stp"
oSTEPTranslator.SaveCopyAs(ThisApplication.ActiveDocument, oContext, oOptions, oData)
End If
End Sub

Sub Export
Dim filePath As String
Dim subFolderName As String
TopDoc = ThisDoc.Document
' Prompt the user to select the file location
subFolderName = Parameter(FileNaming)
Dim FolderBrowserDialog1 As New FolderBrowserDialog
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
	filePath = FolderBrowserDialog1.SelectedPath
	' Create the subfolder
	Path3D = filePath & "\" & subFolderName & "\3D\"
	Path2D = filePath & "\" & subFolderName & "\2D\"
	PathO = filePath & "\" & subFolderName & "\"
	
	If Not IO.Directory.Exists(filePath & "\" & subFolderName) Then
		IO.Directory.CreateDirectory(Path3D)
		IO.Directory.CreateDirectory(Path2D)
			End If
End If
Parameter(Location) = filePath & "\" & subFolderName 
iProperties.Value("Custom", "Location") = Parameter(Location)
End Sub

 

 This code is run from an assembly rule, and saves a copy of assembly as a step file.

 

I haven't used this code in a while, but last time i tried, it was working. 

the parameter called filenaming is updated from a seperate rule, which imports a set of excel sheet values. one of the cells dictate the filenaming, which could be something like partnumber or drawing number.

the parameter called Location is updated when a selected save location is set. 

 

If anything you could always have the DXF/PDF/DWG/STEP exports run in seperate rules, so the step export runs on an assembly level, whereas DXF/DWG/PDF runs from the drawing file. By using the

iLogicVb.RunRule("PartA:1", "ruleName")

I dont know if this is the solution you're looking for, and i bet there's a more straight forward way to do things.

 

I tend to start exporting everything i need from the assembly first, then i have a code which automatically opens a copy of a drawing setup, which is saved to path2D. From here i do all of my PDF/DWG/DXF exports/saves

0 Likes