batch print from assembly to PDF with Revision level in pdf name

batch print from assembly to PDF with Revision level in pdf name

nowell.colsenXK53F
Enthusiast Enthusiast
1,238 Views
16 Replies
Message 1 of 17

batch print from assembly to PDF with Revision level in pdf name

nowell.colsenXK53F
Enthusiast
Enthusiast

I trying to get this batch print to include the Rev level of the drawing in each pdf name, But its putting the top level assy revision level for all the pdfs. I have looked at all the batch print ilogic codes and have tried rewriting many times to look at each drawing rev with no luck. looking for some help on this one.

 

Sub Main()
	Dim oDoc As Document
    oDoc = ThisDoc.Document
    oDocName = System.IO.Path.GetDirectoryName(oDoc.FullFileName) & "\" & System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    
    If Not (oDoc.DocumentType = kAssemblyDocumentObject Or oDoc.DocumentType = kDrawingDocumentObject) Then
        MessageBox.Show("Please run this rule from the assembly or assembly drawing file.", "iLogic")
        Exit Sub
    End If
	
	'get user input
	If MessageBox.Show ( _
	"This will create a PDF file for all of the asembly components that have drawings files." _
	& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
	& vbLf & " " _
	& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
	& vbLf & "This could take a while.", "iLogic - Batch Output PDFs ",MessageBoxButtons.YesNo) = vbNo Then
	Exit Sub
	End If
	Dim PDFAddIn As TranslatorAddIn
	Dim oContext As TranslationContext
	Dim oOptions As NameValueMap
	Dim oDataMedium As DataMedium
	Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	
	oPath = ThisDoc.Path
	oFolder = oPath & "\" & oAsmName & " PDF Files\"
	oPath = System.IO.Directory.GetParent(oPath).FullName 		
	
	If System.IO.Directory.Exists(oFolder) = False Then
		System.IO.Directory.CreateDirectory(oFolder)
	End If
	
	
	
	'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -'look at the files referenced by the assembly
	Dim oRefDoc As Document
	Dim fileName As String
	
	For Each oRefDoc In oDoc.AllReferencedDocuments
	oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) - 4)		
		
		For Each fileName In System.IO.Directory.GetFiles(oPath, "*.dwg",System.IO.SearchOption.AllDirectories)
			If fileName.EndsWith(oFileName + ".dwg") = True Then '
				Dim oDrawDoc As DrawingDocument
				oDrawDoc = ThisApplication.Documents.Open(fileName, True)				
				On Error Resume Next
				'oRevNum = iProperties.Value("Custom", "REV LEVEL")
				oRevNum = iProperties.Value("Project", "Revision Number")
				oDataMedium.FileName = oFolder & "\" & oFileName & " REV-" & oRevNum & ".pdf"
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				oDrawDoc.Close
				On Error GoTo 0
			End If
		Next		
	Next
	'- - - - - - - - - - - - -
	
	'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
	Dim oAsmDrawingName As String = ThisDoc.FileName(False) 'without extension	

	For Each fileName In System.IO.Directory.GetFiles(oPath, "*.dwg", System.IO.SearchOption.AllDirectories)	

		If fileName.EndsWith(oAsmDrawingName + ".dwg") = True Then
			Dim oAsmDrawingDoc As DrawingDocument
			oAsmDrawingDoc = ThisApplication.Documents.Open(fileName, True)
			oAsmRevNum = iProperties.Value("Custom", "REV LEVEL")
			On Error Resume Next
			oDataMedium.FileName = oFolder & oAsmDrawingName & " REV-" & oAsmRevNum & ".pdf"		

			Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
			oAsmDrawingDoc.Close
			On Error GoTo 0
		End If
	Next	
	MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
	Shell("explorer.exe " & oFolder,vbNormalFocus)
End Sub

Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)
	oPath = ThisDoc.Path
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'oOptions.Value("Custom_Begin_Sheet") = 1
	'oOptions.Value("Custom_End_Sheet") = 1
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub
0 Likes
Accepted solutions (1)
1,239 Views
16 Replies
Replies (16)
Message 2 of 17

WCrihfield
Mentor
Mentor
Accepted solution

Hi @nowell.colsenXK53F.  What version of Inventor are you using?  I know that you will need to change this following line:

 

oAsmRevNum = iProperties.Value("Custom", "REV LEVEL")

 

...and this one:

 

oRevNum = iProperties.Value("Project", "Revision Number")

 

...because that iLogic shortcut snippet is accessing the 'active' document (always the main assembly), instead of the referenced document you want it to be accessing.  You could either use the API way to access that iProperty's value, or the newer way which became available in 2021 version.

Below is an example of how to use the API route to access the 'standard' iProperty called "Revision Number"'s value.

 

oRevNum = oDrawDoc.PropertySets.Item(1).Item("Revision Number").Value
oAsmRevNum = oAsmDrawingDoc.PropertySets.Item(1).Item("Revision Number").Value

 

...or, if you have an alternative 'Custom' property you want to read that way, you can use the following API example:

 

oAsmRevNum = oAsmDrawingDoc.PropertySets.Item(4).Item("REV LEVEL").Value

 

There is also another way, if you are interested.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 17

Jesper_S
Collaborator
Collaborator

Hi.

 

Have have used this iLogic before.

This will open a Browser for you to select where you want to save the PDF:s.

This is for .dwg files, but can be changed to .idw.

Imports System.Windows.Forms
'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
	'get user input
	RUsure = MessageBox.Show ( _
	"This will create a PDF file for all of the assembly components that have drawings files." _
	& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
	& vbLf & " " _
	& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
	& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)
If RUsure = vbYes Then
	
	
' Get current location of this file
'Dim ExportPath As String = ThisDoc.Path
Dim ExportPath As String = "C:\"

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
	' User clicked 'ok' on dialog box - capture the export path
	ExportPath = Dialog.SelectedPath & "\"
	
Else
	' User clicked 'cancel' on dialog box - exit
	Return
End If
	
	'If RUsure = vbYes Then
		oAsmDoc = ThisApplication.ActiveDocument
		oAsmName = Left(oAsmDoc.FullDocumentName, Len(oAsmDoc.FullDocumentName) - 4)
	
		'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
		PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
		oContext = ThisApplication.TransientObjects.CreateTranslationContext
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
		
		If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
		'oOptions.Value("All_Color_AS_Black") = 0
		oOptions.Value("Remove_Line_Weights") = 1
		oOptions.Value("Vector_Resolution") = 400
		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Custom_Begin_Sheet") = 1
		'oOptions.Value("Custom_End_Sheet") = 10
		End If
		
		'get PDF target folder path
		oFolder = ExportPath
		'Check for the PDF folder and create it if it does not exist
		If Not System.IO.Directory.Exists(oFolder) Then
			System.IO.Directory.CreateDirectory(oFolder)
		End If
		'- - - - - - - - - - - - -
		
		'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
		'look at the files referenced by the assembly
		Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
		Dim oRefDoc As Document
		
		'work the the drawing files for the referenced models
		'this expects that the model has a drawing of the same path and name
		For Each oRefDoc In oRefDocs
			idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "dwg"
			'check to see that the model has a drawing of the same path and name
			If (System.IO.File.Exists(idwPathName)) Then
				Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
				'oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -0)
				oFileName = oDrawDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
				oRevNum = oDrawDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value

				On Error Resume Next ' if PDF exists and is open or read only, resume next
				'Set the PDF target file name
				If oRevNum = Nothing Then
					oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf"
					'MsgBox(oFolder & "\" & oFileName & ".pdf")
				Else
					oDataMedium.FileName = oFolder & "\" & oFileName & " Rev-" & oRevNum & ".pdf"
					'MsgBox(oFolder & "\" & oFileName & " Rev-" & oRevNum & ".pdf")
				End If 
				'Write out the PDF
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				'close the file
				oDrawDoc.Close
			End If
			Next
		'- - - - - - - - - - - - -
		
		'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
		If (System.IO.File.Exists(oAsmName & ".dwg")) Then
			oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmName & ".dwg", True)
			'oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
			oAsmDrawingName = oAsmDrawingDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
			oAsmDrawingRev = oAsmDrawingDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
			'write out the PDF for the Top Level Assembly Drawing file
			On Error Resume Next ' if PDF exists and is open or read only, resume next
				'Set the PDF target file name
				If oAsmDrawingRev = Nothing Then
					oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & ".pdf"
				Else
					oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & " Rev-" & oAsmDrawingRev & ".pdf"
				End If 
			'Write out the PDF
			Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
			'Close the top level drawing
			oAsmDrawingDoc.Close
		End If
		'- - - - - - - - - - - - -
		
		'MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
		'open the folder where the new files are saved
		'Shell("explorer.exe " & oFolder,vbNormalFocus)
	End If
Else
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
End If

I haven't tested it in 2021 but worked fine in 2019.

 


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Message 4 of 17

WCrihfield
Mentor
Mentor

Hi @nowell.colsenXK53F.  Just another piece of code that you may find useful.  I noticed that the fourth line of your code was really long, and is simply getting the path & file name, without extension.  There are a couple simpler ways do do that, which require less code.

Since you area already using the 'ThisDoc.Document' term on the previous line, you could simply use this other iLogic shortcut snippet:

oDocName = ThisDoc.PathAndFileName(False)

...to get the same result, Or:

oDocName = System.IO.Path.ChangeExtension(oDoc.FullFileName, vbNullString)

...if you wanted to stick to a more vb.net way of doing it.  When using this second one, if you use an regular empty String (""), it will leave the dot (.) at the end, but when using vbNullString, it eliminates the dot.  You could also provide a different extension there, such as ".pdf", to get its regular file extension replaced with that one.  This does not change the input/original file in any way, just returns a modified String value.

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 17

p_jarzembinski89
Explorer
Explorer

Any idea what's wrong with this code ?

This error pops up

p_jarzembinski_0-1684279003635.png

 

I use the 2022 version. 

Thanks in advance for your help

 

 

0 Likes
Message 6 of 17

A.Acheson
Mentor
Mentor

Hi @p_jarzembinski89 

Can you attach the code your using? The error is indicating there is a missing object variable.

Are you receiving filenames/foldernames at the appropriate places? 

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

p_jarzembinski89
Explorer
Explorer

This ilogic from above

Imports System.Windows.Forms
'check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
	'get user input
	RUsure = MessageBox.Show ( _
	"This will create a PDF file for all of the assembly components that have drawings files." _
	& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
	& vbLf & " " _
	& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
	& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)
If RUsure = vbYes Then
	
	
' Get current location of this file
'Dim ExportPath As String = ThisDoc.Path
Dim ExportPath As String = "C:\"

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog
Dialog.SelectedPath = ExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."

' Show dialog box
If DialogResult.OK = Dialog.ShowDialog() Then
	' User clicked 'ok' on dialog box - capture the export path
	ExportPath = Dialog.SelectedPath & "\"
	
Else
	' User clicked 'cancel' on dialog box - exit
	Return
End If
	
	'If RUsure = vbYes Then
		oAsmDoc = ThisApplication.ActiveDocument
		oAsmName = Left(oAsmDoc.FullDocumentName, Len(oAsmDoc.FullDocumentName) - 4)
	
		'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
		PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
		oContext = ThisApplication.TransientObjects.CreateTranslationContext
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
		
		If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
		'oOptions.Value("All_Color_AS_Black") = 0
		oOptions.Value("Remove_Line_Weights") = 1
		oOptions.Value("Vector_Resolution") = 400
		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
		'oOptions.Value("Custom_Begin_Sheet") = 1
		'oOptions.Value("Custom_End_Sheet") = 10
		End If
		
		'get PDF target folder path
		oFolder = ExportPath
		'Check for the PDF folder and create it if it does not exist
		If Not System.IO.Directory.Exists(oFolder) Then
			System.IO.Directory.CreateDirectory(oFolder)
		End If
		'- - - - - - - - - - - - -
		
		'- - - - - - - - - - - - -Component Drawings - - - - - - - - - - - -
		'look at the files referenced by the assembly
		Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
		Dim oRefDoc As Document
		
		'work the the drawing files for the referenced models
		'this expects that the model has a drawing of the same path and name
		For Each oRefDoc In oRefDocs
			idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "dwg"
			'check to see that the model has a drawing of the same path and name
			If (System.IO.File.Exists(idwPathName)) Then
				Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
				'oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -0)
				oFileName = oDrawDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
				oRevNum = oDrawDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value

				On Error Resume Next ' if PDF exists and is open or read only, resume next
				'Set the PDF target file name
				If oRevNum = Nothing Then
					oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf"
					'MsgBox(oFolder & "\" & oFileName & ".pdf")
				Else
					oDataMedium.FileName = oFolder & "\" & oFileName & " Rev-" & oRevNum & ".pdf"
					'MsgBox(oFolder & "\" & oFileName & " Rev-" & oRevNum & ".pdf")
				End If 
				'Write out the PDF
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
				'close the file
				oDrawDoc.Close
			End If
			Next
		'- - - - - - - - - - - - -
		
		'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
		If (System.IO.File.Exists(oAsmName & ".dwg")) Then
			oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmName & ".dwg", True)
			'oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
			oAsmDrawingName = oAsmDrawingDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
			oAsmDrawingRev = oAsmDrawingDoc.PropertySets.Item("Inventor Summary Information").Item("Revision Number").Value
			'write out the PDF for the Top Level Assembly Drawing file
			On Error Resume Next ' if PDF exists and is open or read only, resume next
				'Set the PDF target file name
				If oAsmDrawingRev = Nothing Then
					oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & ".pdf"
				Else
					oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & " Rev-" & oAsmDrawingRev & ".pdf"
				End If 
			'Write out the PDF
			Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
			'Close the top level drawing
			oAsmDrawingDoc.Close
		End If
		'- - - - - - - - - - - - -
		
		'MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
		'open the folder where the new files are saved
		'Shell("explorer.exe " & oFolder,vbNormalFocus)
	End If
Else
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
End If
0 Likes
Message 8 of 17

A.Acheson
Mentor
Mentor

Another thing to check are you using inventor .dwg or .idw?

For trouble shooting insert  a message box at the start of the code and wait for it to pop up then move it down to next line untill the error message returns. This will indicate the line where it fails. Is there any pdfs created? 

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

p_jarzembinski89
Explorer
Explorer

From .idw Now I checked your code from another post and it works fine for me, but could You help me add to make pdf and dwg files to one folder selected from the disk, I am totally rocky with this, I have to make 1000 pdf and dwg files by the end of the week so you are really saving my life 

'check that the active document is an assembly file
If ThisDoc.Document.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 PDF file for all of the asembly components that have drawings files." _						
					& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
					& vbLf & " " _
					& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
					& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then:Return:Else:End If
	
'[PDF setup

PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}")  '0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'oOptions.Value("Custom_Begin_Sheet") = 2
	'oOptions.Value("Custom_End_Sheet") = 4
End If

']

'[Component Drawings 

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document

'look at the files referenced by the assembly
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments

'work the the drawing files for the referenced models
'this expects that the model has a drawing of the same path and name 

For Each oRefDoc As Document In oRefDocs

	Dim idwPathName As String = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"
	
	'check to see that the model has a drawing of the same path and name 
	If (System.IO.File.Exists(idwPathName)) Then
		
		Dim RefDrawFolder As String = IO.Path.GetDirectoryName(idwPathName)
		
		Dim oFileName As String = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
		
		Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
			
		Try
			 'Set the PDF target file name
			oDataMedium.FileName = RefDrawFolder & "\" & oFileName & "pdf"
			
			'Write out the PDF
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Catch
		End Try
		
		'close the file
		oDrawDoc.Close
		
	Else
	'If the model has no drawing of the same path and name - do nothing
	End If
Next
']

'[Top Level Drawing

Dim oAsmDrawing As String  = ThisDoc.ChangeExtension(".idw")

If (System.IO.File.Exists(oAsmDrawing)) Then
	
	Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(oAsmDrawing, True)
	
	Dim AssyDrawFolder As String = IO.Path.GetDirectoryName(oAsmDrawing)
	
	Dim oAsmDrawingName As String = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)

	'write out the PDF for the Top Level Assembly Drawing file
	Try
		 'Set the PDF target file name
		oDataMedium.FileName = AssyDrawFolder & "\" & oAsmDrawingName & "pdf"

		'Write out the PDF
		Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
	Catch ' if PDF exists and is open or read only, resume next
	End Try
	
	'Close the top level drawing
	oAsmDrawingDoc.Close
	
End If
']

 

0 Likes
Message 10 of 17

A.Acheson
Mentor
Mentor

Hi @p_jarzembinski89 

The code here was not working because of incorrect AddIn Item By ID

PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

Correct AddIn Item By ID

PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}")  

 

Code below, Allows the user to browse for a folder and then all pdf's are exported to this folder.

 

 

 

Option Explicit On
' Get user input.
Dim RUsure As MsgBoxResult = MessageBox.Show _
		("This will create a PDF file for all of the asembly components that have drawings files." _						
		& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
		& vbLf & " " _
		& vbLf & "Are you sure you want to create PDF Drawings for all of the assembly components?" _
		& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then:Return:Else:End If

' Check that the active document is an assembly file.
If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Return
End If

' Set start directory for dialog box.
Dim sExportPath As String = "C:\"

' Define folder browse dialog.
Dim Dialog = New FolderBrowserDialog()

' Set options for folder browser dialog.
Dialog.SelectedPath = sExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder to Export PDF's..."

' Show dialog boxb
If DialogResult.OK = Dialog.ShowDialog() Then
	' User clicked 'ok' on dialog box - capture the export path.
	sExportPath = Dialog.SelectedPath & "\"
Else
	' User clicked 'cancel' on dialog box - exit.
	Return
End If
	
'[PDF setup.
Dim PDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}") 
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium

If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	'oOptions.Value("Custom_Begin_Sheet") = 2
	'oOptions.Value("Custom_End_Sheet") = 4
End If
']

'[Component Drawings. 

' Define the active document as an assembly file.
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document

' Look at the files referenced by the assembly.
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments

' Loop through the referenced documents, this expects that the model has a drawing of the same path and name. 
For Each oRefDoc As Document In oRefDocs

	Dim idwPathName As String = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"
	
	' Check to see that the model has a drawing of the same path and name. 
	If (System.IO.File.Exists(idwPathName)) Then
		
		'Dim sRefDrawFolder As String = IO.Path.GetDirectoryName(idwPathName)
		
		Dim sFileName As String = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
		
		Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
			
		Try
			 ' Set the PDF target file name.
			oDataMedium.FileName = sExportPath & sFileName & "pdf"
			
			' Write out the PDF.
			Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		Catch
		End Try
		
		'close the file
		oDrawDoc.Close
		
	Else
	' If the model has no drawing of the same path and name - do nothing.
	End If
Next
']

'[Top Level Drawing.

Dim sAsmDrawing As String  = ThisDoc.ChangeExtension(".idw")

If (System.IO.File.Exists(sAsmDrawing)) Then
	
	Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(sAsmDrawing, True)
	
	'Dim sAssyDrawFolder As String = IO.Path.GetDirectoryName(sAsmDrawing)
	
	Dim sAsmDrawingName As String = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)

	'Write out the PDF for the top level assembly drawing file.
	Try
		 ' Set the PDF target file name.
		oDataMedium.FileName = sExportPath & sAsmDrawingName & "pdf"

		' Write out the PDF.
		Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
	Catch ' if PDF exists and is open or read only, resume next
	End Try
	
	' Close the top level drawing.
	oAsmDrawingDoc.Close
	
End If
']

 

 

 

 

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

p_jarzembinski89
Explorer
Explorer

Works! You are amazing ! Thank you
Last request, can it be made to export pdf and autocad .dwg files?

0 Likes
Message 12 of 17

p_jarzembinski89
Explorer
Explorer

I'm trying to combine it with this code but it doesn't work for me 😕

'define the active document as an assembly file
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
oAsmName = Left(oAsmDoc.DisplayName, Len(oAsmDoc.DisplayName) -4)

'check that the active document is an assembly file
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 DXF file for all of the asembly components that have drawings files." _
& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
& vbLf & " " _
& vbLf & "Are you sure you want to create DXF Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic  - Batch Output DXFs ",MessageBoxButtons.YesNo)

If RUsure = vbNo Then
Return
Else
End If

oPath = ThisDoc.Path

'get DXF target folder path
oFolder = oPath & "\" & oAsmName & " DXF Files"

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



'[ DXF setup

' Get the DXF translator Add-In.
Dim DXFAddIn As TranslatorAddIn
DXFAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveEditDocument
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If DXFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
	Dim strIniFile As String
	strIniFile = "C:\temp\dxfout.ini"
	' Create the name-value that specifies the ini file to use.
	oOptions.Value("Export_Acad_IniFile") = strIniFile
End If

'] end of DXF setup


'[ ComponentDrawings 
	'look at the files referenced by the assembly
	Dim oRefDocs As DocumentsEnumerator
	oRefDocs = oAsmDoc.AllReferencedDocuments
	Dim oRefDoc As Document
	
	'work the the drawing files for the referenced models
	'this expects that the model has a drawing of the same path and name 
For Each oRefDoc In oRefDocs
	idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
	
	'check to see that the model has a drawing of the same path and name 
	If(System.IO.File.Exists(idwPathName)) Then
			Dim oDrawDoc As DrawingDocument
		oDrawDoc = ThisApplication.Documents.Open(idwPathName, True)
		oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
	
		On Error Resume Next ' if DXF exists and is open or read only, resume next
		'Set the DXF target file name
		oDataMedium.FileName = oFolder & "\" & oFileName & "DXF"
		'Write out the DXF
		'Set the destination file name
		oDataMedium.FileName = oFolder & "\" & oFileName & "dxf"
		'Publish document.
		Call DXFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
		'close the file
		oDrawDoc.Close
	Else
	'If the model has no drawing of the same path and name - do nothing
	End If
Next
'] End of ComponentDrawings 



'[ Top Level Drawing 
	oAsmDrawing = ThisDoc.ChangeExtension(".idw")
	oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
	oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
	'write out the DXF for the Top Level Assembly Drawing file
	On Error Resume Next ' if DXF exists and is open or read only, resume next
	'Set the DXF target file name
	oDataMedium.FileName = oFolder & "\" & oAsmDrawingName & "dxf"
	'Write out the DXF
	Call DXFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
	'Close the top level drawing
	oAsmDrawingDoc.Close
'] Top Level Drawing 

MessageBox.Show("New Files Created in: " & vbLf & oFolder, "iLogic")
'open the folder where the new ffiles are saved
Shell("explorer.exe " & oFolder,vbNormalFocus)

  

0 Likes
Message 13 of 17

A.Acheson
Mentor
Mentor

Hi @p_jarzembinski89 

The last code you posted was conversion to dxf. You want autocad.dwg correct?

This is the help sample from API help. The sample is written in VBA so needs to be converted Just follow the pattern from the pdf addin, the main difference is you need to have the ini file in the correct path. 

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

p_jarzembinski89
Explorer
Explorer

Hi, I've already dealt with it. Thanks for the help!

0 Likes
Message 15 of 17

p_jarzembinski89
Explorer
Explorer

Hi, @A.Acheson 

Hi, I noticed a problem with the code working. Sometimes it opens the first drawing it generates pdf and dwg then it closes the drawing and then it doesn't close the idw drawings for the next ones. What is the reason ?

 

Option Explicit On
' Get user input.
Dim RUsure As MsgBoxResult = MessageBox.Show _
("This will create a PDF and DWG file for all of the asembly components that have drawings files." _
& vbLf & "This rule expects that the drawing file shares the same name and location as the component." _
& vbLf & " " _
& vbLf & "Are you sure you want to create PDF and DWG Drawings for all of the assembly components?" _
& vbLf & "This could take a while.", "iLogic  - Batch Output PDFs ",MessageBoxButtons.YesNo)
 
If RUsure = vbNo Then:Return:Else:End If
 
' Check that the active document is an assembly file.
If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then
MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
Return
End If
 
' Set start directory for dialog box.
Dim sExportPath As String = "C:\"
 
' Define folder browse dialog.
Dim Dialog = New FolderBrowserDialog()
 
' Set options for folder browser dialog.
Dialog.SelectedPath = sExportPath
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder to Export PDF's..."
 
' Show dialog boxb
If DialogResult.OK = Dialog.ShowDialog() Then
' User clicked 'ok' on dialog box - capture the export path.
sExportPath = Dialog.SelectedPath & "\"
Else
' User clicked 'cancel' on dialog box - exit.
Return
End If
 
'[PDF setup.
Dim PDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD95-2F4D-42CE-8BE0-8AEA580399E4}") 
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
 
If PDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 1200
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2
'oOptions.Value("Custom_End_Sheet") = 4
End If
']
' [Get the DWG translator Add-In.
Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
 
'Set a reference to the active document (the document to be published).
Dim oDocument As Document = ThisApplication.ActiveDocument
 
 
oContext.Type = kFileBrowseIOMechanism
 
' Create a NameValueMap object
 
 
' Create a DataMedium object
 
 
' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
'your configuration ini file
'Dim strIniFile As String = "C:\tempDWGOut.ini"
 
' Create the name-value that specifies the ini file to use.
'oOptions.Value("Export_Acad_IniFile") = strIniFile
End If
']
 
'[Component Drawings. 
 
' Define the active document as an assembly file.
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
 
' Look at the files referenced by the assembly.
Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
 
' Loop through the referenced documents, this expects that the model has a drawing of the same path and name. 
For Each oRefDoc As Document In oRefDocs
 
Dim idwPathName As String = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) -3) & "idw"
 
' Check to see that the model has a drawing of the same path and name. 
If (System.IO.File.Exists(idwPathName)) Then
 
'Dim sRefDrawFolder As String = IO.Path.GetDirectoryName(idwPathName)
 
Dim sFileName As String = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -3)
 
Dim oDrawDoc As DrawingDocument = ThisApplication.Documents.Open(idwPathName, True)
 
Try
' Set the PDF target file name.
oDataMedium.FileName = sExportPath & sFileName & "pdf" 
 
' Write out the PDF.
Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
Catch
End Try
 
Try
oDataMedium.FileName = sExportPath & sFileName & "dwg" 
Call DWGAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
Catch
End Try
'close the file
oDrawDoc.Close
 
Else
' If the model has no drawing of the same path and name - do nothing.
End If
Next
']
 
'[Top Level Drawing.
 
Dim sAsmDrawing As String  = ThisDoc.ChangeExtension(".idw")
 
If (System.IO.File.Exists(sAsmDrawing)) Then
 
Dim oAsmDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(sAsmDrawing, True)
 
'Dim sAssyDrawFolder As String = IO.Path.GetDirectoryName(sAsmDrawing)
 
Dim sAsmDrawingName As String = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
 
'Write out the PDF for the top level assembly drawing file.
Try
' Set the PDF target file name.
oDataMedium.FileName = sExportPath & sAsmDrawingName & "pdf" 
 
' Write out the PDF.
Call PDFAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
Catch ' if PDF exists and is open or read only, resume next
End Try
Try 
oDataMedium.FileName = sExportPath & sAsmDrawingName & "dwg" 
Call DWGAddIn.SaveCopyAs(oAsmDrawingDoc, oContext, oOptions, oDataMedium)
Catch
End Try
' Close the top level drawing.
oAsmDrawingDoc.Close
 
End If
']

 

0 Likes
Message 16 of 17

A.Acheson
Mentor
Mentor

This could be the close method is not correctly specified. 

Document.Close( [SkipSave] As Boolean )

oDrawDoc.Close(True)

 

It could also be that the rule moves on without adequate time to close the file. This is where having the code set up in sub routines has the advantage. Is it every document in the referenced document section? If you switch of the action(pdf....) on the document does it open and close as expected? 

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

WCrihfield
Mentor
Mentor

You are not supplying the proper inputs into the TranslatorAddIn.HasSaveCopyAs properly in the PDF part of your code, but you are doing it correctly in the DWG part of your code.  You are supplying the 'oDataMedium' variable to it first in the PDF area, when you should be supplying the Document object to it first.  The DataMedium object is not even used in that line.

Also, it might make the process go faster if you opened the drawings invisibly, instead of visibly.  I don't think they need to be opened visibly for the export processes to work properly.  Also, if they are opened invisibly, you could use Document.ReleaseReference when you are done with that document, instead of Document.Close.  Then at the end of your rule you could use ThisApplication.Documents.CloseAll(True), where True means to only close all of the unreferenced documents.  That is a good way to clean up after looping through a bunch of documents by code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes