Author Topic: help with a lisp  (Read 10736 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #15 on: March 27, 2014, 03:57:52 PM »

There are a few number ranges that are not evaluated.

Draw yourself a numberLine and mark off the ranges you ARE testing.
http://en.wikipedia.org/wiki/Number_line

You may need to add conditionals and assignments for those ranges ...
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #16 on: March 27, 2014, 04:01:16 PM »
It looks like you are missing some parenthesis in your conditional statement

(cond
    <HERE> (and (>= area 750) (<= area 1200) <and HERE>

        .. do stuff
     )
    .. more conditionals
)
« Last Edit: March 27, 2014, 04:09:13 PM by Kerry »
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.

pedroantonio

  • Guest
Re: help with a lisp
« Reply #17 on: March 27, 2014, 04:16:14 PM »
I add the parenthesis
The lisp run and when i select 3 option  gives me  this error !!!
; error: bad argument type: numberp: nil

Code - Auto/Visual Lisp: [Select]
  1.  (defun c:test4
  2.        (/ _option1 _option2 _option3 _option4 area dxf ent sdarea skarea)
  3.   (defun _option1 ()
  4.     (cond ((<= area 200)
  5.            (setq sdarea area
  6.                  skarea (* area 0.7)
  7.            )
  8.           )
  9.           ((< area 700)
  10.            (setq sdarea 240
  11.                  skarea (* area 0.6)
  12.            )
  13.           )
  14.           ((>= area 700)
  15.            (setq sdarea 400
  16.                  skarea (* area 0.6)
  17.            )
  18.           )
  19.     )
  20.   )
  21.   (defun _option2 ()
  22.   (cond ((<= sdarea 400)  ; I don't know if this is correct maxD = 400
  23.            (setq sdarea (* area 0.8)
  24.                  skarea (* area 0.6)
  25.            )
  26.           )          
  27.     )
  28.   )
  29.   (defun _option3 ()
  30.  
  31.       (cond ((and (>= area 750) (<= area 1200)
  32.            (setq sdarea (+ 100 ( / (- area 750) 9))
  33.                  skarea (* area 0.1)
  34.            )
  35.           )
  36.                    
  37.            ((and (> area 1200) (<= area 2000)
  38.            (setq sdarea (+ 150 ( / (- area 1200) 16))
  39.                  skarea (* area 0.1)
  40.            )
  41.           )
  42.            ((and (> area 2000) (< area 4000)
  43.            (setq sdarea 0
  44.                  skarea 0
  45.                     )
  46.           )
  47.  
  48.            ((= area 4000)
  49.            (setq sdarea 200
  50.                  skarea (* area 0.1)
  51.            )
  52.           )
  53.           ((and (> area 4000) (<= area 8000)
  54.            (setq sdarea (+ 200 ( * 0.02 (- area 4000)))
  55.                  skarea (* area 0.1)
  56.            )
  57.           )
  58.          
  59.           ((> area 8000)
  60.            (setq sdarea (+ 280 ( * 0.01 (- area 8000)))
  61.                  skarea (* area 0.1)
  62.              )
  63.            )
  64.          )
  65.        )
  66.      )
  67.     )
  68.   )
  69. )
  70.  
  71.   (defun _option4 ()
  72.     (initget 7)
  73.     (setq sdarea (* area (getreal "\n Give sd (example 1.2) : ")))
  74.     (initget 7)
  75.     (setq skarea (* area (getreal "\n Give sk in % (example 0.70) : ")))
  76.   )
  77.   ;; Main -----
  78.   (if (and (setq ent (car (entsel "\n-> Select a closed polyline : ")))
  79.            (wcmatch (cdr (assoc 0 (setq dxf (entget ent)))) "*POLYLINE")
  80.            (member (cdr (assoc 70 dxf)) '(1 129))
  81.       )
  82.     (progn
  83.       (initget 7)
  84.   (PROMPT
  85.     "\n choose option :
  86.  
  87.   1. Choose 1 for this ...
  88.   2. Choose 2 for this ...
  89.   3. Choose 3 for this ...
  90.   4. Choose 4 for this ...
  91.  
  92. " )
  93.       (setq area   (vlax-curve-getarea ent)
  94.             option (getint "Select Option 1 to 4 : ")
  95.       )
  96.       (cond ((= option 1) (_Option1))
  97.             ((= option 2) (_Option2))
  98.             ((= option 3) (_Option3))
  99.             ((= option 4) (_Option4))
  100.             (t (alert "Read the prompt, Idiot\nUsing Option 4. ") (_Option4))
  101.       )
  102.       (princ (strcat "\n Selected Area = "
  103.                      (rtos area 2 2)
  104.                      "\n D = "
  105.                      (rtos sdarea 2 2)
  106.                      " sq.m"
  107.                      "\n K = "
  108.                      (rtos skarea 2 2)
  109.                      " sq.m"
  110.              )
  111.       )
  112.     )
  113.     (princ "\n-> No valid polyline selected...")
  114.   )
  115.   (princ)
  116. )
  117.  
  118.  
  119.  
« Last Edit: March 27, 2014, 04:23:09 PM by Topographer »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #18 on: March 27, 2014, 04:22:06 PM »
I add the parenthesis and now gives me

< .. >

Have another look ... your conditional test is mixed with the assignment code.
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.

pedroantonio

  • Guest
Re: help with a lisp
« Reply #19 on: March 27, 2014, 04:30:53 PM »
I update the code but i still have the same problom with the option 3
; error: bad argument type: numberp: nil

Code - Auto/Visual Lisp: [Select]
  1.  (defun c:test4
  2.        (/ _option1 _option2 _option3 _option4 area dxf ent sdarea skarea)
  3.   (defun _option1 ()
  4.     (cond ((<= area 200)
  5.            (setq sdarea area
  6.                  skarea (* area 0.7)
  7.            )
  8.           )
  9.           ((< area 700)
  10.            (setq sdarea 240
  11.                  skarea (* area 0.6)
  12.            )
  13.           )
  14.           ((>= area 700)
  15.            (setq sdarea 400
  16.                  skarea (* area 0.6)
  17.            )
  18.           )
  19.     )
  20.   )
  21.   (defun _option2 ()
  22.     (cond ((<= area 2000)
  23.             (setq sdarea  (* area 0.8)
  24.              skarea (* area 0.6)
  25.            )
  26.           )    
  27.          ((> area 2000)
  28.            (setq sdarea (+ 400 ( * 0.05 (- area 2000)))
  29.                  skarea (* area 0.1)
  30.            )
  31.           )    
  32.     )
  33.   )
  34.   (defun _option3 ()
  35.  
  36.       (cond ((and (>= area 750) (<= area 1200)
  37.            (setq sdarea (+ 100 ( / (- area 750) 9))
  38.                  skarea (* area 0.1)
  39.            )
  40.           )
  41.  
  42.            ((and (> area 1200) (<= area 2000)
  43.            (setq sdarea (+ 150 ( / (- area 1200) 16))
  44.                  skarea (* area 0.1)
  45.            )
  46.           )
  47.            ((and (> area 2000) (< area 4000)
  48.            (setq sdarea 0
  49.                  skarea 0
  50.                     )
  51.           )
  52.  
  53.            ((= area 4000)
  54.            (setq sdarea 200
  55.                  skarea (* area 0.1)
  56.            )
  57.           )
  58.           ((and (> area 4000) (<= area 8000)
  59.            (setq sdarea (+ 200 ( * 0.02 (- area 4000)))
  60.                  skarea (* area 0.1)
  61.            )
  62.           )
  63.  
  64.           ((> area 8000)
  65.            (setq sdarea (+ 280 ( * 0.01 (- area 8000)))
  66.                  skarea (* area 0.1)
  67.              )
  68.            )
  69.          )
  70.        )
  71.      )
  72.     )
  73.   )
  74. )
  75.  
  76.   (defun _option4 ()
  77.     (initget 7)
  78.     (setq sdarea (* area (getreal "\n Give sd (example 1.2) : ")))
  79.     (initget 7)
  80.     (setq skarea (* area (getreal "\n Give sk in % (example 0.70) : ")))
  81.   )
  82.   ;; Main -----
  83.   (if (and (setq ent (car (entsel "\n-> Select a closed polyline : ")))
  84.            (wcmatch (cdr (assoc 0 (setq dxf (entget ent)))) "*POLYLINE")
  85.            (member (cdr (assoc 70 dxf)) '(1 129))
  86.       )
  87.     (progn
  88.       (initget 7)
  89.   (PROMPT
  90.     "\n choose option :
  91.  
  92.   1. Choose 1 for this ...
  93.   2. Choose 2 for this ...
  94.   3. Choose 3 for this ...
  95.   4. Choose 4 for this ...
  96.  
  97. " )
  98.       (setq area   (vlax-curve-getarea ent)
  99.             option (getint "Select Option 1 to 4 : ")
  100.       )
  101.       (cond ((= option 1) (_Option1))
  102.             ((= option 2) (_Option2))
  103.             ((= option 3) (_Option3))
  104.             ((= option 4) (_Option4))
  105.             (t (alert "Read the prompt, Idiot\nUsing Option 4. ") (_Option4))
  106.       )
  107.       (princ (strcat "\n Selected Area = "
  108.                      (rtos area 2 2)
  109.                      "\n D = "
  110.                      (rtos sdarea 2 2)
  111.                      " sq.m"
  112.                      "\n K = "
  113.                      (rtos skarea 2 2)
  114.                      " sq.m"
  115.              )
  116.       )
  117.     )
  118.     (princ "\n-> No valid polyline selected...")
  119.   )
  120.   (princ)
  121. )
  122.  
  123.  
  124.  

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #20 on: March 27, 2014, 04:33:40 PM »

Try something like this for _option2
Code - Auto/Visual Lisp: [Select]
  1. (defun _option2 ()
  2.   (cond (area
  3.          (setq sdarea (min 400.0 (* area 0.8))
  4.                skarea (* area 0.6)
  5.          )
  6.         )
  7.   )
  8. )
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #21 on: March 27, 2014, 04:35:56 PM »

Try this formatting for option3
I haven't looked at the logic

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun _option3 ()
  3.      (cond ((and (>= area 750) (<= area 1200))
  4.             (setq sdarea (+ 100 (/ (- area 750) 9))
  5.                   skarea (* area 0.1)
  6.             )
  7.            )
  8.            ((and (> area 1200) (<= area 2000))
  9.             (setq sdarea (+ 150 (/ (- area 1200) 16))
  10.                   skarea (* area 0.1)
  11.             )
  12.            )
  13.            ((and (> area 2000) (< area 4000))
  14.             (setq sdarea 0
  15.                   skarea 0
  16.             )
  17.            )
  18.            ((= area 4000)
  19.             (setq sdarea 200
  20.                   skarea (* area 0.1)
  21.             )
  22.            )
  23.            ((and (> area 4000) (<= area 8000))
  24.             (setq sdarea (+ 200 (* 0.02 (- area 4000)))
  25.                   skarea (* area 0.1)
  26.             )
  27.            )
  28.            ((> area 8000)
  29.             (setq sdarea (+ 280 (* 0.01 (- area 8000)))
  30.                   skarea (* area 0.1)
  31.             )
  32.            )
  33.      )
  34.    )
  35.  
  36.  
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.

pedroantonio

  • Guest
Re: help with a lisp
« Reply #22 on: March 27, 2014, 04:44:59 PM »
Thank you Kerry .Now the lisp works fine. Can you explain me were was the mistake. I had exactly the same function before and not working properly !!!


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #23 on: March 27, 2014, 04:50:30 PM »
Thank you Kerry .Now the lisp works fine. Can you explain me were was the mistake. I had exactly the same function before and not working properly !!!
The error was in the parenthesis.
Have a look at this Post reply
It looks like you are missing some parenthesis in your conditional statement

(cond
    <HERE> (and (>= area 750) (<= area 1200) <and HERE>

        .. do stuff
     )
    .. more conditionals
)


Did you build tests for each conditional ?
If you fix the edge cases and the missing ranges you may not need to look at the code again after testing.

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.

pedroantonio

  • Guest
Re: help with a lisp
« Reply #24 on: March 27, 2014, 05:16:08 PM »
Kerry , The only problem i have is with this

Code - Auto/Visual Lisp: [Select]
  1. ((and (>= area 750) (<= area 1200))
  2.             (setq sdarea (+ 100 (/ (- area 750) 9))
  3.                   skarea (* area 0.1)
  4.             )
  5.            )
  6.  

For area = 750 and i change the code to this

Code - Auto/Visual Lisp: [Select]
  1. ((and (<= 750 area) (<= area 1200))
  2.             (setq sdarea (+ 100 (/ (- area 750) 9))
  3.                   skarea (* area 0.1)
  4.             )
  5.            )
  6.  

I test it and work. What is your opinion ?

Thanks

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #25 on: March 27, 2014, 05:19:47 PM »
< .. >
I test it and work. What is your opinion ?

Thanks

The logic is similar I believe ... just different test values.
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.

pedroantonio

  • Guest
Re: help with a lisp
« Reply #26 on: March 27, 2014, 05:31:58 PM »
Kerry, Option 3 crass again for area = 750  i dont know why

; error: bad argument type: numberp: nil


Code - Auto/Visual Lisp: [Select]
  1.  (defun c:test4
  2.        (/ _option1 _option2 _option3 _option4 area dxf ent sdarea skarea)
  3.   (defun _option1 ()
  4.     (cond ((<= area 200)
  5.            (setq sdarea area
  6.                  skarea (* area 0.7)
  7.            )
  8.           )
  9.           ((< area 700)
  10.            (setq sdarea 240
  11.                  skarea (* area 0.6)
  12.            )
  13.           )
  14.           ((>= area 700)
  15.            (setq sdarea 400
  16.                  skarea (* area 0.6)
  17.            )
  18.           )
  19.     )
  20.   )
  21.   (defun _option2 ()
  22.     (cond ((<= area 2000)
  23.             (setq sdarea  (* area 0.8)
  24.              skarea (* area 0.6)
  25.            )
  26.           )    
  27.          ((> area 2000)
  28.            (setq sdarea (+ 400 ( * 0.05 (- area 2000)))
  29.                  skarea (* area 0.1)
  30.            )
  31.           )    
  32.     )
  33.   )
  34.  
  35. (defun _option3 ()
  36.      (cond ((and (>= area 750) (<= area 1200))
  37.             (setq sdarea (+ 100 (/ (- area 750) 9))
  38.                   skarea (* area 0.1)
  39.             )
  40.            )
  41.            ((and (> area 1200) (<= area 2000))
  42.             (setq sdarea (+ 150 (/ (- area 1200) 16))
  43.                   skarea (* area 0.1)
  44.             )
  45.            )
  46.            ((and (> area 2000) (< area 4000))
  47.             (setq sdarea 0
  48.                   skarea 0
  49.             )
  50.            )
  51.            ((= area 4000)
  52.             (setq sdarea 200
  53.                   skarea (* area 0.1)
  54.             )
  55.            )
  56.            ((and (> area 4000) (<= area 8000))
  57.             (setq sdarea (+ 200 (* 0.02 (- area 4000)))
  58.                   skarea (* area 0.1)
  59.             )
  60.            )
  61.            ((> area 8000)
  62.             (setq sdarea (+ 280 (* 0.01 (- area 8000)))
  63.                   skarea (* area 0.1)
  64.             )
  65.            )
  66.      )
  67.    )
  68.  
  69.   (defun _option4 ()
  70.     (initget 7)
  71.     (setq sdarea (* area (getreal "\n Give sd (example 1.2) : ")))
  72.     (initget 7)
  73.     (setq skarea (* area (getreal "\n Give sk in % (example 0.70) : ")))
  74.   )
  75.   ;; Main -----
  76.   (if (and (setq ent (car (entsel "\n-> Select a closed polyline : ")))
  77.            (wcmatch (cdr (assoc 0 (setq dxf (entget ent)))) "*POLYLINE")
  78.            (member (cdr (assoc 70 dxf)) '(1 129))
  79.       )
  80.     (progn
  81.       (initget 7)
  82.   (PROMPT
  83.     "\n choose option :
  84.  
  85.   1. Choose 1 for this ...
  86.   2. Choose 2 for this ...
  87.   3. Choose 3 for this ...
  88.   4. Choose 4 for this ...
  89.  
  90. " )
  91.       (setq area   (vlax-curve-getarea ent)
  92.             option (getint "Select Option 1 to 4 : ")
  93.       )
  94.       (cond ((= option 1) (_Option1))
  95.             ((= option 2) (_Option2))
  96.             ((= option 3) (_Option3))
  97.             ((= option 4) (_Option4))
  98.             (t (alert "Read the prompt, Idiot\nUsing Option 4. ") (_Option4))
  99.       )
  100.       (princ (strcat "\n Selected Area = "
  101.                      (rtos area 2 2)
  102.                      "\n D = "
  103.                      (rtos sdarea 2 2)
  104.                      " sq.m"
  105.                      "\n K = "
  106.                      (rtos skarea 2 2)
  107.                      " sq.m"
  108.              )
  109.       )
  110.     )
  111.     (princ "\n-> No valid polyline selected...")
  112.   )
  113.   (princ)
  114. )
  115.  
  116.  

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #27 on: March 27, 2014, 05:42:41 PM »

is area an integer
area = 750

or a real at 749.9999999999998

I'd close AutoCAD and reload the code.

Have you written tests yet ?

Where does the code crash .. on which line
I assume you're testing in VLIDE with Break on Error toggled ON and using Last Break Source Ctrl-F9 after the crash.
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.

pedroantonio

  • Guest
Re: help with a lisp
« Reply #28 on: March 27, 2014, 06:03:59 PM »
I change the code with this and work fine

Code - Auto/Visual Lisp: [Select]
  1.  (defun _option3 ()
  2.            (cond ((= area 750)
  3.             (setq sdarea 100
  4.                   skarea (* area 0.1)
  5.             )
  6.            )
  7.             ((and (> area 750) (<= area 1200))
  8.             (setq sdarea (+ 100 (/ (- area 750) 9))
  9.                   skarea (* area 0.1)
  10.             )
  11.            )
  12.                       ((and (> area 1200) (<= area 2000))
  13.             (setq sdarea (+ 150 (/ (- area 1200) 16))
  14.                   skarea (* area 0.1)
  15.             )
  16.            )
  17.            ((and (> area 2000) (< area 4000))
  18.             (setq sdarea 0
  19.                   skarea 0
  20.             )
  21.            )
  22.            ((= area 4000)
  23.             (setq sdarea 200
  24.                   skarea (* area 0.1)
  25.             )
  26.            )
  27.            ((and (> area 4000) (<= area 8000))
  28.             (setq sdarea (+ 200 (* 0.02 (- area 4000)))
  29.                   skarea (* area 0.1)
  30.             )
  31.            )
  32.            ((> area 8000)
  33.             (setq sdarea (+ 280 (* 0.01 (- area 8000)))
  34.                   skarea (* area 0.1)
  35.             )
  36.            )
  37.      )
  38.    )
  39.  
  40.  

Thank you Kerry

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #29 on: March 28, 2014, 04:39:20 AM »
I change the code with this and work fine

< .. >

Thank you Kerry


Great. Glad I could help.

Just an observation :
I notice that the code you posted is not well formatted.
I'd suggest that use the VLIDE to format your code. A lot of the problems you have will be resolved by just accepting the formatting provided. Sometimes unbalanced statements are a little more obvious.

This is your code back ... just reformatted. You can see the flow a little easier because the 'paragraphs' line up a little better. This makes it easier to find faults in the logic.

In some regards VLIDE is a crap editor and it's disappointing that AutoDesk have not improved it in the last 15 years but it is a resource we should make the most of.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun _option3 ()
  3.   (cond ((= area 750)
  4.          (setq sdarea 100
  5.                skarea (* area 0.1)
  6.          )
  7.         )
  8.         ((and (> area 750) (<= area 1200))
  9.          (setq sdarea (+ 100 (/ (- area 750) 9))
  10.                skarea (* area 0.1)
  11.          )
  12.         )
  13.         ((and (> area 1200) (<= area 2000))
  14.          (setq sdarea (+ 150 (/ (- area 1200) 16))
  15.                skarea (* area 0.1)
  16.          )
  17.         )
  18.         ((and (> area 2000) (< area 4000))
  19.          (setq sdarea 0
  20.                skarea 0
  21.          )
  22.         )
  23.         ((= area 4000)
  24.          (setq sdarea 200
  25.                skarea (* area 0.1)
  26.          )
  27.         )
  28.         ((and (> area 4000) (<= area 8000))
  29.          (setq sdarea (+ 200 (* 0.02 (- area 4000)))
  30.                skarea (* area 0.1)
  31.          )
  32.         )
  33.         ((> area 8000)
  34.          (setq sdarea (+ 280 (* 0.01 (- area 8000)))
  35.                skarea (* area 0.1)
  36.          )
  37.         )
  38.   )
  39. )
  40.  
  41.  ;|«Visual LISP© Format Options»
  42. (80 2 45 2 nil "end of " 80 45 1 0 nil nil nil nil T)
  43. ;*** DO NOT add text below the comment! ***|;
  44.  

Regards,
« Last Edit: March 28, 2014, 04:47:04 AM by Kerry »
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.