LISP auto page number layout tabs

LISP auto page number layout tabs

infoJV782
Contributor Contributor
7,466 Views
33 Replies
Message 1 of 34

LISP auto page number layout tabs

infoJV782
Contributor
Contributor

Hi,

I have been using a LISP routine written by @dbhunia (attached) that puts page numbers in titleblocks using an attribute. It works great but i would like to make some changes to it, but i'm quite new to lisp and don't really know where to start. 

In addition to the pagenumber for current page, i would like a counter for total amount of layout pages in the document (format: Page x of xx). I know (length (layoutlist)) can be used to get total amount of layout tabs but i can't seem to make it work. 

Can anyone lend a helping hand? 

 

0 Likes
Accepted solutions (1)
7,467 Views
33 Replies
Replies (33)
Message 21 of 34

ronjonp
Mentor
Mentor

Rough day for me ... give this version a try:

(defun c:tabnumber (/ _put a blk_name e exclude i n s tag_name)
  (defun _put (blk tag val)
    (vl-some
      '(lambda (x) (and (wcmatch (vla-get-tagstring x) (strcase tag)) (vla-put-textstring x val)))
      (vlax-invoke blk 'getattributes)
    )
  )
  ;; Edit below for your use
  (setq	blk_name "YOURTITLEBLOCKNAME"
	tag_name "YOURATTRIBUTETAG"
	;; List of tab names to exclude
	exclude	 (mapcar 'strcase '("tabname1" "tabname2" "tabname2"))
  )
  ;; Subtract the excluded tabs from the total
  (setq i (itoa (- (length (layoutlist)) (length exclude))))
  (vlax-for l (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (or	(vl-position (strcase (vla-get-name l)) exclude)
	(setq a (cons (list (vla-get-name l) (vla-get-taborder l)) a))
    )
  )
  (if (setq s (ssget "_A" (list '(0 . "INSERT") (cons 2 blk_name) '(66 . 1))))
    (foreach b (mapcar 'cadr (ssnamex s))
      (if (setq n (cadr (assoc (cdr (assoc 410 (entget b))) a)))
	(_put (vlax-ename->vla-object b) tag_name (strcat "Page " (itoa n) " of " i))
      )
    )
  )
  (princ)
)
(vl-load-com)
0 Likes
Message 22 of 34

infoJV782
Contributor
Contributor

That fixed it! now i realise i might run into the problem of having pages numbered "Page 4 of 2" because i only excluded the tabs from the total numbe, but if i remember to order my tabs correctly that shouldn't happen. 

Thank you, you've been a great help!

0 Likes
Message 23 of 34

ronjonp
Mentor
Mentor

Glad to help 🙂

0 Likes
Message 24 of 34

infoJV782
Contributor
Contributor

I may have spoken to soon... It works well when i have all the layouts listed as excluded  in my drawing, but if one or more of the layouts i have listed to exclude is missing from the drawing it still subtracts them from the total pagecount. 

So let's say i have a drawing with two tabs in, but if three of the tabs i have listed to exclude are not in the drawing i get the output "Page 1 of -1". 

Is there a way around this problem?

0 Likes
Message 25 of 34

ronjonp
Mentor
Mentor

@infoJV782 wrote:

I may have spoken to soon... It works well when i have all the layouts listed as excluded  in my drawing, but if one or more of the layouts i have listed to exclude is missing from the drawing it still subtracts them from the total pagecount. 

So let's say i have a drawing with two tabs in, but if three of the tabs i have listed to exclude are not in the drawing i get the output "Page 1 of -1". 

Is there a way around this problem?


Give this a try:

 

(defun c:tabnumber (/ _put a blk_name e exclude i l n s tag_name)
  (defun _put (blk tag val)
    (vl-some
      '(lambda (x) (and (wcmatch (vla-get-tagstring x) (strcase tag)) (vla-put-textstring x val)))
      (vlax-invoke blk 'getattributes)
    )
  )
  ;; Edit below for your use
  (setq	blk_name "YOURTITLEBLOCKNAME"
	tag_name "YOURATTRIBUTETAG"
	;; List of tab names to exclude
	exclude	 (mapcar 'strcase '("tabname1" "tabname2" "tabname2"))
	l	 (mapcar 'strcase (layoutlist))
  )
  ;; Crosscheck that the excluded tabs exist
  (setq exclude (vl-remove-if-not '(lambda (x) (vl-position x l)) exclude))
  ;; Subtract the excluded tabs from the total
  (setq i (itoa (- (length (layoutlist)) (length exclude))))
  (vlax-for l (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (or	(vl-position (strcase (vla-get-name l)) exclude)
	(setq a (cons (list (vla-get-name l) (vla-get-taborder l)) a))
    )
  )
  (if (setq s (ssget "_A" (list '(0 . "INSERT") (cons 2 blk_name) '(66 . 1))))
    (foreach b (mapcar 'cadr (ssnamex s))
      (if (setq n (cadr (assoc (cdr (assoc 410 (entget b))) a)))
	(_put (vlax-ename->vla-object b) tag_name (strcat "Page " (itoa n) " of " i))
      )
    )
  )
  (princ)
)
(vl-load-com)

 

0 Likes
Message 26 of 34

infoJV782
Contributor
Contributor

That seems to be working more as expected, great! 

Is there an easy way to subtract the same tabs from the current page pagecount too? So the excluded tabs are excluded from the whole pagecount? i have already noticed some difficulties when batchplotting that i think that would solve. 

0 Likes
Message 27 of 34

ronjonp
Mentor
Mentor

@infoJV782 wrote:

That seems to be working more as expected, great! 

Is there an easy way to subtract the same tabs from the current page pagecount too? So the excluded tabs are excluded from the whole pagecount? i have already noticed some difficulties when batchplotting that i think that would solve. 


Not sure I follow what you're asking 🤔

0 Likes
Message 28 of 34

infoJV782
Contributor
Contributor

i mean if you can subtract the excluded tabs from the current tab count aswell? to avoid getting output like Page 4 of 2. 

0 Likes
Message 29 of 34

pbejse
Mentor
Mentor

@infoJV782 wrote:

i mean if you can subtract the excluded tabs from the current tab count aswell? to avoid getting output like Page 4 of 2. 


(defun c:tabnumber (/ _put a blk_name e exclude i l n s tag_name)
  (defun _put (blk tag val)
    (vl-some
      '(lambda (x) (and (wcmatch (vla-get-tagstring x) (strcase tag)) (vla-put-textstring x val)))
      (vlax-invoke blk 'getattributes)
    )
  )
  ;; Edit below for your use
  (setq	blk_name "YOURTITLEBLOCKNAME" 
	tag_name "YOURATTRIBUTETAG" 	
	;; List of tab names to exclude
	exclude	 (mapcar 'strcase '("tabname1" "tabname3" "tabname3"))
	l	 (mapcar 'strcase (layoutlist))
  )
  ;; Crosscheck that the excluded tabs exist
  (setq exclude (vl-remove-if-not '(lambda (x) (vl-position x l)) exclude))
  ;; Subtract the excluded tabs from the total
  (setq i (itoa (- (length (layoutlist)) (length exclude))))
  (vlax-for l (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (or	(vl-position (strcase (vla-get-name l)) exclude)
	(setq a (cons (list (vla-get-name l) (vla-get-taborder l)) a))
    )
  )
((lambda (n)
	(setq a    
	  (mapcar '(lambda (j)
		     (list (Car j) (setq n (1+ n))))
	    (vl-sort (cdr a) '(lambda (n m)(< (cadr n)(cadr m)))))
	      )
	   	)
	   	0
   )
	
  (if (setq s (ssget "_A" (list '(0 . "INSERT") (cons 2 blk_name) '(66 . 1))))
    (foreach b (mapcar 'cadr (ssnamex s))
      (if (setq n (cadr (assoc (cdr (assoc 410 (entget b))) a)))
	(_put (vlax-ename->vla-object b) tag_name (strcat "Page " (itoa n) " of " i))
      )
    )
  )
  (princ)
)

 

0 Likes
Message 30 of 34

pbejse
Mentor
Mentor

@ronjonp wrote:


Not sure I follow what you're asking 🤔

There are three layout tabs "Layout1" "Layout2" "Layout3", exclude "Layout1" 

variable a will be something like  (("Model" 0) ("Layout2" 2) ("Layout3" 3)) <-- ("Layout2" 1) ("Layout3" 2)

 

There are three layout tabs "Layout1" "Layout2" "Layout3", exclude "Layout2" 

variable a will be something like  (("Model" 0) ("Layout2" 1) ("Layout3" 3))<-- ("Layout3" 2)

 

Should renumber to fill the gap.

((lambda (n)
	(setq a    
	  (mapcar '(lambda (j)
		     (list (Car j) (setq n (1+ n))))
	    (vl-sort (cdr a) '(lambda (n m)(< (cadr n)(cadr m)))))
	      )
	   	)
	   	0
   )

 

 

 

0 Likes
Message 31 of 34

infoJV782
Contributor
Contributor

@pbejse I tried your codeand this is the result i got: It counts modeltab (even if i put it as excluded) and does not number all the non-excluded layouts in the drawing. 

I have attached a testfile where i excluded Testlayout2, but as you can see it numbered layout1 as page 2 when it should have been page 1 (probably because of modeltab) and didn't number layout3 at all even though it should. 

0 Likes
Message 32 of 34

pbejse
Mentor
Mentor

@infoJV782 wrote:

@pbejse I tried your codeand this is the result i got: It counts modeltab (even if i put it as excluded) and does not number all the non-excluded layouts in the drawing. 

 


My bad. replace the lambda bit

(vl-sort (Cdr a) '(lambda (n m)(< (cadr n)(cadr m))))

with this

((lambda (n)
	(setq a    
	  (mapcar '(lambda (j)
		     (list (Car j) (setq n (1+ n))))
	   (Cdr (vl-sort a '(lambda (n m)(< (cadr n)(cadr m))))))
	      )
	   	)
	   	0
   )

 

 

0 Likes
Message 33 of 34

ronjonp
Mentor
Mentor

@infoJV782 wrote:

That seems to be working more as expected, great! 

Is there an easy way to subtract the same tabs from the current page pagecount too? So the excluded tabs are excluded from the whole pagecount? i have already noticed some difficulties when batchplotting that i think that would solve. 


Give this a try .. on a side note have you ever worked with sheet sets ? You can manage some of this numbering stuff pretty easy with a field linked to a sheet set, and the batch plotting is easy peasy.

 

(defun c:tabnumber (/ _put a blk_name e exclude i l n s tag_name)
  (defun _put (blk tag val)
    (vl-some
      '(lambda (x) (and (wcmatch (vla-get-tagstring x) (strcase tag)) (vla-put-textstring x val)))
      (vlax-invoke blk 'getattributes)
    )
  )
  ;; Edit below for your use
  (setq	blk_name "pagecount"
	tag_name "page1"
	;; List of tab names to exclude
	exclude	 (mapcar 'strcase '("testlayout2" "tabname2" "tabname2"))
	l	 (mapcar 'strcase (layoutlist))
  )
  ;; Crosscheck that the excluded tabs exist
  (setq exclude (vl-remove-if-not '(lambda (x) (vl-position x l)) exclude))
  ;; Subtract the excluded tabs from the total
  (setq i (itoa (- (length (layoutlist)) (length exclude))))
  (vlax-for l (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
    (or	(vl-position (strcase (vla-get-name l)) exclude)
	(setq a (cons (list (vla-get-name l) (vla-get-taborder l)) a))
    )
  )
  (if (setq s (ssget "_A" (list '(0 . "INSERT") (cons 2 blk_name) '(66 . 1))))
    (foreach b (mapcar 'cadr (ssnamex s))
      (if (setq n (cadr (assoc (cdr (assoc 410 (entget b))) a)))
	(_put (vlax-ename->vla-object b) tag_name (strcat "Page " (itoa (- n (length exclude))) " of " i))
      )
    )
  )
  (princ)
)
(vl-load-com)

 

0 Likes
Message 34 of 34

JAMasiewiczPW
Explorer
Explorer
Hey guys, reviving this old post from the dead.

I was able to successfully implement this code into my templates, however I would like to modify it to exclude a variable amount of layouts that start with the same letter, for example "D1", "D2", "D3"... "Dn".

As well, I would like it to take the variable amount of "D" sheets and give them a separate X of XX sheet count.
0 Likes