Review my lisp

cneelyPABBX
Enthusiast
Enthusiast

Review my lisp

cneelyPABBX
Enthusiast
Enthusiast

Below is the first lisp I have written from “scratch”. And by “scratch” I mean I didn’t take an existing lisp and alter/edit it. However, I did do a little copy/paste from other lisps I have laying around.

I’d like some critique and suggestions about making it more elegant and efficient. It just seems clunky.

What it does is isolate the current layer w/o having to manually select an item on that layer first.

 

Thanks,

 

(defun c:LayIsoCurrent (/ s)
 (if (setq s (ssget "_X" (list (cons 8 (getvar "CLAYER")))))
   (princ (strcat "\nNumber of Found objects : < " (itoa (sslength s)) " >"))
 )
 (sssetfirst nil s)
 (command "layiso")
 (princ)
)

 

0 Likes
Reply
Accepted solutions (3)
223 Views
7 Replies
Replies (7)

ВeekeeCZ
Consultant
Consultant
Accepted solution

Good one for start... see notes.

 

(defun c:LayIsoCurrent (/ s)
  (if (setq s (ssget "_X" (list (cons 8 (getvar "CLAYER")))))
    (progn
      (princ (strcat "\n" (itoa (sslength s)) " objects found."))  ; dont use <>, could be confusing since it marks <ENTER> function. Prefer to use common format.
      ;(sssetfirst nil s)   					   ; no need for this
      (command "layiso" (ssname s 0) "") 			  ; this will also fail in no object is on current layer - wrap it into if...  ;; also, is all objects are on same layer, you can supply just one of them, it would be faster...
      ))
  (princ)
  )

 

0 Likes

Kent1Cooper
Consultant
Consultant
Accepted solution

If you can live without the report on how many objects there are [what is the purpose of that, anyway?}, you can just do this:

(defun c:LayIsoCurrent ()
 (command "_.layer" "_off" "*" "_no" "")
 (princ)
)

But if you want that report, I would suggest that it and the isolating parts should all be within the 'then' argument of the (if) function:

(defun c:LayIsoCurrent (/ s)
 (if (setq s (ssget "_X" (list (cons 8 (getvar "CLAYER")))))
   (progn ; then
     (princ (strcat "\nNumber of Found objects : < " (itoa (sslength s)) " >"))
     (sssetfirst nil s)
     (command "layiso")
   ); progn
 ); if
 (princ)
)
Kent Cooper, AIA
0 Likes

cneelyPABBX
Enthusiast
Enthusiast

I dont want the report, but I want to use layiso so that I can then use unlayiso to restore things back. 

0 Likes

cneelyPABBX
Enthusiast
Enthusiast

I did find that out after I posted the code so I'll need to edit to isolate even if no objects exist on the current layer.  How would I supply just one?

0 Likes

Kent1Cooper
Consultant
Consultant
Accepted solution

@cneelyPABBX wrote:

.... to isolate even if no objects exist on the current layer.  ....


Unfortunately, unlike the LAYDEL command, LAYISO doesn't have an option to specify a Layer name rather than select an object.  If it needs to be via LAYISO in order to have LAYUNISO available later, then I suggest this, which does not depend on whether or not anything is drawn on the Layer:

(defun C:LAYISOCURRENT ()
  (command
    "_.point" (getvar 'viewctr); add an object on the current Layer
    "_.layiso" (entlast) "" ; and use it to pick the Layer
    "_.erase" (entlast) "" ; then get rid of it
  ); command
  (prin1)
)
Kent Cooper, AIA
0 Likes

cneelyPABBX
Enthusiast
Enthusiast

That's great! Added cmdecho and I think I'm good with it!

0 Likes

ВeekeeCZ
Consultant
Consultant

Similar solution as Kent... just different tools used.

 

(defun c:LayIsoCurrent (/ s e)
  (if (setq s (ssget "_X" (list (cons 8 (getvar "CLAYER")))))
    (command "layiso" (ssname s 0) "")
    (progn
      (setq e (entmakex (list '(0 . "POINT") '(10 0 0) (cons 8 (getvar "CLAYER")))))
      (command "layiso" e "")
      (entdel e)))
  (princ)
  )

 

0 Likes