Using the Add Method

Use the Blocks collection's Add method to add a new Block object. The method takes a single string parameter—the new block's name. It returns a reference to the new Block object. If the parameter is not a valid block name, Add raises an exception. If you try to name a new block the same as an existing block, Add returns a reference to the existing Block object. This method has the following syntax:

Set BlockObject = BlocksCollection.Add(InsertionPoint, BlockName) Table 13-2 explains this method's parameters.

When you create this object, it does not contain any entities. You can add them with the AddXXX methods discussed in the next section and in more detail in Chapters 8 and 9.

Table 13-2. The Add Method's Parameters


Data Type




A three-element array of doubles that specifies the block's insertion base point in WCS coordinates



The new Block object's name

The following example adds a simple block definition to the Blocks collection and then adds a circle to it. The user can then insert the new block in the normal way.

Public Sub AddBlock() Dim dblOrigin(2) As Double Dim objBlock As AcadBlock Dim strName As String

'' get a name from user strName = InputBox("Enter a new block name: ")

If "" = strName Then Exit Sub ' exit if no old name

Figure 13-2. Viewing the block code output

'' set the origin point dblOrigin(0) = 0: dblOrigin(1) = 0: dblOrigin(2) = 0

''check if block already exists On Error Resume Next

Set objBlock = ThisDrawing.Blocks.Item(strName) If Not objBlock Is Nothing Then MsgBox "Block already exists" Exit Sub End If

'' create the block

Set objBlock = ThisDrawing.Blocks.Add(dblOrigin, strName)

'' then add entities (circle) objBlock.AddCircle dblOrigin, 10

End Sub

AddXXX Methods

Use the AddXXX methods to add drawing entities to a Block object and therefore populate a new Block object. You've already seen a simple example of this in the AddBlock example. Chapters 8 and 9 cover the AddXXX methods fully.

CopyObject Method

Another way to populate a Block object with new entities is to use the Document object's CopyObject method to add duplicate entities. This method copies objects from one container to another. It also returns a variant array of the objects created during the copy. These new objects are exact duplicates, with the same relative positions, sizes, scales, and properties. This method has the following syntax:

varCopies = Owner.CopyObjects(Objects [, NewOwner] [, IdMap]) Table 13-3 explains this method's parameters.

Table 13-3. The CopyObject Method's Parameters


Data Type



Document, PaperSpace, ModelSpace, or Block objects

The current containing owner of the objects to copy.



An array of objects to copy. The objects must all belong to the Owner object.



Optional. Specifies the objects' new owner. If null, the objects are copied to the Owner object. This can also be another Document object.



Optional. An array that holds IDPair objects.

The IdMap array is most useful for copying objects between databases or drawing files. The IDPair object describes how objects map from source to destination, including nonprimary but referenced objects. A full description of the IDPair object and its use is beyond the scope of this chapter. You will not need this optional functionality when you create blocks.

The following example creates and populates a block definition by copying the specified entities into the block:

Public Sub TestCopyObjects() Dim objSS As AcadSelectionSet Dim varBase As Variant Dim objBlock As AcadBlock Dim strName As String Dim strErase As String Dim varEnt As Variant Dim objSourceEnts() As Object Dim varDestEnts As Variant Dim dblOrigin(2) As Double Dim intI As Integer

'choose a selection set name that you use only as temporary storage and 'ensure that it does not currently exist On Error Resume Next

ThisDrawing.SelectionSets.Item("TempSSet").Delete Set objSS = ThisDrawing.SelectionSets.Add("TempSSet") objSS.SelectOnScreen

'' get the other user input With ThisDrawing.Utility .InitializeUserInput 1

strName = .GetString(True, vbCr & "Enter a block name: ") .InitializeUserInput 1

varBase = .GetPoint(, vbCr & "Pick a base point: ") .InitializeUserInput 1, "Yes No"

strErase = .GetKeyword(vbCr & "Erase originals [Yes/No]? ") End With

'' set WCS origin dblOrigin(o) = 0: dblOrigin(l) = 0: dblOrigin(2) = 0 '' create the block

Set objBlock = ThisDrawing.Blocks.Add(dblOrigin, strName)

'' put selected entities into an array for CopyObjects ReDim objSourceEnts(objSS.Count - l) For intI = 0 To objSS.Count - 1

Set objSourceEnts(intI) = objSS(intI)


'' copy the entities into block varDestEnts = ThisDrawing.CopyObjects(objSourceEnts, objBlock)

'' move copied entities so that base point becomes origin For Each varEnt In varDestEnts varEnt.Move varBase, dblOrigin


'' if requested, erase the originals If strErase = "Yes" Then objSS.Erase End If

ThisDrawing.SendCommand "._-insert" & vbCr & strName & vbCr

'' clean up selection set objSS.Delete End Sub

â–  Note If you try to copy the container object to itself, it reproduces itself an infinite number of times. You can't execute this method at the same time you iterate through a collection. An iteration opens the work space as read-only, while this method tries to perform a read-write operation. Complete any iteration before you call this method. The CopyObjects operation copies objects that the Objects parameter's primary objects own or reference.

Was this article helpful?

0 0

Post a comment