Author Topic: If within if  (Read 3676 times)

0 Members and 1 Guest are viewing this topic.

caddman6425

  • Guest
If within if
« on: January 07, 2012, 08:40:00 PM »
I think that is wrong and you should use a cond statment, but I've got so many cond statments now, that I'm lost.  I want to ask a yes or no question and if they answer yes then it asks to make a choice between 3 answers.  Depending on this answer it goes into a cond statment.  There might not be enough here to see what I'm trying to accomplish.  Jest let me know if you want the whole thing.  I havent got the cond statment all worked out though.
Code: [Select]
;;;                         Choices                               ;;;
(initget "Yes No")
(setq ch  (getkword "\nIs this going to be a Multi-Member Unit? [Yes or No] <No>: "); If the answer is yes then I want to ask this next question
 (if (= ch "Yes")
   (progn
     (initget "2 3 4")
     (setq ans (getkword "\nHow many members will make up this Multi-Member Unit? (2, 3, or 4)  <2> ")
     ); End setq
   ); End prognn
   (progn
     (setq sp     '(0 0)
           wid     (getreal  "\nWhat is the width of this Member Shape?  ")
           dep     (getreal  "\nWhat is the depth of this Member Shape?  ")
           memshna (getstring T "\nWhat is the name of the Member Shape? ")
     ); End setq
   ); End progn
 ); End if
); End setq   
   

« Last Edit: January 08, 2012, 02:56:10 PM by CAB »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Re: If within if
« Reply #1 on: January 07, 2012, 11:16:07 PM »
What you have looks ok except the ans variable when ENTER is pressed.
I would do it this way. Although more error checking is needed.
Code: [Select]
(initget "Yes No")
(if (setq ch (= (getkword "\nIs this going to be a Multi-Member Unit? [Yes or No] <No>: ") "Yes"))
  ;; ONLY USE ch if you need it, in this case ch=true when YES else ch=nil
  (progn
    (initget "2 3 4")
    (if (null (setq ans (getkword "\nHow many members will make up this Multi-Member Unit? (2, 3, or 4)  <2> ")))
      (setq ans "2")
    )
  )
  (setq sp      '(0 0)
        wid     (getreal "\nWhat is the width of this Member Shape?  ")
        dep     (getreal "\nWhat is the depth of this Member Shape?  ")
        memshna (getstring t "\nWhat is the name of the Member Shape? ")
  ) ; End setq
) ; End if

More info here:
http://www.theswamp.org/index.php?topic=13046.msg158557#msg158557
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.

BlackBox

  • King Gator
  • Posts: 3745
Re: If within if
« Reply #2 on: January 08, 2012, 12:29:55 AM »
Another option:

Code: [Select]
(initget "Yes No")
(if (or (setq opt (getkword "\nSelect an option [Yes/No] <Yes>: "))
     (= nil opt))
  ;; Either yes, no, or enter (right click) selected
  (prompt (strcat "\nYou selected " opt))
  ;; Nothing selected
  )
"How we think determines what we do, and what we do determines what we get."

caddman6425

  • Guest
Re: If within if
« Reply #3 on: January 08, 2012, 06:04:13 AM »
Well, that checked out untill I ran the whole thing, Now I get this error, I used my error checking routine:


Code - Auto/Visual Lisp: [Select]
  1. Command: SMC
  2.  
  3. Is this going to be a Multi-Member Unit? [Yes or No] <No>: y
  4.  
  5. How many members will make up this Multi-Member Unit? (2, 3, or 4)  <2> 2
  6.  
  7. AutoLISP Error:  bad argument type: numberp: nil-layer
  8. Current layer:  "0"
  9. Enter an option
  10. [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/PSt
  11. yle/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: s
  12. Enter layer name to make current or <select object>: 0 Enter an option
  13. [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/PSt
  14. yle/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:
  15.  
  16.  
  17. Here is all of the code, predicate 1-3 are all  the same at the moment, so forget them.
  18.  
  19. ;;                                Copyright© 2012 by Nick Haury                                    ;;;
  20. ;*****************************************************************;
  21. (defun *error* (msg);                                                
  22.   (if (/= msg "Function cancelled") ;                                
  23.     (princ (strcat "\nAutoLISP Error:  " msg)) ;                    
  24.     (terpri);                                                        
  25.   );                                                                
  26.   (setvar "CMDECHO" S_CMD)              ;                            
  27.   (setvar "ORTHOMODE" S_ORTH)           ;                            
  28.   (setvar "OSMODE" S_OSM)               ;                            
  29.   (command "-layer" "s" CL "" "redraw") ;                            
  30. ); End defun                                                        
  31. ;;                                                                  
  32. ;;             Subroutine to Start program                          
  33. (defun start ();                                                    
  34.   (graphscr)                           ;                            
  35.   (setq CL (getvar "CLAYER"))          ; Stores Current Layer;      
  36.   (setq S_CMD (getvar "CMDECHO"))      ; Stores Command Mode State;  
  37.   (setq S_ORTH (getvar "ORTHOMODE"))   ; Stores Orthomode State;    
  38.   (setq S_OSM (getvar "OSMODE"))       ; Stores Object Snap State;  
  39.   (setvar "CMDECHO" 1)                 ; No echo;                    
  40.   (setvar "ORTHOMODE" 1)               ; Ortho on;                  
  41.   (setvar "OSMODE" 0)                  ; Snap None;                  
  42. ); End defun                                                        
  43. ;;                                                                  
  44. ;;            Subroutine to end program                              
  45. (defun Stop ()                        ;                              
  46.   (setvar "CMDECHO" S_CMD)            ; Restores Stored Command Echo
  47.   (setvar "OSMODE" S_OSM)             ; Restores Stored Object Snap  
  48.   (command "-LAYER" "s" CL "")        ; Restores Current Layer State
  49. ); End defun                                                        
  50. ;;;                        Start Routine                          ;;;
  51. (defun c:smc ()
  52. (start)
  53. ;;;                         Choices                               ;;;
  54. (initget "Yes No")
  55. (if (setq ch (= (getkword "\nIs this going to be a Multi-Member Unit? [Yes or No] <No>: ") "Yes"))
  56.   (progn
  57.     (initget "2 3 4")
  58.     (if (null (setq ans (getkword "\nHow many members will make up this Multi-Member Unit? (2, 3, or 4)  <2> ")))
  59.       (setq ans "2")
  60.     ); End if
  61.   ); End progn
  62.   (setq sp      '(0 0)
  63.         wid     (getreal "\nWhat is the width of this Member Shape?  ")
  64.         dep     (getreal "\nWhat is the depth of this Member Shape?  ")
  65.         memshna (getstring t "\nWhat is the name of the Member Shape? ")
  66.   ); End setq
  67. ); End if
  68. ;;;                        Do the Math                            ;;;
  69. (setq x1 (car sp)
  70.        x2 (+ x1 (/ wid 2.0))
  71.        x3 (+ x1 wid)
  72.        y1 (cadr sp)
  73.        y2 (+ y1 dep)
  74. ); End setq
  75. ;;;                       Point Assignment                        ;;;
  76. (setq p1 (list x1 y1)
  77.        p2 (list x2 y1)
  78.        p3 (list x3 y1)
  79.        p4 (list x3 y2)
  80.        p5 (list x1 y2)
  81. ); End setq
  82. ;;;                         Lets Draw                             ;;;
  83.      "_.pline" p1 p3 p4 p5 "c"
  84. ); End command
  85. ;;;          Let's Define the Multi-Member Shapes & Styles        ;;;
  86.  (if (= ch "Yes")
  87.   (progn
  88.    ; (setq ans (getkword "\nHow many members will make up this Multi-Member Unit? (2, 3, or 4)  <2> "))
  89. ;;;          Let's Define the Double Multi-Member Shape & Style          ;;;
  90.     (cond
  91.      (; Start predicate1
  92.       (= ans 2)
  93.       (setq memstyna (getstring T "\nWhat is the Member Style Name going to be?  ")); End setq
  94.       (command "_-AecsMemberShapeDefine"
  95.             "_New" memshna
  96.             "_Graphics"
  97.             "_Low"    (entlast) "" p2
  98.             "_Medium" (entlast) p2
  99.             "_High"   (entlast) p2 "" "" ""
  100.       ); End command
  101.       (entdel (entlast))
  102.       (command "-MemberStyle"  
  103.             "_New" memstyna
  104.             "_Component" "_Edit" "1" "_Name" "Section" "STart"
  105.             "_SHape" memshna "" "_ENd" "_SHape" memshna "" ""
  106.             "_Add" "_Edit" "2" "_Name" "Section" "_Start" "_SHape"
  107.             memshna "_Offset" "" wid "" "" "_ENd" "_SHape" memshna
  108.             "_Offset" "" "" "" "" "" "" "" "" ""
  109.       ); End command  
  110.      ); End predicate1
  111. ;;;          Let's Define the Triple Multi-Member Shape & Style          ;;;
  112.      (; Start predicate2
  113.       (= ans 3)
  114.       (setq memstyna (getstring T "\nWhat is the Member Style Name going to be?  ")); End setq
  115.       (command "_-AecsMemberShapeDefine"
  116.             "_New" memshna
  117.             "_Graphics"
  118.             "_Low"    (entlast) "" p2
  119.             "_Medium" (entlast) p2
  120.             "_High"   (entlast) p2 "" "" ""
  121.       ); End command
  122.       (entdel (entlast))
  123.       (command "-MemberStyle"  
  124.             "_New" memstyna
  125.             "_Component" "_Edit" "1" "_Name" "Section" "STart"
  126.             "_SHape" memshna "" "_ENd" "_SHape" memshna "" ""
  127.             "_Add" "_Edit" "2" "_Name" "Section" "_Start" "_SHape"
  128.             memshna "_Offset" "" wid "" "" "_ENd" "_SHape" memshna
  129.             "_Offset" "" "" "" "" "" "" "" "" ""
  130.       ); End command  
  131.      ); End predicate2
  132. ;;;          Let's Define the Quadruple Multi-Member Shape & Style          ;;;
  133.      (; Start predicate3
  134.       (= ans 4)
  135.       (setq memstyna (getstring T "\nWhat is the Member Style Name going to be?  ")); End setq
  136.       (command "_-AecsMemberShapeDefine"
  137.             "_New" memshna
  138.             "_Graphics"
  139.             "_Low"    (entlast) "" p2
  140.             "_Medium" (entlast) p2
  141.             "_High"   (entlast) p2 "" "" ""
  142.       ); End command
  143.       (entdel (entlast))
  144.       (command "-MemberStyle"  
  145.             "_New" memstyna
  146.             "_Component" "_Edit" "1" "_Name" "Section" "STart"
  147.             "_SHape" memshna "" "_ENd" "_SHape" memshna "" ""
  148.             "_Add" "_Edit" "2" "_Name" "Section" "_Start" "_SHape"
  149.             memshna "_Offset" "" wid "" "" "_ENd" "_SHape" memshna
  150.             "_Offset" "" "" "" "" "" "" "" "" ""
  151.       ); End command  
  152.      ); End predicate3
  153.     ); End cond      
  154.   ); End progn        
  155. ;;;          Let's Define the Unequal Multi-Member Shape & Style          ;;;
  156. ;;; Maybe something here
  157. ;;;               Let's Define the Member Shape & Style                   ;;;
  158.    (progn
  159.     (setq memstyna (getstring T "\nWhat is the Member Style Name going to be? ")); End setq
  160.     (command "_-AecsMemberShapeDefine"
  161.              "_New" memshna
  162.              "_Graphics"
  163.              "_Low"    (entlast) "" p2
  164.              "_Medium" (entlast) p2
  165.              "_High"   (entlast) p2 "" "" ""
  166.     ); End command
  167.     (entdel (entlast))
  168.     (command "-MemberStyle"
  169.              "_New" memstyna
  170.              "_Component" "_Edit"    "1" "_Name" "Section"
  171.              "_STart" "_SHape"    memshna "" "_ENd" "_SHape"
  172.              memshna "" "" "" "" ""
  173.     ); End command
  174.    ); end progn
  175.   ); end if
  176. (stop)
  177. ); End C:SMC
« Last Edit: January 08, 2012, 01:05:58 PM by caddman6425 »

ribarm

  • Water Moccasin
  • Posts: 2226
  • Marko Ribar, architect
Re: If within if
« Reply #4 on: January 08, 2012, 07:17:10 AM »
You must define variables if your first choice is "Y" to make routine continue (your actual error is variable wid = nil) - Start VLIDE for debugging, open your *.lsp, press "Load active edit window" button, then press "Activate AutoCAD" button, type SMC in command prompt and if it brakes it will return you automatically to VLIDE => press icon "Last break" and you'll see where error occurs :

;;;                         Choices                               ;;;
(initget "Yes No")
(if (setq ch (= (getkword "\nIs this going to be a Multi-Member Unit? [Yes or No] <No>: ") "Yes"))
  (progn
    (initget "2 3 4")
    (if (null (setq ans (getkword "\nHow many members will make up this Multi-Member Unit? (2, 3, or 4)  <2> ")))
      (setq ans "2")
    ); End if
    (setq sp      '(0 0)
        wid     (getreal "\nWhat is the width of this Member Shape?  ")
        dep     (getreal "\nWhat is the depth of this Member Shape?  ")
        memshna (getstring t "\nWhat is the name of the Member Shape? ")
    ); End setq

  ); End progn
  (setq sp      '(0 0)
        wid     (getreal "\nWhat is the width of this Member Shape?  ")
        dep     (getreal "\nWhat is the depth of this Member Shape?  ")
        memshna (getstring t "\nWhat is the name of the Member Shape? ")
  ); End setq
); End if

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Ketxu

  • Newt
  • Posts: 104
Re: If within if
« Reply #5 on: January 08, 2012, 12:49:00 PM »
@ caddman6425 & ribarm : oh, what happened with Codebox ^^

caddman6425

  • Guest
Re: If within if
« Reply #6 on: January 08, 2012, 01:04:12 PM »
Hmmmm, whats that?  If I should have pasted the code as an attachment, then I'm sorry.  If so how would I do that?  If that is not the case, then what are you talking about?  Is that better?  I was wondering if that was what it was for.  Uh next time, why don't you do a private email and clue me in then  wasting the time to do this.  Sorry I offended you.
« Last Edit: January 08, 2012, 01:08:51 PM by caddman6425 »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Re: If within if
« Reply #7 on: January 08, 2012, 02:55:05 PM »
Not a big deal but he is referring to the [ code] [ /code] tags.
Highlight your code and click on the # button.

I'll revise your first post as an example.
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.

caddman6425

  • Guest
Re: If within if
« Reply #8 on: January 08, 2012, 03:09:16 PM »
Do you prefer just code or code = autolisp


Code: [Select]
Code, code, code
or

Code - Auto/Visual Lisp: [Select]
  1. Code, code, code

Oh yeah, what ribarm did worked but now I think I have definite problems with the cond statements, they don't seem to work.  What I'm trying to accomplish is an understanding of what I'm doing before I put most of the setq to OpenDCL
« Last Edit: January 08, 2012, 03:13:02 PM by caddman6425 »

caddman6425

  • Guest
Re: If within if
« Reply #9 on: January 08, 2012, 03:24:45 PM »
Do I have these cond statments set up correctly?

Code: [Select]
(if (= ch "Yes")
  (progn
;;;          Let's Define the Double Multi-Member Shape & Style          ;;;
    (cond ((= ans 2)
           (setq memstyna (getstring T "\nWhat is the Member Style Name going to be?  ")); End setq
           (shapename)
           (princ "Hi")
           (command "-MemberStyle"   
                    "_New" memstyna
                    "_Component" "_Edit" "1" "_Name" "Section" "STart"
                    "_SHape" memshna "" "_ENd" "_SHape" memshna "" ""
                    "_Add" "_Edit" "2" "_Name" "Section" "_Start" "_SHape"
                    memshna "_Offset" "" wid "" "" "_ENd" "_SHape" memshna
                    "_Offset" "" "" "" "" "" "" "" "" ""
           ); End command 
          ); End predicate1
;;;          Let's Define the Triple Multi-Member Shape & Style          ;;;
          ((= ans 3)
           (setq memstyna (getstring T "\nWhat is the Member Style Name going to be?  ")); End setq
           (shapename)
           (command "-MemberStyle"   
                 "_New" memstyna
                 "_Component" "_Edit" "1" "_Name" "Section" "STart"
                 "_SHape" memshna "" "_ENd" "_SHape" memshna "" ""
                 "_Add" "_Edit" "2" "_Name" "Section" "_Start" "_SHape"
                 memshna "_Offset" "" wid "" "" "_ENd" "_SHape" memshna
                 "_Offset" "" "" "" "" "" "" "" "" ""
           ); End command 
          ); End predicate2
;;;          Let's Define the Quadruple Multi-Member Shape & Style          ;;;
         ((= ans 4)
          (setq memstyna (getstring T "\nWhat is the Member Style Name going to be?  ")); End setq
          (shapename)
          (command "-MemberStyle"   
                "_New" memstyna
                "_Component" "_Edit" "1" "_Name" "Section" "STart"
                "_SHape" memshna "" "_ENd" "_SHape" memshna "" ""
                "_Add" "_Edit" "2" "_Name" "Section" "_Start" "_SHape"
                memshna "_Offset" "" wid "" "" "_ENd" "_SHape" memshna
                "_Offset" "" "" "" "" "" "" "" "" ""
          ); End command 
         ); End predicate3
    ); End cond       
  ); End progn       
;;;          Let's Define the Unequal Multi-Member Shape & Style          ;;;
;;; Maybe something here
;;;               Let's Define the Member Shape & Style                   ;;;
  (progn
   (setq memstyna (getstring T "\nWhat is the Member Style Name going to be? ")); End setq
   (shapename)
   (command "-MemberStyle"
            "_New" memstyna
            "_Component" "_Edit"    "1" "_Name" "Section"
            "_STart" "_SHape"    memshna "" "_ENd" "_SHape"
            memshna "" "" "" "" ""
   ); End command
  ); end progn
 ); end if

Can I use the same 'IF' twice?

ribarm

  • Water Moccasin
  • Posts: 2226
  • Marko Ribar, architect
Re: If within if
« Reply #10 on: January 08, 2012, 03:32:55 PM »
You have to group "then" conditions after predicate statement :
(cond
  ((= ans "2")
   (progn (...)(...)(...)...)
  )
  ((= ans "3")
   (progn ....)
  )
  ...
); End of cond

M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

caddman6425

  • Guest
Re: If within if
« Reply #11 on: January 08, 2012, 03:37:17 PM »
Darn. I just keep forgetting that progn, darn little thing.  No, that argument is not true, I just keep tripping over my own brain. ;-D

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Re: If within if
« Reply #12 on: January 08, 2012, 04:39:28 PM »
(COND does not require progn, only the (IF requires them if you have more than one code group.
The following works just fine.
Code: [Select]
(cond
  ((= ans "2")
    (...)(...)(...)
  )
  ((= ans "3")
    (....)
  )
); End of cond
 
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: 12313
  • London, England
Re: If within if
« Reply #13 on: January 08, 2012, 05:51:32 PM »
Code: [Select]
(if
    (or
        (setq opt (getkword "\nSelect an option [Yes/No] <Yes>: "))
        (= nil opt)
    )
    ...
)

This says: "If (<opt> or (not <opt>)): do this ..."

The IF statement is redundant since the 'else' expression will NEVER be evaluated (<opt> can only be nil or non-nil).


caddman6425

  • Guest
Re: If within if
« Reply #14 on: January 08, 2012, 06:15:01 PM »
OOOKKKay, uh, where and how would I use this? :|