Author Topic: Using DDedit as a Text Editor  (Read 2175 times)

0 Members and 1 Guest are viewing this topic.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Using DDedit as a Text Editor
« on: January 19, 2014, 08:04:25 AM »
I am working on adding features to CAB's "TEXTINSERT" tool.  Currently there is no way to edit the text or add new text items.  (you need to do this in notepad/wordpad) 

See the next post for updated code..

Thanks,

Bruce

« Last Edit: January 26, 2014, 02:45:29 PM by snownut2 »

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Using DDedit as a Text Editor
« Reply #1 on: January 26, 2014, 10:59:17 AM »
Well here goes, the below LISP (EditNote), can be placed in CAB's "TextInsertDCL10_0.lsp" LISP.

Found here; http://www.theswamp.org/index.php?topic=1392.msg508047#msg508047

This function is necessary mainly because word-wrap does not work in a Dialog Box, and it would be beneficial to
be able to edit the TID files directly in CAD.  A much better solution then having all your std notes in a block and inserting/exploding, as they can be individually edited in each drawing or edited and saved as a text file for future use.

To install;
     1.-See Instructions in Header of Code

Couple of rules;
    You cannot format the text while editing. (format stripping code is not fully developed, and saved strictly as plain text)
    You can add/delete lines of text just as in MText.
    DO NOT ACCEPT THE "a Template.TID" as a default in the function.... (there is a gremlin needs to be shooed away...)

One small issues I need to fix (any assistance would be great)
    Need the highlighted Note to not index to the first one once a file is edited.
   
   
Code - Auto/Visual Lisp: [Select]
  1. ;;------------------------------------------------------------------------------;;
  2. ;;                                                                              ;;
  3. ;;                        EditNote by; F. Bruce Fillmore                        ;;
  4. ;;                                      Weare, NH 03281                         ;;
  5. ;;                                       AKA; snownut2                          ;;
  6. ;;                                                                              ;;
  7. ;;                                Edit Existing Note                            ;;
  8. ;;                                      or                                      ;;
  9. ;;                                 Create New Note                              ;;
  10. ;;                                                                              ;;
  11. ;;                                Written 1/26/2014                             ;;
  12. ;;              To Install;                                                     ;;
  13. ;;                      1.- Comment out lines 2424 & 2425                       ;;
  14. ;;                      2.- Paste this "EditNote" Function                      ;;
  15. ;;                          In place of existing "EditNote" Function            ;;
  16. ;;                      3.- Replace Line 3076  with line below                  ;;
  17. ;;                          ((= action 5) (EditNote datarecord))                ;;
  18. ;;______________________________________________________________________________;;
  19.  
  20.   (defun EditNote (datarecord / datalayer datadims datawidth  ent txtlst  newnte
  21.                                 hedtxt    oldnte   file_txt   newdatarecord
  22.                    )
  23.     (defun textedit (txtlist / txt itm allstrings mtxtlist entlist  
  24.                                p1  p2  scrhght    scrwdth  AcadObj
  25.                      )
  26.       (defun mid (a b)
  27.         (mapcar '(lambda (a b) (/ (+ a b) 2.0)) a b)
  28.         ) ; (mid p1 p2)
  29.       (defun scrratio (hght / scsize ratio wdth)
  30.         (setq scsize (getvar 'screensize)
  31.               ratio (/ (car scsize)(cadr scsize))
  32.               wdth (* hght ratio)
  33.               )
  34.         );defun
  35.       (if (not (tblsearch "STYLE" "SATstyle"))
  36.         (entmake
  37.           (list (cons 0  "STYLE")
  38.                 (cons 100  "AcDbSymbolTableRecord")
  39.                 (cons 100  "AcDbTextStyleTableRecord")
  40.                 (cons 2  "SATstyle")
  41.                 (cons 70 0)
  42.                 (cons 40 (getvar 'textsize))
  43.                 (cons 41 0.8)
  44.                 (cons 50 0)
  45.                 (cons 71 0)
  46.                 (cons 42 (getvar 'textsize))
  47.                 (cons 3 "Arial.ttf")
  48.                 (cons 4 "")
  49.                 )
  50.           )
  51.         )
  52.       (setq scrhght (getvar 'viewsize)
  53.             scrwdth (scrratio (getvar 'viewsize))
  54.             p2 (getvar 'viewctr)
  55.             p1 (list (-(car p2)(/ scrwdth 2))(+(cadr p2)(/ scrhght 2)))
  56.             mtxtlist   '()
  57.             allstrings ""
  58.             entlist
  59.              (list
  60.                (cons 0 "MTEXT")
  61.                (cons 100 "AcDbEntity")
  62.                (cons 100 "AcDbMText")
  63.                (cons 10 p1 )
  64.                (cons 40 (* 0.01 scrhght (getvar 'textsize)))
  65.                (cons 41 scrwdth)
  66.                (cons 62 7)
  67.                (cons 71 1)
  68.                (cons 7 "SATstyle")
  69.                )
  70.             )
  71.       ; following code from CAB's textinsert tool
  72.       (foreach itm txtlist ; combine all strings plus EOL code
  73.         (setq allstrings (strcat allstrings itm "\\P"))
  74.         )    ;;  remove the last \\P
  75.       (setq allstrings (substr allstrings 1 (- (strlen allstrings)2)))
  76.       (while (> (strlen allstrings) 250) ;  Break into 250 char groups
  77.         (setq mtxtlist (cons (cons 3 (substr allstrings 1 250)) mtxtlist))
  78.         (setq allstrings (substr allstrings 251)) ; remaining characters
  79.         )
  80.       ;;  Add the remaining text to the list of groups w/ code 1
  81.       (setq mtxtlist (reverse (cons (cons 1 allstrings) mtxtlist)))
  82.       ;;  Combine entity list with text list
  83.       (setq entlist (append entlist mtxtlist))
  84.       (setq txt (entmakex entlist))
  85.       (VLA-ZOOMCENTER
  86.         (vlax-get-acad-object)
  87.         (vlax-3d-point (mid p1 p2))
  88.         scrhght
  89.         )
  90.       (vl-cmdf "_.ddedit" txt)
  91.       (vl-cmdf)
  92.       (setq AcadObj (vlax-get-Acad-Object))
  93.       (VLA-ZOOMPREVIOUS AcadObj)
  94.       (princ)
  95.       );defun
  96.  
  97.     (defun MTxtConvert ( ent / newtxtl newlst lst  MTstr TxtLst newtxtL
  98.                                ct      cnt    tempitem1  tempitem2)
  99.       (defun MTstr->TxtLst ( str / s strl newlst)
  100.         (defun _replacequotes ( str / pos ) ; borrowed from Lee Mac
  101.           (setq pos 0)
  102.           (while (setq pos (vl-string-search  "\"\"" str pos))
  103.             (setq str (vl-string-subst "\"" "\"\"" str pos)
  104.                   pos (1+ pos)
  105.                   )
  106.             )
  107.           str
  108.           );defun
  109.         (defun _\p->lst ( str pos / s )     ; borrowed from Lee Mac - revised slightly
  110.           (cond
  111.             (   (null (setq pos (vl-string-position 92 str pos)))
  112.              (if (wcmatch str "\"*\"")
  113.                (list (_replacequotes (substr str 2 (- (strlen str) 2))))
  114.                (list str)
  115.                )
  116.              )
  117.             (   (wcmatch (setq s (substr str 1 pos)) "\"*\"")
  118.              (cons
  119.                (_replacequotes (substr str 2 (- pos 2)))
  120.                (_\p->lst (substr str (+ pos 2)) 0)
  121.                )
  122.              )
  123.             (   (wcmatch s "\"*[~\"]")
  124.              (_\p->lst str (+ pos 2))
  125.              )
  126.             (   (cons s (_\p->lst (substr str (+ pos 2)) 0)))
  127.             )
  128.           );defun
  129.  
  130.         (setq strl    (_\p->lst str 0)
  131.               s (car strl)
  132.               strl (vl-remove (nth 0 strl) strl)
  133.               )
  134.         (foreach x strl
  135.           (setq newlst (cons  (vl-string-left-trim "P" x) newlst))
  136.           )
  137.         (cons s (reverse newlst))
  138.         );defun (MTstr->TxtLst)
  139.  
  140.       (defun cdrs (key lst / pair rtn)
  141.         (while (setq pair (assoc key lst))
  142.           (setq rtn (cons (cdr pair) rtn)
  143.                 lst (cdr (member pair lst))
  144.                 )
  145.           )
  146.         (reverse rtn)
  147.         );defun
  148.  
  149.       (setq ent   (entget ent))
  150.       (setq MTstr (cdrs 3 ent))
  151.       (foreach x MTstr
  152.         (setq TxtLst (MTstr->TxtLst x)
  153.               newtxtL (cons TxtLst newtxtl)
  154.               )
  155.         )
  156.       (setq ct 0
  157.             MTstr    (cdrs 1 ent)
  158.             TxtLst   (MTstr->TxtLst (car MTstr))
  159.             newtxtL  (cons TxtLst newtxtL)
  160.             newtxtL  (reverse newtxtL)
  161.             cnt      (length newtxtL)
  162.             )
  163.       (while (< ct (- cnt 1))
  164.         (setq tempitem1 (strcat (last (nth ct newtxtL))(car(nth (1+ ct)newtxtL)))
  165.               tempitem1 (subst tempitem1 (last (nth ct newtxtL)) (nth ct newtxtL))
  166.               newtxtL   (subst tempitem1 (nth ct newtxtL) newtxtL)
  167.               )
  168.         (if (> (length (nth(- cnt 1)newtxtL))0)
  169.           (setq tempitem2 (vl-remove (car (nth (1+ ct) newtxtL))(nth (1+ ct) newtxtL))
  170.                 newtxtL   (subst tempitem2 (nth (1+ ct) newtxtL) newtxtL)
  171.                 )
  172.           (setq newtxtL (vl-remove (nth (- cnt 1) newtxtL) newtxtL))
  173.           )
  174.         (setq ct (1+ ct))
  175.         )
  176.       (foreach x newtxtL
  177.         (setq lst (append lst x))
  178.         )
  179.       lst
  180.       );defun
  181.  
  182.      (defun read_file (fn / ln txtitm bodytxt heading)      
  183.       (if (setq fn (findfile (strcat fn ".tid")))
  184.         (progn
  185.           (if (> ti_debug 2) (prompt "\nOpening File to Read"))
  186.           (setq fn (open fn "r")) ; open file for reading
  187.           (while (= T (= 0 (vl-string-search ";" (vl-string-left-trim " \t" (setq ln (read-line fn)))))) ;reading heading section
  188.             (setq heading (cons ln heading))
  189.             );while
  190.           (setq heading  (reverse (cons "***Text Item *****************" heading)))
  191.           (while  (setq ln (read-line fn)) ;reading heading section
  192.             (setq txtitm (cons ln txtitm))         
  193.             (while (/= 0 (vl-string-position (ascii "*") (setq ln (read-line fn)))) ;reading each note section
  194.               (setq txtitm (cons ln txtitm))
  195.               );while
  196.             (setq txtitm  (cons  ln txtitm)
  197.                   txtitm  (reverse txtitm)
  198.                   bodytxt (cons txtitm bodytxt)
  199.                   txtitm nil
  200.                   )
  201.             );while
  202.           (close fn) ; close the open file handle
  203.           (setq bodytxt (cons heading (reverse bodytxt)))
  204.           );progn
  205.         );if
  206.       );defun
  207.  
  208.     (defun write_file (fn file_txt)
  209.       (if (setq fn (findfile (strcat fn ".tid")))
  210.         (vl-file-delete fn)
  211.         )
  212.       (if (> ti_debug 2) (prompt "\nOpening File for Writing"))
  213.       (setq fn (open fn "w")) ; open file for writing
  214.       (foreach x file_txt
  215.         (foreach y x
  216.           (write-line y fn)
  217.           )
  218.         )
  219.       (close fn) ; close file
  220.       );defun
  221.  
  222.     (defun Append_File_Dialog ( / datarecorda dclfiletemp dcl_ida)
  223.       (defun *error* ( msg )
  224.         (and dcl_id (unload_dialog dcl_ida))
  225.         (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
  226.             (princ (strcat "\n** Error: " msg " **"))
  227.             )
  228.         (princ "\n- Function Cancelled -")
  229.         (princ)
  230.         );defun
  231.       (defun write_append_dialog ( / fnappend) ; Writes DCL Dialog to "TEMP" Folder
  232.         (setq dclfiletemp (strcat (getenv "TEMP") "\\temp.dcl"))
  233.         (vl-file-delete dclfiletemp)
  234.         (setq fnappend(open dclfiletemp "w"))
  235.         (foreach x (list
  236.                      "appendtid: dialog {"
  237.                      "  label = \"Append TID File   ---   by F. Bruce Fillmore  \";"
  238.                      "    :  boxed_column  { label = \"Select Appropriate DATA or < current settings >\"; width = 60;"
  239.                      "       :  edit_box   { label = \"Enter Title for Note Item .....\"; key = \"datarecord\"; width = 25; }"
  240.                      "       :  popup_list { label = \"Select Layer for placement \"    ; key = \"datalayer\" ; width = 30; }"
  241.                      "       :  popup_list { label = \"Select the Dimension Style \"    ; key = \"datadims\"  ; width = 30; }"
  242.                      "       :  edit_box   { label = \"Enter Width for MText Note \"    ; key = \"datawidth\" ; fixed_width = true ; width =  5; }"
  243.                      "    }"
  244.                      "    : spacer { height = 0.5; }"
  245.                      "      ok_cancel;"
  246.                      "    : errtile { width= 20; alignment = centered; }  "
  247.                      "  }") ; endlist
  248.           (princ x fnappend)
  249.           (write-line "" fnappend)
  250.           ) ; end foreach
  251.         (close fnappend)
  252.         (princ "\nAppend Dialog Written ;")
  253.         );defun
  254.       (defun chkrecord ( )
  255.         (if (not datarecorda)
  256.           (progn
  257.             (set_tile "error" "You need to ENTER a NOTE TITLE !")
  258.             (mode_tile "datarecorda" 2)
  259.             );progn
  260.           (progn
  261.             (set_tile "error"  "")
  262.             (done_dialog)
  263.             );progn
  264.           )
  265.         );defun
  266.       (write_append_dialog)
  267.       (setq dcl_ida (load_dialog dclfiletemp))
  268.       (if (not(new_dialog "appendtid" dcl_ida))
  269.         (exit)
  270.         );if
  271.       (start_list "datalayer" )
  272.       (mapcar 'add_list lay_list)
  273.       (end_list)
  274.       (start_list "datadims" )
  275.       (mapcar 'add_list dim_list)
  276.       (end_list)
  277.       (action_tile "datarecord" "(setq datarecorda $value)")
  278.       (action_tile "datalayer"  "(setq ovr:layer (nth (atoi $value) lay_list))")
  279.       (action_tile "datadims"   "(setq ovr:dim   (nth (atoi $value) dim_list))")
  280.       (action_tile "datawidth"  "(setq ovr:width (atof $value))")
  281.       (action_tile "accept"     "(chkrecord)")
  282.       (action_tile "cancel"     "(exit)")
  283.       (start_dialog )
  284.       (unload_dialog dcl_ida)
  285.       (vl-file-delete dclfiletemp) ; Deletes DCL Dialog to "TEMP" Folder
  286.       (list  datarecorda " ")
  287.       );defun
  288.  
  289.     (setq datarecord (if (= action 5)(Append_File_Dialog)datarecord))
  290.    
  291.     (setq datalayer (if (null ovr:layer)
  292.                       (list(strcat ":-l " (getvar'clayer)))
  293.                       (list(strcat ":-l " ovr:layer))
  294.                       )
  295.           datawidth (if (null ovr:width)
  296.                       (list ":-w 0" )
  297.                       (list(strcat ":-w " (rtos ovr:width 2 1)))
  298.                       )
  299.           datadims (if (null ovr:dim)
  300.                      (list(strcat ":-sd " (getvar'dimstyle)))
  301.                      (list(strcat ":-sd " ovr:dim))
  302.                      )
  303.           )    
  304.     (textedit (cdr datarecord))
  305.     (setq ent (entlast)
  306.           txtlst (MTXTCONVERT ent)
  307.           )
  308.     (entdel ent)
  309.     (vl-cmdf "redraw")    
  310.     (setq newdatarecord (append (list(car datarecord)) txtlst))
  311.     (if (= action 5)    ; Action = 5 for append file
  312.       (progn            ; Action = 6 for edit file  
  313.         (setq txtlist(append txtlist datarecord)
  314.               newnte (append datalayer datawidth datadims (list(car datarecord)) txtlst)
  315.               )
  316.         (appendfile tid_filename newnte)
  317.         )
  318.       (progn
  319.         (setq file_txt (read_file tid_filename)
  320.               hedtxt (list (strcat "***Text Item**Revised  "(getdate)""))
  321.               newnte (append hedtxt datalayer datawidth datadims (list(car datarecord)) txtlst hedtxt)
  322.               )
  323.         (foreach x file_txt
  324.           (foreach y x
  325.             (setq oldnte (if (eq (car datarecord) y) x oldnte))
  326.             )
  327.           )
  328.         (setq file_txt (subst newnte oldnte file_txt)
  329.               txtlist  (subst newdatarecord datarecord txtlist)
  330.               )
  331.         (write_file tid_filename file_txt)
  332.         )
  333.       )
  334.     (setq datarecord newdatarecord)
  335.     (fileupdt (itoa (vl-position (vl-filename-base tid_filename) fn_list)))
  336.     );defun
  337.  

All works fine in BricsCAD when you specify NOTEPAD as the MText editor in SETTINGS or using the default editor.

This function will edit EXISTING or CREATE a NEW note.
« Last Edit: January 29, 2014, 06:39:23 AM by snownut2 »

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: Using DDedit as a Text Editor
« Reply #2 on: January 29, 2014, 06:41:33 AM »
Added update to make allowances for using TAB in the mtext editor.

It is now allowed and the TAB formatting will not be lost.