CopyObjects Method (ActiveX)

Duplicates multiple objects (deep cloning).

Supported platforms: Windows only

Signature

VBA:

RetVal = object.CopyObjects(Objects [, Owner] [, IDPairs])
object

Type: Database, Document

The objects this method applies to.

Objects

Access: Input-only

Type: Variant (array of Objects)

The array of primary objects to be copied. All the objects must have the same owner, and the owner must belong to the database or document that is calling this method.

Owner

Access: Input-only; optional

Type: Variant (a single object)

The new owner for the copied objects. If no owner is specified, the objects will be created with the same owner as the objects in the Objects array.

IDPairs

Access: Input-output; optional

Type: Variant (array of IDPair objects)

Information on what happened during the copy and translation process.

  • Input: an empty variant.
  • Output: an array of IDPair objects.

Return Value (RetVal)

Type: Variant (array of objects)

An array of newly created duplicate objects. Only primary objects are returned in this array. For more information on what occurred during the CopyObjects operation, or a list of objects owned by primary objects that were also copied, consult the IDPairs parameter.

Remarks

To copy objects to another open drawing, set the Owner parameter to the other drawing's model space.

During the CopyObjects operation, objects that are owned or referenced by the primary objects in the Objects parameter will also be copied.

Note: You cannot execute this method while simultaneously iterating through a collection. An iteration will open the work space for a read-only operation, while this method attempts to perform a read-write operation. Complete any iteration before you call this method.

Examples

VBA:

Sub Example_CopyObjects()
    ' This example creates a Circle object and uses the CopyObjects
    ' method to make a copy of the new Circle.

    Dim DOC1 As AcadDocument
    Dim circleObj1 As AcadCircle, circleObj2 As AcadCircle
    Dim circleObj1Copy As AcadCircle, circleObj2Copy As AcadCircle
    Dim centerPoint(0 To 2) As Double
    Dim radius1 As Double, radius2 As Double
    Dim radius1Copy As Double, radius2Copy As Double
    Dim objCollection(0 To 1) As Object
    Dim retObjects As Variant
    
    ' Define the Circle object
    centerPoint(0) = 0: centerPoint(1) = 0: centerPoint(2) = 0
    radius1 = 5#: radius2 = 7#
    radius1Copy = 1#: radius2Copy = 2#
    
    ' Create a new drawing
    Set DOC1 = Documents.Add
    
    ' Add two circles to the drawing
    Set circleObj1 = DOC1.ModelSpace.AddCircle(centerPoint, radius1)
    Set circleObj2 = DOC1.ModelSpace.AddCircle(centerPoint, radius2)
    ThisDrawing.Application.ZoomAll
    
    ' Copy objects
    '
    ' First put the objects to be copied into a form compatible with CopyObjects
    Set objCollection(0) = circleObj1
    Set objCollection(1) = circleObj2
    
    ' Copy object and get back a collection of the new objects (copies)
    retObjects = DOC1.CopyObjects(objCollection)
    
    ' Get newly created object and apply new properties to the copies
    Set circleObj1Copy = retObjects(0)
    Set circleObj2Copy = retObjects(1)
    
    circleObj1Copy.radius = radius1Copy
    circleObj2Copy.radius = radius2Copy
        
    ThisDrawing.Application.ZoomAll
    
    MsgBox "Circles copied."
End Sub

Visual LISP:

(vl-load-com)
(defun c:Example_CopyObjects()
    ;; This example creates a Circle object and uses the CopyObjects
    ;; method to make a copy of the new Circle.
    (setq acadObj (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acadObj))

    ;; Load the ObjectDBX library
    (if (= acLibImport nil)
	       (progn
	           (vlax-import-type-library :tlb-filename "C:\\Program Files\\Common Files\\Autodesk Shared\\axdb20enu.tlb"
	                                     :methods-prefix "acdbm-"
	                                     :properties-prefix "acdbp-"
	                                     :constants-prefix "acdbc-"
	           )
            (setq acLibImport T)
        )
    )

    ;; Create a reference to the ObjectDBX object
    (setq acdbObj (vlax-create-object "ObjectDBX.AxDbDocument.20"))

    ;; Open an external drawing file
    (acdbm-open acdbObj (findfile ".\\Sample\\VBA\\Tower.dwg"))

    ;; Add two circles to the drawing
    (setq objCollection (vlax-make-safearray vlax-vbObject (cons 0 (- (vla-get-Count (vla-get-ModelSpace acdbObj)) 1)))
	  count 0)

    ;; Copy objects
    (vlax-for eachObj (vla-get-ModelSpace acdbObj)
        (vlax-safearray-put-element objCollection count eachObj)
        (setq count (1+ count))
    )
     
    ;; Copy object and get back a collection of the new objects (copies)
    (setq retObjects (vla-CopyObjects acdbObj objCollection (vla-get-ModelSpace (vla-get-Database doc))))
    
    (vla-ZoomAll acadObj)
    
    (alert "Model space objects copied.")

    ;; Close the in memory drawing
    (vlax-release-object acdbObj)
)