Page number by rs.TextObjectText


(Bojdol) #1

I am trying to use rs.TextObjectTextfunction and it works well, but when i am putting %% into text to achieve page number it is not changed into proper number, but displayed(and printed) as “%%” until I double-click on it and deactivate it. is it common issue? Is any smart way of dealing with it?


#2

Does this work?

rs.TextObjectText(txt_obj,"Page {}".format(n))


(Bojdol) #3

name ‘n’ is not defined. Maybe i’ll add that i am using python.


#4

The “n” represents your page number…


(Bojdol) #5

Yes, but I have template with the same names of text object on a lot of pages, and with this method i will have the same number on every page. I noticed that when I put %pagenumber% in triangle brackets in text it changes into proper number on each page, but i have to refresh it somehow.


#6

@bojdol, maybe you’re missing just a CommitChanges() ? Below will append the %pagenumber% to existing text object using TextFormula:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    
    id = rs.GetObject("Select text object", 512, True, False)
    if not id: return
    
    currentText = rs.TextObjectText(id)
    te = rs.coercerhinoobject(id, True, True)
    te.Geometry.TextFormula = currentText + " %<pagenumber>%" 
    te.CommitChanges()
    
    scriptcontext.doc.Views.Redraw()
    
DoSomething()

_
c.


(Bojdol) #7

This works. Thank you a lot.