Save to single-sheet PDF

Save to single-sheet PDF

vickimclJRCYT
Contributor Contributor
2,153 Views
20 Replies
Message 1 of 21

Save to single-sheet PDF

vickimclJRCYT
Contributor
Contributor

Hi,

 

I am not an ilogic programmer (yet), I am just starting to learn. I copied this code from 2 different sources but I'm having a problem with the sheet number. It's saving with a 2 in the filename, but I do not understand where it is pulling this from especially when running the rule from sheet 1, as it should be a 1 not a 2. All other properties are appearing in filename correctly, except the sheet number. 

 

PDF filename: Project Number-Part Number-Sheet Number Description.pdf (Ex: S2100-A100-1 Tail Frame.pdf)

 

I know you guys are the experts, any help would be greatly appreciated. Thanks.

 

Here is the code:

 

oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
oRevNum = iProperties.Value("Project", "Revision Number")
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDescription = iProperties.Value("Project", "Description")
oPartNumber = iProperties.Value("Project", "Part Number")
oProject = iProperties.Value("Project", "PROJECT")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
oDoc = ThisDoc.Document

' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()
Dialog.SelectedPath = "T:\ENG 3D\JOBS"
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."
If DialogResult.OK = Dialog.ShowDialog() Then
ExportPath = Dialog.SelectedPath & "\"
Else
Exit Sub
End If

'Define the drawing
Dim oDrawing As DrawingDocument
oDrawing = ThisDoc.Document

Dim oSheet As Sheet
Dim lPos As Long
Dim rPos As Long
Dim sLen As Long
Dim sSheetName As String
Dim iSheetNumber As Integer

'step through each drawing sheet
For Each oSheet In oDrawing.Sheets

	'find the seperator in the sheet name:number
	lPos = InStr(oSheet.Name, ":")
	'find the number of characters in the sheet name
	sLen = Len(oSheet.Name)
	'find the sheet name
	sSheetName = Left(oSheet.Name, lPos -1)
	'find the sheet number
	iSheetNumber = Right(oSheet.Name, sLen - lPos)
	Next

'set PDF Options
If oPDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 1
oOptions.Value("Remove_Line_Weights") = 1
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
oOptions.Value("Custom_Begin_Sheet") = iSheetNumber
oOptions.Value("Custom_End_Sheet") = iSheetNumber
End If

If iProperties.Value("Project", "Revision Number") >= "1" Then
ExportPath = ExportPath
End If
oDataMedium.FileName = ExportPath & oProject & "-" & oPartNumber & "-" & iSheetNumber & " " & oDescription & ".pdf"
Try
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

' Ask the user if he/she wants to open the file
question2 = MessageBox.Show("Open PDF file?", "PDF Created",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
If question2 = vbYes Then
ThisDoc.Launch(oDataMedium.FileName)
End If
Catch
MessageBox.Show("PDF not created, document may be open.", "Please close and try again ")
End Try
Exit Sub

Thanks,

Victoria

 

0 Likes
Accepted solutions (1)
2,154 Views
20 Replies
Replies (20)
Message 2 of 21

WCrihfield
Mentor
Mentor

Try getting rid of that whole block of code for getting the sheet number, and all of the variables you created for it, then replace all of that with this:

iSheetNumber = oSheet.Name.Split(":")(1)

Split splits the name into multiple parts as an Array of String, and it will be split by the ":" character, so the resulting string array results in 2 parts...sheet name, then sheet number.  Then the (1) after the Split call is returning the second of the two parts (array's are zero based, so Item(0) is its first item).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 21

vickimclJRCYT
Contributor
Contributor

@WCrihfield 

 

Thanks for the reply. I think I need a bit more help.

 

Should I be replacing this:

 

vickimclJRCYT_0-1638909543841.png

 

 

0 Likes
Message 4 of 21

vickimclJRCYT
Contributor
Contributor

I think my problem is that it is not looping all sheets. Perhaps I need to re-arrange code.

 

I need the dialog box in order to pick the location before save. However with more than one sheet, only one sheet is being saved. Hmm..

0 Likes
Message 5 of 21

WCrihfield
Mentor
Mentor

First of all...this is not a 'solution', just a tip of sorts.  I attempted to clean up your code a bit, but I still don't think it will function the way you are expecting, because the export process is exporting the entire document, not just a single sheet.  By the way, you had the keyword "Next" right after you got the sheet number, so that ended your loop of the sheets, instead of putting Next at the end of the code.  I moved some stuff around and eliminated a couple of other things that did not appear to be used or of any use.

 

Here is the cleaned up code, but as I said, it still won't export just one sheet at a time as it is...in fact, it will now likely try to export the same PDF (of the whole drawing document) as many times as there are sheets.  I doubt inputting the oSheet variable instead of the oDrawing variable as the first input variable of the SaveCopyAs method would work, even though it is defined as an 'Object', because a sheet is not a document by itself.

'Define the drawing
Dim oDrawing As DrawingDocument = ThisDoc.Document

oDescription = iProperties.Value("Project", "Description")
oPartNumber = iProperties.Value("Project", "Part Number")
oProject = iProperties.Value("Project", "PROJECT")

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
'set PDF Options
If oPDFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
	oOptions.Value("Custom_Begin_Sheet") = iSheetNumber
	oOptions.Value("Custom_End_Sheet") = iSheetNumber
End If

Dim ExportPath As String
' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()
Dialog.SelectedPath = "T:\ENG 3D\JOBS"
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."
If DialogResult.OK = Dialog.ShowDialog() Then
	ExportPath = Dialog.SelectedPath & "\"
Else
	Exit Sub
End If

'step through each drawing sheet
For Each oSheet As Sheet In oDrawing.Sheets
	iSheetNumber = oSheet.Name.Split(":")(1)
	oDataMedium.FileName = ExportPath & oProject & "-" & oPartNumber & "-" & iSheetNumber & " " & oDescription & ".pdf"
	Try
		oPDFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
		' Ask the user if he/she wants to open the file
		question2 = MessageBox.Show("Open PDF file?", "PDF Created",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
		If question2 = vbYes Then
			ThisDoc.Launch(oDataMedium.FileName)
		End If
	Catch
		MessageBox.Show("PDF not created, document may be open.", "Please close and try again ")
	End Try
Next

 To fix the one sheet at a time problem, I believe I have seen others copy each sheet out to another new temporary drawing document, then export that other drawing document, in order to get just that one sheet exported each time, then get rid of that other temporary drawing document when the process is done...all by code of course.  If that sounds like a plan that you would want to try, we can go from there.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 21

WCrihfield
Mentor
Mentor

I went ahead and altered your code a bit more to explore the last idea I mentioned at the end of my last post, about copying each sheet over to a new temporary drawing, then exporting that other drawing.  I haven't tested it yet, because the file naming convention would not work for me, but you can try it out.  If you want, you could also specify the full file name of the drawing template file you want the other temporary drawing file to be created from, as the second input variable of the Documents.Add() method I'm using to create that other drawing.  Since that input variable is optional, and I don't know the path or name of your template, I just left it out or now.

'Define the drawing
Dim oDrawing As DrawingDocument = ThisDoc.Document

oDescription = iProperties.Value("Project", "Description")
oPartNumber = iProperties.Value("Project", "Part Number")
oProject = iProperties.Value("Project", "PROJECT")

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
'set PDF Options
If oPDFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
	oOptions.Value("Custom_Begin_Sheet") = iSheetNumber
	oOptions.Value("Custom_End_Sheet") = iSheetNumber
End If

Dim ExportPath As String
' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()
Dialog.SelectedPath = "T:\ENG 3D\JOBS"
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."
If DialogResult.OK = Dialog.ShowDialog() Then
	ExportPath = Dialog.SelectedPath & "\"
Else
	Exit Sub
End If

'step through each drawing sheet
For Each oSheet As Sheet In oDrawing.Sheets
	iSheetNumber = oSheet.Name.Split(":")(1)
	oDataMedium.FileName = ExportPath & oProject & "-" & oPartNumber & "-" & iSheetNumber & " " & oDescription & ".pdf"
	Dim oNewTempDrawing As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject)
	'copy our sheet to the other drawing
	oOtherCopiedSheet = oSheet.CopyTo(oNewTempDrawing)
	'get rid of the default first sheet from that other drawing, leaving just our copied sheet
	oNewTempDrawing.Sheets.Item(1).Delete(False)
	
	Try
		oPDFAddIn.SaveCopyAs(oNewTempDrawing, oContext, oOptions, oDataMedium)
		' Ask the user if he/she wants to open the file
		question2 = MessageBox.Show("Open PDF file?", "PDF Created",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
		If question2 = vbYes Then
			ThisDoc.Launch(oDataMedium.FileName)
		End If
	Catch
		MessageBox.Show("PDF not created, document may be open.", "Please close and try again ")
	End Try
	oNewTempDrawing.Close(True) 'True = Skip Save
Next

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 21

vickimclJRCYT
Contributor
Contributor

@WCrihfield 

 

Thanks for cleaning up the code and for your suggestions. As I mentioned, I pieced this together from 2 different sources and I'm slowly learning.

 

When running the rule now on multiple sheets, it saves just the first sheet as sheet 1, 2, 3, ...etc.. The sheet numbers are working nice now (thank you) but as you said, I need to figure out how to get all the sheets, not just the first one.

 

Thanks,

Victoria

0 Likes
Message 8 of 21

A.Acheson
Mentor
Mentor

@vickimclJRCYT  Is the task here to print to pdf each individual drawing sheet? If so the option would need to be change for each export as you loop through the collection. 

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 21

WCrihfield
Mentor
Mentor

In my last post, we are creating the new temporary drawing document within the loop, and using that in the export method, and are setting the oDataMedium.FileName within the loop, but the oContext and oOptions are being created and set outside the loop.  I wouldn't think that it would make any difference if we moved the oContext & oOptions creation & setting down inside the loop.  But I did just notice how the last two oOptions items were set, which should probably be commented out or deleted.  They are trying to use the variable iSheetNumber to set begin and end sheets in a custom sheet range, and that variable has not even been created yet or had a value set until down within the loop.  Then I would probably also change that other option from "PrintRangeEnum.kPrintSheetRange" to "PrintRangeEnum.kPrintCurrentSheet".

 

If oPDFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet
End If

I might also add in this line to ensure our copied sheet was the 'active' sheet, just after copying it over:

oOtherCopiedSheet.Activate

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 21

vickimclJRCYT
Contributor
Contributor

@A.Acheson 

 

Yes, exactly. 

0 Likes
Message 11 of 21

vickimclJRCYT
Contributor
Contributor

@WCrihfield 

 

Having a drawing with a part inserted only on the first sheet, when I run the code above, PDF's are saved out individually but I noticed the sheets were all labeled sheet 1 of 1, instead of sheet 1 of Total # of sheets like the idw.

 

I decided to cut some sections through the part and put those sections on different sheets, so there was something on all the drawings. After doing that and running the rule, I get the following error:

 

vickimclJRCYT_0-1638983767204.png

 

Man this ilogic stuff is tricky.

0 Likes
Message 12 of 21

A.Acheson
Mentor
Mentor

This rule works, the key thing here is to declare the sheet number as Integer as opposed to string or no declaration. 

As string the export will loop on the first sheet. 

Sub Main

'Define the drawing
Dim oDrawing As DrawingDocument = ThisDoc.Document

'step through each drawing sheet
For Each oSheet As Sheet In oDrawing.Sheets
	Dim iSheetNumber As Integer
	iSheetNumber = oSheet.Name.Split(":")(1)
	
	Print(iSheetNumber,oDrawing)
Next
End Sub


Sub Print (iSheetNumber As Integer, oDrawing As DrawingDocument)
oDescription = iProperties.Value("Project", "Description")
oPartNumber = iProperties.Value("Project", "Part Number")
oProject = iProperties.Value("Project", "PROJECT")

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
'set PDF Options
If oPDFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
	oOptions.Value("All_Color_AS_Black") = 1
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
	MessageBox.Show(iSheetNumber, "Title")

	oOptions.Value("Custom_Begin_Sheet") = iSheetNumber
	oOptions.Value("Custom_End_Sheet") = iSheetNumber
End If

Dim ExportPath As String
' Define folder browse dialog
Dim Dialog = New FolderBrowserDialog()
Dialog.SelectedPath = "T:\ENG 3D\JOBS"
Dialog.ShowNewFolderButton = True
Dialog.Description = "Choose Folder for Export..."
If DialogResult.OK = Dialog.ShowDialog() Then
	ExportPath = Dialog.SelectedPath & "\"
Else
	Exit Sub
End If

oDataMedium.FileName = ExportPath & oProject & "-" & oPartNumber & "-" & iSheetNumber & " " & oDescription & ".pdf"
	Try
		oPDFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
		' Ask the user if he/she wants to open the file
		question2 = MessageBox.Show("Open PDF file?", "PDF Created",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
		If question2 = vbYes Then
			ThisDoc.Launch(oDataMedium.FileName)
		End If
	Catch
		MessageBox.Show("PDF not created, document may be open.", "Please close and try again ")
	End Try
End Sub

 

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

WCrihfield
Mentor
Mentor

@A.Acheson & @vickimclJRCYT 

I was referencing a couple of my older custom codes I did for someone else in the past, in which they wanted the sheet numbers to be 1 of 1 instead of 1 of # or sheets.  In that example I needed to create the other temporary drawing file and copy the sheets over to it.  In your case, since you want the sheet names to remain as they are, that other temporary drawing document and copying process is not needed.  I just did a couple of tests of my own to prove the point (with different code not putting iProperty values in file names).  The code worked as planned...each sheet was exported separately, and individually looked just as they do in the drawing.  The trick was in setting each sheet as 'active' at the start of the loop, then having that one option set to kPrintCurrentSheet.  Everything else is just everything else.  I obviously don't do this process everyday, because I always export all sheets to one multi-page PDF, and don't include iProperty stuff in the file names.

Just for reference though, here is the code I just tested with on my system.

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
	ExportAsPDF(oDDoc)
End Sub

Sub ExportAsPDF(oDrawDoc As DrawingDocument)
	'get the PDF Translator Add-in
	Dim oPDFAddin As TranslatorAddIn
	For Each oAddin As ApplicationAddIn In ThisApplication.ApplicationAddIns
		If oAddin.DisplayName = "Translator: PDF" Then
			oPDFAddin = oAddin
		End If
	Next
	'or
	'Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")

	'Create the variables needed by the Translator Add-in
	oTO = ThisApplication.TransientObjects
	oContext = oTO.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = oTO.CreateNameValueMap
	oDataMedium = oTO.CreateDataMedium
	
	'get static parts of new file name (not sheet depentant)
	oPath = System.IO.Path.GetDirectoryName(oDrawDoc.FullFileName)
	oName = System.IO.Path.GetFileNameWithoutExtension(oDrawDoc.FullFileName)
	oDirChr = System.IO.Path.DirectorySeparatorChar
	
	'record originally 'active' sheet
	oOrigActiveSheet = oDrawDoc.ActiveSheet
	'<<<< Start loop for each sheet - file name includes sheet name >>>>
	For Each oSheet As Sheet In oDrawDoc.Sheets
		oSheet.Activate
		oSheetNumber = oSheet.Name.Split(":")(1) 'get the digit(s) after the ":"
		oNewFileName = oPath & oDirChr & oName & " Sheet " & oSheetNumber & ".pdf"
		
		'Check to see if the PDF already exists, if it does, ask if you want to overwrite it or not.
		If System.IO.File.Exists(oNewFileName) = True Then
			oAnswer = MsgBox("A PDF file with this name already exists." & vbCrLf &
			"Do you want to overwrite it with this new one?",vbYesNo + vbQuestion + vbDefaultButton2, "PDF ALREADY EXISTS")
			If oAnswer = vbNo Then Continue For
		End If
		
		oDataMedium.FileName = oNewFileName
		
		'The following If-Then statement defines the Options available, and their Values.
		If oPDFAddin.HasSaveCopyAsOptions(oDrawDoc, oContext, oOptions) Then
			oOptions.Value("Publish_All_Sheets") = 0 ' 0 = False, 1 = True
			oOptions.Value("Sheet_Range") = PrintRangeEnum.kPrintCurrentSheet
			oOptions.Value("Launch_Viewer") = 0 ' 0 = False, 1 = True
			oOptions.Value("All_Color_AS_Black") = 0 ' 0 = False, 1 = True
			oOptions.Value("Vector_Resolution") = 720 '150, 200, 400, 600, 720, 1200, 2400, 4800 ' DPI
			oOptions.Value("Remove_Line_Weights") = 0 ' 0 = False, 1 = True
		End If

		'Publish PDF
		oPDFAddin.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium)
	Next
	oOrigActiveSheet.Activate
End Sub

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 21

vickimclJRCYT
Contributor
Contributor

@A.Acheson 

 

Yes it's working! Ah I see what you did. I'm learning a lot from you guys.

 - Is there a way to have the dialog box only come up once for all sheets? Or is there no way around that?

 

Thank you,

Victoria

0 Likes
Message 15 of 21

vickimclJRCYT
Contributor
Contributor

@WCrihfield 

 

This works too! And fast.

- I do like the dialog box though because we save to different folders depending on the job.

 

Thank you,

Victoria

0 Likes
Message 16 of 21

A.Acheson
Mentor
Mentor

No problem, I kind of stumbled across that error. If you type in the value it gets recognized as an integer straight away, I had another rule which had the declaration for integer so I was able to see what was missing, we are all learning. 

AAcheson_0-1638987254248.png

The API Help doesn't have any document's on the PDF export other than the sample and some basics so that would be no help either. 

You can just move the dialogue box to the start I believe. 

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

WCrihfield
Mentor
Mentor

If using @A.Acheson code, you would have to move the folder browser code either out into its own Function, or move it up into the Sub Main area.  Right now, since it is within the Print sub routine, and that whole sub routine is being called to run for each sheet, it will want to launch for each sheet.  If you moved it up into your Sub Main area, you would then want to pass that folder data to the Print sub routine, so it can use it.  To do that, you would have to add that extra 'input' variable to the definition line of that Print sub routine, then just use that same variable as you use in the definition line down within that sub routine.

  If you move it out into its own Function, then you could use something like a SharedVariable to store the value from one loop to the next, then check to see if it has a value before calling that Function to run again...something like this:

 

If String.IsNullOrEmpty(ExportPath) Then
	ExportPath = GetFolder()

 

that would prevent it from running when not needed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 18 of 21

vickimclJRCYT
Contributor
Contributor

@WCrihfield 

 

Sorry you lost me. This is getting complicated (for me)...not you guys.

 

Victoria

0 Likes
Message 19 of 21

vickimclJRCYT
Contributor
Contributor

@WCrihfield 

 

Like this:

 

vickimclJRCYT_0-1638989214349.png

 

How do I declare the GetFolder ()  ?

 

Thanks,

Victoria

 

0 Likes
Message 20 of 21

WCrihfield
Mentor
Mentor
Accepted solution

Sorry about that.  I've got so much stuff going around in my head, sometimes I just rattle off a bunch of stuff too quickly, without proper explanations.  I started with the last working code that @A.Acheson posted, then made one of the modifications to it that I mentioned.  See if this works for you as planned.

Sub Main
	'Define the drawing
	Dim oDrawing As DrawingDocument = ThisDoc.Document

	'select folder to export to (runs our custom Function)
	Dim ExportPath As String = SelectFolder

	'step through each drawing sheet
	For Each oSheet As Sheet In oDrawing.Sheets
		'get sheet number
		Dim iSheetNumber As Integer = CInt(oSheet.Name.Split(":")(1))
		'run our custom Sub below to export the sheet to PDF
		Print(iSheetNumber, oDrawing, ExportPath)
	Next
End Sub

Function SelectFolder() As String
	Dim oFolder As String
	Dim Dialog = New FolderBrowserDialog
	Dialog.SelectedPath = "T:\ENG 3D\JOBS"
	Dialog.ShowNewFolderButton = True
	Dialog.Description = "Choose Folder for Export..."
	If DialogResult.OK = Dialog.ShowDialog() Then
		oFolder = Dialog.SelectedPath & "\"
	End If
	Return oFolder
End Function

Sub Print (iSheetNumber As Integer, oDrawing As DrawingDocument, ExportPath As String)
	oDescription = iProperties.Value("Project", "Description")
	oPartNumber = iProperties.Value("Project", "Part Number")
	oProject = iProperties.Value("Project", "PROJECT")

	Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
	("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
	'set PDF Options
	If oPDFAddIn.HasSaveCopyAsOptions(oDrawing, oContext, oOptions) Then
		oOptions.Value("All_Color_AS_Black") = 1
		oOptions.Value("Remove_Line_Weights") = 1
		oOptions.Value("Vector_Resolution") = 400
		oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintSheetRange
		'MessageBox.Show(iSheetNumber, "Title")
		oOptions.Value("Custom_Begin_Sheet") = iSheetNumber
		oOptions.Value("Custom_End_Sheet") = iSheetNumber
	End If

oDataMedium.FileName = ExportPath & oProject & "-" & oPartNumber & "-" & iSheetNumber & " " & oDescription & ".pdf"
	Try
		oPDFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
		' Ask the user if he/she wants to open the file
		question2 = MessageBox.Show("Open PDF file?", "PDF Created",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
		If question2 = vbYes Then
			ThisDoc.Launch(oDataMedium.FileName)
		End If
	Catch
		MessageBox.Show("PDF not created, document may be open.", "Please close and try again ")
	End Try
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)