iLogic Rule to export custom number of pdf sheet

iLogic Rule to export custom number of pdf sheet

Anonymous
Not applicable
2,994 Views
6 Replies
Message 1 of 7

iLogic Rule to export custom number of pdf sheet

Anonymous
Not applicable

Hello,

 

With the help of the comunity I was able to piece together a custom code that print pdf files. I have attached the code below of what  I have. My code currently spits out Page/Sheet 1 to 10. What I want to do is give the user the option to choose which sheet they want to export, instead of all of them. Eg. I want to print only page 2 to 5 or just page 1. Is there any code or example that I can add to my code that will have a pop-up window where the user inputs what sheets he wants?

 

I need to change the these lines to something else that will enable the option I want:

		oOptions.Value("Custom_Begin_Sheet") = 1
		oOptions.Value("Custom_End_Sheet") = 10

 

 

 

'iLogic rule to export PDF from Inventor Drawings
'By Fazlur Rahaman


'May 5, 2016


Imports System.Windows.Forms

'query user
'------start of iLogic-------
question = MessageBox.Show("You want to create PDF of this file?", "Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)

'set condition based on answer
If question = vbYes Then

	' 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! - please save it first",64, "PDF EXPORT!")
		Return
		End If

	' 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

	oPath = ThisDoc.Path
	oFileName = ThisDoc.FileName(False) 'without extension
	oRevNum = iProperties.Value("Project", "Revision Number")
	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
	
	'Formate PDF Setting
	If oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then
		oOptions.Value("All_Color_AS_Black") = 1
		oOptions.Value("Remove_Line_Weights") = 1
		oOptions.Value("Vector_Resolution") = 4800
		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
		'Number of pages to be published
		oOptions.Value("Custom_Begin_Sheet") = 1
		oOptions.Value("Custom_End_Sheet") = 10
	End If

	'get PDF target folder path
	oFolder = ExportPath

	If iProperties.Value("Project", "Revision Number") >= "1" Then

		'Set the PDF target file name
		oDataMedium.FileName = oFolder & "\" & oFileName & _
		" R" & oRevNum & ".pdf"
	Else
		'Set the destination file name
 		oDataMedium.FileName = oFolder & "\" & oFileName & _
		" R" & oRevNum & ".pdf"
	End If
	Else
	Return
End If

On Error Goto handlePDFLock

	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

'--------------------------------------------------?--------------------------------------------------?----------------
	'Option to the user if they want to open the file
	question2 = MessageBox.Show("Open PDF file?", "Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
 
	If question2  = vbYes Then
		ThisDoc.Launch(oDataMedium.FileName)
	End If

Exit Sub

handlePDFLock:MessageBox.Show("PDF not created, most likely someone else has it open.", "No PDF for you " & ThisApplication.GeneralOptions.UserName & "!")
'Resume Next



Accepted solutions (1)
2,995 Views
6 Replies
Replies (6)
Message 2 of 7

Owner2229
Advisor
Advisor
Accepted solution

Hi, I've made some minor spring cleaning in your code, I hope you don't mind Smiley Happy

 

Anyway, red highlighted is the code for Sheet Range input. You can use eighter single sheet (e.g.: 5) or sheet range (e.g. 5-7).

 

'iLogic rule to export PDF from Inventor Drawings
'By Fazlur Rahaman


'May 5, 2016


Imports System.Windows.Forms

' Query user
'------start of iLogic-------
question = MessageBox.Show("You want to create PDF of this file?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

' Set condition based on answer
If question <> vbYes Then Exit Sub

' 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! - please save it first",64, "PDF EXPORT!")
	Exit Sub
End If

' 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..."

If DialogResult.OK = Dialog.ShowDialog() Then ' Show dialog box
	' User clicked 'ok' on dialog box - capture the export path
	ExportPath = Dialog.SelectedPath & "\"
Else
	Exit Sub ' User clicked 'cancel' on dialog box - exit
End If

Dim SheetRange As String = InputBox("Please enter the range of sheets you want to export." & vblf & _
"You can use these input types:" & vblf & "  ' 1 ' ,  ' 1-10 ' ", "Sheet range", "1-10")
If SheetRange = vbNullString Then Exit Sub
Dim SPL As Integer = InStr(SheetRange, "-")
Dim L1 As Integer
Dim L2 As Integer
If SPL > 0 Then
	L1 = Val(Left(SheetRange, SPL))
	L2 = Val(Mid(SheetRange, SPL + 1))
Else
	L1 = Val(SheetRange)
	L2 = Val(SheetRange)
End If

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

' Formate PDF Setting
If Not oPDFAddIn.HasSaveCopyAsOptions(oDataMedium, oContext, oOptions) Then Exit Sub
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 4800
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
' Range of pages for publishing
oOptions.Value("Custom_Begin_Sheet") = L1
oOptions.Value("Custom_End_Sheet") = L2

ExportPath = ExportPath & "\" & ThisDoc.FileName(False)
If iProperties.Value("Project", "Revision Number") >= "1" Then
	ExportPath = ExportPath & " R" & iProperties.Value("Project", "Revision Number")
End If
oDataMedium.FileName = ExportPath & ".pdf"

Try
	' Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
	'--------------------------------------------------?--------------------------------------------------?----------------
	' Ask the user if he wants to open the file
	question2 = MessageBox.Show("Open PDF file?", "Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
	If question2  = vbYes Then
		ThisDoc.Launch(oDataMedium.FileName)
	End If
Catch
	MessageBox.Show("PDF not created, most likely someone else has it open.", "No PDF for you " & ThisApplication.GeneralOptions.UserName & "!")
End Try

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 7

Anonymous
Not applicable
Thank you for your help. It works flawlessly. I appreciated you take your time and cleaning up my codes, Im still new to this and learning 🙂
0 Likes
Message 4 of 7

Owner2229
Advisor
Advisor

You're welcomed Smiley Happy

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 5 of 7

RoyWickrama_RWEI
Advisor
Advisor

Nice code. I has most of the options I need to customize for my needs. Thanks a lot.

0 Likes
Message 6 of 7

Anonymous
Not applicable

Hi
Very often in one IDW I'm inserting parts from whole machine.
Is there any way to save custom sheets ranges using iLogic? For example into one PDF file, sheets from 1-3 and from 10-15 and so on.
I will be very grateful for yours opinion.

0 Likes
Message 7 of 7

william
Advocate
Advocate

Hello @Anonymous 

See this thread here https://forums.autodesk.com/t5/inventor-customization/printing-selected-pages-with-ilogic-how-to-clean-the-code/td-p/9288289 

Instead of specifying a range with kPrintSheetRange, you could use kPrintAllSheets and set the sheets that you dont want output to Exclude from printing. 

For Example, I have a rule that outputs all sheets except ones that have a sales titleblock in them. 

Dim oDrawDoc As Object = ThisApplication.ActiveDocument
Dim oSheet As Sheet

For Each oSheet In oDrawDoc.Sheets
	If oSheet.TitleBlock.Name.Contains("SALES") Then
		oSheet.ExcludeFromPrinting = True
	End If
Next

 At the end of the rule you can reset all the sheets by 

For Each oSheet In oDrawDoc.Sheets
	oSheet.ExcludeFromPrinting = False
Next
0 Likes