working with empty layers

working with empty layers

fbyfatih
Contributor Contributor
542 Views
8 Replies
Message 1 of 9

working with empty layers

fbyfatih
Contributor
Contributor

Hello, I have a layer changing lisp but it ends with errors if I run it second time. What might be my mistake?

 

(defun c:sel()

(setq ss ( ssget "_A" '((8 . "S-_ANOTGRiD_T3" ))))
(setq len (sslength ss))
(if (> len 0)
(command "_chprop" ss "" "_LA" "S-ANNO-DIMS" "")
);if

)

0 Likes
Accepted solutions (1)
543 Views
8 Replies
Replies (8)
Message 2 of 9

fbyfatih
Contributor
Contributor

Ok I got my own answer. Solution is like this

 

(defun c:seli()

(setq ss ( ssget "_A" '((8 . "S-_ANOTGRiD_T3" ))))

(if (/= ss nil)
(command "_chprop" ss "" "_LA" "S-ANNO-DIMS" "")
);if

)

0 Likes
Message 3 of 9

ВeekeeCZ
Consultant
Consultant

Could be used this shortened form.

 

 

  (if (setq ss ( ssget "_A" '((8 . "S-_ANOTGRiD_T3" ))))
    (command "_chprop" ss "" "_LA" "S-ANNO-DIMS" "")
    );if

 

Message 4 of 9

fbyfatih
Contributor
Contributor

Why is this not working?

 

(defun c:seli()
(setq ss ( ssget "_A" '((8 . "S-_ANOTGRiD_T3" ))))
(if (ss)
(command "_chprop" ss "" "_LA" "S-ANNO-DIMS" "")
)

0 Likes
Message 5 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

The parents are the issue. It would work if ss was a function ....   (defun ss () whatever... )

But since it's just a variable, it's just (if ss (command ....

Message 6 of 9

john.kaulB9QW2
Advocate
Advocate

With a few corrections, this is very nicely written code. Good job, @fbyfatih.

 

Remember: When constructing "boolean conditional constructs" (IF/COND statements) you should be using verbs to make code more readable.

 

Instead of typing:
(if (ssget "_A" '((8 . "S-_ANOTGRiD_T3" )))) ...

 

You should create a "boolean variable" to help construct a conditional (like you did). And when creating a `boolean variable` you should use positive variable names.

 

Examples:
full
ready
etc.

 

Using negative verbs results in confusing code like:
(if (not notfull) ...
(if (not notReady) ...

 

(defun c:seli ( / ss)
  (setq ss (ssget "_A" '((8 . "S-_ANOTGRiD_T3" ))))
  (if ss
    (command "_chprop" ss "" "_LA" "S-ANNO-DIMS" "")
    )
 (princ)
 )

 

another swamper
Message 7 of 9

fbyfatih
Contributor
Contributor

Thank you very much, you were the first people who helped me with my Autolisp learning journey

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

A slightly shortened approach....  Instead of first setting the ss variable and then checking whether it exists to work with it, you can do the checking in the process of setting it:

 

(defun c:seli (/ ss)
  (if (setq ss ( ssget "_A" '((8 . "S-_ANOTGRiD_T3" ))))
    (command "_.chprop" ss "" "_LA" "S-ANNO-DIMS" "")

  )
)

Kent Cooper, AIA
0 Likes
Message 9 of 9

john.kaulB9QW2
Advocate
Advocate

You *could* do it without setting the variable altogether but that approach is not "good coding practice"--especially when you graduate to other languages--like: (if (ssget "_A" '((8 . "S-_ANOTGRiD_T3"))).

It's considered a better habit to set the variable as I stated above and develop the habit early in learning.

This should be taken with a grain-of-salt because you should also develop the habit of constructing small functions (small "black boxes") where you may use a function return as your conditional check like:

(defun checkForSpecificThing ()
;; This function's purpose is to check for the existence of a specific thing
;; in the drawing.
;;
;; Return: True or False
(if (ssget "_A" '((8 . "S-_ANOTGRiD_T3")))) )

(if (checkForSpecificThing) ...

 

EDIT: Added code tags.

another swamper
0 Likes