Part or Parts of Assembly Export to STEP and PDF

Part or Parts of Assembly Export to STEP and PDF

donnie.morris
Enthusiast Enthusiast
1,290 Views
1 Reply
Message 1 of 2

Part or Parts of Assembly Export to STEP and PDF

donnie.morris
Enthusiast
Enthusiast

I have a piece of code that runs within a drawing file (.idw) that exports the file to a PDF and creates a STP of the part file. 

The problem is it only works if the drawing files that references a part file in the .idw. Not all drawings files reference a part (.ipt) file. I need the code to look at drawings of parts or assemblies, if the drawing references an assembly I need the code to create a PDF of the drawing and a STP for all parts (.ipt) files within the assembly. Is this possible? any help would be greatly appreciated.

 

Here's the code I am working with:

 

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 = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName)&".pdf"

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
Accepted solutions (1)
1,291 Views
1 Reply
Reply (1)
Message 2 of 2

Zach.Stauffer
Advocate
Advocate
Accepted solution

This rule will pdf the current drawing and export .stp files of all parts. It will not export any .iam files present though.

Imports System.Windows.Forms

Public Sub Main
	
	Dim drawingDoc As Inventor.DrawingDocument = ThisDoc.Document
	Dim sht As Sheet = drawingDoc.ActiveSheet
	Dim view As DrawingView = sht.DrawingViews.Item(1)


	' 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

		pdfAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
		pdfContext = ThisApplication.TransientObjects.CreateTranslationContext
		pdfContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		pdfOptions = ThisApplication.TransientObjects.CreateNameValueMap
		pdfDataMedium = ThisApplication.TransientObjects.CreateDataMedium

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

		'Set the PDF target file name
		pdfDataMedium.FileName = dialog.SelectedPath & "\" & System.IO.Path.ChangeExtension(drawingDoc.DisplayName, ".pdf")

		'Publish document
		pdfAddIn.SaveCopyAs(drawingDoc, pdfContext, pdfOptions, pdfDataMedium)

		'export part if part, export all parts of assembly if assembly
		Dim refDoc As Document = view.ReferencedDocumentDescriptor.ReferencedDocument
		If refDoc.DocumentType = kPartDocumentObject Then
			ExportToStepFile(refDoc, dialog.SelectedPath)
		Else If refDoc.DocumentType = kAssemblyDocumentObject Then
			Dim assemblyDoc As AssemblyDocument = refDoc
			For Each occurrence As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
				If (occurrence.DefinitionDocumentType = kPartDocumentObject) Then
					ExportToStepFile(occurrence.Definition.Document, dialog.SelectedPath)
				End If
			Next
		End If
	End If
End Sub

Public Sub ExportToStepFile(docToExport As Document, selectedFolderPath 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

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

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

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