CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ActiveX 开发指南

关于复制多个对象 (VBA/ActiveX)

2023-1-5 01:08| 发布者: admin| 查看: 660| 评论: 0|来自: AutoCAD

摘要: 若要复制多个对象,请使用 CopyObjects 方法或创建要与 Copy 方法一起使用的对象数组。

要复制多个对象,请使用该方法或创建要与该方法一起使用的对象数组。CopyObjectsCopy

要复制选择集中的对象,请循环访问选择集并将对象保存到数组中。遍历数组,单独复制每个对象,并将新创建的对象收集到第二个数组中。

要将多个对象复制到其他图形,请使用该方法并将Owner参数设置为图形的模型空间。CopyObjects

复制两个圆形对象

此示例创建两个对象,并使用该方法复制圆圈。CircleCopyObjects

Sub Ch4_CopyCircleObjects()
  Dim DOC1 As AcadDocument
  Dim circleObj1 As AcadCircle
  Dim circleObj2 As AcadCircle
  Dim circleObj1Copy As AcadCircle
  Dim circleObj2Copy As AcadCircle
  Dim centerPoint(0 To 2) As Double
  Dim radius1 As Double
  Dim radius2 As Double
  Dim radius1Copy As Double
  Dim 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 = ThisDrawing.Application.Documents.Add

  ' Add two circles to the drawing
  Set circleObj1 = DOC1.ModelSpace.AddCircle(centerPoint, radius1)
  Set circleObj2 = DOC1.ModelSpace.AddCircle(centerPoint, radius2)
  ZoomAll

  ' 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
  circleObj1Copy.Color = acRed
  circleObj2Copy.Radius = radius2Copy
  circleObj2Copy.Color = acRed

  ZoomAll
End Sub

将对象复制到另一个图形

本示例创建对象,然后使用该方法将圆复制到新绘图中。CircleCopyObjects

Sub Ch4_Copy_to_New_Drawing()
  Dim DOC0 As AcadDocument
  Dim circleObj1 As AcadCircle, circleObj2 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#

  ' Add two circles to the current drawing
  Set circleObj1 = ThisDrawing.ModelSpace.AddCircle(centerPoint, radius1)
  Set circleObj2 = ThisDrawing.ModelSpace.AddCircle(centerPoint, radius2)
  ThisDrawing.Application.ZoomAll

  ' Save pointer to the current drawing
  Set DOC0 = ThisDrawing.Application.ActiveDocument

  ' Copy objects
  '
  ' First put the objects to be copied into a form compatible
  ' with CopyObjects
  Set objCollection(0) = circleObj1
  Set objCollection(1) = circleObj2

  ' Create a new drawing and point to its model space
  Dim Doc1MSpace As AcadModelSpace
  Dim DOC1 As AcadDocument

  Set DOC1 = Documents.Add
  Set Doc1MSpace = DOC1.ModelSpace

  ' Copy the objects into the model space of the new drawing. A
  ' collection of the new (copied) objects is returned.
  retObjects = DOC0.CopyObjects(objCollection, Doc1MSpace)

  Dim circleObj1Copy As AcadCircle, circleObj2Copy As AcadCircle

  ' Get the newly created object collection and apply new
  ' properties to the copies.
  Set circleObj1Copy = retObjects(0)
  Set circleObj2Copy = retObjects(1)

  circleObj1Copy.radius = radius1Copy
  circleObj1Copy.Color = acRed
  circleObj2Copy.radius = radius2Copy
  circleObj2Copy.Color = acRed

  ThisDrawing.Application.ZoomAll

  MsgBox "Circles copied."
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-12 03:23

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部