home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 5.img / SUPPORT2.LIB / DDRENAME.LSP < prev    next >
Encoding:
Text File  |  1993-02-08  |  18.8 KB  |  531 lines

  1. ;;;----------------------------------------------------------------------------
  2. ;;;
  3. ;;;   DDRENAME.LSP   ¬⌐Ñ╗ 0.5
  4. ;;;
  5. ;;;   ¬⌐┼v (C) 1991-1992  Autodesk ñ╜Ñq
  6. ;;;
  7. ;;;   Ñ╗│n┼ΘºK╢O¿╤▒z╢iªµÑ⌠ª≤Ñ╬│~╗▌¿D¬║½■¿⌐íB¡╫º∩ñ╬╡oªµ, ª²¼O░╚╜╨┐φ┤`ñU¡z
  8. ;;;   ¡∞½h :
  9. ;;;
  10. ;;;   1)  ñWªC¬║¬⌐┼v│qºi░╚╗▌ÑX▓{ªb¿Cñ@Ñ≈½■¿⌐∙╪íC
  11. ;;;   2)  ¼█├÷¬║╗í⌐·ñσÑ≤ñ]Ñ▓╢╖⌐·╕ⁿ¬⌐┼v│qºiñ╬Ñ╗╢╡│\Ñi│qºiíC
  12. ;;;
  13. ;;;   Ñ╗│n┼Θ╢╚┤ú¿╤º@¼░└│Ñ╬ñW¬║░╤ª╥, ª╙Ñ╝┴n⌐·⌐╬┴⌠ºtÑ⌠ª≤½O├╥; ╣∩⌐≤Ñ⌠ª≤»S«φ
  14. ;;;   Ñ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºtÑX¿π¬║½O├╥, ªbª╣ñ@╖ºñ⌐ÑHº_╗{íC
  15. ;;;
  16. ;;;
  17. ;;;----------------------------------------------------------------------------
  18. ;;;  DESCRIPTION
  19. ;;;
  20. ;;;  An AutoLISP implementation of the AutoCAD command RENAME with a dialogue
  21. ;;;  interface.  Unlike its command counterpart, DDRENAME supports wildcard
  22. ;;;  matching (* and ?), requested particularly by users for manipulating
  23. ;;;  bound Xref symbol table items (aka named objects) with long names.
  24. ;;;
  25. ;;;  DESIGN OUTLINE
  26. ;;;
  27. ;;;  For each table selected a list is generated of items in that table.
  28. ;;;  Renamed items are substituted into the list and on OK this new list
  29. ;;;  is compared to the original list and differing items are put through
  30. ;;;  the AutoCAD rename command.
  31. ;;;
  32. ;;;----------------------------------------------------------------------------
  33. ;;;   Prefixes in command and keyword strings:
  34. ;;;      "."  specifies the built-in AutoCAD command in case it has been
  35. ;;;           redefined.
  36. ;;;      "_"  denotes an AutoCAD command or keyword in the native language
  37. ;;;           version, English.
  38. ;;;----------------------------------------------------------------------------
  39. ;;;
  40. ;;; ===========================================================================
  41. ;;; ===================== load-time error checking ============================
  42. ;;;
  43.  
  44.   (defun ai_abort (app msg)
  45.      (defun *error* (s)
  46.         (if old_error (setq *error* old_error))
  47.         (princ)
  48.      )
  49.      (if msg
  50.        (alert (strcat " └│Ñ╬╡{ªí┐∙╗~: "
  51.                       app
  52.                       " \n\n  "
  53.                       msg
  54.                       "  \n"
  55.               )
  56.        )
  57.      )
  58.      (exit)
  59.   )
  60.  
  61. ;;; Check to see if AI_UTILS is loaded, If not, try to find it,
  62. ;;; and then try to load it.
  63. ;;;
  64. ;;; If it can't be found or it can't be loaded, then abort the
  65. ;;; loading of this file immediately, preserving the (autoload)
  66. ;;; stub function.
  67.  
  68.   (cond
  69.      (  (and ai_dcl (listp ai_dcl)))          ; it's already loaded.
  70.  
  71.      (  (not (findfile "ai_utils.lsp"))                     ; find it
  72.         (ai_abort "DDRENAME"
  73.                   (strcat "ºΣñú¿∞íuAI_UTILS.LSPív└╔«╫;"
  74.                           "\n  ╜╨└╦¼díusupportívÑ╪┐²íC")))
  75.  
  76.      (  (eq "failed" (load "ai_utils" "failed"))            ; load it
  77.         (ai_abort "DDRENAME" "╡L¬k╕ⁿñJíuAI_UTILS.LSPív└╔«╫"))
  78.   )
  79.  
  80.   (if (not (ai_acadapp))               ; defined in AI_UTILS.LSP
  81.       (ai_abort "DDRENAME" nil)         ; a Nil <msg> supresses
  82.   )                                    ; ai_abort's alert box dialog.
  83.  
  84. ;;; ==================== end load-time operations ===========================
  85.  
  86. ;;;----------------------------------------------------------------------------
  87. ;;;  The main function.
  88. ;;;----------------------------------------------------------------------------
  89. (defun c:ddrename (/
  90.         $value                             olderr               style_items
  91.                           globals          old_cmd              tables
  92.         block_items       highlight        old_indices          table_item
  93.         chflag            i                old_pattern          table_items
  94.         cmd               item1            old_pattern_length   table_list
  95.         cmd_old           item2            old_star             table_name
  96.         command_rename    j                one_index            table_selection
  97.         count             just_name        orig_list            ucs_items
  98.         current_items     layer_items      pat_length           update_list
  99.         dcl_id            list1            pat_letter           view_items
  100.         list_name_new                      pick_items           vport_items
  101.         defined_names     ltype_items      rename               undo_init
  102.         dimstyle_items    n                rename_err
  103.         ddrename_main     n1               rename_list
  104.         do_new            new_item_list    report_error
  105.         do_old            new_name         rs_error
  106.         do_tables1        new_name_list    s
  107.         do_tables2        new_pattern
  108.        )
  109.   ;;
  110.   ;; Action on Old Name edit box.
  111.   ;;
  112.   (defun do_old()
  113.     (set_tile "table_items" "")
  114.     (rs_error)
  115.     (setq report_error 1)
  116.     (do_old)
  117.   )
  118.   ;;
  119.   ;; Reset the error tile.
  120.   ;;
  121.   (defun rs_error()
  122.     (set_tile "error" "")
  123.   )
  124.   ;;
  125.   ;; This routine is called when a pick is made in the table list box, the
  126.   ;; one that displays Block, Layer, Linetype, etc.
  127.   ;;
  128.   (defun table_selection()
  129.     (set_tile "error" "")               ; Clear the error tile.
  130.     (do_tables1)                        ; Display items in selected table.
  131.     (if (= "*varies*" (get_tile "old")) ; If old name is *varies*,
  132.       (set_tile "old" "")                 ; clear it,
  133.       (progn                              ; else use it to highlight new items.
  134.         (setq report_error 0)
  135.         (do_old)
  136.       )
  137.     )
  138.   )
  139.   ;;
  140.   ;; This routine is called when a pick is made in the table items list box,
  141.   ;; the one that displays the items in the selected table.
  142.   ;;
  143.   (defun table_items()
  144.     (set_tile "error" "")               ; clear the error tile.
  145.     (setq pick_items (get_tile "table_items"))   ; find the highlight items.
  146.     (cond
  147.       ((= "" pick_items) (set_tile "old" ""))    ; no items selected
  148.       ((= "" (substr pick_items (+ 2 (strlen (itoa (read pick_items))))))
  149.         (set_tile "old" (nth (atoi $value) current_items)) ; if 1 item selected
  150.       )                                                    ; display its name.
  151.       (T (set_tile "old" "*varies*"))          ; else display *varies*.
  152.     )
  153.   )
  154.   ;;
  155.   ;;  This routine displays a new title on the table item list box.
  156.   ;;
  157.   (defun do_tables1()
  158.     (setq table_name (nth (atoi $value) tables))
  159.  
  160.     ;; This (cond) is added for translation purposes.  The list of symbol
  161.     ;; tables in the dialogue box will appear in the local language but
  162.     ;; they must be translated to American so that AutoCAD can understand.
  163.     ;; When translating these strings make sure they correspond exactly
  164.     ;; and precisely to those modified in the table list defined at the
  165.     ;; start of the ddrename_main() function further down the file.
  166.     (cond
  167.       ((= table_name "Block")         ; translate this
  168.         (setq table_name "block")     ; do not translate
  169.       )
  170.       ((= table_name "Dimstyle")      ; translate this
  171.         (setq table_name "dimstyle")  ; do not translate
  172.       )
  173.       ((= table_name "Layer")         ; translate this
  174.         (setq table_name "layer")     ; do not translate
  175.       )
  176.       ((= table_name "Ltype")         ; translate this
  177.         (setq table_name "ltype")     ; do not translate
  178.       )
  179.       ((= table_name "Style")         ; translate this
  180.         (setq table_name "style")     ; do not translate
  181.       )
  182.       ((= table_name "Ucs")           ; translate this
  183.         (setq table_name "ucs")       ; do not translate
  184.       )
  185.       ((= table_name "View")          ; translate this
  186.         (setq table_name "view")      ; do not translate
  187.       )
  188.       ((= table_name "Vport")         ; translate this
  189.         (setq table_name "vport")     ; do not translate
  190.       )
  191.     )
  192.     (do_tables2)
  193.   )
  194.   ;;
  195.   ;; Displays the defined items in a the selected table.
  196.   ;;
  197.   (defun do_tables2()
  198.     ;; If this is the first time this table is selected, set the "table"_items
  199.     ;; list to the currently defined items in the drawing by using ai_table.
  200.     (if (not (eval (read (strcat table_name "_items"))))
  201.       (set (read (eval (strcat table_name "_items")))
  202.            (ai_table table_name 7)
  203.       )
  204.     )
  205.     ;; Set current_items to a sorted version of "table"_items.
  206.     (if (and (>= (getvar "maxsort")
  207.                (length (eval (read (strcat table_name "_items"))))
  208.              )
  209.              (eval (read (strcat table_name "_items")))
  210.         )
  211.       (setq current_items
  212.             (acad_strlsort (eval (read (strcat table_name "_items"))))
  213.       )
  214.       (setq current_items (eval (read (strcat table_name "_items"))))
  215.     )
  216.     (start_list "table_items")          ; display the sorted version.
  217.     (mapcar 'add_list current_items)
  218.     (end_list)
  219.   )
  220.   ;;
  221.   ;; On Apply, check input, generate lists, and update the new list if all
  222.   ;; is well.
  223.   ;;
  224.   (defun rename()
  225.     (setq report_error 1)
  226.     (and (do_old)
  227.          (do_new)
  228.          (update_list)
  229.     )
  230.     (setq report_error 0)
  231.   )
  232.   ;;
  233.   ;; Validation checking for old name.  Called on OK and when focus is removed
  234.   ;; from the old name edit box.
  235.   ;;
  236.   (defun do_old()
  237.     (setq rename_list '())
  238.     (setq new_name_list '())
  239.     (cond
  240.       ((and (/= "" (setq old_pattern (ai_strtrim (get_tile "old"))))
  241.             (/= "*varies*" old_pattern))
  242.         (setq i 0)
  243.         (setq j 1)
  244.         (setq old_star nil)
  245.         (setq highlight "")
  246.         ; Find first * in old_pattern.
  247.         (setq old_pattern_length (strlen old_pattern))
  248.         (while (<= j old_pattern_length)
  249.           (cond
  250.             ((= "*" (substr old_pattern j 1)) (setq old_star j))
  251.              (T)
  252.            )
  253.            (setq j (1+ j))
  254.         )
  255.         (if (not (wcmatch old_pattern
  256.                         "*[]`#`@`.`~`[`,`'!%^&()+={}|`\\:;\"<>/]*"
  257.                  )
  258.             )
  259.           (progn
  260.             (foreach n current_items
  261.               (if (wcmatch n (strcase old_pattern))
  262.                 (progn
  263.                   (setq rename_list (cons n rename_list))
  264.                   (set_tile "table_items" (itoa i))
  265.                   (setq highlight (strcat highlight (itoa i) " "))
  266.                 )
  267.               )
  268.               (setq i (1+ i))
  269.             )
  270.           )
  271.         )
  272.         (if rename_list
  273.           (progn
  274.             (set_tile "table_items" highlight)
  275.              T)                ; if there is a list return T to continue
  276.           (progn
  277.             (if (= 1 report_error)
  278.               (set_tile "error" "íu┬┬ªW║┘ív╡L«─íC")
  279.             )
  280.             nil             ; else set errtile and drop out.
  281.           )
  282.         )
  283.       )
  284.       (T
  285.         (if (/= "" (setq old_indices (get_tile "table_items"))) ; get indices
  286.           (progn
  287.             (setq old_star 1)
  288.             (while (read old_indices)                 ; while an index remains
  289.               (setq one_index (itoa (read old_indices)))    ; get first index
  290.               (setq old_indices (substr old_indices (+ 2 (strlen one_index))))
  291.                                                             ; chop from string
  292.               (setq rename_list
  293.                     (cons (nth (atoi one_index) current_items) rename_list)
  294.               )
  295.             )
  296.           )
  297.           (progn
  298.             (if (= 1 report_error)
  299.               (set_tile "error" "Ñ╝┐∩¿∞íu┬┬ªW║┘ívíC")
  300.             )
  301.             nil
  302.           )
  303.         )
  304.       )
  305.     )
  306.   )
  307.   ;;
  308.   ;; Check the validity of new name and generates new names.
  309.   ;;
  310.   (defun do_new()
  311.     (setq new_pattern (strcase (ai_strtrim (get_tile "new"))))
  312.     (foreach n1 rename_list
  313.       (setq pat_length (strlen new_pattern)
  314.             i          1
  315.             new_name   ""
  316.       )
  317.       (while (<= i pat_length)
  318.         (setq pat_letter (substr new_pattern i 1))
  319.         (cond
  320.           ((= "*" pat_letter)
  321.             (cond
  322.               ((and old_star
  323.                     (>= (strlen n1) old_star)
  324.                )
  325.                 ;; if there is a * in old_pattern and the length of the old
  326.                 ;; name is longer then tag the rest of the letters on.
  327.                 (setq new_name (strcat new_name (substr n1 old_star)))
  328.               )
  329.               (T (setq new_name (strcat new_name (substr n1 i))) )
  330.             )
  331.             (setq i (1+ pat_length))
  332.           )
  333.           ((wcmatch pat_letter "@,#,_,-,$")
  334.             (setq new_name (strcat new_name (substr new_pattern i 1))
  335.                i        (1+ i)
  336.             )
  337.           )
  338.           ((= "?" pat_letter)
  339.             (setq new_name (strcat new_name (substr n1 i 1))
  340.                   i        (1+ i)
  341.             )
  342.           )
  343.           (T (setq new_name "")(setq i (1+ pat_length)))
  344.           ; if weird characters, set new_name to null and catch it later.
  345.         )
  346.       )
  347.       (setq new_name_list (cons new_name new_name_list))
  348.     )
  349.     (setq i             -1
  350.           list_name_new (reverse new_name_list)
  351.           defined_names (ai_table table_name 7)
  352.     )
  353.  
  354.     (while (< i (- (length list_name_new) 1))
  355.       (setq i (1+ i)
  356.             n (nth i list_name_new)
  357.       )
  358.       (cond
  359.         ;; It's OK to rename an item back to original name.  If the new item
  360.         ;; is a member of the original list of items and its position in the
  361.         ;; original list corresponds to the position of the new name then the
  362.         ;; user is renaming an item back to its original name.  If it doesn't
  363.         ;; correspond then give an error message.
  364.         ((and (member n defined_names)
  365.               (/= (length (member n defined_names)) ; old position in list
  366.                   (length (member (nth i rename_list) ; new position
  367.                                   (eval (read (strcat table_name "_items")))
  368.                           )
  369.                   )
  370.               )
  371.          )
  372.          (set_tile "error" "íu╖sªW║┘ív╡L«─íC")
  373.          (setq i (1+ (length list_name_new)))  ; break out
  374.         )
  375.         ((> (strlen n) 31)
  376.          (set_tile "error" "╡L«─ í╨ ╖sªW║┘╢W╣L 31 ¡╙ªrñ╕íC")
  377.          (setq i (1+ (length list_name_new)))  ; break out
  378.         )
  379.         ((= "" n)
  380.          (set_tile "error" "íu╖sªW║┘ív╡L«─íC")
  381.          (setq i (1+ (length list_name_new)))  ; break out
  382.         )
  383.         ((member n (cdr (member n new_name_list)))
  384.          (set_tile "error" "╡L«─ í╨ ╖sªW║┘íu¡½╜╞ívíC")
  385.          (setq i (1+ (length list_name_new)))  ; break out
  386.         )
  387.         ((member n (eval (read (strcat table_name "_items"))))
  388.          (set_tile "error" "╡L«─ í╨ ╖sªW║┘íu¡½╜╞ívíC")
  389.          (setq i (1+ (length list_name_new)))  ; break out
  390.         )
  391.         (T (set (read (eval (strcat table_name "_items")))
  392.                 (subst
  393.                       n                                            ; new
  394.                       (nth i rename_list)                          ; old
  395.                       (eval (read (strcat table_name "_items"))))) ; list
  396.         )
  397.       )
  398.     )
  399.     (if (= i (- (length list_name_new) 1))
  400.       (progn
  401.         (if (and (>= (getvar "maxsort") (length list_name_new))
  402.                  (eval (read (strcat table_name "_items")))
  403.             )
  404.           (setq current_items
  405.             (acad_strlsort (eval (read (strcat table_name "_items"))))
  406.           )
  407.           (setq current_items (eval (read (strcat table_name "_items"))))
  408.         )
  409.       )
  410.       nil
  411.     )
  412.   )
  413.   ;;
  414.   ;; Called by Apply, substitutes the new name for the current item name.
  415.   ;;
  416.   (defun update_list(/ i)
  417.     (setq i             0
  418.           new_item_list current_items
  419.     )
  420.     (foreach n rename_list
  421.       (setq new_item_list (subst (nth i list_name_new) n new_item_list)
  422.             i             (1+ i)
  423.       )
  424.     )
  425.     (start_list "table_items")
  426.     (mapcar 'add_list new_item_list)
  427.     (end_list)
  428.     (setq chflag 1)
  429.     (if (= "*varies*" old_pattern) (set_tile "old" ""))  ; clear old name.
  430.     T
  431.   )
  432.   ;;
  433.   ;; If all input checks out, then for each table that has a corresponding
  434.   ;; old name and new name list, corresponding items in the old list and the new
  435.   ;; list are compared and renamed if different.  For each updated table, a
  436.   ;; message reporting the number of items renamed is displayed.
  437.   ;;
  438.   (defun command_rename(/ orig_list count)
  439.     (foreach n tables
  440.       (setq count 0)
  441.       (if (eval (read (strcat n "_items")))
  442.         (progn
  443.           (setq orig_list (ai_table n 7))
  444.           (setq i 0)
  445.           (foreach n1 (eval (read (strcat n "_items")))
  446.             (if (not (wcmatch n1 (nth i orig_list)))
  447.               (progn
  448.                 (command "_.rename" n (nth i orig_list) n1)
  449.                 (setq count (1+ count))
  450.               )
  451.             )
  452.             (setq i (1+ i))
  453.           )
  454.           (if (/= count 0)
  455.             (princ (strcat "\n" (itoa count) " " n " ╢╡│Qíuº≤ªWívíC"))
  456.           )
  457.         )
  458.       )
  459.     )
  460.   )
  461.   ;;
  462.   ;; Put up the dialogue.
  463.   ;;
  464.   (defun ddrename_main()
  465.  
  466.     (if (not (new_dialog "ddrename" dcl_id)) (exit))
  467.     ;; This is the list of symbol table names that are dispalyed in the
  468.     ;; listbox.  When translating these strings, make sure that the (cond)
  469.     ;; in  do_tables1() is updated to contain exact copies of these strings.
  470.     ;; Re-ordering this list for alphabetising purposes should not cause
  471.     ;; problems, but test it thoroughly.
  472.  
  473.     (setq tables
  474.           '("Block" "Dimstyle" "Layer" "Ltype" "Style" "Ucs" "View" "Vport"))
  475.  
  476.     (setq chflag       0      ; OK needs to know if anything has changed
  477.           report_error 0)     ; Only print the old name errors during Apply.
  478.  
  479.     (start_list "tables")
  480.     (mapcar 'add_list tables)
  481.     (end_list)
  482.  
  483.     ;; Make layer the default selection and display layer list.
  484.     (set_tile "tables" "2")
  485.     (setq table_name "Layer")
  486.     (set_tile "table_type" "╝hªW")
  487.     (do_tables2)
  488.  
  489.     (action_tile "tables" "(table_selection)")
  490.     (action_tile "table_items" "(table_items)")
  491.     (action_tile "old" "(do_old)")
  492.     (action_tile "new" "(rs_error)")
  493.     (action_tile "rename" "(rs_error)(rename)")
  494.     (action_tile "accept" "(done_dialog 1)")
  495.     (action_tile "help" "(acad_helpdlg \"acad.hlp\" \"ddrename\")")
  496.  
  497.     (if (and (= 1 (start_dialog)) (= 1 chflag))
  498.       (command_rename)
  499.       (princ "\n¿Sª│╢╡Ñ╪│Qíuº≤ªWívíC ")
  500.     )
  501.   )
  502.  
  503.   ;; Set up error function.
  504.   (setq old_cmd (getvar "cmdecho")    ; save current setting of cmdecho
  505.         old_error  *error*            ; save current error function
  506.         *error* ai_error              ; new error function
  507.   )
  508.  
  509.   (setvar "cmdecho" 0)
  510.  
  511.   (cond
  512.      (  (not (ai_notrans)))                      ; transparent not OK
  513.      (  (not (ai_acadapp)))                      ; ACADAPP.EXP xloaded?
  514.      (  (not (setq dcl_id (ai_dcl "ddrename"))))  ; is .DCL file loaded?
  515.  
  516.      (t
  517.         (ai_undo_push)
  518.         (ddrename_main)                          ; proceed!
  519.         (ai_undo_pop)
  520.      )
  521.   )
  522.  
  523.   (setq *error* old_error)
  524.   (setvar "cmdecho" old_cmd)
  525.   (princ)
  526. )
  527.  
  528. ;;;----------------------------------------------------------------------------
  529. (princ "  íuDDRENAMEívñw╕ⁿñJíC")
  530. (princ)
  531.