找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 691|回复: 0

[VBA程序]:如何用VB/VBA对块进行处理

[复制链接]

已领礼包: 145个

财富等级: 日进斗金

发表于 2002-9-27 17:44:14 | 显示全部楼层 |阅读模式

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

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

×
How to manipulate blocks in VB/VBA?  
ID    48218  
Applies to:    AutoCAD 2000
  
This document is part of    Block Tables   AcDb (AutoCAD Database)   COM-ActiveX Interfaces   VBA     


Question
How do I add new entities to an existing block and change its insertion point by
selecting a block reference in AutoCAD drawing area?
Answer
It's useful to first examine the AutoCAD 2000 Object Model with regard to
Blocks.

A Block object is actually a block definition in the conventional AutoCAD sense.
A BlockReference is created when you insert a block into a drawing, therefore,
what can be seen and picked in the drawing area is actually a BlockReference
object.

As a result, you can only add entities to a Block object instead of a
BlockReference object. On the other hand, in order to change a block insertion
point, you need to manage the BlockReference object.

Included is a VBA sample that highlights how to get BlockReference object's
properties and add an entity to a block. The macro will prompt twice to select
an entity. Select a BlockReference in drawing area, you will get a message box
showing the insertion points and the name of the block definition. The first way
uses "GetEntity" method to select the BlockReference and the second uses
"SelectionSet.SelectOnScreen". Lastly, a line will be added to the block
definition of the selected block reference, and it will appear accordingly in
all BlockReferences that refer to the block definition.
Sub f_sample()
Dim po_obj As Object
Dim pv_pick As Variant
Dim po_ss1 As AcadSelectionSet
'function to highlight various methods to list the Blockrefernce properties
'by selecting on screen

On Error GoTo ERRHAND

'example using Getentity
ThisDrawing.Utility.GetEntity po_obj, pv_pick
Call f_blockProperties(po_obj)
'example using Select on sreen
Set po_ss1 = ThisDrawing.SelectionSets.Add("ss_1")
po_ss1.SelectOnScreen
'displays the properties if the first selected item is BlockReference
Call f_blockProperties(po_ss1(0))

'draw a line in the block definition
Dim po_blk As AcadBlock
Dim pd_stPt(2) As Double
Dim pd_endPt(2) As Double

pd_stPt(0) = 0: pd_stPt(1) = 0: pd_stPt(2) = 0
pd_endPt(0) = 10: pd_endPt(1) = 10: pd_endPt(2) = 0

Set po_blk = ThisDrawing.Blocks(po_obj.Name)
po_blk.AddLine pd_stPt, pd_endPt

ThisDrawing.Application.Update
ThisDrawing.Regen acAllViewports
'delete the selection set
po_ss1.Delete

Exit Sub
'error handler
ERRHAND:
MsgBox "ERROR : " & Err.Description
End Sub

Sub f_blockProperties(po_inputObject As Object)
Dim po_bref As AcadBlockReference
'check whether the selected object is BlockRefernce or not
If TypeOf po_inputObject Is AcadBlockReference Then
Set po_bref = po_inputObject
MsgBox "InsertionPoint : " & po_bref.InsertionPoint(0) & "," &
po_bref.InsertionPoint(0) & "," & po_bref.InsertionPoint(0) & Chr(13) & _
        "Name : " & po_bref.Name
Else
MsgBox "Selected item is not a Block Reference"
End If
End Sub
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-12-21 06:47 , Processed in 0.360602 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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