Export PDF and STP in one rule

Export PDF and STP in one rule

donnie.morris
Enthusiast Enthusiast
1,635 Views
10 Replies
Message 1 of 11

Export PDF and STP in one rule

donnie.morris
Enthusiast
Enthusiast

I have a piece of code that exports a file from the .idw to a STP. I was wondering if that code could be adapted to export both a .stp and .pdf? Is this possible?

 

Here's the code: Export to STP

 

Dim doc As DrawingDocument = ThisDoc.Document

Dim sheet As Sheet = doc.ActiveSheet

Dim view As DrawingView = sheet.DrawingViews.Item(1)

Dim partDoc As PartDocument = view.ReferencedDocumentDescriptor.ReferencedDocument

 

' 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!" _

        & vbLf & "Please save it first", 64, "Lord iLogic")

        Return

End If

 

Dim drawingNumber = iProperties.Value("Custom", "Drawing Number")

 

Dim dialog As Inventor.FileDialog

ThisApplication.CreateFileDialog(dialog)

 

dialog.InitialDirectory = ExportPath

dialog.FileName = drawingNumber

dialog.DialogTitle = "Save"

dialog.Filter = "Step files(*.stp)|*.stp|All Files (*.*)|*.*"

dialog.CancelError = True

Try

        dialog.ShowSave()

Catch ex As Exception

        Return

End Try

 

' Get the STEP translator Add-In.

Dim oSTEPTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")

Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext

Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap

 

If oSTEPTranslator.HasSaveCopyAsOptions(partDoc, 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 = ThisApplication.TransientObjects.CreateDataMedium

        oData.FileName = dialog.FileName

 

        oSTEPTranslator.SaveCopyAs(partDoc, oContext, oOptions, oData)

End If

0 Likes
Accepted solutions (2)
1,636 Views
10 Replies
Replies (10)
Message 2 of 11

Zach.Stauffer
Advocate
Advocate

Sure, after you export to .stp, export to pdf. There's an example in the API docs: Export to PDF

 

Some of the things in that code you've already done to get the .stp so you won't need to duplicate them for the pdf.

0 Likes
Message 3 of 11

donnie.morris
Enthusiast
Enthusiast

I can get the code to create both the .stp and .pdf; however, the .pdf is not created in the selected directory, it defaults to the same directory as the .idw file.

 

Any help would be greatly appreciated.

 

Dim doc As DrawingDocument = ThisDoc.Document
Dim sheet As Sheet = doc.ActiveSheet
Dim view As DrawingView = sheet.DrawingViews.Item(1)
Dim partDoc As PartDocument = view.ReferencedDocumentDescriptor.ReferencedDocument

' 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!" _
	& vbLf & "Please save it first", 64, "Lord iLogic")
	Return
End If

Dim drawingNumber = iProperties.Value("Custom", "Drawing Number")

Dim dialog As Inventor.FileDialog
ThisApplication.CreateFileDialog(dialog)

dialog.InitialDirectory = ExportPath
dialog.FileName = drawingNumber
dialog.DialogTitle = "Save"
dialog.Filter = "Step files(*.stp)|*.stp|All Files (*.*)|*.*"
dialog.CancelError = True
Try
	dialog.ShowSave()
Catch ex As Exception
	Return
End Try

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

If oSTEPTranslator.HasSaveCopyAsOptions(partDoc, 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 = ThisApplication.TransientObjects.CreateDataMedium
	oData.FileName = dialog.FileName

	oSTEPTranslator.SaveCopyAs(partDoc, oContext, oOptions, oData)
End If 


oFileName = iProperties.Value("Custom", "Drawing Number")

' Define the filename of the file to be exported
' In this Case it Is a PDF file extension
ExportFilename = oFileName & ".pdf"

oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

'set PDF Options
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets

'Set the PDF target file name
oDataMedium.FileName = ExportPath & ExportFilename

Try 
	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
Catch
	MessageBox.Show("Error writing out PDF", "iLogic")
	bError = True
End Try

If bError <> True Then
	'Ask user If they want To open (launch) the file we just exported...
	oMessage = "File exported: " & _
			ExportPath & ExportFilename & vbLf & vbLf & _
			"Do you want to open the PDF Now?"
			
	oQuestion = MessageBox.Show(oMessage, _
	"Lord iLogic - File Exported",MessageBoxButtons.YesNo)
	
	If oQuestion = vbYes Then
		ThisDoc.Launch(ExportPath & ExportFilename)
	End If

End If

0 Likes
Message 4 of 11

Zach.Stauffer
Advocate
Advocate
Accepted solution

If you want both the .stp and the .pdf file in the same directory and with the same filename (except for extension), you can still use the result from the first FileDialog you use. You just have to change the extension of the save file, which is fairly easy to do. In your PDF section, instead of

'Set the PDF target file name
oDataMedium.FileName = ExportPath & ExportFilename

use this:

'Set the PDF target file name
oDataMedium.FileName = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName)&".pdf"

 

0 Likes
Message 5 of 11

donnie.morris
Enthusiast
Enthusiast

So this rule looks at .idw files that contain an .ipt in order to create the .stp file. Can the rule be written for an .idw  that only contains .iam files.

 

I’m looking for a rule that will create .stp files for all .ipt files contained in the .iam used.

I’m not sure I’m asking the question correctly.

0 Likes
Message 6 of 11

Zach.Stauffer
Advocate
Advocate

The rule can be written for an .idw with an assembly, or it can be written for an assembly itself. Which do you need?

0 Likes
Message 7 of 11

donnie.morris
Enthusiast
Enthusiast
I would like the rule to work from the .idw file.
0 Likes
Message 8 of 11

Zach.Stauffer
Advocate
Advocate

This rule assumes view #1 (normally the first view you placed) has the assembly you want to export .stp files from. It also assumes there are only parts, no subassemblies.

Imports System.Windows.Forms

Dim doc As DrawingDocument = ThisDoc.Document
Dim sheet As Sheet = doc.ActiveSheet
Dim view As DrawingView = sheet.DrawingViews.Item(1)
Dim assembyDoc As AssemblyDocument = view.ReferencedDocumentDescriptor.ReferencedDocument

' 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

Dim dialog = New FolderBrowserDialog()

dialog.SelectedPath = ExportPath

If dialog.ShowDialog() = DialogResult.OK Then
	For Each occurrence As ComponentOccurrence In assembyDoc.ComponentDefinition.Occurrences
		If (occurrence.DefinitionDocumentType = kPartDocumentObject) Then
			Dim partDoc As PartDocument = occurrence.Definition.Document
			
			' 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

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

				Dim translatorData As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
				translatorData.FileName = dialog.SelectedPath & "\" & System.IO.Path.ChangeExtension(partDoc.DisplayName, ".stp")

				stepTranslator.SaveCopyAs(partDoc, translatorContext, translatorOptions, translatorData)
			End If 
		End If
	Next
	
	
End If


0 Likes
Message 9 of 11

donnie.morris
Enthusiast
Enthusiast

Is there a way to make the view selectable instead of defaulting to View 1?

0 Likes
Message 10 of 11

Zach.Stauffer
Advocate
Advocate
Accepted solution

Just change the first part to allow a view selection:

 

Dim view As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select View")
0 Likes
Message 11 of 11

MANOLO007565
Explorer
Explorer

Hi, here code to export to STEP



sub main()

path_and_name = ThisDoc.PathAndFileName(False) ' without extension

oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

'Set the destination file name
oDataMedium.FileName = path_and_name & ".stp"

'Publish document.
ThisDoc.Document.SaveAs(oDataMedium.FileName , True)

end sub

0 Likes