home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 6.img / BONUS1.LIB / BMAKE.LSP < prev    next >
Encoding:
Text File  |  1993-02-09  |  16.0 KB  |  539 lines

  1. ;;;---------------------------------------------------------------------------;
  2. ;;;
  3. ;;;   BMAKE.LSP   ¬⌐Ñ╗ 0.5
  4. ;;;
  5. ;;;   (C) ¬⌐┼v 1988-1992  Autodesk ñ╜Ñq
  6. ;;;
  7. ;;;   Ñ╗╡{ªíñwÑ╤ Autodesk ñ╜Ñq╡∙ÑU¬⌐┼v, ╢╚⌐≤ñU¡z▒í¬pñUÑi▒┬╗P▒zíu│\ÑiívíC
  8. ;;;   ╗╒ñUñú▒oÑHÑ⌠ª≤º╬ªí╡oªµ⌐╬ÑX¬⌐ª╣╡{ªí¬║íu¡∞⌐l╜Xív; ª²ñ╣│\▒zªb»S⌐w¡lÑ═
  9. ;;;   ¬║ñuº@ñW╡▓ªXª╣╡{ªí¬║íuÑ╪¬║╜Xív¿╧Ñ╬íCª│├÷│o├■¡lÑ═ñuº@¬║▒°Ñ≤ªpñU:
  10. ;;;
  11. ;;;   ( i)  │]¡pñW╗Pñuº@ñW¼╥»┬║Θ░w╣∩ Autodesk ñ╜Ñq¬║▓ú½~íC
  12. ;;;   (ii)  ╕ⁿª│íu¬⌐┼v  (C) 1988-1992  Autodesk ñ╜Ñqív¬║¬⌐┼v│qºiíC
  13. ;;;
  14. ;;;
  15. ;;;
  16. ;;;   AUTODESKñ╜Ñq┤ú¿╤ª╣╡{ªí╢╚¿╤º@íu├■ªⁿív¬║░╤ª╥, ª╙ÑBñú▒╞░úª│Ñ⌠ª≤┐∙╗~¬║
  17. ;;;   Ñi»αíCAUTODESKñ╜Ñq»Sª╣º_╗{Ñ⌠ª≤»S⌐wÑ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºt
  18. ;;;   ÑX¿π¬║½O├╥íCAUTODESKñ╜ÑqªP«╔ÑτñúÑX¿πª╣╡{ªí░⌡ªµ«╔ñ@⌐wñú╖|íuññ┬_ív⌐╬
  19. ;;;   íuº╣Ñ■╡L╗~ív¬║½O├╥íC
  20. ;;;
  21. ;;;
  22. ;;;   by Kieran V. McKeogh
  23. ;;;   28 Feb 1991
  24. ;;;
  25. ;;;---------------------------------------------------------------------------;
  26. ;;;  DESCRIPTION
  27. ;;;
  28. ;;;  Programming example of defining blocks using (entmake) with a dialogue
  29. ;;;  interface.  Uses Bmake.dcl.
  30. ;;;
  31. ;;;  REVISION
  32. ;;;
  33. ;;;---------------------------------------------------------------------------;
  34.  
  35.  
  36. ;;;---------------------------------------------------------------------------;
  37. ;;; The Main Function which pops up the dialogue with the defaults.  A while
  38. ;;; loop is used to allow the dialogue to be hidden for point and object
  39. ;;; selection.
  40. ;;;---------------------------------------------------------------------------;
  41. (defun c:bmake (/ bname unnamed x_pt y_pt z_pt retain selection_set)
  42.   ;;
  43.   ;;  Main error routine.
  44.   ;;
  45.   (defun bmake_error (s)                ; If an error (such as CTRL-C) occurs
  46.     (if (/= s "Function cancelled")
  47.       (princ (strcat "\n┐∙╗~: " s))
  48.     )
  49.     (if olderr (setq *error* olderr)) ; Restore old *error* handler
  50.     (princ)
  51.   )
  52.   ;;
  53.   ;;  Standard Help function
  54.   ;;
  55.   (defun do_help (cmd)
  56.     (if (= (type acad_helpdlg) 'EXSUBR)
  57.       (acad_helpdlg "acad.hlp" cmd)
  58.       (alert "ºΣñú¿∞íu╗▓ºU╣∩╕▄íví╨ xload acadapp íC")
  59.     )
  60.     (princ)
  61.   )
  62.   ;;
  63.   ;; If unnamed is toggled on, disable Block Name edit box and vice versa.
  64.   ;;
  65.   (defun do_unnamed()
  66.     (rs_error)
  67.     (mode_tile "bname" (setq unnamed (atoi (get_tile "unnamed"))))
  68.   )
  69.   ;;
  70.   ;; Check validity of the Block name.
  71.   ;;
  72.   (defun do_bname()
  73.     (check_name (setq bname (strcase (get_tile "bname"))))
  74.   )
  75.   ;;
  76.   ;;  Figure defaults, for initial dialogue and when returning from object
  77.   ;;  selection or point picking.
  78.   ;;
  79.   (defun defaults()
  80.     (if bname
  81.       (set_tile "bname" bname)
  82.     )
  83.     (if (= 0 retain)
  84.       (set_tile "retain" "0")
  85.       (progn
  86.         (set_tile "retain" "1")
  87.         (setq retain 1)
  88.       )
  89.     )
  90.     (if (= 1 unnamed)
  91.       (progn
  92.         (mode_tile "bname" 1)
  93.         (set_tile "unnamed" "1")
  94.       )
  95.     )
  96.     (if x_pt
  97.       (set_tile "x_pt" x_pt)
  98.       (progn
  99.         (set_tile "x_pt" (rtos 0.0000 2))
  100.         (setq x_pt (rtos 0.0000 2))
  101.       )
  102.     )
  103.     (if y_pt
  104.       (set_tile "y_pt" y_pt)
  105.       (progn
  106.         (set_tile "y_pt" (rtos 0.0000 2))
  107.         (setq y_pt (rtos 0.0000 2))
  108.       )
  109.     )
  110.     (if z_pt
  111.       (set_tile "z_pt" z_pt)
  112.       (progn
  113.         (set_tile "z_pt" (rtos 0.0000 2))
  114.         (setq z_pt (rtos 0.0000 2))
  115.       )
  116.     )
  117.     (set_tile "how_many"
  118.               (if (/= selection_set nil)
  119.                 (rtos (sslength selection_set) 2 0)
  120.                 "0"
  121.               )
  122.     )
  123.   )
  124.   ;;
  125.   ;;  X coordinate action.
  126.   ;;
  127.   (defun do_x_pt()
  128.     (check_real (setq x_pt (get_tile "x_pt")) "x_pt")   ; if valid input
  129.   )
  130.   ;;
  131.   ;;  Y coordinate action.
  132.   ;;
  133.   (defun do_y_pt()
  134.     (check_real (setq y_pt (get_tile "y_pt")) "y_pt")   ; if valid input
  135.   )
  136.   ;;
  137.   ;;  Z coordinate action.
  138.   ;;
  139.   (defun do_z_pt()
  140.     (check_real (setq z_pt (get_tile "z_pt")) "z_pt")   ; if valid input
  141.   )
  142.   ;;
  143.   ;; Reset the error tile to null.
  144.   ;;
  145.   (defun rs_error()
  146.     (set_tile "error" "")
  147.   )
  148.   ;;
  149.   ;;  This function checks the validity of the coordinates.  It returns the
  150.   ;;  real number or nil.
  151.   ;;
  152.   (defun check_real (real_number coord)
  153.     (if (distof real_number 2)
  154.       (progn
  155.         (rs_error)
  156.         real_number
  157.       )
  158.       (progn
  159.         (set_tile "error"
  160.                   (strcat (strcase (substr coord 1 1))
  161.                           " «y╝╨╡L«─íC"
  162.                   )
  163.         )
  164.         nil
  165.       )
  166.     )
  167.   )
  168.   ;;
  169.   ;;  This function checks the validity of the Block name.  If legitimate, the
  170.   ;;  Block name is returned, nil otherwise.
  171.   ;;
  172.   (defun check_name(name)
  173.     (if (wcmatch name "*[] `#`@`.`?`*`~`[`,`'!%^&()+={}|`\\:;\"<>/]*")
  174.       (progn
  175.         (set_tile "error" "íu╣╧╕sªW║┘ívññª│╡L«─ªrñ╕íC")
  176.         nil
  177.       )
  178.       (progn
  179.         (rs_error)
  180.         name
  181.       )
  182.     )
  183.   )
  184.   ;;
  185.   ;;  This function is called on OK in the main dialogue.  It confirms that all
  186.   ;;  input is correct and whether the block name already exists...
  187.   ;;
  188.   (defun bexist()
  189.     (setq reference 0)
  190.     (cond
  191.       ;; Check each coordinate.
  192.       ((not (check_real x_pt "x_pt")))
  193.       ((not (check_real y_pt "y_pt")))
  194.       ((not (check_real z_pt "z_pt")))
  195.  
  196.       ;; If block name is null, give message.
  197.       ((and (/= 1 unnamed)
  198.             (or (not bname) (= "" bname))
  199.        )
  200.         (set_tile "error" "ñúñ╣│\íuNull ╣╧╕sªW║┘ívíC")
  201.       )
  202.  
  203.       ;; Self-referencing check, check_ref returns T on a self reference.
  204.       ((and selection_set (check_ref)))
  205.  
  206.       ;; If the name exists, question via dialogue to overwrite it.
  207.       ((and (member bname table_list) (/= 1 unnamed))
  208.         (if (not (new_dialog "bname_exists" dcl_id)) (exit))
  209.         (action_tile "yes" "(done_dialog 2)")
  210.         (action_tile "no" "(done_dialog 0)")
  211.         (if (= (start_dialog) 2) (done_dialog 2))
  212.       )
  213.  
  214.       ;; If a new named block, check validity of name.
  215.       ((/= unnamed 1)
  216.         (if (check_name bname)
  217.           (done_dialog 2)
  218.           T
  219.         )
  220.       )
  221.       ;; If unnamed, just make it.
  222.       ((= 1 unnamed) (done_dialog 2))
  223.       ;; if none of above then error.
  224.       (t (princ "\níubexist()ívññª│╡{ªíñW¬║┐∙╗~íC"))
  225.     )
  226.   )
  227.   ;;
  228.   ;; Check to see if the block contains a self reference.
  229.   ;;
  230.   (defun check_ref (/ ref)
  231.     (setq a 0)
  232.     (setq self_list '())
  233.     ;; make a list of all insert entities in the selection set.
  234.     (while (< a (setq ss_length (sslength selection_set)))
  235.       (if (= (cdr (assoc '0 (entget (ssname selection_set a)))) "INSERT")
  236.         (setq self_list
  237.               (cons (cdr (assoc '2 (entget (ssname selection_set a))))
  238.                     self_list
  239.               )
  240.         )
  241.       )
  242.       (setq a (1+ a))
  243.     )
  244.     (cond
  245.       ;; if bname is in the selection set, report error.
  246.       ((and self_list (member bname self_list))
  247.         (set_tile "error" "┐∙╗~ í╨ ª╣╣╧╕síuª█┼Θ░╤ª╥ívíC")
  248.       )
  249.       ;;
  250.       ((and self_list (self_ref bname self_list))
  251.         (set_tile "error" "┐∙╗~ í╨ ª╣╣╧╕síuª█┼Θ░╤ª╥ívíC")
  252.       )
  253.       (t nil)
  254.     )
  255.   )
  256.   ;;
  257.   ;; This recursive function takes two arguments, a Block name and a list of
  258.   ;; Block names.  It checks to see whether any of the Blocks in the list
  259.   ;; contain  a reference to the first argument.  Recursion is used to take
  260.   ;; care of possible nested references.  Candidate for rainy day optimisation.
  261.   ;;
  262.   (defun self_ref (self others / ref other_list)
  263.     (setq other_list '())
  264.     (foreach n others
  265.       (setq en1 (cdr (assoc '-2 (tblsearch "block" n)))) ; first entity
  266.       (while en1
  267.         (if (and (= "INSERT" (cdr (assoc '0 (entget en1))))
  268.                  (not (member
  269.                         (setq other_name (cdr (assoc '2 (entget en1))))
  270.                         others
  271.                       )
  272.                  )
  273.              )
  274.           (setq other_list (cons other_name other_list))
  275.         )
  276.         (setq en1 (entnext en1))
  277.       )
  278.       (if (and other_list
  279.                (member self other_list)
  280.           )
  281.         (setq ref t)
  282.         (self_ref self other_list)
  283.       )
  284.     )
  285.     ref                              ; return t on a self reference, else nil.
  286.   )
  287.   ;;
  288.   ;; This function, when passed a symbol table name, returns a list of
  289.   ;; entries in that table.
  290.   ;;
  291.   (defun get_table (table_name)
  292.     (setq table_item (tblnext table_name T))
  293.     (setq table_list '())
  294.     (while (and table_item)
  295.       (setq just_name (cdr (assoc 2 table_item)))
  296.       (setq table_list (cons just_name table_list))
  297.       (setq table_item (tblnext table_name))
  298.     )
  299.   )
  300.   ;;
  301.   ;; Displays a nested dialogue containing an edit box for wildcards and
  302.   ;; a list box of the associated blocks in the drawing.
  303.   ;;
  304.   (defun list_blocks()
  305.     (setq bl_match '())
  306.     (if (not (new_dialog "list_blocks" dcl_id)) (exit))
  307.     (if (not pat) (setq pat "*"))
  308.     (set_tile "pattern" pat)
  309.     (pat_match pat)
  310.     (action_tile "bl_match" "(set_tile \"bl_match\" \"\")")
  311.     (action_tile "pattern" "(pat_match (setq pat (strcase $value)))")
  312.     (action_tile "accept" "(done_dialog 0)")
  313.     (start_dialog)
  314.   )
  315.   ;;
  316.   ;; This function displays the block list based on the pattern.
  317.   ;;
  318.   (defun pat_match (pat)
  319.     (setq bl_match '())
  320.     (foreach n table_list
  321.       (if (wcmatch n pat)
  322.         (setq bl_match (cons n bl_match))
  323.       )
  324.     )
  325.     (if (>= (getvar "maxsort") (length bl_match)) ; Alphabetise the list
  326.       (setq bl_match (sort bl_match))             ; in accordance with maxsort
  327.     )
  328.     (start_list "bl_match")
  329.     (mapcar 'add_list bl_match)
  330.     (end_list)
  331.   )
  332.   ;;
  333.   ;; Alphabetize a list.
  334.   ;;
  335.   (defun sort (list1 / item1 item2)
  336.     (setq item1 (car list1))
  337.     (foreach item2 (cdr list1)
  338.       (if (> item2 item1)
  339.         (setq item1 item2)
  340.       )
  341.     )
  342.     (if list1
  343.       (append
  344.         (sort
  345.           (append  (cdr (member item1 list1))
  346.                    (cdr (member item1 (reverse list1))))
  347.         )
  348.         (list item1)
  349.       )
  350.     )
  351.   )
  352.   ;;
  353.   ;; Routine that makes the block.
  354.   ;;
  355.   (defun entmake_block()
  356.     (setq a 0)
  357.     (setq att 0)
  358.  
  359.     ;; Check selection set for an ATTDEF.
  360.     (if selection_set
  361.       (while (< a (sslength selection_set))
  362.         (if (= "ATTDEF" (cdr (assoc '0 (entget (ssname selection_set a)))))
  363.           (setq att 1 a (+ (sslength selection_set) a))
  364.         )
  365.       (setq a (1+ a))
  366.     ))
  367.  
  368.     ;; Set header_name and 70 flag depending on named/unnamed and whether an
  369.     ;; ATTDEF exists.
  370.     (cond
  371.       ((= unnamed 1)
  372.         (setq header_name "*U")
  373.         (if (= 1 att) (setq flag70 (+ 1 2)) (setq flag70 1))
  374.       )
  375.       ((setq header_name bname)
  376.         (if (= 1 att) (setq flag70 (+ 64 2)) (setq flag70 64))
  377.       )
  378.     )
  379.     ;; Block header information.
  380.     (setq header (list
  381.       (cons 0 "block")
  382.       (cons 2 header_name)
  383.       (cons 70 flag70)
  384.       (cons 3 "")
  385.       (list 10 0.0 0.0 0.0)
  386.     ))
  387.     (setq a 0)
  388.  
  389.     ;; Start (entmake)ing the entities...
  390.     (if (entmake header)
  391.       (progn
  392.         (if selection_set
  393.           (while (< a (sslength selection_set))
  394.             (ent_copy (ssname selection_set a)
  395.                       (- (atof x_pt))
  396.                       (- (atof y_pt))
  397.                       (- (atof z_pt)))
  398.             (setq a (1+ a))
  399.           )
  400.         )
  401.       )
  402.     )
  403.     (entmake (list (cons 0 "endblk")))    ; Entmake the block end.
  404.  
  405.     (if (= 0 retain)              ; Delete entities after entmake is sucessful.
  406.       (progn
  407.         (setq a 0)
  408.         (if selection_set
  409.           (while (< a (sslength selection_set))
  410.             (entdel (ssname selection_set a))
  411.             (setq a (1+ a))
  412.           )
  413.         )
  414.       )
  415.     )
  416.   )
  417.   ;;
  418.   ;; Routine that copies an entity to a new location.  Pass the ename and the
  419.   ;; X, Y, and Z coordinates of the displacement vector and a new entity is
  420.   ;; created.
  421.   ;;
  422.   (defun ent_copy(ent x2 y2 z2)
  423.     (setq ent_type (cdr (assoc 0 (entget ent))))
  424.     (setq ent_list (cdr (entget ent (list "*"))))      ; don't forget the xdata.
  425.  
  426.     ;; A cond with two choices, a complex entity or a regular entity.
  427.     (cond
  428.       ;; Complex entities like Polyline and Insert with attributes.
  429.       ((or (= "POLYLINE" ent_type)
  430.            (and (= "INSERT" ent_type) (= 1 (cdr (assoc '66 ent_list))))
  431.        )
  432.         (if (= "POLYLINE" ent_type)
  433.           (entmake ent_list)     ; Make polyline header with no changes.
  434.           (entmake               ; Insert needs it's 10 group updated.
  435.             (subst (mapcar '+ (list 0 x2 y2 z2) (assoc 10 ent_list))
  436.                    (assoc 10 ent_list)
  437.                    ent_list
  438.             )
  439.           )
  440.         )
  441.         (while (/= "SEQEND" (cdr (assoc '0 (entget (entnext ent)))))
  442.           (entmake
  443.             (subst (mapcar '+ (list 0 x2 y2 z2)
  444.                               (assoc 10 (cdr (entget (entnext ent))))
  445.                    )
  446.                    (assoc 10 (cdr (entget (entnext ent))))
  447.                    (cdr (entget (entnext ent)))
  448.             )
  449.           )
  450.           (setq ent (entnext ent))
  451.         )
  452.         (entmake '((0 . "SEQEND")))
  453.       )
  454.       (t
  455.         (foreach n '(10 11 12 13 14 15 16)
  456.           (if (assoc n ent_list)
  457.             (progn
  458.               (setq ent_list
  459.                     (subst (mapcar '+ (list 0 x2 y2 z2) (assoc n ent_list));new
  460.                            (assoc n ent_list)                              ;old
  461.                            ent_list                                       ;list
  462.                     )
  463.               )
  464.             )
  465.           )
  466.         )
  467.         (entmake ent_list)              ; make the copy
  468.       )
  469.     )
  470.   )
  471.   (if (< (setq dcl_id (load_dialog "bmake.dcl")) 0) (exit))
  472.   (setq olderr  *error*
  473.         *error* bmake_error)
  474.   (get_table "block")                ; Make a list of blocks in the drawing.
  475.   (setq what_next 5)
  476.   (while (< 2 what_next)             ; Start the dialogue.
  477.     (if (not (new_dialog "bmake" dcl_id)) (exit))
  478.     ;; Set up defaults, for initial load and when returning from object
  479.     ;; selection or point picking.
  480.     (defaults)
  481.     (if (= 5 what_next) (mode_tile "bname" 2)) ; set focus to block name.
  482.     ;; Define what happens when each control is picked.  Mode_tile is
  483.     ;; used to set focus to the next relevant action, cuts down mouse
  484.     ;; handling in the dialogue.
  485.     (action_tile "bname"       "(do_bname)")
  486.     (action_tile "unnamed"     "(do_unnamed)")
  487.     (action_tile "pick_pt"     "(done_dialog 4)")
  488.     (action_tile "x_pt"        "(do_x_pt)")
  489.     (action_tile "y_pt"        "(do_y_pt)")
  490.     (action_tile "z_pt"        "(do_z_pt)")
  491.     (action_tile "sel_objs"    "(done_dialog 3)")
  492.     (action_tile "list_blocks" "(list_blocks)")
  493.     (action_tile "retain"      "(setq retain (atoi $value))")
  494.     (action_tile "accept"      "(bexist)")
  495.     (action_tile "cancel"      "(done_dialog 0)")
  496.     (action_tile "help"        "(do_help \"block\")")
  497.  
  498.     (setq what_next (start_dialog))   ; Throw up the dialogue.
  499.  
  500.     (cond                                   ; Decide what to do next.
  501.       ;; If select objects was picked...
  502.       ((= what_next 3)
  503.         (setq selection_set
  504.               ;; disallow viewports and shapes as these cannot be (entmake)d
  505.               ;; currently.
  506.               (ssget '((-4 . "<AND")
  507.                          (-4 . "<NOT")(0 . "VIEWPORT")(-4 . "NOT>")
  508.                          (-4 . "<NOT")(0 . "SHAPE")(-4 . "NOT>")
  509.                       (-4 . "AND>"))
  510.               )
  511.         )
  512.         (setq ssflag 1)
  513.         (rs_error)
  514.       )
  515.       ;; If base point was picked...
  516.       ((= what_next 4)
  517.         (initget 1)
  518.         (setq pick_pt (getpoint "┤íñ▐íu░≥╖╟┬Iív: "))
  519.         (setq x_pt (rtos (car pick_pt) 2 4))
  520.         (setq y_pt (rtos (cadr pick_pt) 2 4))
  521.         (setq z_pt (rtos (caddr pick_pt) 2 4))
  522.       )
  523.     )
  524.   )
  525.   ;; If OK was picked.
  526.   (if (= what_next 2)
  527.        (entmake_block)
  528.   )
  529.   (setq *error* olderr)
  530.   (princ)
  531. )
  532.  
  533. ;;;---------------------------------------------------------------------------;
  534. ;;; This is printed on loading.
  535. ;;;---------------------------------------------------------------------------;
  536. (princ "\níuC:BMAKEívñw╕ⁿñJ; ╜╨ÑH BMAKE ▒╥░╩½ⁿÑOíC")
  537. (princ)
  538.  
  539.