home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p037 / cr12_5.ddi / R11SUPP.EXE / XPLODE.LSP < prev    next >
Encoding:
Lisp/Scheme  |  1990-12-03  |  15.8 KB  |  538 lines

  1. ;;;--------------------------------------------------------------------------;
  2. ;;; XPLODE.LSP                                  
  3. ;;;   Copyright (C) 1990 by Autodesk, Inc.
  4. ;;;  
  5. ;;;   Permission to use, copy, modify, and distribute this software and its
  6. ;;;   documentation for any purpose and without fee is hereby granted, provided
  7. ;;;   that the above copyright notice appear in all copies and that both that
  8. ;;;   copyright notice and this permission notice appear in supporting
  9. ;;;   documentation.  This software is provided "as is" without express or
  10. ;;;   implied warranty.
  11. ;;;
  12. ;;;   Jan S. Yoder & K.C. Jones   Version 1.0                            
  13. ;;;
  14. ;;; --------------------------------------------------------------------------;
  15. ;;; DESCRIPTION
  16. ;;;
  17. ;;;
  18. ;;;   This is a replacement for the EXPLODE command in AutoCAD.  It allows
  19. ;;;   you to control all of the attributes of the comonent entities of a
  20. ;;;   block or set of blocks while exploding them.  There are several major
  21. ;;;   differences between XPlode and the EXPLODE command in AutoCAD.
  22. ;;;   
  23. ;;;   First, you can select as many entities as you wish; all dimensions,
  24. ;;;   polyline and polymeshes, and block insertions will be extracted from
  25. ;;;   your selection set, and you will be asked to XPlode them either
  26. ;;;   globally or individually.  If you chose to explode them globally, you
  27. ;;;   will see the following prompt for all of the candidate entities:
  28. ;;;   
  29. ;;;     All/Color/LAyer/LType/Inherit from parent block/<Explode>: 
  30. ;;;   
  31. ;;;   If, on the other hand, you elect to operate on them individually, you
  32. ;;;   will need to make a selection from this prompt for each entity.
  33. ;;;   
  34. ;;;   Second, the EXPLODE command in AutoCAD does not allow you to specify
  35. ;;;   any of the attributes for the component entities when you explode a
  36. ;;;   block.  Nor does it allow you to let the component entities inherit
  37. ;;;   the attributes of the parent block.
  38. ;;;   
  39. ;;;   
  40. ;;;   ALL
  41. ;;;   
  42. ;;;   This option allows you to specify a color, linetype, and layer for the
  43. ;;;   new entities.
  44. ;;;   
  45. ;;;   COLOR
  46. ;;;   
  47. ;;;   This option prompts you for a new color for the component entities.
  48. ;;;   
  49. ;;;     New color for exploded entities.
  50. ;;;     Red/Yellow/Green/Cyan/Blue/Magenta/White/BYLayer/BYBlock/<cecolor>:
  51. ;;;   
  52. ;;;   You may enter any color number from 1 through 255, or one of the 
  53. ;;;   standard color names listed.  "Cecolor" is the current entity color
  54. ;;;   from the CECOLOR system variable.
  55. ;;;   
  56. ;;;   LAYER
  57. ;;;   
  58. ;;;   This option prompts you to enter the name of the layer on which you 
  59. ;;;   want the component entities to be placed.
  60. ;;;   
  61. ;;;     XPlode onto what layer?  <clayer>:
  62. ;;;   
  63. ;;;   The layer name entered is verified and if it does not exist you are
  64. ;;;   reprompted for a layer name.  Pressing RETURN causes the current 
  65. ;;;   layer to be used.
  66. ;;;   
  67. ;;;   LTYPE
  68. ;;;   
  69. ;;;   This option lists all of the loaded linetypes in the current drawing,
  70. ;;;   and prompts you to choose one of them.  You must type the entire 
  71. ;;;   linetype name (sorry), or you may press RETURN to use the current one.
  72. ;;;   
  73. ;;;     Choose from the following list of linetypes.
  74. ;;;     CONTinuous/...others.../<CONTINUOUS>:
  75. ;;;   
  76. ;;;   INHERIT
  77. ;;;   
  78. ;;;   Inherit from parent block means that the attributes of the block
  79. ;;;   being XPloded will be the attributes of component entities.  No other
  80. ;;;   choices are required.
  81. ;;;   
  82. ;;;   EXPLODE
  83. ;;;   
  84. ;;;   This option issues the current EXPLODE command for each of the entities
  85. ;;;   in the selection set.
  86. ;;;   
  87. ;;; --------------------------------------------------------------------------;
  88.  
  89. ;;; ------------------------ INTERNAL ERROR HANDLER --------------------------;
  90.  
  91. (defun xp_err (s)                     ; If an error (such as CTRL-C) occurs
  92.   ;; while this command is active...
  93.   (if (/= s "Function cancelled") 
  94.     (princ (strcat "\nError: " s))
  95.   ) 
  96.   (if xp_oce (setvar "cmdecho" xp_oce)) ; restore old cmdecho value
  97.   (setq *error* olderr)               ; restore old *error* handler
  98.   (princ)
  99.  
  100. ;;; ---------------------------- COMMON FUNCTION -----------------------------;
  101.  
  102. (defun xp_val (n e f) 
  103.   (if f ; if f then e is an entity list.
  104.     (cdr (assoc n e))
  105.     (cdr (assoc n (entget e)))
  106.   )
  107.  
  108. ;;; ------------------------- GET ENTITY TO EXPLODE --------------------------;
  109. ;;; ---------------------------- MAIN PROGRAM --------------------------------;
  110.  
  111. (defun explode ( / oce ohl e0 en e1 s0) 
  112.  
  113.   ;; Version number.  Reset this local if you make a change.
  114.   (setq xp_ver "1.00a")  
  115.   
  116.   (setq xp_oer *error* 
  117.         *error* xp_err)
  118.   (setq xp_oce (getvar "cmdecho"))    ; save value of cmdecho
  119.   (setvar "cmdecho" 0)                ; turn cmdecho off
  120.  
  121.   (graphscr)
  122.   (princ (strcat "\nXPlode, Version " xp_ver ", (c) 1990 by Autodesk, Inc. "))
  123.  
  124.   (princ "\nSelect entities to XPlode. ")
  125.   (setq ss (ssget))
  126.  
  127.   (if ss
  128.     (progn
  129.       ;; Sort out any entities not explodeable...
  130.       (setq ss (xp_sxe)) ; DLine_Sort_Xplodable_Entities
  131.     
  132.       ;; XPlode Individually or Globally?
  133.     
  134.       (if (> (sslength ss) 0)
  135.         (progn
  136.           (if (> (sslength ss) 1)
  137.             (progn
  138.               (initget "Individually Globally")
  139.               (setq ans (getkword "\n\nXPlode Individually/<Globally>: "))
  140.             )
  141.             (setq ans "Globally")
  142.           )
  143.         
  144.         
  145.           (cond
  146.             ((= ans "Individually")
  147.               (setq sslen (sslength ss)
  148.                     j    0
  149.               )
  150.               (while (< j sslen)
  151.                 (setq temp  (ssname ss j)
  152.                       prmpt T
  153.                 )
  154.         
  155.                 (redraw temp 3)
  156.                 (setq typ (xp_gxt))
  157.                 (xp_xpe temp typ)
  158.                 (redraw temp 4)
  159.                 (setq j (1+ j))
  160.               )
  161.             )
  162.             (T
  163.               (setq sslen (sslength ss)
  164.                     j     0
  165.                     ans   "Globally"
  166.                     prmpt T
  167.               )
  168.               (setq typ (xp_gxt))
  169.               (while (< j sslen)
  170.                 (setq temp (ssname ss j))
  171.                 (xp_xpe temp typ)
  172.                 (setq j (1+ j))
  173.               )
  174.             )
  175.           )
  176.         )
  177.       )
  178.     )
  179.   )
  180.   
  181.   (if xp_oce (setvar "cmdecho" xp_oce)) ; restore old cmdecho value
  182.   (setq *error* xp_err)               ; restore old *error* handler
  183.   (prin1)
  184. ;;;
  185. ;;; Sort out all of the entities which can be exploded from the selection
  186. ;;; set.  Also ensure that block insertions have equal X, Y and Z scale factors.
  187. ;;;
  188. ;;; xp_sxe == DLine_Sort_Xplodable_Entities
  189. ;;;
  190. (defun xp_sxe (/ temp bad)
  191.  
  192.   (setq sslen (sslength ss)
  193.         j     0
  194.         ss1   (ssadd)
  195.   )
  196.   (while (< j sslen)
  197.     (setq temp (ssname ss j))
  198.     (setq j (1+ j))
  199.     (if (member (xp_val 0 temp nil) '("INSERT" "DIMENSION" "POLYLINE"))
  200.       (if (= (xp_val 0 temp nil) "INSERT")
  201.         (if (and (= (xp_val 41 temp nil) (xp_val 42 temp nil)) 
  202.                  (= (xp_val 41 temp nil) (xp_val 43 temp nil))
  203.             )
  204.           (ssadd temp ss1)
  205.         )
  206.         (ssadd temp ss1)
  207.       )
  208.     )
  209.   )
  210.   (setq sslen (sslength ss)
  211.         bad (sslength ss1)
  212.   )
  213.   (princ "\n")
  214.   (princ sslen)
  215.   (princ " entities found.  ")
  216.   (if (> (- sslen bad) 0)
  217.     (progn
  218.       (princ (- sslen bad))
  219.       (princ " invalid. ")
  220.     )
  221.   )
  222.   ss1
  223. )
  224. ;;;
  225. ;;; Set the type of explode to do.
  226. ;;;
  227. ;;; xp_gxt == XPlode_Get_Xplode_Type
  228. ;;;
  229. (defun xp_gxt (/ temp)
  230.   
  231.   (initget "All Color LAyer LType Inherit Explode")
  232.   (setq temp (getkword
  233.     "\n\nAll/Color/LAyer/LType/Inherit from parent block/<Explode>: "))
  234.  
  235.   (if (or (= temp "") (null temp))
  236.     (setq temp "Explode")
  237.   )
  238.   temp
  239. )
  240. ;;;
  241. ;;; Do the explosion of an entity.
  242. ;;;
  243. ;;; xp_xpe == XPlode_XPlode_Entity
  244. ;;;
  245. (defun xp_xpe (ent typ /  )
  246.  
  247.   (cond 
  248.     ((= typ "All")
  249.       (if prmpt
  250.         (progn
  251.           (setq color (xp_scn))
  252.           (setq ltype (xp_slt))
  253.           (setq layer (xp_sla))
  254.           (setq prmpt nil)
  255.         )
  256.       )
  257.  
  258.       (xp_xfa)
  259.       (if (or (= ans "Individually") (= j (1- sslen)))
  260.         (progn
  261.           (if (and (> sslen 1) (= ans "Globally"))
  262.             (princ "\nEntities ")
  263.             (princ "\nEntity ")
  264.           )
  265.           (princ (strcat "exploded with color of " 
  266.                          (if (= (type color) 'INT) (itoa color) color) ", "
  267.                          "linetype of " ltype ", "
  268.                          "and layer " layer "."))
  269.         )
  270.       )
  271.     )
  272.     ((= typ "Color")
  273.       (if prmpt
  274.         (progn
  275.           (setq color (xp_scn))
  276.           (setq ltype (getvar "celtype"))
  277.           (setq layer (getvar "clayer"))
  278.           (setq prmpt nil)
  279.         )
  280.       )
  281.  
  282.       (xp_xfa)
  283.       (if (or (= ans "Individually") (= j (1- sslen)))
  284.         (progn
  285.           (if (and (> sslen 1) (= ans "Globally"))
  286.             (princ "\nEntities ")
  287.             (princ "\nEntity ")
  288.           )
  289.           (princ (strcat "exploded with color of  " 
  290.                          (if (= (type color) 'INT) (itoa color) color) ".")) 
  291.         )
  292.       )
  293.     )
  294.     ((= 
  295.     typ "LAyer")
  296.       (if prmpt
  297.         (progn
  298.           (setq color (getvar "cecolor"))
  299.           (setq ltype (getvar "celtype"))
  300.           (setq layer (xp_sla))
  301.           (setq prmpt nil)
  302.         )
  303.       )
  304.  
  305.       (xp_xfa)
  306.       (if (or (= ans "Individually") (= j (1- sslen)))
  307.         (progn
  308.           (if (and (> sslen 1) (= ans "Globally"))
  309.             (princ "\nEntities ")
  310.             (princ "\nEntity ")
  311.           )
  312.           (princ (strcat "exploded onto layer " layer ".")) 
  313.         )
  314.       )
  315.     )
  316.     ((= typ "LType")
  317.       (if prmpt
  318.         (progn
  319.           (setq color (getvar "cecolor"))
  320.           (setq ltype (xp_slt))
  321.           (setq layer (getvar "clayer"))
  322.           (setq prmpt nil)
  323.         )
  324.       )
  325.  
  326.       (xp_xfa)
  327.       (if (or (= ans "Individually") (= j (1- sslen)))
  328.         (progn
  329.           (if (and (> sslen 1) (= ans "Globally"))
  330.             (princ "\nEntities ")
  331.             (princ "\nEntity ")
  332.           )
  333.           (princ (strcat "exploded with linetype of  " ltype ".")) 
  334.         )
  335.       )
  336.     )
  337.     ((= typ "Inherit")
  338.       (xp_iap ent)
  339.     )
  340.     (T
  341.       (command "explode" (xp_val -1 ent nil))
  342.     )
  343.   )
  344. )
  345. ;;;
  346. ;;; Force the color, linetype and layer attributes after exploding.
  347. ;;;
  348. ;;; xp_xea == XPlode_Xplode_Force_All
  349. ;;;
  350. (defun xp_xfa ()
  351.  
  352.   (setq e0 (entlast))
  353.   (setq en (entnext e0))
  354.   (while (not (null en))              ; find the last entity              
  355.     (setq e0 en)
  356.     (setq en (entnext e0))
  357.   ) 
  358.       
  359.   (command "explode" (xp_val -1 ent nil)) ; explode the entity
  360.  
  361.   (setq s0 (ssadd))
  362.   
  363.   (while (entnext e0) 
  364.     (ssadd (setq e0 (entnext e0))
  365.            s0
  366.     )
  367.   ) 
  368.   (command "chprop" s0 ""             ; change entities to the proper layer
  369.            "c"  color                 ; color, and linetype, regardless
  370.            "lt" ltype                 ; of their extrusion direction
  371.            "la" layer
  372.            ""
  373.   ) 
  374. )
  375. ;;;
  376. ;;; Inherit attributes (if BYBLOCK) from parent.
  377. ;;;
  378. ;;; xp_iap == XPlode_Inherit_Attributes_from_Parent
  379. ;;;
  380. (defun xp_iap (t1 / t1cl t1lt t1ly s0ly s0lt s0cl t0e)
  381.   (setq t0 (entlast))
  382.   (setq tn (entnext t0))
  383.   (while (not (null tn))              ; find the last entity              
  384.     (setq t0 tn)
  385.     (setq tn (entnext t0))
  386.   ) 
  387.       
  388.   (setq t1cl (xp_val 62 t1 nil))      ; record the attributes of the block
  389.   (setq t1lt (xp_val 6  t1 nil))
  390.   (setq t1ly (xp_val 8  t1 nil))
  391.   (command "explode" (xp_val -1 t1 nil)) ; explode the entity
  392.   (setq s0ly (ssadd))                 ; create nil selection sets for layer
  393.   (setq s0lt (ssadd))                 ; linetype and color changes
  394.   (setq s0cl (ssadd))
  395.   (setq t0 (entnext t0))
  396.   (while t0                           ; can exploded entities
  397.     (setq t0e (entget t0))            ; and build selection sets
  398.     
  399.     (if (=  (xp_val 62 t0e T) "BYBLOCK") (ssadd t0 s0cl))
  400.     (if (=  (xp_val 6  t0e T) "BYBLOCK") (ssadd t0 s0lt))
  401.     (if (=  (xp_val 8  t0e T) "0")       (ssadd t0 s0ly))
  402.     (setq t0 (entnext t0))
  403.   )
  404.   (if (> (sslength s0cl) 0)           ; is selection set non-nil...
  405.       (command "chprop" s0cl ""       ; Change exploded entities with color
  406.                "co" t1cl "")          ; BYBLOCK to color of old block
  407.   )
  408.   (if (> (sslength s0lt) 0)
  409.       (command "chprop" s0lt ""       ; Change exploded entities with linetype
  410.                "lt" t1lt "")          ; BYBLOCK to linetype of old block
  411.   )
  412.   (if (> (sslength s0ly) 0)
  413.       (command "chprop" s0ly ""       ; Change exploded entities with linetype
  414.                "la" t1ly "")          ; BYBLOCK to linetype of old block
  415.   )
  416.   (if (or (= ans "Individually") (= j (1- sslen)))
  417.     (progn
  418.       (if (and (> sslen 1) (= ans "Globally"))
  419.         (princ "\nEntities ")
  420.         (princ "\nEntity ")
  421.       )
  422.       (princ "exploded.") 
  423.     )
  424.   )
  425. )
  426.  
  427. ;;;
  428. ;;; Set the color for the exploded entities.
  429. ;;;
  430. ;;; xp_scn == XPlode_Set_Color_Number
  431. ;;;
  432. (defun xp_scn ()
  433.   (setq arg 257)
  434.   (while (> arg 256)
  435.     (initget 2 "Red Yellow Green Cyan Blue Magenta White BYLayer BYBlock")
  436.     (setq arg (getint (strcat
  437.       "\n\nNew color for exploded entities.  "
  438.       "\nRed/Yellow/Green/Cyan/Blue/"
  439.       "Magenta/White/BYLayer/BYBlock/<"
  440.       (if (= (type (getvar "cecolor")) 'INT)
  441.         (itoa (getvar "cecolor")) 
  442.         (getvar "cecolor")
  443.       ) 
  444.       ">: ")))
  445.     (cond
  446.       ((= arg "BYBlock") (setq arg 0))
  447.       ((= arg "Red")     (setq arg 1))
  448.       ((= arg "Yellow")  (setq arg 2))
  449.       ((= arg "Green")   (setq arg 3))
  450.       ((= arg "Cyan")    (setq arg 4))
  451.       ((= arg "Blue")    (setq arg 5))
  452.       ((= arg "Magenta") (setq arg 6))
  453.       ((= arg "White")   (setq arg 7))
  454.       ((= arg "BYLayer") (setq arg 256))
  455.       (T
  456.         (if (= (type arg) 'INT)
  457.           (if (> arg 255)
  458.             (progn
  459.               (princ "\nColor number out of range 1 - 255. ")
  460.               (setq arg 257) ; kludge
  461.             )
  462.           )
  463.           (setq arg (if (= (type (setq arg (getvar "cecolor"))) 'INT)
  464.                       (getvar "cecolor") 
  465.                       (cond
  466.                         ((= arg "BYBLOCK") (setq arg 0))
  467.                         ((= arg "BYLAYER") (setq arg 256))
  468.                       )
  469.                     )
  470.           )
  471.         )
  472.       )
  473.     )
  474.   )
  475.   (cond
  476.     ((= arg 0) (setq arg "BYBLOCK"))
  477.     ((= arg 256) (setq arg "BYLAYER"))
  478.   )
  479.   arg
  480. )
  481. ;;;
  482. ;;; Set the linetype from the loaded linetypes.
  483. ;;;
  484. ;;; xp_slt == XPlode_Set_Line_Type
  485. ;;;
  486. (defun xp_slt ()
  487.   (princ "\n\nChoose from the following list of linetypes. ")
  488.   (tblnext "ltype" T)
  489.   (setq xp_lta "CONTINUOUS,CONT BYLayer BYBlock"
  490.         xp_ltb "BYBlock/BYLayer/CONTinuous")
  491.  
  492.   (while (setq xp_lt (cdr(assoc 2 (tblnext "ltype"))))
  493.     (setq xp_lta (strcat xp_lta " " xp_lt)
  494.           xp_ltb (strcat xp_ltb "/" xp_lt))
  495.   )
  496.   (initget xp_lta)
  497.   (princ (strcat 
  498.     "\nEnter new linetype name. \n" xp_ltb "/<"
  499.     (getvar "celtype") "> : "))
  500.   (setq xp_nln (getkword) )
  501.   (if (or (= xp_nln nil) (= xp_nln ""))
  502.     (setq xp_nln (getvar "celtype"))
  503.   )
  504.   xp_nln
  505. )
  506. ;;;
  507. ;;; Set a layer if it exists.
  508. ;;;
  509. ;;; xp_sla == XPlode_Set_LAyer
  510. ;;;
  511. (defun xp_sla (/ temp)
  512.   (while (null temp)
  513.     (initget 1)
  514.     (setq temp (getstring (strcat
  515.       "\n\nXPlode onto what layer?  <" (getvar "clayer") ">: ")))
  516.     (if (or (= temp "") (null temp))
  517.       (setq temp (getvar "clayer"))
  518.       (if (not (tblsearch "layer" temp))
  519.         (progn
  520.           (princ "\nInvalid layer name. ")
  521.           (setq temp nil)
  522.         )
  523.       )
  524.     )
  525.   )
  526.   temp
  527. )
  528.  
  529. ;;; --------------------------------------------------------------------------;
  530. (defun c:xp       () (explode))
  531. (defun c:xplode   () (explode))
  532. (princ 
  533.   "\n\tC:XPlode loaded.  Start command with XP or XPLODE.")
  534. (princ)
  535.