Author Topic: Write String Formatted DCL  (Read 5868 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Write String Formatted DCL
« on: December 02, 2016, 02:58:01 PM »
Hi guys (again),
For any of you that has been recently following the Visual DCL Programming section, might already read the inspiration I got from Lee Mac, about writing this one:

Code - Auto/Visual Lisp: [Select]
  1. ; returns T if everything is okay, else nil
  2. (defun WriteStringFormattedDCL ( fpathDCL keepOriginalDCLcode / f1leDCL row LstDCLcode LstDCLcodeFormatted rtn )
  3.        
  4.         (if (and (eq 'STR (type fpathDCL)) (= ".dcl" (last (fnsplitl fpathDCL))) )
  5.                 (progn
  6.                         (setq f1leDCL (open fpathDCL "r"))
  7.                         (while (setq row (read-line f1leDCL)) (setq LstDCLcode (cons row LstDCLcode)) )
  8.                         (and f1leDCL (close f1leDCL))
  9.                         (and LstDCLcode (setq LstDCLcode (reverse LstDCLcode)))
  10.                         (setq f1leDCL (open fpathDCL "w"))
  11.                         (setq LstDCLcodeFormatted
  12.                                 (mapcar
  13.                                         (function
  14.                                                 (lambda (x)
  15.                                                         (strcat "\""
  16.                                                                 (apply 'strcat
  17.                                                                         (mapcar
  18.                                                                                 (function
  19.                                                                                         (lambda (c)
  20.                                                                                                 (cond ((= c "\"") (strcat "\\" "\"")) (T c))
  21.                                                                                         )
  22.                                                                                 )
  23.                                                                                 (mapcar 'chr (vl-string->list x))
  24.                                                                         ); mapcar
  25.                                                                 ); apply 'strcat
  26.                                                                 "\""
  27.                                                         ); strcat
  28.                                                 ); lambda
  29.                                         ); function
  30.                                         LstDCLcode
  31.                                 ); mapcar
  32.                         ); setq LstDCLcodeFormatted
  33.                         (if keepOriginalDCLcode
  34.                                 (progn
  35.                                         (write-line "//==============================================================================================================//" f1leDCL)
  36.                                         (write-line "//==========================================>>> Original DCL code: <<<==========================================//" f1leDCL)
  37.                                         (foreach x LstDCLcode (write-line x f1leDCL))
  38.                                         (write-line "//==============================================================================================================//" f1leDCL)
  39.                                         (repeat 3 (write-line "" f1leDCL))
  40.                                 ); progn
  41.                         ); if
  42.                         (write-line ";==============================================================================================================;" f1leDCL)
  43.                         (write-line ";======================================>>> String Formatted DCL code: <<<======================================;" f1leDCL)
  44.                         (foreach x LstDCLcodeFormatted (write-line x f1leDCL))
  45.                         (write-line ";==============================================================================================================;" f1leDCL)
  46.                         (and f1leDCL (close f1leDCL))
  47.                         (setq rtn T)
  48.                 ); progn
  49.         ); if
  50.         rtn
  51. );| defun WriteStringFormattedDCL |; (or vlax-get-acad-object (vl-load-com)) (princ)
  52.  
It expects an external .dcl file, with the pure DCL code written inside it (without any additional stuff inside that may affect it - like lisp comments).
Example usage:
Code - Auto/Visual Lisp: [Select]
  1. ; Example test function:
  2. (defun C:test ( / fpath )
  3.         (and
  4.                 (or
  5.                         (setq fpath (findfile "ConvertDCLtoString.dcl"))
  6.                         (setq fpath (getfiled "Open DCL File" "" "dcl" 0))
  7.                 )
  8.                 (WriteStringFormattedDCL fpath T)
  9.         )
  10. ); defun C:test
  11.  
Example output:
Code - C++: [Select]
  1. //==============================================================================================================//
  2. //==========================================>>> Original DCL code: <<<==========================================//
  3.  
  4.  
  5.  
  6. // Radios
  7.  
  8. Radios_Test : dialog // practicing with radio buttons
  9. {
  10.         label = "Radio buttons Excercise";
  11.         : row
  12.         {
  13.                 : column
  14.                 {
  15.                         : boxed_radio_column // As you can see boxed_radio_column / boxed_radio_row are used to gather a number of radio_buttons, where only one option is valid from these
  16.                         {
  17.                                 label = "Type";
  18.                                 alignment = top;
  19.                                 : radio_button { label = "A"; key = "A"; value = 1; }
  20.                                 : radio_button { label = "B"; key = "B"; }
  21.                                 : radio_button { label = "C"; key = "C"; }
  22.                         }
  23.                         : boxed_radio_column
  24.                         {
  25.                                 label = "Orientation";
  26.                                 alignment = bottom;
  27.                                 : radio_button { label = "Landscape"; key = "L"; value = 1; }
  28.                                 : radio_button { label = "Portrait"; key = "P"; }
  29.                         }
  30.                 }
  31.                 : boxed_radio_column
  32.                 {
  33.                         label = "Size";
  34.                         alignment = centered;
  35.                         : radio_button { label = "1"; key = "1"; }
  36.                         : radio_button { label = "2"; key = "2"; }
  37.                         : radio_button { label = "3"; key = "3"; }
  38.                         : radio_button { label = "4"; key = "4"; value = 1; }
  39.                         : radio_button { label = "5"; key = "5"; }
  40.                 }
  41.         }
  42.         spacer;
  43.         : row
  44.         {
  45.                 : button { label = "Okay"; key = "OK"; alignment = left; fixed_width = true; }
  46.                 : button { label = "Cancel"; is_cancel = true; alignment = right; fixed_width = true; }
  47.         }
  48. }
  49.  
  50.  
  51. On_Off : dialog
  52. {
  53.         label = "On/Off Excercise";
  54.         : toggle
  55.         {
  56.                 label = "Toggle On/Off";
  57.                 key = "Toggle";
  58.                 value = 1;
  59.                 alignment = centered;
  60.         }
  61.         : popup_list
  62.         {
  63.                 key = "Pop_Lst";
  64.                 value = "On Off"; // How to populate the popup_list - from the lisp code I guess ??
  65.                 width = 11.42;
  66.                 fixed_width = true;
  67.         }
  68.         : boxed_radio_row
  69.         {
  70.                 label = "Choose status";
  71.                 alignment = centered;
  72.                 : radio_button { label = "On"; key = "On_Radio_Button"; value = 1; }
  73.                 : radio_button { label = "Off"; key = "Off_Radio_Button"; }
  74.         }
  75.         : boxed_row
  76.         {
  77.                 label = "Choose button";
  78.                 : button { label = "On"; key = "On_Button"; is_default = true; }
  79.                 : button { label = "Off"; key = "Off_Button"; }
  80.         }
  81.         spacer;
  82.         ok_cancel;
  83. }
  84.  
  85. //==============================================================================================================//
  86.  
  87.  
  88.  
  89. ;==============================================================================================================;
  90. ;======================================>>> String Formatted DCL code: <<<======================================;
  91. ""
  92. ""
  93. ""
  94. "// Radios"
  95. ""
  96. "Radios_Test : dialog // practicing with radio buttons"
  97. "{"
  98. "       label = \"Radio buttons Excercise\";"
  99. "       : row"
  100. "       {"
  101. "               : column"
  102. "               {"
  103. "                       : boxed_radio_column // As you can see boxed_radio_column / boxed_radio_row are used to gather a number of radio_buttons, where only one option is valid from these"
  104. "                       {"
  105. "                               label = \"Type\";"
  106. "                               alignment = top;"
  107. "                               : radio_button { label = \"A\"; key = \"A\"; value = 1; }"
  108. "                               : radio_button { label = \"B\"; key = \"B\"; }"
  109. "                               : radio_button { label = \"C\"; key = \"C\"; }"
  110. "                       }"
  111. "                       : boxed_radio_column"
  112. "                       {"
  113. "                               label = \"Orientation\";"
  114. "                               alignment = bottom;"
  115. "                               : radio_button { label = \"Landscape\"; key = \"L\"; value = 1; }"
  116. "                               : radio_button { label = \"Portrait\"; key = \"P\"; }"
  117. "                       }"
  118. "               }"
  119. "               : boxed_radio_column"
  120. "               {"
  121. "                       label = \"Size\";"
  122. "                       alignment = centered;"
  123. "                       : radio_button { label = \"1\"; key = \"1\"; }"
  124. "                       : radio_button { label = \"2\"; key = \"2\"; }"
  125. "                       : radio_button { label = \"3\"; key = \"3\"; }"
  126. "                       : radio_button { label = \"4\"; key = \"4\"; value = 1; }"
  127. "                       : radio_button { label = \"5\"; key = \"5\"; }"
  128. "               }"
  129. "       }"
  130. "       spacer;"
  131. "       : row"
  132. "       {"
  133. "               : button { label = \"Okay\"; key = \"OK\"; alignment = left; fixed_width = true; }"
  134. "               : button { label = \"Cancel\"; is_cancel = true; alignment = right; fixed_width = true; }"
  135. "       }"
  136. "}"
  137. ""
  138. ""
  139. "On_Off : dialog"
  140. "{"
  141. "       label = \"On/Off Excercise\";"
  142. "       : toggle"
  143. "       {"
  144. "               label = \"Toggle On/Off\";"
  145. "               key = \"Toggle\";"
  146. "               value = 1;"
  147. "               alignment = centered;"
  148. "       }"
  149. "       : popup_list "
  150. "       {"
  151. "               key = \"Pop_Lst\";"
  152. "               value = \"On Off\"; // How to populate the popup_list - from the lisp code I guess ??"
  153. "               width = 11.42;"
  154. "               fixed_width = true;"
  155. "       }"
  156. "       : boxed_radio_row"
  157. "       {"
  158. "               label = \"Choose status\";"
  159. "               alignment = centered;"
  160. "               : radio_button { label = \"On\"; key = \"On_Radio_Button\"; value = 1; }"
  161. "               : radio_button { label = \"Off\"; key = \"Off_Radio_Button\"; }"
  162. "       }"
  163. "       : boxed_row"
  164. "       {"
  165. "               label = \"Choose button\";"
  166. "               : button { label = \"On\"; key = \"On_Button\"; is_default = true; }"
  167. "               : button { label = \"Off\"; key = \"Off_Button\"; }"
  168. "       }"
  169. "       spacer;"
  170. "       ok_cancel;"
  171. "}"
  172. ""
  173. ;==============================================================================================================;
  174.  
No more tedious formatting by hand by typing numerous double-quotes with the risk of typos!
Any questions, critics, comments and help are wellcome. :)
BTW I tried to write it shorter by using prin1 / print functions:
Code: [Select]
(foreach x LstDCLcodeFormatted (prin1 x f1leDCL))But the formatting is not so readable, and I'm not sure will the formatted version "work/evaluate properly", but if interested I may post my 2nd attempt.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ribarm

  • Gator
  • Posts: 3212
  • Marko Ribar, architect
Re: Write String Formatted DCL
« Reply #1 on: December 02, 2016, 03:05:27 PM »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Write String Formatted DCL
« Reply #2 on: December 02, 2016, 03:21:57 PM »
Nice, here is my version :
https://www.theswamp.org/index.php?topic=40611
Oh, I wasn't aware of it!
I'll check it, thanks ! :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg