Author Topic: If then else...  (Read 3123 times)

0 Members and 1 Guest are viewing this topic.

hudster

  • Gator
  • Posts: 2848
If then else...
« on: April 04, 2005, 12:07:14 PM »
I'm trying to writing a lisp which has one variable.
But dependant on that variable 3 different things can happen, but I don't have a clue on how to go about writing the routine to do it.

Here is the pseudo code of what I'm attempting to do, any help would be appreciated.

Code: [Select]
(setq lim (getvar "limmax"))
(setq lim1 (car lim))
(setq lim2 (cadr lim))
;;
(if (> 1180 lim1) Plot A0)
    (< 1180 lim1) (> 840 lim1) plot A1)
    (< 840 lim1) (> 420 lim1) Plot A3)))


Sorry if this isn't good, but I'm trying to get better with stigs course it's getting easier, but still a way to go.
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
If then else...
« Reply #1 on: April 04, 2005, 12:37:54 PM »
"cond" is what your looking for.

"if" only allows two options; one for a true conditional and one for a false conditional.

Look up "cond" and post the def. here and we'll get cha on the right track.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
If then else...
« Reply #2 on: April 04, 2005, 01:16:08 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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
If then else...
« Reply #3 on: April 04, 2005, 03:54:45 PM »
Oh, no, no, no. Get back here Hudster. I had a nice lil example written up that i was waitin' to post.

After you read my example, then you can go play with CAB.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
If then else...
« Reply #4 on: April 04, 2005, 04:26:53 PM »
:D  :D  :D
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
If then else...
« Reply #5 on: April 04, 2005, 05:08:49 PM »
Okay, since I'm going home now I'm just gonna post my write-up (that I was waiting to post till after you did a bit of home work) now. Ill give you the def. of 'cond' here in this post.

Enjoy...


Quote
"COND"

Serves as the primary conditional function for AutoLISP
(cond [(test result ...) ...])

The cond function accepts any number of lists as arguments. It evaluates the first item in each list (in the order supplied) until one of these items returns a value other than nil. It then evaluates those expressions that follow the test that succeeded.


Since we know that 'cond' is what we want to use, let's come up with an example to help ya understand the power of a simple 'cond' statment. (And since I never "GIVE" you the answer, im gonna use an entirely diff example then what you want and make you "work" for your answer. ...lmao!)

Lets say that we are building and program that asks the user for their answer to a specific problem.

Im gonna do all the coding on this example so you just sit back and watch. (All the snipits of code I give you, you should cut and paste into your command line to see how they run.)

First off I'm gonna build a seperate procedure to ask the user for their answer. (This is a nice trick if you have any special formatting or whatever you want to do over and over again.)

Code: [Select]
;;; GETANS
;;; Prompt the user for an answer. Return 'nil' if no answer is supplied.
;;; Example:
;;;   Your answer please: 7
;;;   > "7"
(defun getans ( / a)
  (setq a (getstring "\nYour answer please: "))
  ;; ask the user for an anser
  (if (eq a "")
    ;; test to see if the anser is nothing
   
    nil
    ;; if it is return 'nil' instead of ' "" '
   
    a
    ;; if its a real answer, return that instead.
   
    );_ end if
  );_ end defun


Now that I have a way to ask the user for their answer, I need to know how to use it. We use this procedure like this: (getans) But that dosent help us if we want to capture what the user said and or we want to test that answer for a specific answer so let's use it like this: (setq ans (getans)) Now we are creating a variable and filling it with the users answer. Good now how can I use this to make sure the user actualy answers and dosent hit enter or anything. The answer to that is simeple; use a loop to check to see if the user supplied an answer. *lol*
Code: [Select]

(while (not ans)
   (setq ans (getans))
  );_ end while


** Cut and paste this one instead of that one to test it out. **

Code: [Select]
(progn
  ;;; kinda a 'poor mans defun' for testing pourposes.

  (setq ans (getans))
 
  (while (not ans)
    (setq ans (getans))
   );_ end while

 );_ end progn


So now if the user tries to just hit enter and trick us we know. And sice we know that (s)he just hit enter we can keep asking them to answer untill they actualy do.

If the user finaly answers our question we want to test to see if its the answer we were expecting. Since we are expecting the user to answer with either "a" or "b" we need a way to check for both instances. TIP: And since the user could answer with either a capital letter or a lowercase letter we have at lest four conditions to test for?! *phew!* (OBTW, There is a couple of easier ways to do this, but I want to keep this real simple for ya so bear with me okay?)

Code: [Select]
(cond
  ((eq ans "a") (alert "You answered 'a'!"))
  ((eq ans "A") (alert "You answered 'A'!"))
  ((eq ans "b") (alert "You answered 'b'!"))
  ((eq ans "B") (alert "You answered 'B'!"))
  (t (setq ans (alert "You did not answer correctly")))
 );_ end cond



Did you catch all that? What we just did is:
1. Ask the user for an answer.
2. Check to see if they really did answer and not just his enter
3. Check to see if that answer is either 'a' or 'b'

Lets put all that together into a nice little test package for ya.

Code: [Select]
;;; GETANS
;;; Prompt the user for an answer. Return 'nil' if no answer is supplied.
;;; Example:
;;;   Your answer please: 7
;;;   > "7"
(defun getans ( / a)
  (setq a (getstring "\nYour answer please: "))
  ;; ask the user for an anser
  (if (eq a "")
    ;; test to see if the anser is nothing
   
    nil
    ;; if it is return 'nil' instead of ' "" '
   
    a
    ;; if its a real answer, return that instead.
   
    );_ end if
  );_ end defun

(defun c:MyAnswerTester ( / ans)
  ;;; defun for testing pourposes.

  (setq ans (getans))
  ;; Prompt the user for an answer

  ;; if they dont enter an answer ask then again ...untill they do answer.
  (while (not ans)
     (setq ans (getans))
    );_ end while

  (cond
    ((eq ans "a") (alert "You answered 'a'!"))
    ((eq ans "A") (alert "You answered 'A'!"))
    ((eq ans "b") (alert "You answered 'b'!"))
    ((eq ans "B") (alert "You answered 'B'!"))
    (t (setq ans (alert "You did not answer correctly")))
   );_ end cond

 );_ end defun


Were you able to follow all of that? Do you have any questions on any of it?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

hudster

  • Gator
  • Posts: 2848
If then else...
« Reply #6 on: April 05, 2005, 05:07:36 AM »
Yes I managed to follow it, very informative.

I came up with this routine, it's not pretty but it works.

Code: [Select]
;;;
;;; TITLE:Autoplot_file
;;;
;;; Copyright (C) 2005 by Andy Hudson
;;;
;;; Permission to use, copy, modify, and distribute this
;;; software and its documentation for any purpose and without
;;; fee is hereby granted
;;;
;;; THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR
;;; IMPLIED WARRANTY. ALL IMPLIED WARRANTIES OF FITNESS FOR ANY
;;; PARTICULAR PURPOSE AND OF MERCHANTABILITY ARE HEREBY
;;; DISCLAIMED.
;;;
;;; Andy Hudson
;;; April 2005
;;;
;;;-------------------------------------------------------------
;;; Description:
;;;
;;; Routine to determine drawing size and then
;;; plot to file using appropriate sheet size
;;;
;;;
;;;
;;;-------------------------------------------------------------
;;; COMMAND LINE: Autoplot
;;;-------------------------------------------------------------
(DEFUN C:AUTOPLOT ()
  (SETVAR "TILEMODE" 0)
  (SETQ LIM (GETVAR "LIMMAX"))
;;;GET THE CO-ORDINATES
  (SETQ LIM1 (CAR LIM))
  (SETQ LIM2 (CADR LIM))
  (cond
    ((< 1180 LIM1)
(princ "\nA0")
     (command "zoom" "extents") ;zoom extents
     (COMMAND "-PLOT" ;Plot the drawing
     "Y" ;Detailed plot configuration
     "" ;Enter a layout name
     "\\\\GLASGOW\\1050" ;Enter an output device name
     "Oversize: ISO A0 " ;Enter paper size
     "millimeters" ;Enter paper units [Inches/Millimeters]
     "landscape" ;Enter drawing orientation [Portrait/Landscape]
     "no" ;Plot upside down? Yes/No
     "extents" ;Enter plot area
     "1:1" ;Enter plot scale
     "center" ;Enter plot offset
     "yes" ;Plot with plot styles?
     "rybka battle - STANDARD.ctb" ;Enter plot style table name
     "yes" ;Plot with lineweights?
     "no" ;Scale lineweights with plot scale?
     "no" ;Plot paper space first?
     "no" ;Hide paperspace objects?
     "yes" ;Write the plot to a file
     "" ;filename
     "NO" ;Save changes to page setup
     "yes" ;Proceed with plot
 )
  (princ)
)
    (and (< 840 LIM1) (> 1180 LIM1)
(princ "\nA1")
       (command "zoom" "extents") ;zoom extents
  (COMMAND "-PLOT" ;Plot the drawing
  "Y" ;Detailed plot configuration
  "" ;Enter a layout name
  "\\\\GLASGOW\\1050" ;Enter an output device name
  "Oversize: ISO A1  (landscape)" ;Enter paper size
  "millimeters" ;Enter paper units [Inches/Millimeters]
  "landscape" ;Enter drawing orientation [Portrait/Landscape]
  "no" ;Plot upside down? Yes/No
  "extents" ;Enter plot area
  "1:1" ;Enter plot scale
  "center" ;Enter plot offset  
  "yes" ;Plot with plot styles?
  "rybka battle - STANDARD.ctb" ;Enter plot style table name
  "yes" ;Plot with lineweights?
  "no" ;Scale lineweights with plot scale?
  "no" ;Plot paper space first?
  "no" ;Hide paperspace objects?
  "yes" ;Write the plot to a file
  "" ;filename
  "NO" ;Save changes to page setup
  "yes" ;Proceed with plot
 )
  (princ)))
  )
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
If then else...
« Reply #7 on: April 05, 2005, 06:06:23 AM »
Hudster,
See if this suits you any better.
It may be a little easier to modify for future changes ,
and takes into account a limitsMin other than 0,0,0

Code: [Select]

;;;-------------------------------------------------------------
;;; COMMAND LINE: Autoplot
;;;-------------------------------------------------------------
(defun c:autoplot
       (/ limin limax limit-width limit-height plot-it)
  ;;
  (setvar "TILEMODE" 0)
  ;;-----------------------------------
  ;;-----------------------------------  
  (defun plot-it (device-name paper-size /)
    (command "zoom" "extents")     ;zoom extents
    (command "-PLOT"               ;Plot the drawing
             "Y"                   ;Detailed plot configuration
             ""                    ;Enter a layout name
             device-name           ;Enter an output device name
             paper-size            ;Enter paper size
             "millimeters"         ;Enter paper units [Inches/Millimeters]
             "landscape"           ;Enter drawing orientation [Portrait/Landscape]
             "no"                  ;Plot upside down? Yes/No
             "extents"             ;Enter plot area
             "1:1"                 ;Enter plot scale
             "center"              ;Enter plot offset
             "yes"                 ;Plot with plot styles?
             "rybka battle - STANDARD.ctb"
                                   ;Enter plot style table name
             "yes"                 ;Plot with lineweights?
             "no"                  ;Scale lineweights with plot scale?
             "no"                  ;Plot paper space first?
             "no"                  ;Hide paperspace objects?
             "yes"                 ;Write the plot to a file
             ""                    ;filename
             "NO"                  ;Save changes to page setup
             "yes"                 ;Proceed with plot
            )
  )
  ;;-----------------------------------
  ;;-----------------------------------  
;;;GET THE CO-ORDINATES
  (setq limin        (getvar "LIMMIN")
        limax        (getvar "LIMMAX")
        limit-width  (- (car limax) (car limin))
        limit-height (- (cadr limax) (cadr limin))
  )
  ;;
  (cond ((>= limit-width 1180)
         (princ "\nA0")
         (setq device-name "\\\\GLASGOW\\1050"
               paper-size  "Oversize: ISO A0 "
         )
        )
        ((and (< limit-width 1180)
              (>= limit-width 840)
          )
         (princ "\nA1")
         (setq device-name "\\\\GLASGOW\\1050"
               paper-size  "Oversize: ISO A1  (landscape)"
         )
        )
  )
  ;;
  (if paper-size
    (plot-it device-name paper-size)
  )
  ;;
  (princ)
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

daron

  • Guest
If then else...
« Reply #8 on: April 05, 2005, 07:17:37 AM »
Andy, now that you've gotten your head wrapped around your plotting function, you might want to take a look into the code that CAB used to write >this<.