Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Export multiple sheets to separate PDF's with choosing sheets

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
Anonymous
3344 Views, 25 Replies

Export multiple sheets to separate PDF's with choosing sheets

Hi everyone

I have iLogic which export multiple sheets from idw to PDF's. This iLogic works excellent but problem is that it export all sheets from idw. Sometimes I need only 10 sheets from 30. Have anyone idea how to change iLogic to get possibility to choose few sheets from bulk, for example sheet 1, 3, 5 and 20 to 30? Thanks a lot for help in advance, Greg.

 

oPath = ThisDoc.Path
oFileName = ThisDoc.FileName(False) 'without extension
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

'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)

	 
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 0
	oOptions.Value("Vector_Resolution") = 400
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet 
	 

	'get PDF target folder path
	oFolder = Left(oPath, InStrRev(oPath, "\")) & "PDF"

	'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


	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & sSheetName & " " & iSheetNumber  & ".pdf"
	oSheet.Activate
	'Publish document
	oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

Next

 

Labels (3)
25 REPLIES 25
Message 2 of 26
FINET_Laurent
in reply to: Anonymous

Hi,

 

You could exclude the sheets from printing, the option is aviable in the sheet properties.

 

qdqsdqsdsqd.JPG

 

Or create 30 True/False parameter and toggle the variable on and off according to it like so :

 

If Sheet1Param = True Then 
	
	ThisDoc.Document.Sheets.Item("Sheet:1").ExcludeFromPrinting = True

Else If Sheet1Param = False Then
	
	ThisDoc.Document.Sheets.Item("Sheet:1").ExcludeFromPrinting = False
	
End If 

You could then create an user form with the 30 parameters..

 

On your code could add on top of that a check to see if the ExcludeFromPrinting statement is true or not before printing.

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 3 of 26
Anonymous
in reply to: FINET_Laurent

Hi Laurent

Thank you very much for solution but excluding from printing doesn't exclude it in fact when you do it by iLogic. It only works when you print via  standard printing option but then you have one file without these excluded sheets, not separate PDFs files as I need to have.

 

Second solution could be good, but if you have 50 sheets and you want to make pdfs only for 10 sheets, than clicking YES/NO 50 times is rather not nice and take some time. It is faster to print 50 sheets at once and to delete 40 that you don't need.

 

I rather thought about solution that could show some small window as in Word, when you can easily choose pages and ranges, e.g. 1-3, 10, 12, 37-42 etc.

 

Nevertheless thank you very much for help.

Message 4 of 26
WCrihfield
in reply to: Anonymous

The translators options only allow for setting "Custom_Begin_Sheet" & "Custom_End_Sheet" when its "Sheet_Range" is set to "Inventor.PrintRangeEnum.KPrintSheetRange", so the options are somewhat limited for such a long complex listing of pages and page ranges.  My advise would be to present the user with an InputBox with multiple lines of very specific instructions for the expected input (specified within the InputBox's Prompt string), so you can enter that complex comma and/or dash separated list of pages you want to publish.  Then a block of code right after that which interprets the entered data from that InputBox.  Once interpreted, you can assemble an ObjectCollection (or similar) of all the target sheets that you can use as the source of a loop, in which you publish the pages within.  Does that make sense to you?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 26
Anonymous
in reply to: WCrihfield

Well, I cannot relate to what you wrote because I don't know enough about iLogic. I feel that you are right but I cannot use it. But thank you very much for help.

 

I tried to use code below which has dialog box where you can choose sheets. Unfortunately you can choose sheets only using comma, not range what is a pity, because you cannot chose sheets with range 1-10 from 50 sheets in one idw, you must write each sheet separately 1,2,3,4 etc. Secondly it make only one PDF file, not separates PDS.

 

I tried to mix these two codes but I failed. Mayby you are right and and maybe this is not possible, I don't know.

 

Dim oDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet
Dim i As Integer

For i = 1 To oDoc.Sheets.Count
	oDoc.Sheets(i).ExcludeFromPrinting = True
Next

Dim NumSheets As Integer = oDoc.Sheets.Count
oMessage = "Select the numbers of the sheets to export." & vbCrLf & " Use "","" separator between the range of 1 to " & NumSheets

oPrints = InputBox(oMessage, "Export To pdf", "1,2")

If oPrints = "" Then Exit Sub
	
For Each wrd As Integer In oPrints.Split(",")
	oDoc.Sheets(wrd).ExcludeFromPrinting =False
Next

'-------------------Export pdf--------------------
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

oDrawName = Left(oDoc.DisplayName, Len(oDoc.DisplayName) -4)
oDataMedium.FileName = oPath & "\" & oDrawName & ".pdf"

Call PDFAddIn.SaveCopyAs(oDoc, oContext, oOptions, oDataMedium)


For i = 1 To oDoc.Sheets.Count
	oDoc.Sheets(i).ExcludeFromPrinting = False
Next
Message 6 of 26
WCrihfield
in reply to: WCrihfield

Here's an example of what I was talking about with the InputBox and interpreting its returned data to get a usable list of drawing sheets you want to export.  I haven't tested it yet, but you can give it a try if you want.

Anyways, this is just an example of an idea of how to specify a complex list of sheet numbers and sheet number ranges as input, similarly to how you would when using the print manager.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDrawing As DrawingDocument = ThisDrawing.Document
Dim oSheetsCount As Integer = oDrawing.Sheets.Count

Dim oInstructions As String = "Enter the sheet numbers you want to publish to PDF." & vbCrLf & _
"Use commas, to separate individual sheet numbers, and to separate sheet ranges." & vbCrLf & _
"Use a dash (-) between two numbers to specify a sheet range." & vbCrLf & _
"NO SPACES!"
Dim oInputSheets As String = InputBox(oInstructions, "Sheets To Publish", "1-" & oSheetsCount)
'split that string up into an array of individual strings so they can be more easily interpreted
Dim oSheetRanges() As String = Split(oInputSheets, ",")
Dim oSheetCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim oRangeStart, oRangeEnd As Integer
For Each oSR As String In oSheetRanges
	'check to see if the string includes the "-" character
	If oSR.Contains("-") Then
		'it is a sheet range, so get the two numbers
		oRangeStart = Split(oSR,"-")(0) 'Left(oSR, (InStr(oSR, "-") -1))
		oRangeEnd = Split(oSR, "-")(1)
		For i = oRangeStart To oRangeEnd
			oSheetCol.Add(oDrawing.Sheets.Item(i))
		Next
	Else
		oSheetCol.Add(oDrawing.Sheets.Item(oSR))
	End If
Next

Dim oPath As String = IO.Path.GetDirectoryName(oDrawing.FullFileName)
Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oDrawing.FullFileName)

'get PDF target folder path
Dim oFolder As String = Left(oPath, InStrRev(oPath, "\")) & "PDF"

'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

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-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

oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet

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 oSheetCol
	oSheet.Activate
	'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)
	
	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & sSheetName & " " & iSheetNumber  & ".pdf"
	
	'Publish document
	oPDFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
Next

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS :light_bulb:and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 26
FINET_Laurent
in reply to: WCrihfield

In my defense :

 

You can create a simple user form like this (with true/false parameters ) :

 

121231231.JPG

 

As I explained you can then excude the sheet from printing by checking if it is excuded or not before printing :

 

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

Dim oSheet As Sheet 
For Each oSheet In oDoc.Sheets
	If oSheet.ExcludeFromPrinting = False Then
		'Print
	End If
Next

This looks like solid to me and easy to implement...

 

Regards,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 8 of 26
Anonymous
in reply to: WCrihfield

Wow I'm in shock how fast you can write iLogic. Very impressive!

This work almost perfectly. I can choose e.g. 3-8, but cannot choose 3,5 or 3,5,7-10 that means single sheet cannot be printed using comma but, what is interesting, when I write 3-3,5-5 it works, single pages 3 and 5 are printed! Even with this malfuction your iLogic works great, thank you so much.

 

The message which shows when error occurs is below.

I tried to change sth in code but I made bigger mess than before, it's too high level for me 🙂

 

Pages 1,3.JPG

Pages 1,3_error.JPG

Message 9 of 26
WCrihfield
in reply to: Anonymous

I think I've got it fixed right this time.  Last time I forgot to convert the String version of the input numbers over to Integer type data before trying to use them as Integers.  I created my own 10 page (empty) drawing document to test this code on, and after making these tweaks, I believe I have got the data entry interpretation working correctly now.

When I entered the following into the InputBox:

1,3,5-8,10

it published 7 sheets (Sheet1, Sheet3, Sheet5, Sheet6, Sheet7, Sheet8, & Sheet10), as expected.

Oh, and I added an additional line at the end that re-activates the first sheet in the drawing, because after the code activates each sheet as they are being processed, it was leaving the last sheet in the range active when done, which was annoying.

Se if this is working OK for you now too.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDrawing As DrawingDocument = ThisDrawing.Document
Dim oSheetsCount As Integer = oDrawing.Sheets.Count

Dim oInstructions As String = "Enter the sheet numbers you want to publish to PDF." & vbCrLf & _
"Use commas, to separate individual sheet numbers, and to separate sheet ranges." & vbCrLf & _
"Use a dash (-) between two numbers to specify a sheet range." & vbCrLf & _
"NO SPACES!"
Dim oInputSheets As String = InputBox(oInstructions, "Sheets To Publish", "1-" & oSheetsCount)
'split that string up into an array of individual strings so they can be more easily interpreted
Dim oSheetRanges() As String = Split(oInputSheets, ",")
Dim oSheetCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim oRangeStart, oRangeEnd As Integer
For Each oSR As String In oSheetRanges
	'check to see if the string includes the "-" character
	If oSR.Contains("-") Then
		'it is a sheet range, so get the two numbers
		oRangeStart = CInt(Split(oSR, "-")(0))
		oRangeEnd = CInt(Split(oSR, "-")(1))
		For i As Integer = oRangeStart To oRangeEnd
			oSheetCol.Add(oDrawing.Sheets.Item(i))
		Next
	Else
		oSheetCol.Add(oDrawing.Sheets.Item(CInt(oSR)))
	End If
Next

Dim oPath As String = IO.Path.GetDirectoryName(oDrawing.FullFileName)
Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oDrawing.FullFileName)

'get PDF target folder path
Dim oFolder As String = Left(oPath, InStrRev(oPath, "\")) & "PDF"

'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

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-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

oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet

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 oSheetCol
	oSheet.Activate
	'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)
	
	'Set the PDF target file name
	oDataMedium.FileName = oFolder & "\" & sSheetName & " " & iSheetNumber  & ".pdf"
	
	'Publish document
	oPDFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
Next
'Activate the first sheet again
oDrawing.Sheets.Item(1).Activate

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 26
GosponZ
in reply to: WCrihfield

This rule work good. I do have one question. If you export for eg. 5 out of 10 sheets still in title block have 1 of 10, 2 of 10 etc.. So my question is if you guys can make to change also number of sheets in title block. That would be awesome.

Thank you guys

Message 11 of 26
Anonymous
in reply to: WCrihfield

Dear friend, it works perfectly! Now it is possible to print separate sheets in range as well as single ones. That's great! I didn't expected that this problem can be solved so fast. Thank you very much for help, I think that you helped many people here with this code. Thank you again and best regards! Greg! 

Message 12 of 26
Anonymous
in reply to: FINET_Laurent

I think that you are right but but I need to try to implement your idea what needs many time because I'm not good in iLogic. I will try to follow your tip. I hope I will manage. Thank you for help.

Message 13 of 26
WCrihfield
in reply to: Anonymous

@GosponZ 

The sheet numbers change sounds like a reasonable request.  However, it's not going to be nearly as easy as it sounds.

   In the past I believe I have seen others attempt to save each sheet of the drawing out as its own drawing document, just before exporting them to PDF, then delete the extra documents afterwards near the end of the code.  That process may get the job done, but may take too much time and seem to be a heavy user of your system's resources.

   The difficult thing about this request is that the Textbox within most TitleBlocks that contains the Sheet number and the total number of sheets in the drawing, contains 'live linked' Properties, that aren't readily available to access and modify through normal means.  The current sheet number is a 'derived' property called "Sheet Number" and seems to be within a property set called "Sheet Properties".  The total number of sheets is a 'derived' property called "Number of sheets", and seems to be within a property set called "Drawing Properties".  These seem to be 'hidden' properties that I'm not sure how to access at this time.  They aren't listed with all the other iProperties, of either the Drawing document, or the model document.

   So, what I'm doing here is just searching within the TitleBlock's sketch for a TextBox that contains the text "SHEET" OR "Sheet" to find the right TextBox.  Then I capture its current "FormattedText" value, to save it, so I can restore it later.  Then I attempt to change it to something like "SHEET 1 OF 1", just long enough for the export process of that sheet.  Then I attempt to restore the original value of that TextBox's FormattedText property, as if nothing has changed.  However, after many tweaks and tests, I haven't got it working just right yet.

Here's what I've got so far.

(Beware:  I have a couple of your settings commented out and my own settings in place so it will work in my environment.  The changes are the oFolder variable and its folder check process, as well as the oDataMedium.FileName setting.  You can just un-comment those if you need them, then comment out my version of the oDataMedium.FileName setting, before running it.)

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDrawing As DrawingDocument = ThisDrawing.Document
Dim oSheetsCount As Integer = oDrawing.Sheets.Count

Dim oInstructions As String = "Enter the sheet numbers you want to publish to PDF." & vbCrLf & _
"Use commas, to separate individual sheet numbers, and to separate sheet ranges." & vbCrLf & _
"Use a dash (-) between two numbers to specify a sheet range." & vbCrLf & _
"NO SPACES!"
Dim oInputSheets As String = InputBox(oInstructions, "Sheets To Publish", "1-" & oSheetsCount)
'split that string up into an array of individual strings so they can be more easily interpreted
Dim oSheetRanges() As String = Split(oInputSheets, ",")
Dim oSheetCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
Dim oRangeStart, oRangeEnd As Integer
For Each oSR As String In oSheetRanges
	'check to see if the string includes the "-" character
	If oSR.Contains("-") Then
		'it is a sheet range, so get the two numbers
		oRangeStart = CInt(Split(oSR, "-")(0))
		oRangeEnd = CInt(Split(oSR, "-")(1))
		For i As Integer = oRangeStart To oRangeEnd
			oSheetCol.Add(oDrawing.Sheets.Item(i))
		Next
	Else
		oSheetCol.Add(oDrawing.Sheets.Item(CInt(oSR)))
	End If
Next

Dim oPath As String = IO.Path.GetDirectoryName(oDrawing.FullFileName)
Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oDrawing.FullFileName)

''get PDF target folder path
'Dim oFolder As String = Left(oPath, InStrRev(oPath, "\")) & "PDF"

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

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-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

oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 400
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintCurrentSheet

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

Dim oTBDef As TitleBlockDefinition
Dim oSketch As DrawingSketch
Dim oTBox As Inventor.TextBox
Dim oFText As String

'step through each drawing sheet
For Each oSheet In oSheetCol
	oSheet.Activate
	'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)
	
'	oDataMedium.FileName = oFolder & "\" & sSheetName & " " & iSheetNumber  & ".pdf"
	oDataMedium.FileName = oPath & "\" & sSheetName & " " & iSheetNumber & ".pdf"
	
	oTBDef = oSheet.TitleBlock.Definition
	oTBDef.Edit(oSketch)
	For Each oTBox In oSketch.TextBoxes
		If oTBox.Text.Contains("SHEET") Or _
			oTBox.Text.Contains("Sheet") Then
			'save its current FormattedText so we can restore it later
			oFText = oTBox.FormattedText
'			oTBox.FormattedText = oTBox.FormattedText.Replace(oFText, "SHEET 1 OF 1")
			oTBox.FormattedText = oTBox.FormattedText.Replace("<DerivedProperty DerivedID='29704'>Sheet Number</DerivedProperty>", "1")
			oTBox.FormattedText = oTBox.FormattedText.Replace("<DerivedProperty DerivedID='29703'>Number of sheets</DerivedProperty>", "1")
			'change its Text temporarily for this process
'			oTBox.Text = "SHEET 1 OF 1"
			'when we exit the For loop right now, we preserve the oTBox variable's value as this TextBox
			Exit For
		End If
	Next
	oTBDef.ExitEdit(True)
	
	'Publish document
	oPDFAddIn.SaveCopyAs(oDrawing, oContext, oOptions, oDataMedium)
	
	'now restore the Sheet textbox to original setting
	'since I have to redefine oSketch here, I'm also redefining oTBox to be safe
	oTBDef.Edit(oSketch)
	For Each oTBox In oSketch.TextBoxes
		If oTBox.Text.Contains("SHEET") Or _
			oTBox.Text.Contains("Sheet") Then
			oTBox.FormattedText = oFText
			Exit For
		End If
	Next
	oTBDef.ExitEdit(True)
Next
'Activate the first sheet again
oDrawing.Sheets.Item(1).Activate

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 26
GosponZ
in reply to: WCrihfield

Great rule working great. I know it is difficult, but if you can try  if is possible, those exported pdfs to be consolidated and have as i mention earlier 1 of 5, 2 of 5 etc...

 Anyway i appreciated you time work and effort.  Thank you very much

I believe if other great programmers gives input on this. Also for some people would be great time saver.

 

Thanks again

Message 15 of 26
WCrihfield
in reply to: GosponZ

OK.  I think I understand now.  You would like a solution in which all the selected sheets are exported to one multi-page PDF document, right?  And you want the title block of each drawing sheet in the new PDF document to show that the total number of sheets is equal to only the number of sheets you exported to the PDF, right?  So if you choose sheets 1,3,5-8,10 of 10 possible sheets, you want the PDF document to have 7 pages, and the new Sheet 1 to say "SHEET 1 OF 7" and new last Sheet to say "SHEET 7 OF 7", and so on, right?  This is likely doable, but will likely require creating a new drawing document (within the code), then copying those sheets to that new document, then publishing that new drawing document to a single PDF.  Then, depending on if we saved the new drawing document or not, we can just delete it at the end, if needed.  I don't think it will be as difficult as it sounds.  I believe the sheet numbers will correct themselves automatically that way too, as long as the TitleBlock's TextBox for the  is laid out the way most are (Sheet Number Of Number of Sheets).  It may take a little while, depending on how everything else I'm working on goes.  Or someone else may jump in with their own similar solutions too, since Exporting PDF's is a fairly common topic here this forum.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 26
GosponZ
in reply to: WCrihfield

Yes that is what i mean. You understood very well. Well even with new drawings where sheet numbers will be self created would be good on close new sheet delete or perhaps with option yes or no. 

 

Thanks again

Message 17 of 26
WCrihfield
in reply to: GosponZ

Here's a rule that does the job for me, just as previously described, without having to save or delete the new drawing document it uses.  The new drawing document is only loaded in memory during code execution, then is forgotten (instead of saved), so no need to delete it later.  Although, I left that bit of code in there (commented out), just in case anyone wanted to keep it.  See if it also works for you.

Keep in mind, in this rule, I'm using the same path and file name as the original drawing, but adding " (Selected Sheets)" just before the file extension.  You can change that if you want.  So it is saving the new PDF to the same directory as the original drawing was in.  If that doesn't work for you, you can always edit the oNewName variable in my code, or completely make your own replacement for it.  One other change you may notice, is I changed value of the oOption called "Sheet_Range" to ...kPrintAllSheets, so it will include all sheets that have been copied to the new drawing document.  Then in the final line of code, I changed that target document from oDrawing to oNewDrawing, to reflect the new variable I created for it.

Here's the new code.

Edit:  OH YEAH. I am also specifying a Drawing document template location (variable named oTemplate) on my system for use when creating the new Drawing Document.  You will need to change that to point to your own drawing template file.  If you don't specify a Template, the first sheet of the new drawing may not have a border or title block around it (unless you add it by code).

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDrawing As DrawingDocument = ThisDrawing.Document
Dim oTemplate As String = ThisApplication.DesignProjectManager.ActiveDesignProject.TemplatesPath & "\A-Size Standard Drawing.idw" 
Dim oNewDrawing As DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, oTemplate, False)
Dim oSheet As Inventor.Sheet
Dim oSheetsCount As Integer = oDrawing.Sheets.Count
Dim oInstructions As String = "Enter the sheet numbers you want to publish to PDF." & vbCrLf & _
"Use commas, to separate individual sheet numbers, and to separate sheet ranges." & vbCrLf & _
"Use a dash (-) between two numbers to specify a sheet range." & vbCrLf & _
"NO SPACES!"
Dim oInputSheets As String = InputBox(oInstructions, "Sheets To Publish", "1-" & oSheetsCount)
'split that string up into an array of individual strings so they can be more easily interpreted
Dim oSheetRanges() As String = Split(oInputSheets, ",")
Dim oRangeStart, oRangeEnd As Integer
For Each oSR As String In oSheetRanges
	'check to see if the string includes the "-" character
	If oSR.Contains("-") Then
		'it is a sheet range, so get the two numbers
		oRangeStart = CInt(Split(oSR, "-")(0))
		oRangeEnd = CInt(Split(oSR, "-")(1))
		For i As Integer = oRangeStart To oRangeEnd
			oDrawing.Sheets.Item(i).CopyTo(oNewDrawing)
		Next
	Else
		oDrawing.Sheets.Item(CInt(oSR)).CopyTo(oNewDrawing)
	End If
Next

Dim oPath As String = IO.Path.GetDirectoryName(oDrawing.FullFileName)
Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oDrawing.FullFileName)

Dim oNewName As String = oPath & "\" & oFileName & " (Selected Sheets)"

'Try
'	oNewDrawing.SaveAs(oNewName & ".idw", False)
'Catch
'	MsgBox("Failed to save new drawing as: " & oNewName & ".idw.   Exiting.", vbOKOnly, " ")
'	Exit Sub
'End Try

Dim oPDFAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-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

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

'oDataMedium.FileName = oPath & "\" & oFileName & " (Selected Sheets).pdf"
oDataMedium.FileName = oNewName & ".pdf"

Try
	'Publish document
	oPDFAddIn.SaveCopyAs(oNewDrawing, oContext, oOptions, oDataMedium)
Catch
	MsgBox("Failed while trying to export the PDF as: " & oNewName & ".pdf.   Exiting.", vbOKOnly, " ")
End Try

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 18 of 26
RolandhJonsson5834
in reply to: Anonymous

Hello

Where in the string do i change, if i only want the file name and no pagenumber.

 

Message 19 of 26
jolantavoigt9563
in reply to: Anonymous

Thank you Grzegorz, this is exactly what I needed and it works pretty good. Question, what would need to be changed to save files into current location instead of creating the pdf folder?
Message 20 of 26

Hi @jolantavoigt9563.  Which message number above are you using the code from?  I need to know that, in order to determine how to change the code for you?  One of my favorite lines of code to use, when I just need the exact same path & file name, but need a different file extension, is the following one:

Dim oPDFFileName As String = System.IO.Path.ChangeExtension(oDrawing.FullFileName, ".pdf")

That does not actually change the input document's file name, is just returns a String matching that request.  I like to use that line (iLogic/vb.net), instead of the shorter iLogic only shortcut snippet (ThisDoc.PathAndFileName(False)), because the term 'ThisDoc' can often be pointing to a completely different document than you are expecting, and I already have a reference to the exact document I want to use as input.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report