the vlax-for looks for the layout name if there does nothing if not there adds only one layout, I don't see the logic of it trying to add the layout multiple times. What am I missing? What part makes it try to add multple times?
The
vlax-for expression in your code is iterating over every layout in the Layouts Collection (analogous to a
foreach loop for a Collection), and, if the upper-case layout name doesn't equal "PLOT", then it will add a new layout called "PLOT".
Hence, if the first layout isn't "PLOT" then "PLOT" will be added, now, if the second layout doesn't equal "PLOT" then the program will again attempt to create the layout "PLOT", which already exists in the Collection.
You could test for the existence of the layout using
vla-item before creating it:
(setq layouts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
(if (vl-catch-all-error-p (vl-catch-all-apply 'vla-item (list layouts "PLOT")))
(vla-add layouts "PLOT")
)
However, since the use of
vl-catch-all-apply is required for
vla-item to prevent an exception should the layout not already exist, you may as well wrap the
vla-add in a
vl-catch-all-apply statement as ronjonp has demonstrated.