Automatic page numbers rule ilogic

Automatic page numbers rule ilogic

CADZLTM4
Enthusiast Enthusiast
346 Views
11 Replies
Message 1 of 12

Automatic page numbers rule ilogic

CADZLTM4
Enthusiast
Enthusiast

Goal:

-Have a rule that runs and Numbers pages to follow something like "ABC100, ABC101, ABC102, ABC103". I will manually change the "ABC" for each different drawing file.

 

Issues:

-I've tried using my very basic coding knowledge and some ChatGPT to attempt this for 2-3 hours with no avail. GPT's method is to have a prompted entry that it will change, and then increment the number. This has not worked for some reason I can't find/understand.  I've checked 100 times, and I have a working and valid prompted entry that matches the name and pops up when I change pages. (code pasted below).

 

-There are times I add pages to the same drawings that I exclude from print/count for one-off asks from the welders, which I worry will get caught up in the rule (maybe as long as I keep them at the end, it won't matter). I was hoping to later make a Table of contents that I can insert manually on the title page. This "push it down" method will probably get caught in that created table (maybe have a condition that skips pages that have no title block active).

 

-I've thought about having a text block that is "ABC10<Sheet Number>" but at page 10, it would show "ABC1010" instead of "ABC110"

 

This is one of the many code blocks I've attempted to no avail.

Dim sheetPrefix As String = "ABC"
Dim startNumber As Integer = 100

Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim sheetIndex As Integer = 0

For Each oSheet As Sheet In oDrawDoc.Sheets
    Dim pageCode As String = sheetPrefix & (startNumber + sheetIndex).ToString

    ' Access the title block
    Dim oTB As TitleBlock
    oTB = oSheet.TitleBlock

    ' Try to update the "PAGE_CODE" text field
    Try
        oTB.SetPromptResultText("PAGE_CODE", pageCode)
    Catch
        MessageBox.Show("Field 'PAGE_CODE' not found in title block on sheet: " & oSheet.Name)
    End Try

    sheetIndex += 1
Next

 

I'm at a loss on why it's not working and frustrated that something this simple can't be done in Base Inventor (to my knowledge)

 

Thank you for any help

0 Likes
Accepted solutions (2)
347 Views
11 Replies
Replies (11)
Message 2 of 12

machiel.veldkamp
Collaborator
Collaborator

Hey... just so I understand... this is what you want to achieve?

One large .idw (or .dwg) file with a bunch of sheets?

And each sheet needs to have a different 'partnumber' displayed on it?



Try this and see what happens in your code :

 

 

Sub Main()
	Dim sheetPrefix As String = "ABC"
	Dim startNumber As Integer = 100

	Dim oDrawDoc As DrawingDocument = ThisDoc.Document
	Dim sheetIndex As Integer = 0
	Dim pageCode As String
	Dim AddNewNumber as Interger

''' Download DebugView from Microsoft sysinternals (just google it...)
''' if you don't want or cannot download anything replace "Trace.WriteLine" with "Logger.Info" and make sure the logger is active in Inventor

	trace.writeline("Value of startNumber is now " & startNumber 
	trace.writeline("Value of AddNewNumber is now " & AddNewNumber)	
	trace.writeline("Starting For Each Loop")

	For Each oSheet As Sheet In oDrawDoc.Sheets
		trace.writeline("Value of AddNewNumber starts the loop with: " & AddNewNumber)	
		trace.writeline("Value of sheetIndex is now " & sheetIndex)
	
		AddNewNumber = startNumber + sheetIndex
	
		trace.writeline("Value of AddNewNumber is now " & AddNewNumber)	
		pageCode = sheetPrefix & AddNewNumber ' I don't think you need .tostring here
		trace.writeline("Value of pageCode is now " & pageCode )		
	
		' Access the title block
		Dim oTB As TitleBlock
		oTB = oSheet.TitleBlock
	
		' Try to update the "PAGE_CODE" text field
		Try
	        	oTB.SetPromptResultText("PAGE_CODE", pageCode)
	Catch
	        	MessageBox.Show("Field 'PAGE_CODE' not found in title block on sheet: " & oSheet.Name)
		End Try
		sheetIndex += 1
		trace.writeline("Next Sheet")
	Next
End Sub

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
0 Likes
Message 3 of 12

WCrihfield
Mentor
Mentor

Hi @CADZLTM4.  I don't know if you are already aware of this or not, but Inventor automatically puts the little ":1" & ":2" index numbers at the end of our drawing sheet names, and there is nothing we can do to change that.  Because of that built-in system, if you had a Sheet named "ABC123:2" as the second sheet in the drawing, then you manually dragged it down to between the fourth & fifth sheets, that sheet would then be named "ABC123:5" automatically, and the sheet that was in fifth place before, will now have the ":6" index number at the end of its name, and so on for all Sheets after that point.  We can only control the portion of the sheet name before that automated index number, but can not control that automated index number.  With that in mind, we just have to work around that limitation.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 12

CADZLTM4
Enthusiast
Enthusiast

I tried running it. There was a missing ")" on line 13 and a typo of "interger" on line 8. After that fix, it is sending the error code of:

"Field 'PAGE_CODE' not found in title block on sheet: "

 

 

Here are attached screenshots with proof of the existence of a prompted entry named "PAGE_CODE". It's in the title block, and it's set as active on all pages plus the master template. Let me know what I'm missing. Thank you for the help.

CADZLTM4_0-1750267334920.pngCADZLTM4_1-1750267374025.png

 

0 Likes
Message 5 of 12

WCrihfield
Mentor
Mentor

Hi @machiel.veldkamp.  You may already know this, but the following line of code in your example:

oTB.SetPromptResultText("PAGE_CODE", pageCode)

...will always fail, because the first 'input parameter' that the TitleBlock.SetPromptResultText method is asking for is an actual Inventor.TextBox type object, not just a String.  To get the needed TextBox, you must dig down into the TitleBlockDefinition, through the TitleBlock.Definition property, get its sketch (a DrawingSketch type object), then iterate through its TextBoxes collection, checking each one, until you find the one for that exact 'promted entry'.  The TextBox.Text property will likely contain something like "<PAGE_CODE>", then the TextBox.FormattedText property will likely contain something like "<Prompt>PAGE_CODE</Prompt>".

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 12

CADZLTM4
Enthusiast
Enthusiast

@WCrihfield 

To the first reply: I understand about the :1 , :2 after page names on the browser, I just want a text box in the title block. My "exclude from count" comment was an example of what I do currently, but the default "page 1 of 5" isn't cutting it anymore due to combining packages and causing duplicate page numbers. The inspectors and permit people don't like that, but I don't like having to remember to change it manually, especially if I add a page and the domino effect that causes with page numbers. I just want something simple like in the screenshot below, and have it increment for other pages. I already have an Ilogic script for exporting to PDF and changing the rev# to (current+1) kinda like how fusion does with saves. Id like to tack it on to that or run it right before to avoid errors in sent out files.

CADZLTM4_0-1750269916327.png

 

In reference to your reply to @machiel.veldkamp: That is good information, but sadly, I don't know how I'd even start to implement that fix. Any recommendation?

0 Likes
Message 7 of 12

J-Camper
Advisor
Advisor
Accepted solution

@CADZLTM4 ,

This should do what you want:

Dim sheetPrefix As String = "ABC"
Dim startNumber As Integer = 100

Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim sheetIndex As Integer = 0

For Each oSheet As Sheet In oDrawDoc.Sheets
    Dim pageCode As String = sheetPrefix & (startNumber + sheetIndex).ToString

    ' Access the title block
    Dim oTB As TitleBlock = oSheet.TitleBlock
	Dim oTBtBox As Inventor.TextBox = Nothing
	For Each tBox As Inventor.TextBox In oTB.Definition.Sketch.TextBoxes
		If Not tBox.Text.Contains("<PAGE_CODE>") Then Continue For
		oTBtBox = tBox
		Exit For
	Next
	If oTBtBox Is Nothing Then Logger.Debug("No Prompted Entry: ""<PAGE_CODE>"" found in the titleblock : " & oTB.Definition.Name) : Continue For
    
	oTB.SetPromptResultText(oTBtBox, pageCode)
    sheetIndex += 1
Next

 

Message 8 of 12

CADZLTM4
Enthusiast
Enthusiast

@J-Camper You nailed it.

 

A note for future eyes, there must be a prompted entry text box named "<PAGE_CODE>" in the title block to address it to each page. It still runs when a page has no title block on it, but won't continue to number after that page. Therefore, if I keep any one-off print pages in the drawing file, it won't kill the code as long as they are after the real drawing pages. I will look into adding a "skip page if no title block exists" so the error doesn't pop up each time. Thank you everyone for your help.

0 Likes
Message 9 of 12

WCrihfield
Mentor
Mentor

Hi @CADZLTM4.  The Sheet.TitleBlock property will return Nothing if that sheet does not have a TitleBlock placed on it.

You can also check the Sheet.ExcludeFromCount property &/or the Sheet.ExcludeFromPrinting property values, if you wanted to use those as a means of which ones to skip.  There are other ways too.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 12

CADZLTM4
Enthusiast
Enthusiast

Thank you for that information. I added the skip if it's excluded from print or count, added a prompt box when run for the prefix, and added a "-" in between as a personal want.

' Set Prefix
Dim sheetPrefix As String = "ABC"
sheetPrefix = InputBox("Enter sheet prefix:", "Sheet Prefix Input", sheetPrefix)

' Starting Number
Dim startNumber As Integer = 100

Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim sheetIndex As Integer = 0

    ' Sheet Loop
For Each oSheet As Sheet In oDrawDoc.Sheets
    ' Skip sheets that are excluded from count or printing
    If oSheet.ExcludeFromCount OrElse oSheet.ExcludeFromPrinting Then Continue For

    Dim pageCode As String = sheetPrefix & "-" & (startNumber + sheetIndex).ToString

    ' Access the title block
    Dim oTB As TitleBlock = oSheet.TitleBlock
    Dim oTBtBox As Inventor.TextBox = Nothing

    For Each tBox As Inventor.TextBox In oTB.Definition.Sketch.TextBoxes
        If Not tBox.Text.Contains("<PAGE_CODE>") Then Continue For
        oTBtBox = tBox
        Exit For
    Next

    If oTBtBox Is Nothing Then
        Logger.Debug("No Prompted Entry: ""<PAGE_CODE>"" found in the titleblock : " & oTB.Definition.Name)
        Continue For
    End If

    oTB.SetPromptResultText(oTBtBox, pageCode)
    sheetIndex += 1
Next

 

0 Likes
Message 11 of 12

WCrihfield
Mentor
Mentor

You should move the check for the TitleBlock being Nothing to before it tries to access the definition of that TitleBlock, to avoid that potential error.  Because it will not be able to access its definition if it is Nothing.  Where that check is now is too late.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 12

CADZLTM4
Enthusiast
Enthusiast
Accepted solution

Thanks for the note, I've changed that. I've also added a feature in the beginning that has the abbreviation stored in a custom property called "PagePrefix". Before, it would prompt you for the prefix each time and not have memory of the current prefix, just "ABC", so anytime you want to update the sheets it would ask. Now it will ask once on a "new document" event trigger, and once the "PagePrefix" has anything but blank, it will skip the popup and just update the page numbers. Works like a dream. Ive also added it to trigger on my personal pdf export rule so no forgetting.

 

TLDR (for future readers): 

-This rule will automatically number pages that are NOT excluded from count AND print.

-It will be in the format of "ABC-100, ABC-101, ABC-102......."

-The prefix is asked once on document creation and stored in the drawing file.

-You can have a prefix as long or short as you like. You can have it start at any number.

 

Must have to work

-A Prompted entry named "<PAGE_CODE>" in your title block where you want the page number to be

-Custom Property named "PagePrefix". This is for prefix storage in each document 

-Event trigger on document creation to run this rule (not technically necessary, but convenient)

 

Downsides

-The prompted entry pops up on creation of the document and new pages but only once (you can just hit "esc") .

If someone in the future can stop that, please let me know.

 

' Check if Project Abbreviation is already set 
Dim abbrev As String

Try
    abbrev = iProperties.Value("Custom", "PagePrefix")
Catch
    abbrev = ""
End Try

If abbrev <> "" Then
    ' Already set, do nothing
    Return
End If

' Ask user for abbreviation
abbrev = InputBox("Enter project abbreviation (e.g., ABC):", "Set Project Abbreviation", "ABC")

If abbrev <> "" Then
    ' Store in Custom iProperty
    iProperties.Value("Custom", "PagePrefix") = abbrev
End If

' Set Prefix
Dim sheetPrefix As String = iProperties.Value("Custom", "PagePrefix")

' Starting Number
Dim startNumber As Integer = 100

Dim oDrawDoc As DrawingDocument = ThisDoc.Document
Dim sheetIndex As Integer = 0

' Sheet Loop
For Each oSheet As Sheet In oDrawDoc.Sheets
    ' Skip sheets that are excluded from count or printing
    If oSheet.ExcludeFromCount OrElse oSheet.ExcludeFromPrinting Then Continue For

    Dim pageCode As String = sheetPrefix & "-" & (startNumber + sheetIndex).ToString

    ' Access the title block
    Dim oTBtBox As Inventor.TextBox = Nothing
    Dim oTB As TitleBlock = oSheet.TitleBlock

    For Each tBox As Inventor.TextBox In oTB.Definition.Sketch.TextBoxes
        If Not tBox.Text.Contains("<PAGE_CODE>") Then Continue For
        oTBtBox = tBox
        Exit For
    Next

    If oTBtBox Is Nothing Then
        Logger.Debug("No Prompted Entry: ""<PAGE_CODE>"" found in the titleblock : " & oTB.Definition.Name)
        Continue For
    End If

    oTB.SetPromptResultText(oTBtBox, pageCode)
    sheetIndex += 1
Next

 

 

 

  

0 Likes