明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: 自贡黄明儒

[讨论] 块增加对象

  [复制链接]
发表于 2015-6-19 14:15 | 显示全部楼层
回复,下载,学习!!!!!
发表于 2015-6-19 15:51 | 显示全部楼层
看看什么东西
发表于 2015-6-26 12:16 | 显示全部楼层
学习大师作品   
发表于 2015-6-26 12:33 | 显示全部楼层
看看东西先。
发表于 2015-6-26 12:41 | 显示全部楼层
学习一下。。。。。。。。。。。。。
发表于 2015-6-26 16:22 | 显示全部楼层
黄工又出新作了,哈哈哈,谢谢啦。
发表于 2015-6-27 11:23 | 显示全部楼层
块中添加对象LEE-MAC的修改,解决无法框选问题。
entmake方式有问题,会更改block的句柄组码,而vla方式不会,
CAD04 08 11测试通过
  1. ;;----------------=={ Add Objects to Block }==----------------;;
  2. ;;                                                            ;;
  3. ;;  Adds all objects in the provided SelectionSet to the      ;;
  4. ;;  definition of the specified block.                        ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  doc   - Document Object in which block resides.           ;;
  10. ;;  block - Entity name of reference insert                   ;;
  11. ;;  ss    - SelectionSet of objects to add to definition      ;;
  12. ;;------------------------------------------------------------;;

  13. (defun LM:AddObjectstoBlock ( doc block ss / lst mat )
  14.   
  15.   (setq lst (LM:ss->vla ss)
  16.         mat (LM:Ref->Def block)
  17.         mat (vlax-tmatrix (append (mapcar 'append (car mat) (mapcar 'list (cadr mat))) '((0. 0. 0. 1.))))
  18.   )
  19.   (foreach obj lst (vla-transformby obj mat))

  20.   (vla-CopyObjects doc (LM:SafearrayVariant vlax-vbobject lst)
  21.     (vla-item (vla-get-Blocks doc) (cdr (assoc 2 (entget block))))
  22.   )
  23.   (foreach obj lst (vla-delete obj))
  24.   (vla-regen doc acAllViewports)
  25. )

  26. ;;-----------------=={ Remove From Block }==------------------;;
  27. ;;                                                            ;;
  28. ;;  Removes an Entity from a Block Definition                 ;;
  29. ;;------------------------------------------------------------;;
  30. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  31. ;;------------------------------------------------------------;;
  32. ;;  Arguments:                                                ;;
  33. ;;  ent - Entity name of Object to Delete from Block [ENAME]  ;;
  34. ;;------------------------------------------------------------;;

  35. (defun LM:RemovefromBlock ( doc ent )
  36.   (vla-delete (vlax-ename->vla-object ent))
  37.   (vla-regen doc acAllViewports)
  38.   (princ)
  39. )

  40. ;;------------------=={ Safearray Variant }==-----------------;;
  41. ;;                                                            ;;
  42. ;;  Creates a populated Safearray Variant of a specified      ;;
  43. ;;  data type                                                 ;;
  44. ;;------------------------------------------------------------;;
  45. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  46. ;;------------------------------------------------------------;;
  47. ;;  Arguments:                                                ;;
  48. ;;  datatype - variant type enum (eg vlax-vbDouble)           ;;
  49. ;;  data     - list of static type data                       ;;
  50. ;;------------------------------------------------------------;;
  51. ;;  Returns:  VLA Variant Object of type specified            ;;
  52. ;;------------------------------------------------------------;;
  53.                         
  54. (defun LM:SafearrayVariant ( datatype data )
  55.   (vlax-make-variant
  56.     (vlax-safearray-fill
  57.       (vlax-make-safearray datatype (cons 0 (1- (length data)))) data
  58.     )   
  59.   )
  60. )

  61. ;;------------=={ SelectionSet -> VLA Objects }==-------------;;
  62. ;;                                                            ;;
  63. ;;  Converts a SelectionSet to a list of VLA Objects          ;;
  64. ;;------------------------------------------------------------;;
  65. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  66. ;;------------------------------------------------------------;;
  67. ;;  Arguments:                                                ;;
  68. ;;  ss - Valid SelectionSet (Pickset)                         ;;
  69. ;;------------------------------------------------------------;;
  70. ;;  Returns:  List of VLA Objects, else nil                   ;;
  71. ;;------------------------------------------------------------;;

  72. (defun LM:ss->vla ( ss / i l )
  73.   (if ss
  74.     (repeat (setq i (sslength ss))
  75.       (setq l (cons (vlax-ename->vla-object (ssname ss (setq i (1- i)))) l))
  76.     )
  77.   )
  78. )

  79. ;;---------------=={ Block Ref -> Block Def }==---------------;;
  80. ;;                                                            ;;
  81. ;;  Returns the Transformation Matrix and Translation Vector  ;;
  82. ;;  for transforming Block Reference Geometry to the Block    ;;
  83. ;;  Definiton.                                                ;;
  84. ;;------------------------------------------------------------;;
  85. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  86. ;;------------------------------------------------------------;;
  87. ;;  Arguments:                                                ;;
  88. ;;  e - Block Reference Entity                                ;;
  89. ;;------------------------------------------------------------;;
  90. ;;  Returns:  List of 3x3 Transformation Matrix, Vector       ;;
  91. ;;------------------------------------------------------------;;

  92. (defun LM:Ref->Def ( e / _dxf a l n )

  93.   (defun _dxf ( x l ) (cdr (assoc x l)))

  94.   (setq l (entget e) a (- (_dxf 50 l)) n (_dxf 210 l))
  95.   (
  96.     (lambda ( m )
  97.       (list m
  98.         (mapcar '- (_dxf 10 (tblsearch "BLOCK" (_dxf 2 l)))
  99.           (mxv m
  100.             (trans (_dxf 10 l) n 0)
  101.           )
  102.         )
  103.       )
  104.     )
  105.     (mxm
  106.       (list
  107.         (list (/ 1. (_dxf 41 l)) 0. 0.)
  108.         (list 0. (/ 1. (_dxf 42 l)) 0.)
  109.         (list 0. 0. (/ 1. (_dxf 43 l)))
  110.       )
  111.       (mxm
  112.         (list
  113.           (list (cos a) (sin (- a)) 0.)
  114.           (list (sin a) (cos a)     0.)
  115.           (list    0.        0.     1.)
  116.         )
  117.         (mapcar '(lambda ( e ) (trans e n 0 t))
  118.          '(
  119.             (1. 0. 0.)
  120.             (0. 1. 0.)
  121.             (0. 0. 1.)
  122.           )
  123.         )
  124.       )
  125.     )
  126.   )
  127. )

  128. ;; Matrix x Vector  -  Vladimir Nesterovsky
  129. (defun mxv ( m v )
  130.   (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  131. )

  132. ;; Matrix x Matrix  -  Vladimir Nesterovsky
  133. (defun mxm ( m q )
  134.   (mapcar (function (lambda ( r ) (mxv (trp q) r))) m)
  135. )

  136. ;; Matrix Transpose  -  Doug Wilson
  137. (defun trp ( m )
  138.   (apply 'mapcar (cons 'list m))
  139. )

  140. ;;---------------------=={ Select if }==----------------------;;
  141. ;;                                                            ;;
  142. ;;  Provides continuous selection prompts until either a      ;;
  143. ;;  predicate function is validated or a keyword is supplied. ;;
  144. ;;------------------------------------------------------------;;
  145. ;;  Author: Lee Mac, Copyright ? 2011 - www.lee-mac.com       ;;
  146. ;;------------------------------------------------------------;;
  147. ;;  Arguments:                                                ;;
  148. ;;  msg  - prompt string                                      ;;
  149. ;;  pred - optional predicate function [selection list arg]   ;;
  150. ;;  func - selection function to invoke                       ;;
  151. ;;  keyw - optional initget argument list                     ;;
  152. ;;------------------------------------------------------------;;
  153. ;;  Returns:  Entity selection list, keyword, or nil          ;;
  154. ;;------------------------------------------------------------;;

  155. (defun LM:SelectIf ( msg pred func keyw / sel ) (setq pred (eval pred))  
  156.   (while
  157.     (progn (setvar 'ERRNO 0) (if keyw (apply 'initget keyw)) (setq sel (func msg))
  158.       (cond
  159.         ( (= 7 (getvar 'ERRNO))

  160.           (princ "\nMissed, Try again.")
  161.         )
  162.         ( (eq 'STR (type sel))

  163.           nil
  164.         )
  165.         ( (vl-consp sel)

  166.           (if (and pred (not (pred sel)))
  167.             (princ "\nInvalid Object Selected.")
  168.           )
  169.         )
  170.       )
  171.     )
  172.   )
  173.   sel
  174. )

  175. ;-------------------------------------------------------------;
  176. ;                   -- Test Functions --                      ;
  177. ;-------------------------------------------------------------;

  178. (defun c:Add2Block ( / *error* _StartUndo _EndUndo acdoc ss e )

  179.   (defun *error* ( msg )
  180.     (if acdoc (_EndUndo acdoc))
  181.     (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
  182.         (princ (strcat "\n** Error: " msg " **")))
  183.     (princ)
  184.   )

  185.   (defun _StartUndo ( doc ) (_EndUndo doc)
  186.     (vla-StartUndoMark doc)
  187.   )

  188.   (defun _EndUndo ( doc )
  189.     (if (= 8 (logand 8 (getvar 'UNDOCTL)))
  190.       (vla-EndUndoMark doc)
  191.     )
  192.   )
  193.   
  194.   (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))

  195.   (if
  196.     (and (setq ss (ssget "_:L"))
  197.       (setq e
  198.         (LM:SelectIf "\nSelect Block to Add Objects to: "
  199.          '(lambda ( x ) (eq "INSERT" (cdr (assoc 0 (entget (car x)))))) entsel nil
  200.         )
  201.       )
  202.          
  203.     )
  204.     (progn
  205.       (_StartUndo acdoc) (LM:AddObjectstoBlock acdoc (car e) ss)(LM:entupdInsert (car e)) (_EndUndo acdoc)
  206.     )
  207.   )
  208.   (princ)
  209. )

  210. ;-------------------------------------------------------------;

  211. (defun c:Remove ( / *error* _StartUndo _EndUndo acdoc e )

  212.   (defun *error* ( msg )
  213.     (if acdoc (_EndUndo acdoc))
  214.     (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
  215.         (princ (strcat "\n** Error: " msg " **")))
  216.     (princ)
  217.   )

  218.   (defun _StartUndo ( doc ) (_EndUndo doc)
  219.     (vla-StartUndoMark doc)
  220.   )

  221.   (defun _EndUndo ( doc )
  222.     (if (= 8 (logand 8 (getvar 'UNDOCTL)))
  223.       (vla-EndUndoMark doc)
  224.     )
  225.   )
  226.   
  227.   (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))

  228.   (while (setq e (car (nentsel "\nSelect Object to Remove: ")))
  229.     (_StartUndo acdoc) (LM:RemovefromBlock acdoc e)(LM:entupdInsert (car e)) (_EndUndo acdoc)
  230.   )
  231.   (princ)
  232. )

  233. (vl-load-com) (princ)

  234. ;;------------------------------------------------------------;;
  235. ;;                         End of File                        ;;
  236. ;;------------------------------------------------------------;;


  237. ;;------------------------------------------------------------;;
  238. ;;                         Modify by edata@mjtd               ;;
  239. ;;------------------------------------------------------------;;
  240. ;;Entupd insert object 2015-6-27
  241. (defun LM:entupdInsert(blk / ss1 en)
  242.    (setq ss1(ssget "x" (list '(0 . "insert")(cons 2 (cdr(assoc 2(entget blk)))))))
  243.   (while (setq en(ssname ss1 0))
  244.     (entupd en)   
  245.     (setq ss1 (ssdel en ss1))
  246.     )
  247.   (princ)
  248.   )
  249. ;;End Modify

点评

E大, 真乃神人也  发表于 2015-6-27 11:29

评分

参与人数 3明经币 +3 收起 理由
669423907 + 1 神马都是浮云
自贡黄明儒 + 1 很给力!
lucas_3333 + 1 神马都是浮云

查看全部评分

 楼主| 发表于 2015-6-27 13:58 | 显示全部楼层
edata 发表于 2015-6-27 11:23
块中添加对象LEE-MAC的修改,解决无法框选问题。
entmake方式有问题,会更改block的句柄组码,而vla方式不 ...

你主要加了个entupd ,就解决问题了?

点评

是的,entupd解决你的问题。  发表于 2015-6-27 15:26
发表于 2015-6-30 10:07 | 显示全部楼层
什么土办法,瞧瞧
发表于 2015-7-6 13:14 | 显示全部楼层
提示: 该帖被管理员或版主屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-27 00:57 , Processed in 0.214449 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表