Author Topic: Should I be using "IF"  (Read 404 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Newt
  • Posts: 103
Should I be using "IF"
« on: September 13, 2019, 11:49:48 AM »
My thinking is flawed here, somehow.

Using this...
Code - Auto/Visual Lisp: [Select]
  1. (if (this is true)
  2.  (then do this)
  3.  (else do this when false)
  4. )

(If
dimscale is 8
and
objects exist at this location
and
on any layer starting with "TEXT"
do change)

(objects exist at this location
and
on any layer starting with "TEXT"
do change)
)

Code - Auto/Visual Lisp: [Select]
  1. (if (= (getvar 'dimscale) 8)
  2.                  (setq ref1 (ssget "c"
  3.                 '(268.5 171.0)
  4.                 '(254.25 42.125)
  5.                 '((8 . "Text*"))))
  6.         (command "._change" ref1 "" "p" "la" "Text2" "")
  7. )
  8.          (setq ref1 (ssget "c"
  9.                 '(33.375 21.375)
  10.                 '(31.875 5.375)
  11.                 '((8 . "Text*"))))
  12.          )
  13.  (command "._change" ref1 "" "p" "la" "TBLK_TBLK_REFTEXT" "")

My problem is, what if it nothing exists? There is more code after this that runs fine, but I get...
Quote
Command: STA_STD
STA_STD
Working, please wait... Regenerating model.
Unknown command "P".  Press F1 for help.
Unknown command "LA".  Press F1 for help.
Unknown command "TBLK_TBLK_REFTEXT".  Press F1 for help.
Unknown command "STA_STD".  Press F1 for help.
Unknown command "STA_STD".  Press F1 for help.

when I change everything from Layer TEXT* to Layer TBLK_TBLK_REFTEXT

Am, I using "IF" properly (clearly not, you say) and how do I account for nothing being found?
 
J. Logan
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9275
Re: Should I be using "IF"
« Reply #1 on: September 13, 2019, 12:05:20 PM »
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

ronjonp

  • Needs a day job
  • Posts: 7051
Re: Should I be using "IF"
« Reply #2 on: September 13, 2019, 12:26:20 PM »
You can also do something like this:
Code - Auto/Visual Lisp: [Select]
  1. (if (and (= (getvar 'dimscale) 8)
  2.          (setq ref1 (ssget "c" '(268.5 171.0) '(254.25 42.125) '((8 . "Text*"))))
  3.     )
  4.   (command "._change" ref1 "" "p" "la" "Text2" "")
  5.   (and (setq ref1 (ssget "c" '(33.375 21.375) '(31.875 5.375) '((8 . "Text*"))))
  6.        (command "._change" ref1 "" "p" "la" "TBLK_TBLK_REFTEXT" "")
  7.   )
  8. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MP

  • Seagull
  • Posts: 17471
Re: Should I be using "IF"
« Reply #3 on: September 13, 2019, 01:54:50 PM »
If I understood correctly and assuming there's no need to ensure layers exist perhaps this --

Code: [Select]
(if
    (or
        (and
            (eq 8 (getvar 'dimscale))
            (setq ss (ssget "c" '(268.5 171.0) '(254.25 42.125) '((8 . "Text*"))))
            (setq layer "Text2")
        )
        (and
            (setq ss (ssget "c" '(33.375 21.375) '(31.875 5.375) '((8 . "Text*"))))
            (setq layer "TBLK_TBLK_REFTEXT")
        )
    )
    (command "._change" ss "" "p" "la" layer "")
)

Cheers.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17471
Re: Should I be using "IF"
« Reply #4 on: September 13, 2019, 02:53:28 PM »
A different variant exploiting setq's behavior (return last value assigned):

Code: [Select]
(if
    (or
        (and
            (eq 8 (getvar 'dimscale))
            (setq
                layer "Text2"
                ss    (ssget "c" '(268.5 171.0) '(254.25 42.125) '((8 . "Text*")))
            )
        )
        (setq
            layer "TBLK_TBLK_REFTEXT"
            ss    (ssget "c" '(33.375 21.375) '(31.875 5.375) '((8 . "Text*")))
        )
    )
    (command "._change" ss "" "p" "la" layer "")
)

Fixed via mobile so may still be fubar’d.
« Last Edit: September 13, 2019, 07:38:27 PM by MP »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10369
Re: Should I be using "IF"
« Reply #5 on: September 13, 2019, 03:33:18 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12284
  • London, England
Re: Should I be using "IF"
« Reply #6 on: September 13, 2019, 06:58:03 PM »
A different variant exploiting setq's behavior (return last value assigned)

This isn't testing dimscale ...?

MP

  • Seagull
  • Posts: 17471
Re: Should I be using "IF"
« Reply #7 on: September 13, 2019, 07:35:37 PM »
lol I gotta stop posting from my phone, every time I do ...

Thanks for paying attention Lee.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

jlogan02

  • Newt
  • Posts: 103
Re: Should I be using "IF"
« Reply #8 on: September 16, 2019, 01:21:30 PM »
Thanks for all the responses guys.

The code I provided was not dealing with the correct layer names, my bad for changing
things on the fly and not reading what I changed. Once I noticed my error, things worked as desired.

I started with...
http://www.theswamp.org/index.php?topic=13046.msg158557#msg158557

Cause that's my go to. None of that helps if I don't read things better. That's what it all comes down to.

J. Logan
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9275
Re: Should I be using "IF"
« Reply #9 on: September 16, 2019, 03:23:56 PM »
I noticed this thread got a few responses and the OP even got an answer but I was a little brief in my response (sorry, I am extremely busy these days). Please someone with more AutoLisp experience--or even AutoCAD installed--please translate this into proper syntax/form/etc. if I made a mistake. I also apologize ahead of time for my very crude AutoLisp skills (I have been removed from AutoLisp for many years.)

There is a specific programming construct I was thinking about when I wrote my initial--horrible--post and the following two will attempt to demonstrate (the second is one that I feel is a little too obfuscated--if it even runs--over the first but the first is a little long in the tooth). Also, if I remember my lisp properly, the second demonstrates a method better used by SET (vs SETQ) but I could be wrong.

This construct will help with future revisions to the rule-sets; I remember using this form more often on programs for company standards maintenance because it was easier to add to to the conditions then an "IF"--or "nested IF"--construct.

Code - Auto/Visual Lisp: [Select]
  1.     (cond
  2.       ((eq 8 (getvar 'dimscale))
  3.        (setq ref1 (ssget "c" '(268.5 171.0) '(254.25 42.125) '((8 . "Text*"))))
  4.        (setq layername "Text2")
  5.        )
  6.       ((setq ref1 (ssget "c" '(33.375 21.375) '(31.875 5.375) '((8 . "Text*"))))
  7.        (setq layername "TBLK_TBLK_REFTEXT")
  8.        )
  9.       )
  10.  
  11.     (if (and ref1 layername) (command "._change" ref1 "" "p" "la" layername ""))

Code - Auto/Visual Lisp: [Select]
  1.     (setq ref1
  2.           (cond
  3.             ((eq 8 (getvar 'dimscale))
  4.              (setq layname "Text2")
  5.              (ssget "c" '(268.5 171.0) '(254.25 42.125) '((8 . "Text*")))
  6.              )
  7.             ((setq layername "TBLK_TBLK_REFTEXT")
  8.              (setq ref1 (ssget "c" '(33.375 21.375) '(31.875 5.375) '((8 . "Text*"))))
  9.              )
  10.             )
  11.           )
  12.  
  13.     (if (and ref1 layername) (command "._change" ref1 "" "p" "la" layername ""))
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--