Author Topic: Choosing routine  (Read 5108 times)

0 Members and 1 Guest are viewing this topic.

DEVITG

  • Bull Frog
  • Posts: 480
Choosing routine
« on: February 23, 2004, 05:32:30 PM »
Please help with this routine.

I'm trying to choose one of two defun (CCW) or (CW) according to the L or R


Code: [Select]

;;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(DEFUN ORIENT ()

  (INITGET 1 "L R")
  (SETQ MODE
(GETKWORD
  "\nWHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH "
) ;_end getkword
  ) ;_end setq code
 
  (if (= MODE (or "l" "L"))
    (CcW)
    (cw)
  )


) ;_end orient

;;*//*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/




As  MODE only could be "l" "L" or "r" "R"
The if should route to CCW if MODE= "l" or "L"
or to CW if MODE ="r" or "R"
But it  allways route me to CW

  What I'm doing wrong??? :oops:  :cry:  :roll:
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Choosing routine
« Reply #1 on: February 23, 2004, 05:41:13 PM »
You dont have to worry about upper/lower case because of the
initget. And you don't need to check 'mode' because an escape
is the only way to exit and that would stop the whole routine.
Instead of an 'if' use a 'cond' to test for either 'L' or 'R'
Code: [Select]

(cond ((= mode "L")(Ccw))
 ((= mode "R")(cw))
 )
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Choosing routine
« Reply #2 on: February 23, 2004, 05:50:49 PM »
As they say, "the proof is in the pudding"
Code: [Select]

Command: (orient)

WHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH r
R
Command: (orient)

WHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH l
L
Command: (orient)

WHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH <enter>

Invalid option keyword.

WHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH 4

Invalid option keyword.

WHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH -1

Invalid option keyword.

WHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH  <escape>*Cancel*
; error: Function cancelled

notice when I entered a lowercase 'r' it returned an uppercase 'R' :D
TheSwamp.org  (serving the CAD community since 2003)

DEVITG

  • Bull Frog
  • Posts: 480
I got it
« Reply #3 on: February 23, 2004, 07:23:58 PM »
After a lot of trial and error , and half an hour at the TV seing The Simpson, I got the way to do it.

Despite the initget "L R" only allow to type an L or R no matter if capital or not, the MODE is setq to R , r  L , l .
Just to tes I did it

Quote


Command: !MODE
"L"
Command: (= MODE "l")
nil


So I solved it as follow
Code: [Select]


(DEFUN ORIENT (/ MODE)
  (INITGET 1 "L R")
  (SETQ MODE
(GETKWORD
  "\nWHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH "
) ;_end getkword
  ) ;_end setq MODE
 
 
  (COND
    ((= MODE "l") (CCW))
    ((= MODE "L") (CCW))
    ((= MODE "R") (CW))
    ((= MODE "r") (CW))

  ) ;_END COND
) ;_END ORIENT


:)
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Choosing routine
« Reply #4 on: February 23, 2004, 08:07:21 PM »
DEVITG
When you use (INITGET 1 "L" "R") the return value R or L is in uppercase.
No need to test for lower case.
See the test run Mark printed. If you enter r then Mode will be R.

This is all you need. Enter r and see.

Code: [Select]
(DEFUN ORIENT (/ MODE)
  (INITGET 1 "L R")
  (SETQ   MODE
    (GETKWORD
      "\nWHICH WAY YOU WANT TO DRAW THE ELLBOWS  ? LEFT or RIGTH "
    ) ;_end getkword
  ) ;_end setq MODE
 
   (COND
    ((= MODE "L") (CCW))
    ((= MODE "R") (CW))
  ) ;_END COND
) ;_END ORIENT
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.

DEVITG

  • Bull Frog
  • Posts: 480
Choosing routine
« Reply #5 on: February 23, 2004, 09:51:02 PM »
Ok I will see it tomorrow.
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

hendie

  • Guest
Choosing routine
« Reply #6 on: February 24, 2004, 03:19:58 AM »
*ahem* ELLBOW only has 1 "L" (ELBOW)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Choosing routine
« Reply #7 on: February 24, 2004, 06:18:49 AM »
:yikes:
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

hudster

  • Gator
  • Posts: 2848
Choosing routine
« Reply #8 on: February 24, 2004, 07:17:03 AM »
Seeing as how we are on spelling.

rigth is actually spelt RIGHT. :lol:
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

SMadsen

  • Guest
Choosing routine
« Reply #9 on: February 24, 2004, 08:47:10 AM »
Here's one using the "RRB Default Method":
Code: [Select]
(defun orient ()
  (initget "Left Right")
  (cond ((= "Left" (cond ((getkword
                   "\nElbow direction [Left/Right] <Right>: "))
                   ("Right")))
         (ccw))
        ((cw))
  )
)

DEVITG

  • Bull Frog
  • Posts: 480
I WAS ON A MISSTAKE:oops: :oops:
« Reply #10 on: February 24, 2004, 12:43:04 PM »
Cab, you are rigth, I learned other more LISP topic today.

Keith , english is my second language , so often I make errors.

SMadsen.

Would you please explain how it  work? , there are  nested COND I do not see clear.
It work well , but I do not know why :oops:
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

SMadsen

  • Guest
Choosing routine
« Reply #11 on: February 24, 2004, 01:26:40 PM »
DEVITG, it'll be my pleasure.

As you might know, COND works like a select-case function:

Code: [Select]

(cond  ((is this true?) (then do this))
       ((ok, so is this true?) (then do this))
       ((hmm, is this true then?) (then do this))
       ...
)


(setq a 7)

(cond ((= a 9)(princ "No way this is printed"))
      ((= 0 (rem a 2))
       (princ "Nope, the remainder of 7/2 is not 0")
       (princ "This will not be printed, either")
      )
      ((= a (* 2 3.5))
       (princ "Yeah, this is true! Print me!")
      )
)


Sorry about the silly example, but have a look at the conditions and forget everything else. COND runs down the "list" of conditions and evaluates each one. When - or if - it finds a condition that is not nil, it will execute not only the condition but also any following statements and RETURN the value of the last statement.
Hence, the silly example above will print "Yeah, this is true! Print me!" to the screen.

Now, remove all following statements, leaving only the conditions:

Code: [Select]

(setq a 7)

(cond ((= a 9))
      ((= 0 (rem a 2)))
      ((= a (* 2 3.5)))
)


What will this return? Again, it runs down all conditions and evaluates them. When - or if - it finds a condition that is not nil, it executes not only the condition but .. ummm, wait, there are no following statements! So, it will simply return the value of the condition - which in this case is T.

Let's take a look at the innermost COND (of course, we'll include the INITGET, without which the GETKWORD makes no sense):

Code: [Select]

(initget "Left Right")
(cond ((getkword "\nElbow direction [Left/Right] <Right>: "))
      ("Right")
)


Notice that there are only conditions within the COND. But it doesn't matter because we just learned that COND will return the value of the first non-nil condition when there are no statements following the condition.
So, GETKWORD will switch to AutoCAD and await an answer. We can expect three different answers to arrive:
1. the user hits Enter and GETKWORD returns nil
2. the user types "Left" (or "L" or "Le" ...) and GETKWORD returns "Left"
3. the user types "Right" (or "R" ...) and GETKWORD returns "Right"

How will COND react to each of these answers? Let's take them in reverse order:

3. Because GETKWORD is the condition and it returns "Right", COND will see this as a non-nil value and return the result of the condition - which is "Right"
2. The same as 3., only that GETKWORD returns "Left".
1. If the user hits Enter and causes GETKWORD to return nil, COND will jump to the next condition which simply is the string "Right". A string is not a non-nil value and will therefore be returned by COND.

Because "Right" is offered as the default answer, it shall also be returned if the user hits Enter. This is accomplished by inserting "Right" as a condition.

The outermost COND simply says: if the innermost COND returns "Left" then do CCW, otherwise do CW. The latter is triggered by "Right", which is triggered by the user typing "R" or by hitting Enter.

The outermost COND could easily be written as an IF if it makes more sense:

Code: [Select]
(if (= "Left"
       (cond ((getkword "\nElbow direction [Left/Right] <Right>: "))
             ("Right")
       )
    )
  (ccw)
  (cw)
)

DEVITG

  • Bull Frog
  • Posts: 480
nested cond
« Reply #12 on: February 24, 2004, 02:16:34 PM »
SMadsen, thank for your explanation , I shall chew it a lot  , and see what I get.

Thanks againg
Location @ Córdoba Argentina Using ACAD 2019  at Window 10