找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 621|回复: 1

[转贴]:添加实体到指定块

[复制链接]
发表于 2003-6-26 09:30:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
添加实体到指定块

  1. (vl-load-com)
  2. ;; Bill Kramer - PointA Wizards Lab Scroll
  3. ;; See related article for details about what is going on in this
  4. ;; code!  Function utilities are provided for adding and removing
  5. ;; specific entity objects from block definitions.
  6. ;; AppendBlock - Given a block name, an entity name in model space,
  7. ;; and a base point (normally location of a given insert of the block)
  8. ;; this routine will append the entity to the block definition.
  9. ;; Returns object reference when successful for new item inserted into
  10. ;; the block definition.  Returns nil if the entity type is not supported.
  11. (defun AppendBlock  (BN         ; block name
  12.            EN         ; entity name to add
  13.            BP         ; base point of insert occurance
  14.            /
  15.            acadObj      ;object reference variable
  16.            Etype      ;entity type string
  17.            )
  18.   (setq   acadObj   (Get_Block_Ref_Obj BN)   ;get block reference object
  19.    EN   (entget EN)      ;entity specifics
  20.    EType   (cdr (assoc 0 EN))   ;entity descriptive name
  21.    )
  22.   ;; Add... method used to add objects to the block collection object.
  23.   ;;        is specific for each type of entity you wish to support.
  24.   (cond
  25.     ((= eType "LINE")         ; Add a line object to the block definition
  26.      (vla-addline
  27.        acadObj
  28.        (vlax-3d-point (mapcar '- (cdr (assoc 10 EN)) BP))
  29.        (vlax-3d-point (mapcar '- (cdr (assoc 11 EN)) BP))
  30.        )
  31.      )
  32.     ((= eType "CIRCLE")         ; Add a circle object to the block definition
  33.      (vla-addcircle
  34.        acadObj
  35.        (vlax-3d-point (mapcar '- (cdr (assoc 10 EN)) BP))
  36.        (vlax-make-variant (cdr (assoc 40 EN)))
  37.        )
  38.      )
  39.     ((= eType "ARC")         ; Add an arc object to the block definition
  40.      (vla-addarc
  41.        acadObj
  42.        (vlax-3d-point (mapcar '- (cdr (assoc 10 EN)) BP))
  43.        (vlax-make-variant (cdr (assoc 40 EN)))
  44.        (vlax-make-variant (cdr (assoc 50 EN)))
  45.        (vlax-make-variant (cdr (assoc 51 EN)))
  46.        )
  47.      )
  48.     ((= eType "ATTDEF")
  49.      (vla-addattribute
  50.        acadObj
  51.        (vlax-make-variant (cdr (assoc 40 EN))) ;height
  52.        (vlax-make-variant (cdr (assoc 70 EN))) ;mode
  53.        (vlax-make-variant (cdr (assoc 2 EN))) ;prompt
  54.        (vlax-3d-point (mapcar '- (cdr (assoc 10 EN)) BP)) ;insert point
  55.        (vlax-make-variant (cdr (assoc 2 EN))) ;tag
  56.        (vlax-make-variant (cdr (assoc 1 EN))) ;default value
  57.        )
  58.      )
  59.     ;; Add more objects!
  60.     (t
  61.      (prompt
  62.        (strcat "\nAppend block does not know about "
  63.           eType
  64.           " objects.")))
  65.     )
  66.   )
  67. ;; RemoveBlock - Given a block name and an entity name of an entity
  68. ;; inside the block definition, routine will remove the entity from
  69. ;; the definition.
  70. ;; Entity name should be selected using something like NENTSEL to
  71. ;; obtain the nested entity name.
  72. ;; No returning value of interest.
  73. (defun RemoveBlock  (BN         ;block name
  74.            EN         ;entity name of object to remove
  75.            /
  76.            acadObj      ;object reference variable
  77.            vlEN      ;entity object name
  78.            )
  79.   (setq vlEN (vlax-ename->vla-object EN))
  80.   (Vla-delete vlEN)
  81. )
  82. ;; Get_Block_Ref_Obj - given block name string this function returns
  83. ;; object reference to the block table entry.  The extended Active X
  84. ;; object references are used to obtain this information.
  85. (defun Get_Block_Ref_Obj  (BN      ; string name of block
  86.             /
  87.             acadObj   ;AutoCAD object reference symbol
  88.             )
  89.   (if (tblsearch "BLOCK" BN)      ;first check if a valid block
  90.     (setq acadObj (vlax-get-acad-object) ;acad object
  91.      acadObj (vla-get-documents acadObj) ;documents collection
  92.      acadObj (vla-item acadObj (getvar "DWGNAME"))
  93.                ;current document
  94.      acadObj (vla-get-blocks acadObj) ;blocks collection
  95.      acadObj (vla-item acadObj BN)   ;block object
  96.      )
  97.     )
  98.   )
  99. ;; TESTING AND DEMONSTRATION OF USE FUNCTIONS
  100. ;; Test 1 prep - new drawing, create some entities, define a block,
  101. ;; insert the block at numerous places.  Add another entity (LINE or
  102. ;; ARC).  Run TEST1 pick the nearest insert to the new entity,
  103. ;; then pick the new entity - it will become part of the block def.
  104. (defun C:TEST1   ()
  105.   (prompt "\nTest/demonstrate the AppendBlock function")
  106.   (setq   EN1 (entsel "\nPick a block insert: ")
  107.    EN1 (entget (car EN1))
  108.    EN2 (entsel "\nPick object to add: ")
  109.    )
  110.   (appendblock
  111.     (cdr (assoc 2 EN1))
  112.     (car EN2)
  113.     (cdr (assoc 10 EN1)))
  114.   (command "_REGEN")         ;regen display
  115.   (princ)
  116.   )
  117. ;; Test 2 prep - run test 1.
  118. (defun C:TEST2   ()
  119.   (prompt "\nTest/demonstrate the RemoveBlock function")
  120.   (setq   EN1
  121.     (nentsel
  122.       "\nPick entity inserted in block to remove from definition: "))
  123.   (if EN1
  124.     (progn
  125.       (setq BN   (cdr (assoc 2 (entget (car (last EN1)))))
  126.        EN1   (car EN1))
  127.       (removeBlock BN EN1)
  128.       (command "_REGEN")
  129.       )
  130.     )
  131.   )
  132. (prompt "\nFunction set for BlockFun has been loaded. See source for details on test runs.")
  133. (princ)
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 488个

财富等级: 日进斗金

发表于 2003-6-26 20:38:13 | 显示全部楼层
找找论坛,有更好的.
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-26 19:12 , Processed in 0.157790 second(s), 34 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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