Jump to content

Advice on combining these two LISP routines


tzframpton

Recommended Posts

I don't claim to know LISP at all, but I sure can monkey with code and make reeeeeeally simple routines all by myself. :wink:

 

So anyways, I created two extremely useful LISPs today while working on a multiple scale drawing. I was wondering if I could combine the two, so that it would freeze and thaw the layers I'm wanting to manipulate sort of like a "toggle". I'm assuming this will make me dive into the (if) statement, or expression, or argument.... I can't ever remember which one the operators are called, if it even is an operator. So here's my two separate test LISP files I made, which basically is a command line -LAYER script is all....

 

(DEFUN C:4IN (/ )
 (command "._layer" "_t" "*-048" "_t" "*-4.5" "_f" "*-096" "_f" "*-09" "")
 (princ)
 )

(DEFUN C:8IN (/ )
 (command "._layer" "_f" "*-048" "_f" "*-4.5" "_t" "*-096" "_t" "*-09" "")
 (princ)
 )

 

So my question to all would be to (1) point me in the right direction, or maybe with some sample code, guide me in combining these and (2) possible show me how to control the Layer entities without using this outdated method of command line style scripting. Just a couple of humble requests is all, I technically have what I need and it works well but I'm interested in expanding my skills to super dooper novice to just super novice. Thanks in advance. 8)

Link to comment
Share on other sites

Maybe something like this:

 

(defun c:mylay (/ input tl fl)

 (initget 1 "4 8")
 (setq input (getkword "\n4 or 8:   "))
 (cond ((= input "4")
           (setq tl (list "*048" "*-4.5")   ;THAW LIST
                 fl (list "*096" "*-09")))  ;FREEZE LIST
       ((= input "8")
           (setq fl (list "*048" "*-4.5")   ;FREEZE LIST
                 tl (list "*096" "*-09")))) ;THAW LIST

 (command "_.LAYER")
 (foreach l tl
   (command "_.Thaw" l))
 (foreach l fl
   (command "_Freeze" l))
 (command "")
 (princ))

 

 

You can manipulate the freeze and thaw lists, and add more if you need to.

-David

Link to comment
Share on other sites

If you wanted to avoid the command line call, here is another way:

 

(defun c:laytog ( / GetLayers Freeze Thaw l1 l2 )
 ;; Lee Mac  ~  22.04.10

 (defun GetLayers ( wc / def n lst )
   (while (setq def (tblnext "LAYER" (not def)))
     (if (wcmatch (setq n (cdr (assoc 2 def))) wc)
       (setq lst (cons (tblobjname "LAYER" n) lst))))
   lst)  

 
 (defun Freeze ( layer / dx70 el )
   (setq dx70 (cdr (assoc 70 (setq el (entget layer)))))
   (entmod
     (subst
       (cons 70 (boole 7 1 dx70)) (assoc 70 el) el)))
 

 (defun Thaw ( layer / dx70 el )
   (setq dx70 (cdr (assoc 70 (setq el (entget layer)))))
   (entmod
     (subst
       (cons 70 (boole 4 1 dx70)) (assoc 70 el) el)))
 

 (setq l1 (GetLayers "*-4.5") l2 (GetLayers "*-09"))  

 (cond (  (setq *flag* (not *flag*))
        
          (mapcar (function Freeze) l1)
          (mapcar (function Thaw)   l2))

       (  (mapcar (function Freeze) l2)
          (mapcar (function Thaw)   l1)))

 (princ))
 

Link to comment
Share on other sites

Another, if you want to be fancy :)

 

(defun c:laytog ( / GetLayers FTLayer l1 l2 )
 ;; Lee Mac  ~  22.04.10

 (defun GetLayers ( wc / def n lst )
   (while (setq def (tblnext "LAYER" (not def)))
     (if (wcmatch (setq n (cdr (assoc 2 def))) wc)
       (setq lst (cons (tblobjname "LAYER" n) lst))))
   lst)  

 
 (defun FTLayer ( layer freeze / dx70 el )
   (setq dx70 (cdr (assoc 70 (setq el (entget layer)))))
   (entmod
     (subst
       (cons 70 (boole (+ 4 (* freeze 3)) 1 dx70)) (assoc 70 el) el)))  

 (setq l1 (GetLayers "*-4.5") l2 (GetLayers "*-09"))

 (mapcar
   (function
     (lambda ( layers flag )
       (mapcar
         (function
           (lambda ( layer )
             (FTLayer layer flag)
           )
         )
         layers
       )
     )
   )
   (list l1 l2) (if (setq *flag* (not *flag*)) '(1 0) '(0 1))
 )

 (princ))

Link to comment
Share on other sites

No flaming please... this is not meant to discredit David's or Lee's code ..... but..... strictly combining your original code into a "toggle".. how about this?

 

This example does make an assumption that there is a layer named "A-048", you can change this --- or if you don't want to make any assumptions, you would have to iterate the layer collection, checking each of the *-048" layers, and then make a determination if their freeze/thaw status isn't the same for all...

 

Also, you might want to set layer 0 current first so that you don't try to freeze the current layer.

 


(defun c:laytog ()
 (if (eq 1 (logand 1 (cdr (assoc 70 (tblsearch "layer" "A-048")))))
   (command "._layer" "_t" "*-048" "_t" "*-4.5" "_f" "*-096" "_f" "*-09" "")
   (command "._layer" "_f" "*-048" "_f" "*-4.5" "_t" "*-096" "_t" "*-09" "")
 )
)

Link to comment
Share on other sites

Just for reference sake. When using -layer, you don't have to separate your strings.

 

eg.

(command "._layer" "_t" "*-048" "_t" "*-4.5" "_f" "*-096" "_f" "*-09" "")

 

could be executed as:

(command "_.layer" _t" "*-048,*-4.5" "_f" "*-096,*-09" "")

 

Not that there's anything wrong with your code or David's or Lee's.

Link to comment
Share on other sites

FLAME!!

 

 

Also, you might want to set layer 0 current first so that you don't try to freeze the current layer.

A most excellent point.

 

(if (wcmatch (getvar 'clayer) "*-096,*-09")
 (command "_.layer" "_t" "0" "_s" "0" ""))

Link to comment
Share on other sites

(2) possible show me how to control the Layer entities without using this outdated method of command line style scripting

 

I was just providing an alternative :)

Link to comment
Share on other sites

I was just providing an alternative :)

 

Nothing wrong with that. :)

 

Nothing wrong with using command if that's all one knows. Hell, I still have a few specific layer on/off macros that use command and I don't see myself ever editing them.

Link to comment
Share on other sites

I was just providing an alternative :)

 

Nothing wrong with that. :)

 

Nothing wrong with using command if that's all one knows. Hell, I still have a few specific layer on/off macros that use command and I don't see myself ever editing them.

 

I agree with both. There are always many ways to attack a problem.

Link to comment
Share on other sites

Awesome guys, thanks for the help. McSwain raises an excellent point of the "current layer" issue. Before I continue, I have another question, is there a way when I'm in the VLIDE that I can just run the program straight into AutoCAD without having to Ctrl+S and drag/drop the file back into AutoCAD like I'm doing??

 

And Alan, thanks for that tip about combining the strings.

Link to comment
Share on other sites

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...