Body {Background-Color: #ffffff; color: # 000000} TD {font-family:
Arial, Helvetica, Sans-Serif; Font-size: 11pt; line-height: 14pt; color: # 000000} .NORMAL {Font-Family:
Arial, Helvetica, Sans-Serif; Font-Size: 11pt; line-height: 14pt; color: # 000000}
.p1 {font-size: 10pt; line-height: 13pt; font-family: Song;}
.p2 {font-size: 9pt; line-height: 13pt; font-family: Song;}
.p3 {font-size: 12pt; line-height: 16pt; font-family: Song;}
.p4 {font-size: 14pt; line-height: 18pt; font-family: Song;}
.title
{Font-weight: bold} .subtitle {} .author {} .tocheader {font-weight:
Bold} .toc {} .tocdisabled {color: # 999999} .summary {} .body
{Background-Color: #ffffff; color: # 000000; Font-Family: Arial, Helvetica, Sans-Serif;
Font-size: 10pt} .header {font-weight: bold; font-size: 16pt} .subheader {font-style:
Italic; font-size: 14pt} .Intro {font-size: 12pt} .caption {font-size: 8pt} .quote
{Font-style: italic; font-weight: bold} .footer {color: # 666666;
Font-size: 9pt} .filedescr {Color: #aaaaaa; font-size: 8pt} .bold
{Font-weight: bold}
Use VBA Access Blocks and Block Reference Data Summary: This document describes how to use VBA (Visual Basic for Applications) access blocks and block reference data.
Product version operating system AutoCAD (R) 2000Windows 95, Windows 98, Windows NT 4.0 Related Documents: Keywords: VBA Block Model Space Space Access Reference Data Document Number: TD105102.DOC Scope: API, Customization Revision Time: September 30, 1999 catalog
Introduction Create an application interface to create a block
Add the primitives to the block in the model space inserted block access blocks and block reference objects
Access Block Object Access Block Reference Modify The object introduction in the block can use VBA to manage blocks and block references. A block is an object set that is associated, referring to a process as a separate object definition, is called a block definition. A block reference is generated when inserting a block in the figure. The block definition is referenced to the block reference. It is very important to understand the difference between block definitions and block references, because this will help you understand how these objects are manipulated in VBA. We explain how to distinguish between a small program that demonstrates the process to distinguish between block definitions and block references. The following sections use the annotated code instance to demonstrate how to use the VBA access block (block definition) and block reference data. Creating an application interface assumes that you have used the following controls to define a VBA's main window.
A list box name with LstblockObject is named LSTBLOCKRECERENCE name box named cmdcreateBlock command button named cmdlister command button Create a module and add the following declaration. 'Block collection
Public BLKCOLL AS Acadblocks
'Block object
Public BLKOBJ AS Acadblock
'Block reference object
Public BLKREFRENCE As AcadblockReference
'Model space collection
Public Mspace As AcadmodelSpace
'Drawing space collection
Public pspace as acadpaperspace
Public country as integer
Public I as integer
Public Elem as Object
Public SUBELEM AS Object
'Block insertion point
Public BLKINSPNT (0 to 2) as Double
Public Circleobj As Acadcircle
'Round point
Public Center (0 to 2) as Double
'Round radius
Public Radius as DOUBLE
Public LineObj As Acadline
'Starting point in line segment
Public StartPNT (0 to 2) as Double
'Line segment endpoint
Public Endpnt (0 to 2) as Double
Add the following declarations in the initialization event of the window.
'Return to the model space collection
Set mspace = thisdrawing.modelspace
'Back to the paper space collection
Set pspace = thisdrawing.paperspace
'Return block collection
Set blkcoll = thisdrawing.blocks
Create a block
To create a block reference, you must first create a block definition (block) and use the "Add" mode. After creating a block, you can use the "InsertBlock" mode to insert a sample of the block in the figure. In this example, we create a block in the model space, which consists of a circle and one line segment.
Add the following code to the CMDCreateBlock button.
BLKINSPNT (0) = 0 #: blkinspnt (1) = 0 #: blkinspnt (2) = 0 #
'Add a block to the block collection
Set blkobj = blkcoll.add (blkinspnt, "newblock")
Add the primitive to the block
This section provides an example example of adding primitives to the block.
Add round
Center (0) = 0 #: center (1) = 0 #: center (2) = 0 #
RADIUS = 1
'Add a circle into the block
SET CIRCLEOBJ = BLKOBJ.Addcircle (Center, Radius)
Add a line segment
StartPNT (0) = 1 #: startpnt (1) = 1 #: startpnt (2) = 0 #
Endpnt (0) = 5 #: endpnt (1) = 5 #: Endpnt (2) = 0 #
'Add a line segment into the block
Set lineobj = blkobj.addline (startpnt, endpnt)
Insert a block in the model space
The following code examples are inserted into the block in the model space.
Set BLKREFRENCE = Mspace.insertblock (Blkinspnt, "Newblock", 1 #, 1 #, 1 #, 0)
'Enlarge block
Zoomextents
Access block and block reference object
VBA provides links to activate graphics in the current AutoCAD process via thisDrawing objects. You can immediately access the current document and all other objects in the documentation by using thisDrawing.
To access the block, look for thisDrawing objects; to access block references, look up model space collection or drawings space collection.
Access block object
Now you need a scanning block collection, return the block name and put them in the LstblockObject list box. There are two ways of scanning blocks.
You can use an index to scan a set of blocks. Once you know how much it contains, you can use the for.next condition cycle. To implement the above functions, add the following code in the CMDLister command button.
'Return to the number of blocks in blocks
Count = blkcoll.count
'Empty list box
LSTBLOCKOBJECT.CLEAR
'Creating a block list
For i = 0 to count - 1
LstblockObject.addItem BlkColl.Item (i) .name
NEXT
LstblockObject.listIndex = 0
Note: A collection of n items and indexes from 0 to N-1.
However, a better way is to use the for Each.NEXT condition cycle. For example, the following statement repeats a statement block on each of the collections or each element in the array. VBA automatically sets a variable each time the cycle is executed. Therefore, you can override the above code with the following example.
'Empty list box
LSTBLOCKOBJECT.CLEAR
'Creating a block list
For Each Elem in thisdrawing.blocks
LstblockObject.addItem elem.name
NEXT
LstblockObject.listIndex = 0
By using this code, you can
Reduce the operation steps. Enhance the readability of the code. Reduce custom variables. Reduce the workload of the error check (for example, the range of conditional statements). Make more programming styles.
Access block reference
To return a similar element, you have to complete a similar operation; however, it is not a searcher collection, but the model space or paper space collection of AutoCAD activation documents. To implement the above functions, add the following code in CMDLister's click event program.
'Empty list box
LstblockReference.clear
'Creating a block list
'Scanning model space collection
For Each Elem in Mspace
'Screening out block reference
If Elem.EntityName = "ACDBBLOCKREFERENCE" THEN
LstblockReference.addItem elem.name
END IF
NEXT
LstblockReference.listIndex = 0
At this time, each time you click the Create Block button, there is only one block named newblock in the LstblockObject list box. A block reference called Newblock is added to the LstblockReference list box. Since creating a new block reference, a new block reference is added.
Note: Even if the block references is referenced to the existing block, the handle is different. For AutoCAD, they are different objects.
Modify the object in the block
You can also modify the object in the block without breaking blocks. To implement this feature, you must enter the block object, return to the object to be modified and change its properties. The following code example changes the color properties of the line segment to green.
Add the following code to the CMDChangeColor button Click the event:
Scanning block collection
For Each Elem in BLKCOLL 'Search for the block to modify
If ELEM.NAME = "Newblock" THEN 'Scan block
For Each Subelem in ELEM
'Find line segment
If Subelem.EntityName = "ACDBLINE" THEN
'Assign a new color R
SUBELEM.COLOR = ACRED
END IF
NEXT
END IF
NEXT
'Refresh graphics
THISDRAWING.REGEN TRUE Pre-
These codes change the color properties of the line segments in all block references named newblock. The real situation that happens that these codes change the color properties of the block object, and the block reference is also updated because they are referenced by block objects.
Disclaimer: The points, techniques, examples, and suggestions listed in the Autodesk product support technical documentation are just Autodesk's recommendations for users, and users do their own risk. Autodesk's modification of the document does not require the user to agree. Autodesk will be irresponsible for the user due to the use of Autodesk product technical support documents.
C) Copyright 2001 Autodesk, Inc. All rights reserved. Not allowed to copy. Autodesk Copyright and Registered Trademark Information | Personal Information Ordinance | Contact Autodesk