Jump to content

Recommended Posts

Posted

Happened upon a well-written bit of tutorial by CAB while searching this site yesterday: http://www.cadtutor.net/forum/showthread.php?t=27101

So I’m trying my hand at using the COND command inside a current LISP routine.

 

This first attempt is rudimentary, but it works (kinda), so if someone would kindly hint me in the right direction.

I tried the AutoCAD help, but it’s not written for the rank amateur (me), and couldn’t find any existing threads on the subject, but I’m sure I’ve seen at least one in the not-to-distant past.

Anyway, I need to make something happen if a certain layer is visible.

I started with the code below, and discovered instant gratification, but quickly found that some of the follow-on routines I need to use cause the current layer to change, so that method won’t work except for a short few seconds.

 

 

 
(COND
       ((= (getvar "CLAYER") "ITCH")  ; if ITCH layer is the current layer
       (c:ScratchAnItch)  ; run the ScratchAnItch program
       )

And etc, so forth and “But wait there’s more!”
(There are four more conditionals for other things)

)

 

Even though follow-on routines change the current layer, the ITCH layer stays ON throughout the process.

What I can’t seem to find is the variable or LISP method to discern a layer is ON, so I can use “If ITCH is visible, scratch it.”

 

Steve

.

Posted

Are you looking to see if they are froze / off? If so look at these:

 

(setq Frozen (assoc 70 (entget (tblobjname "LAYER" <your layer here>)))); 0 = Thawed 1 = Froze
(setq Off (assoc 62  (entget (tblobjname "LAYER" <your layer here>)))); - number is off + number is on

Posted

 

Are you looking to see if they are froze / off?

 

Not trying to set or maintain a layer ON; I can do that. What I can't seem to find, is a way to check to see if a layer is on with LISP.

 

--

Look to see if the layer is ON

If so, do what's next

--

 

There's got to be a way. Am I looking too hard? Is the answer so easy I'm missing it?

 

Steve

.

Posted

ok look at the comments that i put in this code... this should give you an idea.

 

(defun c:laysw (/ lay1 lay2)
 (vl-load-com); loads vl commands
 
 (setq lay1 (tblobjname "LAYER" "APPROVED")); checks to see if the layers exists "approved" being the layer name
 
 (setq lay1 (vlax-ename->vla-object lay1)); sets the layer to a vla object

 (eq :vlax-true (vla-get-layeron lay1)); this checks to see if it's on
      
 (vla-put-layeron lay1 :vlax-false); this turns off the layer
            
 (vla-put-layeron lay1 :vlax-true); this turns on the layer

 (princ))

 

hopefully this helps

Posted

Steve, this will test the ON/OFF status

;;;  Returns T if On. nill if OFF or not found
(defun islayeron (lname / entlst)
 (and (= (type lname) 'str)
      (setq entlst (tblsearch "layer" lname))
      (null (minusp (cdr (assoc 62 entlst))))
 )
) ; end defun

Posted

Thanks to all for the help and input.

I can see it's a bit more involved than I was prepared for, so I'll be trying them all out at work next week, to see if I can get one of them implemented.

 

Thanks again,

Steve

.

Posted

Yee Ha!!!

Works perfectly using a buncha IF statements.

I'm sure there's a better way to do this, but as is, it's simple and easy for me to follow the logic.

That helps cut way down on my frustration level while trying to learn LISP.

Here's what it looks like:

 

 

 
(defun c:TEST ()

(if (> (cdr (assoc 62 (entget (tblobjname "layer" "TEXT")))) 0);If TEXT layer is on,
      (c:LISProutine1); run routine 1
);endif 1

(if (> (cdr (assoc 62 (entget (tblobjname "layer" "FIND")))) 0);If FIND layer is on,
      (c:LISProutine2); run routine 2
);endif 2

(if (and (< (cdr (assoc 62 (entget (tblobjname "layer" "TEXT")))) 0);If TEXT layer is off *AND*
        (< (cdr (assoc 62 (entget (tblobjname "layer" "FIND")))) 0));if FIND layer is off,
        (c:LISProutine3); run routine 3
);endif 3

(PRINC)
)

Thanks for all the helpful ideas, guys.

Steve

.

Posted

Well done Steve - congrats on the LISP routine :)

 

Just a quick pointer - for multiple options - the COND function is faster and more concise :)

 

Lee

Posted

Thanks , Lee.

 

That's the next step for this.

Doesn't look too difficult, I was just starting out easy.

 

Steve

.

Posted

Doesn't quite lend itself to a COND.

Here with the subroutine.

(defun c:TEST ()
;;;  Returns T if On. nill if OFF or not found
 (defun islayeron (lname / entlst)
   (and (= (type lname) 'str)
        (setq entlst (tblsearch "layer" lname))
        (null (minusp (cdr (assoc 62 entlst))))
   )
 )                           ; end defun
 (if (islayeron "TEXT")      ;If TEXT layer is on,
   (c:LISProutine1)          ; run routine 1
 )                           ;endif 1
 (if (islayeron "FIND")      ;If FIND layer is on,
   (c:LISProutine2)          ; run routine 2
 )                           ;endif 2
 ;;  If both layers are off
 (if (and (not (islayeron "TEXT")) ;If TEXT layer is off *AND*
          (not (islayeron "FIND")) ;if FIND layer is off,
     )                      
   (c:LISProutine3)          ; run routine 3
 )                           ;endif 3
 (PRINC)
)

Posted

Alright, you guys.

The difference between this IF version:

 

 
(defun c:TEST ()

(if (> (cdr (assoc 62 (entget (tblobjname "layer" "TEXT")))) 0)
      (c:LISProutine1)
)
(if (> (cdr (assoc 62 (entget (tblobjname "layer" "FIND")))) 0)
      (c:LISProutine2)
)
(if (and (< (cdr (assoc 62 (entget (tblobjname "layer" "TEXT")))) 0)
        (< (cdr (assoc 62 (entget (tblobjname "layer" "FIND")))) 0))
        (c:LISProutine3)
)
(PRINC)
)

 

and this COND version:

 

 
(defun c:TEST ()
(COND ((> (cdr (assoc 62 (entget (tblobjname "layer" "TEXT")))) 0)
     (c:LISProutine1)
     )

     ((> (cdr (assoc 62 (entget (tblobjname "layer" "FIND")))) 0)
     (c:LISProutine2)
     )

     ((and (< (cdr (assoc 62 (entget (tblobjname "layer" "TEXT")))) 0)
           (< (cdr (assoc 62 (entget (tblobjname "layer" "FIND")))) 0))
     (c:LISProutine3)
     )
)
(PRINC)
)

 

Doesn't seem all that significant to me, and they both work equally well.

 

However, the IF version does seem more a "brute force" method,

and the COND version is a bit cleaner, looking less like a hack job than the IF version.

 

And CAB's entry, now that I see how it's implemented, works just as well, too. Thanks, CAB.

It does seem a bit more involved, though.

 

I'm all smiles about the wealth of choices, but is there a prefered method?

 

Steve

.

Posted

The difference with the COND method is that when COND reaches a test expression that returns T, it evaluates all the statements after that expression, and finishes there.

 

Whereas with the IF statements, each IF statement is evaluated in turn.

 

Hence, if the TEXT layer is on, and the FIND layer is on:

 

In the COND version, LispRoutine1 will be evluated, but LispRoutine2 will not.

 

In the IF version, both will be evaluated.

 

Hope this makes some sort of sense... :)

Posted

As I understand it, in the COND version, the conditional check will exit when the first TRUE is evaluated, but in the IF version, every line is evaluated regardless of the outcome of any of the preceding checks.

 

So the IF version gets the job done, but sacrifices efficiency.

COND wins.

 

Thanks,

Steve

.

Posted
COND wins.

 

But as I stated above, what if both layers are on?

Posted

In the IF version, both subroutines run.

 

In the COND version, only the first subroutine runs.

 

Which method I use then, may depend on what I want to happen in that circumstance.

Posted
In the IF version, both subroutines run.

 

In the COND version, only the first subroutine runs.

 

Which method I use then, may depend on what I want to happen in that circumstance.

 

Exactly - I was under the impression that you want the routines to run if the conditions were met - in the COND version, some conditions may not even be tested.

Posted

I found that, because of the automatic way the drawing is processed, only one state can be true, and the other states will be false.

So either the TEXT layer is on, or the FIND layer is on, or they are both off.

 

That makes the COND version the better choice after all.

Posted
I found that, because of the automatic way the drawing is processed, only one state can be true, and the other states will be false.

So either the TEXT layer is on, or the FIND layer is on, or they are both off.

 

That makes the COND version the better choice after all.

 

Ok, as long as you understand the differences, there will be no problems :)

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