- UID
- 1
- 积分
- 15892
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-3
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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 |
|