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

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
help with a lisp
« on: March 24, 2014, 09:36:50 AM »
I am trying to learn some things in lisp so i need help.

I write this code but a lot of things is missing .I want this lisp to do

1) Select a close polyline
2)give sd and sk
3)use the area of the close polyline to calculate D and K


Code - Auto/Visual Lisp: [Select]
  1. (DEFUN C:TEST (/ area sd sk)
  2.  
  3. ;Select a close polyline and use the area to calculate
  4. ;d and k
  5.  
  6.   (SETQ SD ("\ngive sd (example 1.2): ")
  7.   (SETQ SK ("\ngive sk (exmple 0.70 or 70%): ")
  8.   (PRINC (STRCAT "\n D = " ((RTOS (* area sd) 2 2) " sq.m")
  9.                  "\n K = " ((RTOS (* area sk) 2 2) " sq.m")
  10.           )
  11.           (PRINC)
  12.  

Thanks

efernal

  • Bull Frog
  • Posts: 206
Re: help with a lisp
« Reply #1 on: March 24, 2014, 03:29:01 PM »
Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ area dxf ent sd sk)
  2.   (IF (AND (SETQ ent (CAR (ENTSEL "\n-> Select a closed polyline : ")))
  3.            (WCMATCH (CDR (ASSOC 0 (SETQ dxf (ENTGET ent)))) "*POLYLINE")
  4.            (MEMBER (CDR (ASSOC 70 dxf)) '(1 129))
  5.       )
  6.     (PROGN
  7.       (INITGET 7)
  8.       (SETQ sd (GETREAL "\n Give sd (example 1.2) : "))
  9.       (INITGET 7)
  10.       (SETQ sk (GETREAL "\n Give sk in % (example 0.70) : "))
  11.       (SETQ area (VLAX-GET-PROPERTY (VLAX-ENAME->VLA-OBJECT ent) 'area))
  12.       (PRINC
  13.         (STRCAT "\n D = " (RTOS (* area sd) 2 2) " sq.m" "\n K = " (RTOS (* area sk) 2 2) " sq.m")
  14.       )
  15.     )
  16.     (PRINC "\n-> No valid polyline selected...")
  17.   )
  18.   (PRINC)
  19. )
  20.  
e.fernal

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: help with a lisp
« Reply #2 on: March 24, 2014, 03:34:55 PM »
You could also use vlax-curve-getArea. Then you wouldn't have to convert it to a vla-object.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

efernal

  • Bull Frog
  • Posts: 206
Re: help with a lisp
« Reply #3 on: March 24, 2014, 04:53:20 PM »
As suggested by Alanjt

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ area dxf ent sd sk)
  2.   (IF (AND (SETQ ent (CAR (ENTSEL "\n-> Select a closed polyline : ")))
  3.            (WCMATCH (CDR (ASSOC 0 (SETQ dxf (ENTGET ent)))) "*POLYLINE")
  4.            (MEMBER (CDR (ASSOC 70 dxf)) '(1 129))
  5.       )
  6.     (PROGN
  7.       (INITGET 7)
  8.       (SETQ sd (GETREAL "\n Give sd (example 1.2) : "))
  9.       (INITGET 7)
  10.       (SETQ sk (GETREAL "\n Give sk in % (example 0.70) : "))
  11.       (SETQ area (VLAX-CURVE-GETAREA ent))
  12.       (PRINC
  13.         (STRCAT "\n D = " (RTOS (* area sd) 2 2) " sq.m" "\n K = " (RTOS (* area sk) 2 2) " sq.m")
  14.       )
  15.     )
  16.     (PRINC "\n-> No valid polyline selected...")
  17.   )
  18.   (PRINC)
  19. )
e.fernal

pedroantonio

  • Guest
Re: help with a lisp
« Reply #4 on: March 24, 2014, 06:00:33 PM »
Thank you for the help

pedroantonio

  • Guest
Re: help with a lisp
« Reply #5 on: March 26, 2014, 12:32:05 PM »
i want to update this lisp with few thing and i need help

first i want to add an oprtion menu

1)Choose 1 for this ...
2)Choose 2 for this ...
3)Choose 3 for this ...
4)Choose 4 for this ...

for 1

if Area < 700 sqm then D = 240 sqm    and K = Area * 0.60
if Area >=700 sqm then D = 400 sqm    and K = Area * 0.60
if Area =< 200 sqm then D = Area  sqm    and K = Area * 0.70

for 2

D = Area * 0.80   but max D= 400sqm
K= Area * 0.60

for 3

if Area = 4000 sqm then D = 200 sqm    and K = Area * 0.10
if 4000 sqm < Area =<8000 sqm then D = 200 + (Area - 4000)*0.02    and K = Area * 0.10
if Area > 8000 sqm then D = 280 + (Area - 8000)*0.01    and K = Area * 0.10
if 750 sqm =< Area <1200 sqm then D = 100 + ((Area - 750) / 9)    and K = Area * 0.10
if 1200 sqm =< Area =<2000 sqm then D = 150 + ((Area - 1200) / 16)    and K = Area * 0.10

for 4

Is the code we have already

Thanks

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #6 on: March 26, 2014, 07:03:04 PM »
Untested basic shell.

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. (defun c:test4
  4.        (/ _option1 _option2 _option3 _option4 area dxf ent sdarea skarea)
  5.   (defun _option1 ()
  6.     (cond ((<= area 200)
  7.            (setq sdarea area
  8.                  skarea (* area 0.7)
  9.            )
  10.           )
  11.           ((< area 700)
  12.            (setq sdarea 240
  13.                  skarea (* area 0.6)
  14.            )
  15.           )
  16.           ((>= area 700)
  17.            (setq sdarea 400
  18.                  skarea (* area 0.6)
  19.            )
  20.           )
  21.     )
  22.   )
  23.   (defun _option2 () (alert "Option 2\nTo be Completed by Client ") (_Option4))
  24.   (defun _option3 () (alert "Option 3\nTo be Completed by Client") (_Option4))
  25.   (defun _option4 ()
  26.     (initget 7)
  27.     (setq sdarea (* area (getreal "\n Give sd (example 1.2) : ")))
  28.     (initget 7)
  29.     (setq skarea (* area (getreal "\n Give sk in % (example 0.70) : ")))
  30.   )
  31.   ;; Main -----
  32.   (if (and (setq ent (car (entsel "\n-> Select a closed polyline : ")))
  33.            (wcmatch (cdr (assoc 0 (setq dxf (entget ent)))) "*POLYLINE")
  34.            (member (cdr (assoc 70 dxf)) '(1 129))
  35.       )
  36.     (progn
  37.       (initget 7)
  38.       (setq area   (vlax-curve-getarea ent)
  39.             option (getint "Select Option 1 to 4 : ")
  40.       )
  41.       (cond ((= option 1) (_Option1))
  42.             ((= option 2) (_Option2))
  43.             ((= option 3) (_Option3))
  44.             ((= option 4) (_Option4))
  45.             (t (alert "Read the prompt, Idiot\nUsing Option 4. ") (_Option4))
  46.       )
  47.       (princ (strcat "\n Selected Area = "
  48.                      (rtos area 2 2)
  49.                      "\n D = "
  50.                      (rtos sdarea 2 2)
  51.                      " sq.m"
  52.                      "\n K = "
  53.                      (rtos skarea 2 2)
  54.                      " sq.m"
  55.              )
  56.       )
  57.     )
  58.     (princ "\n-> No valid polyline selected...")
  59.   )
  60.   (princ)
  61. )
  62.  
« Last Edit: March 26, 2014, 07:18:22 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 #7 on: March 27, 2014, 02:44:23 AM »
Thank you Kerry for the help . I add a start up menu but samething is wrong. I coplete options 2 and option 3 but i need to check the code

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 ((<= area 4000)
  32.            (setq sdarea (= 200)
  33.                  skarea (* area 0.10)
  34.            )
  35.           )
  36.       (( 4000 < area =<8000) ; I don't know how to write it
  37.            (setq sdarea (+ 200 ( * 0.02 (- area 4000))) ; please check it  200 + (Area - 4000)*0.02
  38.                  skarea (* area 0.1)
  39.            )
  40.           )
  41.           ((> area 8000)
  42.            (setq sdarea (+ 280 ( * 0.01 (- area 8000))) ; please check it  280 + (Area - 8000)*0.01
  43.                  skarea (* area 0.1)
  44.            )
  45.           )
  46.      (( 750 =< Area < 1200); I don't know how to write it
  47.            (setq sdarea (+ 280 ( / 9 (- area 750))) ; please check it   D = 100 + ((Area - 750) / 9)
  48.                  skarea (* area 0.1)
  49.            )
  50.           )
  51.         (( 1200 =< Area =< 2000); I don't know how to write it
  52.            (setq sdarea (+ 150 ( / 16 (- area 1200))) ; please check it    D = 150 + ((Area - 1200) / 16)
  53.                  skarea (* area 0.1)
  54.            )
  55.           )
  56.     )
  57.   )
  58.  
  59.   (defun _option4 ()
  60.     (initget 7)
  61.     (setq sdarea (* area (getreal "\n Give sd (example 1.2) : ")))
  62.     (initget 7)
  63.     (setq skarea (* area (getreal "\n Give sk in % (example 0.70) : ")))
  64.   )
  65.   ;; Main -----
  66.   (if (and (setq ent (car (entsel "\n-> Select a closed polyline : ")))
  67.            (wcmatch (cdr (assoc 0 (setq dxf (entget ent)))) "*POLYLINE")
  68.            (member (cdr (assoc 70 dxf)) '(1 129))
  69.       )
  70.     (progn
  71.       (initget 7)
  72.   (PROMPT
  73.     "\n choose option :
  74.  
  75.   1. Choose 1 for this ...
  76.   2. Choose 2 for this ...
  77.   3. Choose 3 for this ...
  78.   4. Choose 4 for this ...
  79.  
  80. " )
  81.       (setq area   (vlax-curve-getarea ent)
  82.             option (getint "Select Option 1 to 4 : ")
  83.       )
  84.       (cond ((= option 1) (_Option1))
  85.             ((= option 2) (_Option2))
  86.             ((= option 3) (_Option3))
  87.             ((= option 4) (_Option4))
  88.             (t (alert "Read the prompt, Idiot\nUsing Option 4. ") (_Option4))
  89.       )
  90.       (princ (strcat "\n Selected Area = "
  91.                      (rtos area 2 2)
  92.                      "\n D = "
  93.                      (rtos sdarea 2 2)
  94.                      " sq.m"
  95.                      "\n K = "
  96.                      (rtos skarea 2 2)
  97.                      " sq.m"
  98.              )
  99.       )
  100.     )
  101.     (princ "\n-> No valid polyline selected...")
  102.   )
  103.   (princ)
  104. )
  105.  
  106.  


Thanks
« Last Edit: March 27, 2014, 06:36:22 AM by Topographer »

pedroantonio

  • Guest
Re: help with a lisp
« Reply #8 on: March 27, 2014, 12:07:27 PM »
i think i fix it
« Last Edit: March 27, 2014, 12:23:31 PM by Topographer »

Kerry

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

I can't make time to look closely at the moment but option 2 seems incorrect.

I'll have a look when I finish up what I'm doing.
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 #10 on: March 27, 2014, 12:42:22 PM »
Kerry if you can fix option 3. I fix option 2 but in option 3 i have calculation problems

Code - Auto/Visual Lisp: [Select]
  1.   (defun _option3 ()
  2.  
  3.       (cond ((= area 4000)
  4.            (setq sdarea (= 200)
  5.                  skarea (* area 0.10)
  6.            )
  7.           )
  8.       (( 4000 < area =<8000) ; I don't know how to write it
  9.            (setq sdarea (+ 200 ( * 0.02 (- area 4000))) ; please check it  200 + (Area - 4000)*0.02
  10.                  skarea (* area 0.1)
  11.            )
  12.           )
  13.           ((area > 8000)
  14.            (setq sdarea (+ 280 ( * 0.01 (- area 8000))) ; please check it  280 + (Area - 8000)*0.01
  15.                  skarea (* area 0.1)
  16.            )
  17.           )
  18.      (( 750 =< Area < 1200); I don't know how to write it
  19.            (setq sdarea (+ 280 ( /  (- area 750) 9)) ; please check it   D = 100 + ((Area - 750) / 9)
  20.                  skarea (* area 0.1)
  21.            )
  22.           )
  23.         (( 1200 =< Area =< 2000); I don't know how to write it
  24.            (setq sdarea (+ 150 ( / (- area 1200) 16)) ; please check it    D = 150 + ((Area - 1200) / 16)
  25.                  skarea (* area 0.1)
  26.            )
  27.           )
  28.     )
  29.   )
  30.  

thanks
« Last Edit: March 27, 2014, 01:11:32 PM by Topographer »

pedroantonio

  • Guest
Re: help with a lisp
« Reply #11 on: March 27, 2014, 01:46:15 PM »
Kerry , I change the option 3 code but i have this problem

for    1200 < Area < = 2000      D = 150 + ((Area - 1200) / 16)    and K = Area * 0.10

but

for    2000 < Area < 40000      D = 0   and  K =0


I dont know how to write it

Please check all the functions

if Area = 4000 sqm then D = 200 sqm    and K = Area * 0.10
if 4000 sqm < Area =<8000 sqm then D = 200 + (Area - 4000)*0.02    and K = Area * 0.10
if Area > 8000 sqm then D = 280 + (Area - 8000)*0.01    and K = Area * 0.10
if 750 sqm =< Area <1200 sqm then D = 100 + ((Area - 750) / 9)    and K = Area * 0.10
if 1200 sqm =< Area =<2000 sqm then D = 150 + ((Area - 1200) / 16)    and K = Area * 0.10



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

thanks
« Last Edit: March 27, 2014, 02:11:53 PM by Topographer »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: help with a lisp
« Reply #12 on: March 27, 2014, 03:09:57 PM »
Have a play with this for testing ... best not to change ( kdub:assert-expect <.> <.> )
Build your own tests.

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. ;;;------------------------------------------------------------------
  4. ;;;------------------------------------------------------------------
  5. ;;;
  6. ;;; (kdub:assert-expect <teststatement> <expected>)
  7. ;;; kwb 20020715
  8. ;;; <teststatement> argument must be quoted
  9. ;|
  10. ( kdub:assert-expect '(+ 2 2) 4)
  11. ( kdub:assert-expect '(+ 2 2) (+ 1 3))
  12. |;
  13. (defun kdub:assert-expect (teststatement expected / catchit tmp)
  14.         (setq catchit (vl-catch-all-apply
  15.                         '=
  16.                         (list expected (setq tmp (eval teststatement)))
  17.                       )
  18.         )
  19.       )
  20.     ;; else
  21.     (if (not catchit)
  22.       (progn (mapcar 'princ
  23.                      (list "\n **** Assertion Failure : "
  24.                            (vl-prin1-to-string teststatement)
  25.                            "\n expected : "
  26.                            (vl-prin1-to-string expected)
  27.                            " returned : "
  28.                            (vl-prin1-to-string tmp)
  29.                      )
  30.              )
  31.              ;; decide what to do here later ?
  32.              (if *assert-debug_on*
  33.                (princ "  ")
  34.                (vl-exit-with-error (strcat "We bombed! "))
  35.              )
  36.       )
  37.       (mapcar 'princ
  38.               (list "\nAssertion-expect : "
  39.                     (vl-prin1-to-string teststatement)
  40.                     " returned : "
  41.                     (vl-prin1-to-string tmp)
  42.               )
  43.       )
  44.     )
  45.   )
  46.   (princ)
  47. )
  48. ;;;------------------------------------------------------------------
  49. ;;;------------------------------------------------------------------
  50. ;;;
  51.  


These are the tests I used for _option1()
You will need to load _option1() into memory ...
Code - Auto/Visual Lisp: [Select]
  1. (setq area 1)
  2. (_option1)
  3. ( kdub:assert-expect 'sdarea 1)
  4. ( kdub:assert-expect 'skarea 1)  ; a failing test
  5.  
  6. (setq area 199)
  7. (_option1)
  8. ( kdub:assert-expect 'sdarea area)
  9. ( kdub:assert-expect 'skarea (* area 0.7))
  10.  
  11. (setq area 600)
  12. (_option1)
  13. ( kdub:assert-expect 'sdarea 240)
  14. ( kdub:assert-expect 'skarea (* area 0.6))
  15.  
  16. (setq area 701)
  17. (_option1)
  18. ( kdub:assert-expect 'sdarea 400)
  19. ( kdub:assert-expect 'skarea (* area 0.6))
  20.  
  21.  

Just comment out the tests when you're happy.
You can uncomment them when you make changes to your code under test in the future ....

Added:
Testing like this is one reason to keep your functions compact.
« Last Edit: March 27, 2014, 03:14:17 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.

Kerry

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

Lets try a little self help.

Can you write tests for _option3()
Then we can look at the ones that fail.

Regards,


added:

See if something like these conditionals brings you joy
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (= area 4000)
  3. (and (> area 4000) (<= area 8000))
  4. (> area 8000)
  5. (and (>= area 750) (< area 1200))
  6. (and (>= area 1200) (<= area 2000))
  7.  
  8.  

These are the conditionals you specified.
There seems to be some gaps in the range that will not be evaluated.
« Last Edit: March 27, 2014, 03:48:43 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 #14 on: March 27, 2014, 03:50:03 PM »
I write this

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

but i have problem  with this

for    2000 < Area < 40000    --->  D = 0   and  K =0

example

for area 3950 sqm  --- >  D= 455.56 and K= 395 , understand the fuction as   Area >=750 .

I don't know what to do