1. ;;;往图块中加入图元
    2. (vl-load-com)
    3. (defun LM:AddObjectstoBlock ( doc block ss / lst mat )
    4. (setq lst (LM:ss->vla ss)
    5. mat (LM:Ref->Def block)
    6. mat (vlax-tmatrix (append (mapcar 'append (car mat) (mapcar 'list (cadr mat))) '((0. 0. 0. 1.))))
    7. )
    8. (foreach obj lst (vla-transformby obj mat))
    9. (vla-CopyObjects doc (LM:SafearrayVariant vlax-vbobject lst)
    10. (vla-item (vla-get-Blocks doc) (cdr (assoc 2 (entget block))))
    11. )
    12. (foreach obj lst (vla-delete obj))
    13. (vla-regen doc acAllViewports)
    14. )
    15. ;;-----------------=={ Remove From Block }==------------------;;
    16. ;; ;;
    17. ;; Removes an Entity from a Block Definition ;;
    18. ;;------------------------------------------------------------;;
    19. ;; Arguments: ;;
    20. ;; ent - Entity name of Object to Delete from Block [ENAME] ;;
    21. ;;------------------------------------------------------------;;
    22. (defun LM:RemovefromBlock ( doc ent )
    23. (vla-delete (vlax-ename->vla-object ent))
    24. (vla-regen doc acAllViewports)
    25. (princ)
    26. )
    27. ;;------------------=={ Safearray Variant }==-----------------;;
    28. ;; ;;
    29. ;; Creates a populated Safearray Variant of a specified ;;
    30. ;; data type ;;
    31. ;;------------------------------------------------------------;;
    32. ;; Author: Lee Mac, Copyright ?2011 - www.lee-mac.com ;;
    33. ;;------------------------------------------------------------;;
    34. ;; Arguments: ;;
    35. ;; datatype - variant type enum (eg vlax-vbDouble) ;;
    36. ;; data - list of static type data ;;
    37. ;;------------------------------------------------------------;;
    38. ;; Returns: VLA Variant Object of type specified ;;
    39. ;;------------------------------------------------------------;;
    40. (defun LM:SafearrayVariant ( datatype data )
    41. (vlax-make-variant
    42. (vlax-safearray-fill
    43. (vlax-make-safearray datatype (cons 0 (1- (length data)))) data
    44. )
    45. )
    46. )
    47. ;;------------=={ SelectionSet -> VLA Objects }==-------------;;
    48. ;; ;;
    49. ;; Converts a SelectionSet to a list of VLA Objects ;;
    50. ;;------------------------------------------------------------;;
    51. ;; Author: Lee Mac, Copyright ?2011 - www.lee-mac.com ;;
    52. ;;------------------------------------------------------------;;
    53. ;; Arguments: ;;
    54. ;; ss - Valid SelectionSet (Pickset) ;;
    55. ;;------------------------------------------------------------;;
    56. ;; Returns: List of VLA Objects, else nil ;;
    57. ;;------------------------------------------------------------;;
    58. (defun LM:ss->vla ( ss / i l )
    59. (if ss
    60. (repeat (setq i (sslength ss))
    61. (setq l (cons (vlax-ename->vla-object (ssname ss (setq i (1- i)))) l))
    62. )
    63. )
    64. )
    65. ;;---------------=={ Block Ref -> Block Def }==---------------;;
    66. ;; ;;
    67. ;; Returns the Transformation Matrix and Translation Vector ;;
    68. ;; for transforming Block Reference Geometry to the Block ;;
    69. ;; Definiton. ;;
    70. ;;------------------------------------------------------------;;
    71. ;; Author: Lee Mac, Copyright ?2011 - www.lee-mac.com ;;
    72. ;;------------------------------------------------------------;;
    73. ;; Arguments: ;;
    74. ;; e - Block Reference Entity ;;
    75. ;;------------------------------------------------------------;;
    76. ;; Returns: List of 3x3 Transformation Matrix, Vector ;;
    77. ;;------------------------------------------------------------;;
    78. (defun LM:Ref->Def ( e / _dxf a l n )
    79. (defun _dxf ( x l ) (cdr (assoc x l)))
    80. (setq l (entget e) a (- (_dxf 50 l)) n (_dxf 210 l))
    81. (
    82. (lambda ( m )
    83. (list m
    84. (mapcar '- (_dxf 10 (tblsearch "BLOCK" (_dxf 2 l)))
    85. (mxv m
    86. (trans (_dxf 10 l) n 0)
    87. )
    88. )
    89. )
    90. )
    91. (mxm
    92. (list
    93. (list (/ 1. (_dxf 41 l)) 0. 0.)
    94. (list 0. (/ 1. (_dxf 42 l)) 0.)
    95. (list 0. 0. (/ 1. (_dxf 43 l)))
    96. )
    97. (mxm
    98. (list
    99. (list (cos a) (sin (- a)) 0.)
    100. (list (sin a) (cos a) 0.)
    101. (list 0. 0. 1.)
    102. )
    103. (mapcar '(lambda ( e ) (trans e n 0 t))
    104. '(
    105. (1. 0. 0.)
    106. (0. 1. 0.)
    107. (0. 0. 1.)
    108. )
    109. )
    110. )
    111. )
    112. )
    113. )
    114. ;; Matrix x Vector - Vladimir Nesterovsky
    115. (defun mxv ( m v )
    116. (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
    117. )
    118. ;; Matrix x Matrix - Vladimir Nesterovsky
    119. (defun mxm ( m q )
    120. (mapcar (function (lambda ( r ) (mxv (trp q) r))) m)
    121. )
    122. ;; Matrix Transpose - Doug Wilson
    123. (defun trp ( m )
    124. (apply 'mapcar (cons 'list m))
    125. )
    126. ;;---------------------=={ Select if }==----------------------;;
    127. ;; ;;
    128. ;; Provides continuous selection prompts until either a ;;
    129. ;; predicate function is validated or a keyword is supplied. ;;
    130. ;;------------------------------------------------------------;;
    131. ;; Author: Lee Mac, Copyright ?2011 - www.lee-mac.com ;;
    132. ;;------------------------------------------------------------;;
    133. ;; Arguments: ;;
    134. ;; msg - prompt string ;;
    135. ;; pred - optional predicate function [selection list arg] ;;
    136. ;; func - selection function to invoke ;;
    137. ;; keyw - optional initget argument list ;;
    138. ;;------------------------------------------------------------;;
    139. ;; Returns: Entity selection list, keyword, or nil ;;
    140. ;;------------------------------------------------------------;;
    141. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))
    142. (while
    143. (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
    144. (cond
    145. ( (= 7 (getvar 'ERRNO))
    146. (princ "\nMissed, Try again.")
    147. )
    148. ( (eq 'STR (type sel))
    149. nil
    150. )
    151. ( (vl-consp sel)
    152. (if (and pred (not (pred sel)))
    153. (princ "\nInvalid Object Selected.")
    154. )
    155. )
    156. )
    157. )
    158. )
    159. sel
    160. )
    161. ;-------------------------------------------------------------;
    162. ; -- Test Functions -- ;
    163. ;-------------------------------------------------------------;
    164. (defun c:ado ( / *error* _StartUndo _EndUndo acdoc ss e )
    165. (princ "\n往图块中加入图元(测试版)(注意:会更新所有同名块)")
    166. (princ "\n请选取要加入到图块中的图元:")
    167. (defun *error* ( msg )
    168. (if acdoc (_EndUndo acdoc))
    169. (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
    170. (princ (strcat "\n** Error: " msg " **")))
    171. (princ)
    172. )
    173. (defun _StartUndo ( doc ) (_EndUndo doc)
    174. (vla-StartUndoMark doc)
    175. )
    176. (defun _EndUndo ( doc )
    177. (if (= 8 (logand 8 (getvar 'UNDOCTL)))
    178. (vla-EndUndoMark doc)
    179. )
    180. )
    181. (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
    182. (if
    183. (and
    184. (setq ss (ssget "_:L"))
    185. (setq e
    186. (LM:SelectIf "\n选择需要增加图元的块: "
    187. '(lambda ( x ) (eq "INSERT" (cdr (assoc 0 (entget (car x)))))) entsel nil
    188. )
    189. )
    190. )
    191. (progn
    192. (_StartUndo acdoc) (LM:AddObjectstoBlock acdoc (car e) ss) (_EndUndo acdoc)
    193. )
    194. )
    195. (princ)
    196. )
    197. ;-------------------------------------------------------------;
    198. (defun c:Remove ( / *error* _StartUndo _EndUndo acdoc e )
    199. (defun *error* ( msg )
    200. (if acdoc (_EndUndo acdoc))
    201. (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
    202. (princ (strcat "\n** Error: " msg " **")))
    203. (princ)
    204. )
    205. (defun _StartUndo ( doc ) (_EndUndo doc)
    206. (vla-StartUndoMark doc)
    207. )
    208. (defun _EndUndo ( doc )
    209. (if (= 8 (logand 8 (getvar 'UNDOCTL)))
    210. (vla-EndUndoMark doc)
    211. )
    212. )
    213. (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
    214. (while (setq e (car (nentsel "\nSelect Object to Remove: ")))
    215. (_StartUndo acdoc) (LM:RemovefromBlock acdoc e) (_EndUndo acdoc)
    216. )
    217. (princ)
    218. )
    219. (vl-load-com) (princ)
    220. ;;------------------------------------------------------------;;
    221. ;; End of File ;;
    222. ;;------------------------------------------------------------;;