Author Topic: If statement is wrong ?  (Read 1305 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
If statement is wrong ?
« on: July 02, 2021, 12:17:10 PM »
I have to find text objects on these layers and in the specified crossing window area. There are times though when there won't be anything in that area. The layers will always exist.

My routine bombs when the text objects don't exist.


Code - Auto/Visual Lisp: [Select]
  1.  (if (= (tblsearch "Layer" "TEXT2,TBLK_TBLK_REVTEXT"))
  2.      (setq rev1 (ssget "c"
  3.         '(31.75 21.75)
  4.         '(31.6875 5.25)
  5.         '((8 . "TEXT2,TBLK_TBLK_REVTEXT"))
  6.                 )
  7.           )
  8.   (command "._change" rev1 "" "p" "la" "TBLK_TBLK_REVINIT" "")
  9.  
  10. )

Have I been misunderstanding (if ???

I think my code is saying...

If the layers exist....
do this
and if they don't exist, carry on.
J. Logan
ACAD 2018

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

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: If statement is wrong ?
« Reply #1 on: July 02, 2021, 12:29:24 PM »
TABLESEARCH will return nil if no entry is found but my immediate question is what is the EQUAL function checking against (for)?

Code - Auto/Visual Lisp: [Select]
  1. (if (= (tblsearch "Layer" "TEXT2,TBLK_TBLK_REVTEXT"))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

jlogan02

  • Bull Frog
  • Posts: 327
Re: If statement is wrong ?
« Reply #2 on: July 02, 2021, 01:00:24 PM »
Yeah, I forgot to erase that before I posted it. I was doing some stab in the dark crap.
J. Logan
ACAD 2018

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

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: If statement is wrong ?
« Reply #3 on: July 02, 2021, 01:44:13 PM »
Ah. got it. Well, I'm very fuzzy on my lisp knowledge so let me tackle some of the higher level stuff and we'll see if you can't get this figured out.

1. I don't think you can use tablesearch to search for two layers like that. You may just have to search each layer one at a time.
2. Your IF statement is formatted incorrectly.

The proper format is:
Code - Auto/Visual Lisp: [Select]
  1. (if testexpr
  2.     thenexpr
  3.     [elseexpr]
  4.     )

so if you take a look at your structure, you'll see that the THENEXPR is just a simple SETQ and the optional ELSEEXPR is a command call. If you need to group those two statements (SETQ and COMMAND) into one expression you can use PROGN

Code - Auto/Visual Lisp: [Select]
  1. (if testexpr
  2.     (progn
  3.        thenexpr
  4.        subsiquent thenexpr
  5.      )
  6.     [elseexpr]
  7.     )

But keep in mind as your structuring your expressions not to fall into the trap of sloppy structure (akin to sloppy drafting) because often times when you have more TESTEXPRs that need to be met, a cleaner method is to use COND.

Code - Auto/Visual Lisp: [Select]
  1.   ((testexpr)
  2.     result
  3.    )
  4.   ((testexpr)
  5.     result
  6.    )
  7.   ...
  8. )

So, clean up that and we'll see if I can't do a crash course on SSGET.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7527
Re: If statement is wrong ?
« Reply #4 on: July 02, 2021, 03:35:38 PM »
I have to find text objects on these layers and in the specified crossing window area. There are times though when there won't be anything in that area. The layers will always exist.

My routine bombs when the text objects don't exist.


Code - Auto/Visual Lisp: [Select]
  1.  (if (= (tblsearch "Layer" "TEXT2,TBLK_TBLK_REVTEXT"))
  2.      (setq rev1 (ssget "c"
  3.         '(31.75 21.75)
  4.         '(31.6875 5.25)
  5.         '((8 . "TEXT2,TBLK_TBLK_REVTEXT"))
  6.                 )
  7.           )
  8.   (command "._change" rev1 "" "p" "la" "TBLK_TBLK_REVINIT" "")
  9.  
  10. )

Have I been misunderstanding (if ???

I think my code is saying...

If the layers exist....
do this
and if they don't exist, carry on.
Just check that you have a selection:
Code - Auto/Visual Lisp: [Select]
  1. (if (setq rev1 (ssget "_C" '(31.75 21.75) '(31.6875 5.25) '((8 . "TEXT2,TBLK_TBLK_REVTEXT"))))
  2.   (command "._change" rev1 "" "p" "la" "TBLK_TBLK_REVINIT" "")
  3. )
And you cannot pass multiple layers to tblsearch.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: If statement is wrong ?
« Reply #5 on: July 02, 2021, 04:28:11 PM »
Oh, well I was moving at a bit slower pace but that works too (I got pulled away in two different directions anyways). Thanks, ronjonp
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ronjonp

  • Needs a day job
  • Posts: 7527
Re: If statement is wrong ?
« Reply #6 on: July 02, 2021, 11:45:55 PM »
Oh, well I was moving at a bit slower pace but that works too (I got pulled away in two different directions anyways). Thanks, ronjonp
*cheers*

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC