Jump to content

(foreach lay (layoutlist) - Does not work


Recommended Posts

Posted

Given a drawing with multiple tables/table styles on multiple layouts and an existing layer named MYLAYER.

 

This line

(setq ss (ssget "x" '((0 . "ACAD_TABLE"))))

Places ALL Autocad Tables on ALL Layouts into one selection set [ ss ]

If I manually go to any layout and enter this code

 

(command "change" ss "" "p" "la" "MyLayer" "")

 

It changes all the tables on that layout to MYLAYER.


The following code works on the first layout but does not work for others.

 

(defun C:foo ()
(setq ss (ssget "x" '((0 . "ACAD_TABLE"))))
  (foreach lay (layoutlist)
    (setvar 'ctab lay)

(command "change" ss "" "p" "la" "MyLayer" "")

  ); foreach

); defun

 

this code

 

(defun C:foo ()
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "line" "0,0" "30,20" "")
  ); foreach

); defun

 

and this code

 

(defun C:Foo ()
(setq ss (ssget "x" '((0 . "ACAD_TABLE"))))
  (foreach lay (layoutlist)
    (setvar 'ctab lay)
    (command "line" "0,0" "30,20" "")
  ); foreach

); defun

 

Work on all layouts of the same drawing.

 

Anyone have any idea why the first routine would not work on ALL layouts of a specific drawing?

I cannot upload the drawing without losing my job.

 

Posted

Can the all tables on all layouts be moved to a different pre-existing layer without changing to the different layouts?

Posted

This should work as desired...

 

(defun c:foo ( / *error* cmd pck ss sss )

  (defun *error* ( m )
    (if (= 8 (logand 8 (getvar (quote undoctl))))
      (if command-s
        (command-s "_.undo" "_e")
        (vl-cmdf "_.undo" "_e")
      )
    )
    (if pck
      (setvar (quote pickfirst) pck)
    )
    (if cmd
      (setvar (quote cmdecho) cmd)
    )
    (if m
      (prompt m)
    )
    (princ)
  )

  (setq cmd (getvar (quote cmdecho)))
  (setvar (quote cmdecho) 0)
  (setq pck (getvar (quote pickfirst)))
  (setvar (quote pickfirst) 1)
  (if (= 8 (logand 8 (getvar (quote undoctl))))
    (vl-cmdf "_.undo" "_e")
  )
  (vl-cmdf "_.undo" "_be")
  (if (setq ss (ssget "_X" (list (cons 0 "ACAD_TABLE"))))
    (foreach lay (append (list "Model") (layoutlist))
      (setvar (quote ctab) lay)
      (sssetfirst nil ss)
      (setq sss (ssget "_:L-I"))
      (if (tblsearch "layer" "MyLayer")
        (vl-cmdf "_.change" sss "" "_p" "_la" "MyLayer" ""); then
        (progn
          (vl-cmdf "_.-layer" "_m" "MyLayer")
          (while (< 0 (getvar (quote cmdactive)))
            (vl-cmdf "")
          )
          (vl-cmdf "_.change" sss "" "_p" "_la" "MyLayer" "")
        ); else
      ); if
    ); foreach
  ); if
  (setvar (quote clayer) "0")
  (setvar (quote ctab) "Model")
  (*error* nil)
); defun

 

HTH.

Regards, M.R.

Posted (edited)

In your original code, the initial selection set contains all tables in the drawing database, hence, supplying this selection set to the command when each layout is active may result in the selection being empty if that layout does not contain a table, or if all tables reside in other layouts/spaces.

 

Instead, it is better to acquire the selection set for each layout and check its existence prior to calling the command, e.g.:

(defun c:foo ( / ctb sel )
    (setq ctb (getvar 'ctab))
    (foreach lyt (layoutlist)
        (setvar 'ctab lyt)
        (if (setq sel (ssget "_X" (list '(0 . "ACAD_TABLE") (cons 410 lyt))))
            (command "_.change" sel "" "_p" "_la" "MyLayer" "")
        )
    )
    (setvar 'ctab ctb)
    (princ)
)

 

(Assuming the layer already exists)

Edited by Lee Mac
Posted

Of course, alternatively, no layout switching is required if you forego the command call:
 

(defun c:foo ( / ent idx sel )
    (if (setq sel (ssget "_X" '((0 . "ACAD_TABLE"))))
        (repeat (setq idx (sslength sel))
            (setq idx (1- idx)
                  enx (entget (ssname sel idx))
            )
            (entmod (subst '(8 . "MyLayer") (assoc 8 enx) enx))
        )
    )
    (princ)
)

 

Posted

Lee Mac to the rescue (Once again!)   "Here I come to save the day...!"

<marko_ribar>

Thank you for the reply.

On the drawing that I cannot upload your code works on the very first layout like mine.

 


Lee, thank you so very much Sir!!
 

Posted

Lee...
Change your avatar to Mighty Mouse

 

image.png.f2f8ae1c743221160ee71d1fd50599f2.png

 

😉

  • Like 1
Posted
Quote

forego the command call:

 

Man! That is SO MUCH faster!!

 

Thank you even more!

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...