Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP to erase

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
w64bit
6128 Views, 5 Replies

LISP to erase

I have a lisp which I use to erase all points in a specific layer.

 

(defun c:DPL ()
 (setq ss1 (ssget "x" (list '(0 . "point")'(8 . "Points"))))
 (command ".erase" ss1 "")
 (princ)
)

 

It is working only if I have the points in the DWG.

If there are no points, I receive "Unknow command DPL".

Is there anything I can do in order to avoid this error?

 

Thank you.

 

5 REPLIES 5
Message 2 of 6
hgasty1001
in reply to: w64bit

Hi,

 

try this:

 

(defun c:DPL ()
 (setq ss1 (ssget "x" (list '(0 . "point")'(8 . "Points"))))
 (if ss1
  (command ".erase" ss1 "")
  (alert "No points in this drawing!")
 ) 
 (princ)
)

 

Gaston Nunez

Message 3 of 6
_Tharwat
in reply to: hgasty1001

 

(alert "No points Lay on Points layer")

 

If you have points lay on other layers you can not consider that you don't have points in the drawing because you routine

would not search points other than Points layer .Smiley Wink

Message 4 of 6
_Tharwat
in reply to: w64bit

e.g.

(defun c:Test (/ sel)
  (cond
    ((eq 4
	 (logand 4
		 (cdr (assoc 70 (entget (tblobjname "LAYER" "Points"))))
	 )
     )
     (alert "\n <!> Layer Points is locked <!>")
    )
    (t
     (if (setq sel (ssget "_X" '((0 . "POINT") (8 . "Points"))))
       (command "_.erase" sel "")
     )
    )
  )
  (princ)
)

 

Message 5 of 6
Kent1Cooper
in reply to: w64bit


@w64bit wrote:
....
 (setq ss1 (ssget "x" (list '(0 . "point")'(8 . "Points"))))
 (command ".erase" ss1 "")
....

If there are no points, I receive "Unknow command DPL".

....


When it finds no Points, it returns nil, and that closes the Erase command.  Then the "" Enter is taken as a recall of the previous command, which works with Enter from the keyboard, but not with Enter inside an AutoLISP (command) function, which will only recognize native AutoCAD command names.

 

You can do gasty1001's suggestion, or shrink it slightly:

 

(defun c:DPL (/ ss1)
  (if (setq ss1 (ssget "_X" '((0 . "POINT") (8 . "Points"))))
    (command "_.erase" ss1 "")
    (alert "No points in this drawing!"); could use (prompt) instead

  ); if
  (princ)
)

 

Or, here's another interesting way to do it, without the use of a variable.  It does the object selection right inside the Erase command, and then checks whether the command is still active before giving it the Enter to close, which is needed only it if there were any Points found:

 

(defun C:XYZ ()
  (command
    "_.erase"
    (ssget "_X" '((0 . "POINT") (8 . "Points")))
  ); command [leaves in Erase command if any Points were found, gets out if not]
  (if (> (getvar 'cmdactive) 0)

    (command ""); then -- finish Erase command

    (prompt "\nNo Points found on that Layer."); else

  ); if

  (princ)
); defun

 

Any of those will Erase only Points in the current space [(ssget) will find them anywhere, but Erase will "see" only those in the current space].  If you want to remove them from everywhere in the drawing, try something like this:

 

(defun C:JKL (/ ss)

  (if (setq ss (ssget "_X" '((0 . "POINT") (8 . "Points"))))

    (foreach pt (mapcar 'cadr (ssnamex ss)); then -- list of entity names

      (entdel pt); unlike Erase, will remove from any layout, in paper or model space

    ); foreach

    (prompt "\nNo Points found on that Layer."); else [optional]

  ); if

  (princ)

); defun

 

or:

 

(defun C:PQR (/ ss)

  (if (setq ss (ssget "_X" '((0 . "POINT") (8 . "Points"))))

    (repeat (sslength ss); then

      (entdel (ssname ss 0))

      (ssdel (ssname ss 0) ss)

    ); repeat

    (prompt "\nNo Points found on that Layer."); else

  ); if

  (princ)

); defun

   

Kent Cooper, AIA
Message 6 of 6
w64bit
in reply to: Kent1Cooper

Thank you very much to you all.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost