Batch export PDF with PartNr and Revnr

Batch export PDF with PartNr and Revnr

Jesper_S
Collaborator Collaborator
1,923 Views
11 Replies
Message 1 of 12

Batch export PDF with PartNr and Revnr

Jesper_S
Collaborator
Collaborator

Hi.

 

Im trying to modify Curtis Batch Out PDF code so that instead of using the file name for the PDF name,

i want to use the Part Number. 

And i want to add any possible RevisionNr of the drawing to be included in the PDF name.

Ex: Drawing Partnr 123456  Rev 2

will give PDF name  123456 Rev-2

 

 

'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 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 - - - - - - - - - - - -
oPath = ThisDoc.Path
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 = oPath & "\" & oAsmName & " PDF Files" 
'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
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) -0)
				oFileName = iProperties.Value("Project", "Part Number")
				oRevNum = iProperties.Value("Project", "Revision Number")

            	On Error Resume Next ' if PDF exists and is open or read only, resume next
                 'Set the PDF target file name
				If iProperties.Value("Project", "Revision Number") = Nothing Then
                                oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf"
				Else
				oDataMedium.FileName = oFolder & "\" & oFileName & " Rev-" & oRevNum & ".pdf"
				End If 
                'Write out the PDF
                Call PDFAddIn.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
'- - - - - - - - - - - - -

'- - - - - - - - - - - - -Top Level Drawing - - - - - - - - - - - -
oAsmDrawing = ThisDoc.ChangeExtension(".idw")
oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmDrawing, True)
'oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
oAsmDrawingName = iProperties.Value("Project", "Part Number")
oAsmDrawingRev = iProperties.Value("Project", "Revision Number")
'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 iProperties.Value("Project", "Revision Number") = 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
'- - - - - - - - - - - - -

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

 

 


//Jesper

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.
0 Likes
Accepted solutions (1)
1,924 Views
11 Replies
Replies (11)
Message 2 of 12

rossano_praderi
Collaborator
Collaborator

Hi Jesper,

I've modified you code which is supposed to be an external rule and read Iproperties of the referenced models.
Your original code don't have any control on the old revision, I suppose you are not interested on manage it.

 

'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
		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 = oAsmName & " PDF Files"
		'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) & "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 = ThisApplication.Documents.Open(idwPathName, True)
				'oFileName = Left(oRefDoc.DisplayName, Len(oRefDoc.DisplayName) -0)
				oFileName = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
				oRevNum = oRefDoc.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 & ".idw")) Then
			oAsmDrawingDoc = ThisApplication.Documents.Open(oAsmName & ".idw", True)
			'oAsmDrawingName = Left(oAsmDrawingDoc.DisplayName, Len(oAsmDrawingDoc.DisplayName) -3)
			oAsmDrawingName = oAsmDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
			oAsmDrawingRev = oAsmDoc.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

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 3 of 12

Jesper_S
Collaborator
Collaborator

Hi.

 

Thanks alot. Would never had figured that out by myself.

But i get an error in the PDF setup section on Line 18 and 20.

"End Of Statement expected"

'- - - - - - - - - - - - -PDF setup - - - - - - - - - - - -
		oPath = ThisDoc.Path
		PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6F​D96-2F4D-42CE-8BE0-8AEA580399E4}")
		oContext = ThisApplication.TransientObjects.CreateTranslation​Context
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		oOptions = ThisApplication.TransientObjects.CreateNameValueMa​p		oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

//Jesper

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

rossano_praderi
Collaborator
Collaborator
Accepted solution

Hi Jesper,

I didn't have any troubles with my code during my tests, may be something gone wrong when you copied and pasted.

 

Here attached you will find the .TXT file containing the same untouched code.

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 5 of 12

Jesper_S
Collaborator
Collaborator

Solved the "End Of Statement" issue.

A copy  / paste problem.

 

But doenst work anyway.

Error.PNG

Get this error message after i answer Yes to the Question in the rule.

 

 

Thanks... Worked like a charm now.

 


//Jesper

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

rossano_praderi
Collaborator
Collaborator

Ok, nice to hear it.

 

 

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 7 of 12

Jesper_S
Collaborator
Collaborator

Hi again.

 

How do i get the rule to take the Partnumber and Revisionnumber from the drawing instead of the referenced document (3D)?

Or is that even possible?

 


//Jesper

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

rossano_praderi
Collaborator
Collaborator

Hi Jesper,

you have to change the referenced document "oRefDoc" with "oDrawDoc", for the assembly is already implemented.

 

				oFileName = oDrawDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
				'oFileName = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 9 of 12

Anonymous
Not applicable

This code is excellent and works well.

I am new to ilogic and want to read the .idw from a "Drawings" folder in the same location as the models.

I am not sure how to alter the code to check this location instead. Can someone help me out?

0 Likes
Message 10 of 12

rossano_praderi
Collaborator
Collaborator

Hi,

if you would like to change only the "IDW's" folder you have to change the follow line of code.

 

idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"

with this

 

System.IO.Path.GetDirectoryName(odoc.File.FullFileName) & "\Drawings\" & System.IO.Path.GetFileNameWithoutExtension(odoc.File.FullFileName) & ".idw"

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 11 of 12

Anonymous
Not applicable

Thanks for your reply Rossano. I was very close to solving it in my attempts. 

 

The code had be tweaked slightly (I have the document declared as oRefdoc not oDoc)

 


idwPathName
= System.IO.Path.GetDirectoryName(oRefDoc.File.FullFileName) & "\Drawings\" & System.IO.Path.GetFileNameWithoutExtension(oRefDoc.File.FullFileName) & ".idw"

 

Now that I have this working, the next step is to encorperate the .stp and .dxf flat pattern exporting also 🙂

0 Likes
Message 12 of 12

rossano_praderi
Collaborator
Collaborator
You're welcome 🙂


--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes