CopyObjects 方法 (ActiveX)
复制多个对象(深度克隆)。 支持的平台:仅限 Windows 签名VBA: RetVal = object.CopyObjects(Objects [, Owner] [, IDPairs]) 返回值 (RetVal)类型:变体(对象数组) 新创建的重复对象的数组。此数组中仅返回主要对象。有关操作期间发生的情况的详细信息,或者也复制了主对象所拥有的对象列表,请参阅 IDPairs 参数。CopyObjects 言论若要将对象复制到另一个打开的图形,请将 Owner 参数设置为另一个图形的模型空间。 在操作过程中,还将复制 Objects 参数中的主要对象拥有或引用的对象。CopyObjects 注意:不能在同时循环访问集合时执行此方法。迭代将为只读操作打开工作空间,而此方法将尝试执行读写操作。在调用此方法之前,请完成任何迭代。
例子VBA: Sub Example_CopyObjects() ' This example creates a Circle object and uses the CopyObjects ' method to make a copy of the new Circle. Dim DOCOrg As AcadDocument 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# ' Store current drawing Set DOCOrg = ThisDrawing.Application.ActiveDocument ' 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) ThisDrawing.Application.ActiveDocument = DOCOrg retObjects = DOC1.CopyObjects(objCollection, DOCOrg.ModelSpace) ' 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 可视化 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)
doc (vla-get-ActiveDocument acadObj))
;; Define the Circle object
(setq centerPoint (vlax-3d-point 0 0 0)
radius1 5 radius2 7
radius1Copy 1 radius2Copy 2)
;; Create a new drawing
(setq DOC1 (vla-Add (vla-get-Documents acadObj)))
' Add two circles to the drawing
(setq circleObj1 (vla-AddCircle (vla-get-ModelSpace DOC1) centerPoint radius1)
circleObj2 (vla-AddCircle (vla-get-ModelSpace DOC1) centerPoint radius2))
;;(vla-ZoomAll acadObj)
;; Copy objects
;;
;; First put the objects to be copied into a form compatible with CopyObjects
(setq objCollection (vlax-make-safearray vlax-vbObject '(0 . 1)))
(vlax-safearray-put-element objCollection 0 circleObj1)
(vlax-safearray-put-element objCollection 1 circleObj2)
;; Copy object and get back a collection of the new objects (copies)
(setq retObjects (vlax-variant-value (vla-CopyObjects DOC1 objCollection (vla-get-ModelSpace (vla-get-Database doc)))))
;; Get newly created object and apply new properties to the copies
(setq circleObj1Copy (vlax-safearray-get-element retObjects 0)
circleObj2Copy (vlax-safearray-get-element retObjects 1))
(vla-put-Radius circleObj1Copy radius1Copy)
(vla-put-Radius circleObj2Copy radius2Copy)
(vla-ZoomAll acadObj)
(alert "Circles copied.")
)
;; The following sample works with AutoCAD only
(defun c:Example_CopyObjectsFromExternalDrawing()
;; 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)
doc (vla-get-ActiveDocument acadObj))
;; Create a reference to the ObjectDBX object
(setq acdbObj (vla-GetInterfaceObject acadObj "ObjectDBX.AxDbDocument.25"))
;; Open an external drawing file
(vlax-invoke-method acdbObj 'Open (findfile ".\\Sample\\en-us\\DesignCenter\\Landscaping.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)
)
|
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-1-19 07:31
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.