View Full Version : Getting the number of a layout tab
whackoprogrammer760234
2017-12-03, 04:56 AM
Hey all,
Just getting started with AutoLISP.
At work we don't use sheets but layouts instead. Perhaps it's not the most proper way but that's the way it is. We find layouts are faster since each person completes 15-20 multi-page projects a day.
Considering the volume of work I want to automate some things, like populating fields.
The subject of this thread is getting a text object to display a page count such as "3 of 5"
I Googled the subject and you may be quick to point out that similar sounding questions have been asked before. Maybe not quite.
What I have:
(setq layout_count_var (length(layoutlist)))
in acaddoc.lsp happily returns the count of layout tabs, but that's only half of the solution.
I found a suggested solution that used DieselExpression, BUT. IT. JUST. DIDN'T. WORK.
Is there an AutoLISP solution to make a text object (in each layout) display a layout tab's order number?
I use AutoCAD 2018 if it matters.
Insight is appreciated.
Thanks!
Tharwat
2017-12-03, 04:12 PM
Hi,
Give this routine a try and let me know.
(defun c:lay2fld (/ doc lay pnt ids id1 id2)
;;------------------------------------;;
;; Tharwat - Date: 03.Dec.2017 ;;
;;------------------------------------;;
(defun ids (obj doc / uty)
(if (vlax-method-applicable-p
(setq uty (vla-get-utility doc))
'getobjectidstring
)
(vla-getobjectidstring uty obj :vlax-false)
(itoa (vla-get-objectid obj))
)
)
(and (setq pnt (getpoint "\nSpecify a point :"))
(setq doc (vla-get-activedocument (vlax-get-acad-object))
lay (vla-get-layouts doc)
id1 (ids (vla-item lay (getvar 'ctab)) doc)
id2 (ids lay doc)
)
(vla-addtext
(vla-get-block (vla-get-activelayout doc))
(strcat
"%<\\AcObjProp Object(%<\\_ObjId " id1 ">%).TabOrder>%"
" of " "%<\\AcExpr %<\\AcObjProp Object(%<\\_ObjId " id2
">%).Count>%-1 >%"
)
(vlax-3d-point pnt)
(getvar 'textsize)
)
)
(princ)
) (vl-load-com)
whackoprogrammer760234
2017-12-03, 07:54 PM
Hi,
Give this routine a try and let me know.
(defun c:lay2fld (/ doc lay pnt ids id1 id2)
;;------------------------------------;;
;; Tharwat - Date: 03.Dec.2017 ;;
;;------------------------------------;;
(defun ids (obj doc / uty)
(if (vlax-method-applicable-p
(setq uty (vla-get-utility doc))
'getobjectidstring
)
(vla-getobjectidstring uty obj :vlax-false)
(itoa (vla-get-objectid obj))
)
)
(and (setq pnt (getpoint "\nSpecify a point :"))
(setq doc (vla-get-activedocument (vlax-get-acad-object))
lay (vla-get-layouts doc)
id1 (ids (vla-item lay (getvar 'ctab)) doc)
id2 (ids lay doc)
)
(vla-addtext
(vla-get-block (vla-get-activelayout doc))
(strcat
"%<\\AcObjProp Object(%<\\_ObjId " id1 ">%).TabOrder>%"
" of " "%<\\AcExpr %<\\AcObjProp Object(%<\\_ObjId " id2
">%).Count>%-1 >%"
)
(vlax-3d-point pnt)
(getvar 'textsize)
)
)
(princ)
) (vl-load-com)
Seems to work as intended! Thanks!
Tharwat
2017-12-04, 04:48 AM
Seems to work as intended! Thanks!
You're welcome.
BIG-AL
2017-12-09, 02:12 AM
if you want just post, I have read the layout name and put the numeric part as the sheet number (layout1 D01 this is handy if you have say two dwgs with layouts 1-10 the second with layouts 11-20 etc Also have a total renumber it will put sheet 1 of 9 next 2 of 9 etc as our title block has a attribute for most values so can change any of them.
What I am saying is if your title block is in your layout you can do lots of stuff very easy.
Others to do with layouts include GOTO x jump to layout X useful I have 88 layouts in one project.
an example
; dwg index to a table
; by Alan H NOV 2013
(defun AH:dwgindex (/ doc objtable ss1 lay ans ans2 plotabs ss1 tag2 tag3 list1 list2 curlayout colwidth numcolumns numrows INC rowheight )
(vl-load-com)
(setq curlayout (getvar "ctab"))
(if (= curlayout "Model")
(progn
(Alert "You need to be in a layout for this option")
(exit)
) ; end progn
) ; end if model
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq curspace (vla-get-paperspace doc))
(setq pt1 (vlax-3d-point (getpoint "\nPick point for top left hand of table: ")))
; read values from title blocks
(setq bname "DA1DRTXT")
(setq tag2 "DRG_NO") ;attribute tag name
(setq tag3 "WORKS_DESCRIPTION") ;attribute tag name
(setq ss1 (ssget "x" (list (cons 0 "INSERT") (cons 2 bname))))
(if (= ss1 nil) ; for xxxx jobs
(progn
(setq bname "my_TITLE")
(setq ss1 (ssget "x" (list (cons 0 "INSERT") (cons 2 bname))))
)
)
(setq INC (sslength ss1))
(repeat INC
(foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS1 (SETQ INC (- INC 1)) )) 'getattributes)
(if (= tag2 (strcase (vla-get-tagstring att)))
(progn
(setq ans (vla-get-textstring att))
(if (/= ans NIL)
(setq list1 (cons ans list1))
) ; if
); end progn
) ; end if
(if (= tag3 (strcase (vla-get-tagstring att)))
(progn
(setq ans2 (vla-get-textstring att))
(if (/= ans2 NIL)
(setq list2 (cons ans2 list2))
) ; end if
) ; end progn
) ; end if tag3
) ; end foreach
) ; end repeat
(setvar 'ctab curlayout)
(command "Zoom" "E")
(command "regen")
(reverse list1)
;(reverse list2)
; now do table
(setq numrows (+ 2 (sslength ss1)))
(setq numcolumns 2)
(setq rowheight 0.2)
(setq colwidth 150)
(setq objtable (vla-addtable curspace pt1 numrows numcolumns rowheight colwidth))
(vla-settext objtable 0 0 "DRAWING REGISTER")
(vla-settext objtable 1 0 "DRAWING NUMBER")
(vla-settext objtable 1 1 "DRAWING TITLE")
(SETQ X 0)
(SETQ Y 2)
(REPEAT (sslength ss1)
(vla-settext objtable Y 0 (NTH X LIST1))
(vla-settext objtable Y 1 (NTH X LIST2))
(vla-setrowheight objtable y 7)
(SETQ X (+ X 1))
(SETQ Y (+ Y 1))
)
(vla-setcolumnwidth objtable 0 55)
(vla-setcolumnwidth objtable 1 170)
(command "_zoom" "e")
); end AH defun
(AH:dwgindex)
(princ)
asilvacotto803291
2023-01-07, 11:16 PM
Hi, Tharwat;
What field do I put in the text box in which the number goes in the titleblock for this code to work?
Thanks!
Tharwat
2023-01-08, 02:31 PM
Hi, Tharwat;
What field do I put in the text box in which the number goes in the titleblock for this code to work?
Thanks!
Hi,
Something like this ?
(defun c:test (/ doc lay sel tag ids id1 id2)
;;----------------------------------------------------;;
;; Author : Tharwat Al Choufi ;;
;; website: https://autolispprograms.wordpress.com ;;
;;----------------------------------------------------;;
(defun ids (obj doc / uty)
(if (vlax-method-applicable-p
(setq uty (vla-get-utility doc))
'getobjectidstring
)
(vla-getobjectidstring uty obj :vlax-false)
(itoa (vla-get-objectid obj))
)
)
(and
(setq tag "MYTAG") ;; specify the tag name to allow the program to write the field into it.
(princ "\nSelect attributed block :")
(setq sel (ssget "_+.:S:E" '((0 . "INSERT") (66 . 1))))
(setq doc (vla-get-activedocument (vlax-get-acad-object))
lay (vla-get-layouts doc)
id1 (ids (vla-item lay (getvar 'ctab)) doc)
id2 (ids lay doc)
)
(vl-some '(lambda (x)
(and (= (vla-get-tagstring x) tag)
(or (vla-put-textstring
x
(strcat
"%<\\AcObjProp Object(%<\\_ObjId "
id1
">%).TabOrder>%"
" of "
"%<\\AcExpr %<\\AcObjProp Object(%<\\_ObjId "
id2
">%).Count>%-1 >%"
)
)
t
)
)
)
(vlax-invoke
(vlax-ename->vla-object (ssname sel 0))
'GetAttributes
)
)
(vl-cmdf "_.regen")
)
(princ)
) (vl-load-com)
peter
2023-01-09, 09:23 PM
So this is my code the way I would do it if it were on my machine.
;___________________________________________________________________________________________________________|
;
; Function to get layout number from name of layout
;___________________________________________________________________________________________________________|
(defun LayoutNumber (strLayoutName / colLayouts lstLayoutObjects lstOfLayouts)
(if (and (setq colLayouts (Layouts 'activedocument))
(setq lstLayoutObjects (toobjects colLayouts))
(setq lstOfLayouts (mapcar '(lambda (X)(list (strcase (vlax-get X "Name"))
(vlax-get X "Taborder")
)
)
lstLayoutObjects)
)
)
(cadr (assoc (strcase strLayoutName) lstOfLayouts))
)
)
I have rewritten activeX to give me access to all activeX commands with error trapping built in and without vla- prefixes.
In one of my libraries I have a function called toobjects that converts collections, autolisp selectionsets, lists of handles, objectid's, entity names, entity lists, activeX selection sets to a list of vla objects. One of the functions is collection to list. So I rewrote it here as an example below.
I stopped nesting functions a long time ago, because I found it is better to test every expression for success (error trapping).
All of the functions return nil for failure.
This approach gives me libraries of functions (1000+ functions) that load at every drawing is less than a second.
This one below will work for you.
;___________________________________________________________________________________________________________|
;
; Function to get layout number from name of layout
;___________________________________________________________________________________________________________|
(defun LayoutNumber (strLayoutName / colLayouts lstLayoutObjects lstOfLayouts objApplication objActiveDocument)
(if (and (setq objApplication (vlax-get-acad-object))
(setq objActiveDocument (vlax-get objApplication "ActiveDocument"))
(setq colLayouts (vlax-get objActiveDocument "Layouts"))
(setq lstLayoutObjects (CollectionToListOfObjects colLayouts))
(setq lstOfLayouts (mapcar '(lambda (X)(list (strcase (vlax-get X "Name"))
(vlax-get X "Taborder")
)
)
lstLayoutObjects)
)
)
(cadr (assoc (strcase strLayoutName) lstOfLayouts))
)
)
;___________________________________________________________________________________________________________|
;
; Function to convert a collection to a list of objects
;___________________________________________________________________________________________________________|
(defun CollectionToListOfObjects (colLayouts / lstObjects objLayout)
(vlax-for objLayout colLayouts
(setq lstObjects (cons objLayout lstObjects))
)
(reverse lstObjects)
)
(vl-load-com)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.