Author Topic: Help: calculations in DCL file?  (Read 830 times)

0 Members and 1 Guest are viewing this topic.

mhy3sx

  • Newt
  • Posts: 120
Help: calculations in DCL file?
« on: March 11, 2024, 05:29:03 AM »
Hi. I am trying to learn how DCL dialogs work. I want to create a dcl dialog with  some cells and do some calculations with them and  insert the calculation to a cell in the same dcl file. Like the image. Is only for educational purposes , but i need to understand how it works.I search in google but i can't find something similar.


The x1 ,y1 ...etc is  width and height for multiple objects

Thanks

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Help: calculations in DCL file?
« Reply #1 on: March 11, 2024, 09:12:05 AM »
Here is a relatively simple example to demonstrate the general technique -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / *error* dch dcl des len wid )
  2.  
  3.     (defun *error* ( msg )
  4.         (if (and (= 'int (type dch)) (< 0 dch))
  5.             (unload_dialog dch)
  6.         )
  7.         (if (= 'file (type des))
  8.             (close des)
  9.         )
  10.         (if (and (= 'str (type dcl)) (findfile dcl))
  11.             (vl-file-delete dcl)
  12.         )
  13.         (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  14.             (princ (strcat "\nError: " msg))
  15.         )
  16.         (princ)
  17.     )
  18.  
  19.     (cond
  20.         (   (not
  21.                 (setq dcl (vl-filename-mktemp nil nil ".dcl")
  22.                       des (open dcl "w")
  23.                 )
  24.             )
  25.             (princ "\nUnable to open DCL for writing.")
  26.         )
  27.         (   (progn
  28.                 (foreach str
  29.                    '(
  30.                         "ed : edit_box"
  31.                         "{"
  32.                         "    alignment = left;"
  33.                         "    width = 20;"
  34.                         "    edit_width = 10;"
  35.                         "    fixed_width = true;"
  36.                         "}"
  37.                         ""
  38.                         "test : dialog"
  39.                         "{"
  40.                         "    spacer;"
  41.                         "    key = \"dcl\";"
  42.                         "    : ed"
  43.                         "    {"
  44.                         "        key = \"len\";"
  45.                         "        label = \"Length:\";"
  46.                         "    }"
  47.                         "    : ed"
  48.                         "    {"
  49.                         "        key = \"wid\";"
  50.                         "        label = \"Width:\";"
  51.                         "    }"
  52.                         "    : row"
  53.                         "    {"
  54.                         "        : ed { key = \"res\"; label = \"Area:\"; is_enabled = false; }"
  55.                         "        : button"
  56.                         "        {"
  57.                         "            key = \"cal\";"
  58.                         "            label = \"Calculate\";"
  59.                         "        }"
  60.                         "    }"
  61.                         "    spacer;"
  62.                         "    ok_only;"
  63.                         "}"
  64.                     )
  65.                     (write-line str des)
  66.                 )
  67.                 (setq des (close des)
  68.                       dch (load_dialog dcl)
  69.                 )
  70.                 (<= dch 0)
  71.             )
  72.             (princ "\nUnable to load DCL file.")
  73.         )
  74.         (   (not (new_dialog "test" dch))
  75.             (princ "\nUnable to display 'test' dialog.")
  76.         )
  77.         (   t
  78.             (set_tile "dcl" "Calculate Area")
  79.             (action_tile "len" "(setq len $value)")
  80.             (action_tile "wid" "(setq wid $value)")
  81.             (action_tile "cal"
  82.                 (vl-prin1-to-string
  83.                    '(
  84.                         (lambda ( / x y )
  85.                             (set_tile "res" "")
  86.                             (cond
  87.                                 (   (or (not len) (= "" len))
  88.                                     (alert "Please enter a length value.")
  89.                                     (mode_tile "len" 2)
  90.                                 )
  91.                                 (   (or (not wid) (= "" wid))
  92.                                     (alert "Please enter a width value.")
  93.                                     (mode_tile "wid" 2)
  94.                                 )
  95.                                 (   (not (setq x (distof len)))
  96.                                     (alert "The length must be numerical.")
  97.                                     (mode_tile "len" 2)
  98.                                 )
  99.                                 (   (not (setq y (distof wid)))
  100.                                     (alert "The width must be numerical.")
  101.                                     (mode_tile "wid" 2)
  102.                                 )
  103.                                 (   (<= x 0.0)
  104.                                     (alert "The length must be greater than zero.")
  105.                                     (mode_tile "len" 2)
  106.                                 )
  107.                                 (   (<= y 0.0)
  108.                                     (alert "The width must be greater than zero.")
  109.                                     (mode_tile "wid" 2)
  110.                                 )
  111.                                 (   (set_tile "res" (rtos (* x y) 2)))
  112.                             )
  113.                         )
  114.                     )
  115.                 )
  116.             )
  117.             (start_dialog)
  118.         )
  119.     )
  120.     (*error* nil)
  121.     (princ)
  122. )

mhy3sx

  • Newt
  • Posts: 120
Re: Help: calculations in DCL file?
« Reply #2 on: March 11, 2024, 10:02:06 AM »
Thanks Lee Mac !!

mhy3sx

  • Newt
  • Posts: 120
Re: Help: calculations in DCL file?
« Reply #3 on: March 11, 2024, 12:14:13 PM »
hI Lee Mac . Is any program except OpenDCL helping me to write DCL code. I want to see what I am doing !!!

Thanks

mhy3sx

  • Newt
  • Posts: 120
Re: Help: calculations in DCL file?
« Reply #4 on: March 11, 2024, 03:07:24 PM »
I try to make a simle example. I can not print rhe results !!!

I wan't to update the code . The calculation work for 3 different situations and after the result  print a text for explanation for eatch situation.I want this text to print in dcl after the calculation. How to do this?

Code: [Select]
  ((<= a 250)
      (setq k (/ (+ (* a 2.5) 75) 1.24))
  ((set_tile "res" (rtos k 2 2)))
   ; PRINT TEXT 1 IN THE DCL DIALOG   
    )
    ((and (> a 250) (<= a 1000))
      (setq k (/ (+ (* a 2.5) 75) 1.24))
  ((set_tile "res" (rtos k 2 2)))
         ; PRINT TEXT 2 IN THE DCL DIALOG   
    )
    ((> a 1000)
       (setq k (/ (+ (* a 2.5) 75) 1.24))
  ((set_tile "res" (rtos k 2 2)))
         ; PRINT TEXT 3 IN THE DCL DIALOG   
    )

Code - Auto/Visual Lisp: [Select]
  1. (defun c:TEST2 ( / a k)
  2.  
  3.      
  4.         (defun *error* ( msg )
  5.             (if (and (= 'int (type dch)) (< 0 dch))
  6.                 (unload_dialog dch)
  7.             )
  8.             (if (= 'file (type des))
  9.                 (close des)
  10.             )
  11.             (if (and (= 'str (type dcl)) (findfile dcl))
  12.                 (vl-file-delete dcl)
  13.             )
  14.             (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  15.                 (princ (strcat "\nError: " msg))
  16.             )
  17.             (princ)
  18.         )
  19.      
  20.         (cond
  21.             (   (not
  22.                     (setq dcl (vl-filename-mktemp nil nil ".dcl")
  23.                           des (open dcl "w")
  24.                     )
  25.                 )
  26.                 (princ "\nUnable to open DCL for writing.")
  27.             )
  28.             (   (progn
  29.                     (foreach str
  30.                        '(
  31.                             "ed : edit_box"
  32.                             "{"
  33.                             "    alignment = left;"
  34.                             "    width = 20;"
  35.                             "    edit_width = 10;"
  36.                             "    fixed_width = true;"
  37.                             "}"
  38.                             ""
  39.                             "test : dialog"
  40.                             "{"
  41.                             "    spacer;"
  42.                             "    key = \"dcl\";"
  43.                             "    : ed"
  44.                             "    {"
  45.                             "        key = \"a\";"
  46.                             "        label = \"Give area in sqm:\";"
  47.                             "    }"
  48.  
  49.                             "    : row"
  50.                             "    {"
  51.                             "        : ed { key = \"res\"; label = \"Calculate:\"; is_enabled = false; }"
  52.                                                        
  53.                                                         ; I WANT TO PRINT TEXT1 OR TEXT2 OR TEXT3 IN THE DCL  UNDER THE CALCULATION !!!
  54.                             "        : button"
  55.                             "        {"
  56.                             "            key = \"cal\";"
  57.                             "            label = \"Calc\";"
  58.                             "        }"
  59.                             "    }"
  60.                             "    spacer;"
  61.                             "    ok_only;"
  62.                             "}"
  63.                         )
  64.  
  65.  
  66.  
  67.                 (write-line str des)
  68.                     )
  69.                     (setq des (close des)
  70.                           dch (load_dialog dcl)
  71.                     )
  72.                     (<= dch 0)
  73.                 )
  74.                 (princ "\nUnable to load DCL file.")
  75.             )
  76.             (   (not (new_dialog "test" dch))
  77.                 (princ "\nUnable to display 'test' dialog.")
  78.             )
  79.             (   t
  80.                 (set_tile "dcl" "MY TITLE TEXT")
  81.                 (action_tile "a" "(setq a $value)")
  82.                 (action_tile "cal"
  83.                     (vl-prin1-to-string
  84.                        '(
  85.                             (lambda ( / a )
  86.                                 (set_tile "res" "")
  87.                                 (cond
  88.  
  89.  
  90.   ((<= a 250)
  91.       (setq k (/ (+ (* a 2.5) 75) 1.24))
  92.           ((set_tile "res" (rtos k 2 2)))
  93.    ; PRINT TEXT 1 IN THE DCL DIALOG  
  94.     )
  95.     ((and (> a 250) (<= a 1000))
  96.       (setq k (/ (+ (* a 2.5) 75) 1.24))
  97.           ((set_tile "res" (rtos k 2 2)))
  98.          ; PRINT TEXT 2 IN THE DCL DIALOG  
  99.     )
  100.     ((> a 1000)
  101.        (setq k (/ (+ (* a 2.5) 75) 1.24))
  102.           ((set_tile "res" (rtos k 2 2)))
  103.          ; PRINT TEXT 3 IN THE DCL DIALOG  
  104.     )
  105.   )
  106.                             )
  107.                         )
  108.                     )
  109.                 )
  110.                 (start_dialog)
  111.             )
  112.         )
  113.         (*error* nil)
  114.         (princ)
  115. )
  116.  

Thanks

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Help: calculations in DCL file?
« Reply #5 on: March 11, 2024, 07:10:10 PM »
A few points:

1. Your second condition can be reduced to (<= a 1000) without the (> a 250)

2. You have extra parentheses surrounding these expressions: ((set_tile "res" (rtos k 2 2)))

3. Why remove the localised variables (des/dch/dcl)? Those are important as the localisation ensures that the error handler operates correctly.

BIGAL

  • Swamp Rat
  • Posts: 1419
  • 40 + years of using Autocad
Re: Help: calculations in DCL file?
« Reply #6 on: March 12, 2024, 05:37:40 AM »
This is just an example of doing calcs this image uses either sliders or type in a value and the result is shown as a total instantly updating. Just uses a set_tile. So ask for help if you cant get it to work.



A man who never made a mistake never made anything

mhy3sx

  • Newt
  • Posts: 120
Re: Help: calculations in DCL file?
« Reply #7 on: March 13, 2024, 01:29:38 PM »
Hi LeeMac ,I have a question for your code in the post 1
In the begining you set the   "    edit_width = 10;"  for all cells. If I want for Length to have    "    edit_width =50;"   and for  Width:   to have    "    edit_width =30;"  and for Calculate  to have    "    edit_width =90;"    or  the  edit_width = ?   change every time if the text is bigger or smaller, how to do that ?
 
Code - Auto/Visual Lisp: [Select]
  1.                             "ed : edit_box"
  2.                             "{"
  3.                             "    alignment = left;"
  4.                             "    width = 20;"
  5.                             "    edit_width = 10;"
  6.                             "    fixed_width = true;"
  7.                             "}"
  8.  
  9.  




Code - Auto/Visual Lisp: [Select]
  1.     (defun c:test ( / *error* dch dcl des len wid )
  2.      
  3.         (defun *error* ( msg )
  4.             (if (and (= 'int (type dch)) (< 0 dch))
  5.                 (unload_dialog dch)
  6.             )
  7.             (if (= 'file (type des))
  8.                 (close des)
  9.             )
  10.             (if (and (= 'str (type dcl)) (findfile dcl))
  11.                 (vl-file-delete dcl)
  12.             )
  13.             (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
  14.                 (princ (strcat "\nError: " msg))
  15.             )
  16.             (princ)
  17.         )
  18.      
  19.         (cond
  20.             (   (not
  21.                     (setq dcl (vl-filename-mktemp nil nil ".dcl")
  22.                           des (open dcl "w")
  23.                     )
  24.                 )
  25.                 (princ "\nUnable to open DCL for writing.")
  26.             )
  27.             (   (progn
  28.                     (foreach str
  29.                        '(
  30.                             "ed : edit_box"
  31.                             "{"
  32.                             "    alignment = left;"
  33.                             "    width = 20;"
  34.                             "    edit_width = 10;"
  35.                             "    fixed_width = true;"
  36.                             "}"
  37.                             ""
  38.                             "test : dialog"
  39.                             "{"
  40.                             "    spacer;"
  41.                             "    key = \"dcl\";"
  42.                             "    : ed"
  43.                             "    {"
  44.                             "        key = \"len\";"
  45.                             "        label = \"Length:\";"
  46.                             "    }"
  47.                             "    : ed"
  48.                             "    {"
  49.                             "        key = \"wid\";"
  50.                             "        label = \"Width:\";"
  51.                             "    }"
  52.                             "    : row"
  53.                             "    {"
  54.                             "        : ed { key = \"res\"; label = \"Area:\"; is_enabled = false; }"
  55.                             "        : button"
  56.                             "        {"
  57.                             "            key = \"cal\";"
  58.                             "            label = \"Calculate\";"
  59.                             "        }"
  60.                             "    }"
  61.                             "    spacer;"
  62.                             "    ok_only;"
  63.                             "}"
  64.                         )
  65.                         (write-line str des)
  66.                     )
  67.                     (setq des (close des)
  68.                           dch (load_dialog dcl)
  69.                     )
  70.                     (<= dch 0)
  71.                 )
  72.                 (princ "\nUnable to load DCL file.")
  73.             )
  74.             (   (not (new_dialog "test" dch))
  75.                 (princ "\nUnable to display 'test' dialog.")
  76.             )
  77.             (   t
  78.                 (set_tile "dcl" "Calculate Area")
  79.                 (action_tile "len" "(setq len $value)")
  80.                 (action_tile "wid" "(setq wid $value)")
  81.                 (action_tile "cal"
  82.                     (vl-prin1-to-string
  83.                        '(
  84.                             (lambda ( / x y )
  85.                                 (set_tile "res" "")
  86.                                 (cond
  87.                                     (   (or (not len) (= "" len))
  88.                                         (alert "Please enter a length value.")
  89.                                         (mode_tile "len" 2)
  90.                                     )
  91.                                     (   (or (not wid) (= "" wid))
  92.                                         (alert "Please enter a width value.")
  93.                                         (mode_tile "wid" 2)
  94.                                     )
  95.                                     (   (not (setq x (distof len)))
  96.                                         (alert "The length must be numerical.")
  97.                                         (mode_tile "len" 2)
  98.                                     )
  99.                                     (   (not (setq y (distof wid)))
  100.                                         (alert "The width must be numerical.")
  101.                                         (mode_tile "wid" 2)
  102.                                     )
  103.                                     (   (<= x 0.0)
  104.                                         (alert "The length must be greater than zero.")
  105.                                         (mode_tile "len" 2)
  106.                                     )
  107.                                     (   (<= y 0.0)
  108.                                         (alert "The width must be greater than zero.")
  109.                                         (mode_tile "wid" 2)
  110.                                     )
  111.                                     (   (set_tile "res" (rtos (* x y) 2)))
  112.                                 )
  113.                             )
  114.                         )
  115.                     )
  116.                 )
  117.                 (start_dialog)
  118.             )
  119.         )
  120.         (*error* nil)
  121.         (princ)
  122.     )
  123.  


Thanks
« Last Edit: March 13, 2024, 01:40:35 PM by mhy3sx »

d2010

  • Bull Frog
  • Posts: 326
Re: Help: calculations in DCL file?
« Reply #8 on: March 23, 2024, 03:17:14 AM »
Hi. I am trying to learn how DCL dialogs work.
Thanks

Please You do not learn DCL-editing ascii-codes, because I have  a tool "DCL-editor.arx".
I can create your-DCL with many many items inside, as fast modes.
My tool "DCL-editor.arx" for AutoCad x86, create&edit the file.DCL on disk.
 :-D
My_problems are not files*.DCL ,  (because I can create fast  inside short my time),
my problem is he=autolisp-sources;
he=Source*.lsp need manually writer, need  a long time, for(fill.dcl, with Dates.
they=Sources.lsp need manually writing by my hands.
God bless you.
God bless you.
 (or? anybody!)
Code: [Select]
OR !!You learning file/s.DCL from too lower-level; (or?
If you are interested for collaboration with me, then -please you send PM to me?
« Last Edit: March 23, 2024, 08:50:10 AM by d2010 »