Task: to have a sheet counter to be used in fields that automatically update their values. The ultimate goal is creating a text like "Table {1} of {5}" for each layout, where {1} is the layout name (found at system variables / ctab
) and {5} is the total number of layouts. The latter one is not found in built-in fields, so the only way to know it is to use the lisp code (length (layoutlist))
.
I created an AcadDoc.lsp file and putted in the autodesk folder. The LISP code is as follows:
(setq *LayoutCount*
(length
(vl-remove-if
(function
(lambda (aLayout) (= (strcase aLayout) "MODEL")))
(layoutlist))))
which runs every time i open a DWG file. Sad thing is that the field doesn't update until the DWG is reloaded (close and reopen). Does anyone have any clue on how to update this *LayoutCount* variable every time a layout is added/removed from the list?
NB - I already checked the option to regen when switching layouts, and forcing update fields raise no results. I guess my code runs only the first time the DWG is opened and then no more.
As you have gathered, code present in the acaddoc.lsp
file will only be evaluated on drawing startup, hence the value of your *LayoutCount*
variable will only be correct at the point at which the drawing is first opened.
One possible way around this could be to use a Visual LISP Reactor to update the value of this variable following certain actions.
For example, you could use a Miscellaneous Reactor in the following way to update the variable value when the active layout is changed:
(
(lambda ( )
(vl-load-com)
(foreach grp (vlr-reactors :vlr-miscellaneous-reactor)
(foreach rtr (cdr grp)
(if (= "layoutcount-reactor" (vlr-data rtr))
(vlr-remove rtr)
)
)
)
(vlr-set-notification
(vlr-miscellaneous-reactor "layoutcount-reactor"
'(
(:vlr-layoutswitched . layoutswitched-callback)
)
)
'active-document-only
)
(defun layoutswitched-callback ( rtr arg )
(setq *layoutcount* (length (layoutlist)))
)
(layoutswitched-callback nil nil)
(princ)
)
)
Copy the above code to your acaddoc.lsp
and the *layoutcount*
variable will be defined on drawing startup and updated every time the active layout is changed.
Aside, it is not necessary to remove Model
from the list returned by the layoutlist
function, as this function only returns a list of Paperspace layouts.
However, the disadvantage of using global variables to provide this information is that the global variable will only be defined within the document namespace during the active AutoCAD session and will need to be redefined for every session.
As such, if one of your colleagues or a third-party were to open the drawing file, unless they were also running the code found in your acaddoc.lsp
, the *layoutcount*
variable would not be defined and consequently the field would not display correctly.
Therefore, an alternative is to exploit the fact that a Field Expression can actually reference any ActiveX property, not just those displayed in the FIELD
command dialog.
I demonstrate this technique with my Layout Field application, which allows you to create a Field Expression referencing the Count
property of the Layouts Collection.
This approach has several advantages:
CTAB
system variable is not referenced, meaning the DATAEXTRACTION
command will output the correct information, rather than the same value for every layout.