65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 和 AutoCAD COM API 进行 AutoCAD 2D 绘图

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.89/5 (12投票s)

2016年3月1日

CPOL

7分钟阅读

viewsIcon

86781

downloadIcon

4605

使用编程自动化 AutoCAD 中的绘图过程,以提高设计工程师的生产力。

引言

本文介绍了使用 C# 在 AutoCAD 中进行 2D 图形自动化绘制。它为初学者提供了 AutoCAD 自动化入门的分步说明。

背景

必备组件

1. C# 编程基础

2. AutoCAD 图形基础。

所需工具

1. Visual Studio Express

2. AutoCAD

什么是 AutoCAD 自动化?

AutoCAD 是一款用于创建 2D 和 3D 图形的软件。AutoCAD 自动化意味着使用编程在 AutoCAD 中自动创建图形。我们可以通过编程在几秒钟内绘制非常大的图形。AutoCAD 支持与其他应用程序的互操作性,通过使用 AutoCAD 对象模型,我们可以在 AutoCAD 中创建图形。上图显示了 AutoCAD 自动化的思想。自定义的 C# 应用程序通过调用 AutoCAD 中可用的命令来在 AutoCAD 中创建图形。

在此示例中,我们将使用 AutoCAD COM API 通过 C# 应用程序在 AutoCAD 中创建一个简单的图形。在继续之前,我们需要了解以下内容:

1. AutoCAD 自动化是如何工作的?

2. 什么是 AutoCAD 对象模型?

3. 如何将我们的应用程序与 AutoCAD 的活动实例连接?

AutoCAD 自动化是如何工作的?

看看下面的图,它展示了 AutoCAD 自动化的实际工作机制。

实际上发生了什么?AutoCAD 将一切组织为对象(OOP),圆、线、矩形、正方形、椭圆等都被视为 AutoCAD 中的对象。AutoCAD 中的每个对象都有其自身的属性和函数。AutoCAD 通过 AutoCAD COM API 向外界公开这些对象,通过在我们的编程中使用此 API,我们可以操作 AutoCAD 的对象。因此,通过 AutoCAD COM API 操作 AutoCAD 对象是 AutoCAD 自动化的基本思想。

考虑上图中的示例,AutoCAD 对象集合及其组织在一起称为 AutoCAD 对象模型。该模型包含有关所有 AutoCAD 对象属性和函数的详细信息。这些对象在 AutoCAD COM API 中可用。这个对象模型就像一个 AutoCAD 对象池,我们可以使用这些对象来在 AutoCAD 中创建图形。该图显示了 AutoCAD COM API 中存在的 Circle 对象的一些属性和函数。通过从 C# 应用程序调用 AddCircle 方法,将在 AutoCAD 中创建圆。通过设置 Radius 和 CenterPoint 属性,我们可以操作创建的圆。

注意:AddCircle 不是 AutoCAD Circle 对象的成员,它是 AutoCAD ModelSpace 对象的成员。我在这里放置 AddCircle 方法只是为了便于理解。

什么是 AutoCAD 对象模型?

现在我们知道了 AutoCAD 对象模型的重要性,AutoCAD 对象模型是 Line、Circle、Polyline、Dimension 等 AutoCAD 对象的集合。它定义了 AutoCAD 对象的属性、方法和事件。它还定义了 AutoCAD 对象的组织和结构。

下图表示了 AutoCAD 对象模型的层次结构。

参考图片 1:Autodesk Object Model (ActiveX),由 Autodesk Knowledge Network 授权(CC BY-NC-SA 3.0

从上图,我们需要了解以下重要内容:

Application (Global) - 代表 AutoCAD 应用程序。它有一个 ActiveDocument 属性,代表在 AutoCAD 中打开的当前工作文档。

Documents - AutoCAD 中打开的文档集合,包括 ActiveDocument。

Document - 单个 AutoCAD 文档。

Modelspace - AutoCAD 中的工作区或绘图画布。

看看下面的图,它只是以上内容的视觉化。

从这张图中我们可以理解以下几点:

AutoCAD 应用程序包含在应用程序中打开的文档集合,这个文档集合包含 ActiveDocument(AutoCAD 中的当前工作文档)。每个 AutoCAD 文档都有 ModelSpace(建模空间或工作区域或绘图画布)。如果我们想在活动文档的模型空间中创建圆,我们必须从 AutoCAD -> Documents -> Active Document -> Modelspace 进行遍历,并调用相应的函数,这里创建模型空间中的圆的函数是 AddCircle。像这样,我们可以通过调用适当的函数来创建不同的形状和图形。

好的,我们对 AutoCAD 自动化应用程序的工作机制有了一个想法。现在让我们看看我们的应用程序和 AutoCAD 之间的连接。需要连接我们的应用程序和 AutoCAD。这里的连接是指正在运行的 AutoCAD 应用程序的对象引用。通过使用此对象引用,我们可以遍历 AutoCAD 对象树并对其进行操作。

如何将我们的应用程序与 AutoCAD 的活动实例连接?

我们如何将我们的 C# 应用程序与 AutoCAD 连接?在通过 C# 应用程序创建图形之前,我们需要在我们的应用程序和 AutoCAD 的活动实例之间建立连接。这是通过 COM 互操作性完成的。在创建连接之前,我们需要将 AutoCAD 的 COM 互操作程序集添加到我们的项目中作为引用。那么 AutoCAD COM 互操作程序集在哪里?它通常位于 Windows 操作系统下面的以下文件夹中。

C:\Program Files\Common Files\Autodesk Shared - 此文件夹包含 AutoCAD COM 程序集。

1. acax19enu.tlb

2. axdb19enu.tlb

这些文件是 AutoCAD 的 COM 互操作程序集,只需将这两个文件添加到我们的项目中。这里 19 是 AutoCAD 的版本号。不同 AutoCAD 版本可能会有所不同。

让我们看看下面的代码:

// Getting active AutoCAD instance by Marshalling by passing Programmatic ID as a string, AutoCAD.Application is the Programmatic ID for AutoCAD.
AcadApplication AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.19");
// Now AcadApp has the reference to the running AutoCAD instance, AcadApp object acts as a port for accessing active instance of AutoCAD.

上面的代码获取了正在运行的 AutoCAD 实例的对象引用。从上面的代码:

AcadApplication - 指的是 AutoCAD 应用程序类 - 它代表 AutoCAD 应用程序。

GetActiveObject - 获取指定应用程序的正在运行的实例。

Marshal - System.Runtime.InteropServices 命名空间中的类,它提供与互操作性相关的函数。

"AutoCAD.Application.19" - 操作系统中 AutoCAD 的应用程序程序 ID,19 代表 AutoCAD 的版本。

使用上面的代码,我们将获得活动 AutoCAD 实例的引用。获取连接后,我们可以通过调用 ModelSpace 对象的函数成员来创建图形。好的,现在让我们看一个示例图形,看看如何通过编程在 AutoCAD 中创建这个图形。

示例图形

以如下图形为例,让我们看看如何使用 C# 创建此图形的步骤。

无尺寸标注的示例图形

Sample Drawing without Dimension

带尺寸标注的示例图形

Sample Drawing with Dimension

 

 

 

 

 

步骤 1

在 Visual Studio 中创建一个 Windows Forms (C#) 项目。将项目命名为 **AutoCADApp** 或您想要的任何名称。

第二步

添加对 AutoCAD COM DLL 的引用:

1. AutoCAD Type Library (acax19enu.tlb)。(这里 19 是版本号,取决于 AutoCAD 版本)

2. AutoCAD / ObjectDBX Common Type Library (axdb19enu.tlb)。(这里 19 是版本号,取决于 AutoCAD 版本)

步骤 3

将控件拖到主窗体,如下面的图片所示:

Main Form Design

并为按钮创建事件。

步骤 4:创建圆

在创建圆之前,让我们了解 AutoCAD 对象模型中的 **AcadCircle**,

AcadCircle
它代表 AutoCAD 中的圆。它具有以下属性:

  • Area - 获取创建的圆的面积。
  • Center Point - 获取或设置圆的 3 维中心点。
  • Diameter - 圆的直径。
  • Radius - 圆的半径。

AddCircle 函数

    语法:AddCircle ( CenterPoint, Radius )

    -- CenterPoint - 3 维双精度数组

    -- Radius - 表示半径的双精度值

利用上述详细信息,让我们编写在“创建圆”按钮的单击事件中创建圆的代码:

声明全局变量来保存要创建的圆和 AutoCAD 的引用。

// Creating object reference to hold the reference of the circle to be created.
private AcadCircle Circle = default(AcadCircle);

// Creating object reference to hold the reference of running AutoCAD instance.
private AcadApplication AcadApp = default(AcadApplication);

"Create Cirlce" 按钮单击事件。

private void btnCircle_Click(object sender, EventArgs e)
{
  // Getting running AutoCAD instance by Marshalling by passing Programmatic ID as a string, AutoCAD.Application is the Programmatic ID for AutoCAD.
  AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");

  // Now AcadApp has the reference to the running AutoCAD instance, AcadApp object acts as a port for accessing running instance of AutoCAD.

  // Syntax for creating circle in AutoCAD
  // AcadApp.ActiveDocument.ModelSpace.AddCircle(CenterOfCircle, RadiusOfCircle);
  //
  // AcadApp        - Reference to the running instance of AutoCAD.
  // ActiveDocument - Represents the current working drawing in AutoCAD.
  // ModelSpace     - Work Area in the current drawing.
  // AddCricle      - Method, which adds a circle to the modelspace of the current drawing using the CenterPoint and Radius.
  // CenterOfCircle - 3 Dimensional double array variable holds the center point of the circle in the X, Y and Z axis.
  // RadiusOfCircle - Double variable holds the radius of circle.

  // Definfing the center point for the circle, in this example, we are using origin(0,0,0) as the center of circle.
  double[] CenterOfCircle = new double[3];
  CenterOfCircle[0] = 0;
  CenterOfCircle[1] = 0;
  CenterOfCircle[2] = 0;

  // Defining radius of circle from the user input.
  double RadiusOfCircle = Convert.ToDouble(txtRadius.Text.Trim());

  // Adding Circle to the modelspace and getting reference to the circle created.
  Circle = AcadApp.ActiveDocument.ModelSpace.AddCircle(CenterOfCircle, RadiusOfCircle);
}

步骤 4:为圆添加标注

在创建标注之前,让我们了解 `AcadText` 对象。

AcadText 对象
它代表字符串值。它具有以下属性:

  • Height - 获取或设置创建的文本的高度。
  • InsertionPoint - 获取或设置 AcadText 在 3 维双精度数组中的位置。
  • TextString - 获取或设置 AcadText 对象的内容。

AddText 函数

    语法:AddText ( TextString, InsertionPoint, Height )

    -- InsertionPoint - 表示 AcadText 位置的 3 维双精度数组。

    -- TextString - 表示 AcadText 的内容。

    -- Height - 表示 AcadText 的高度。

利用上述详细信息,让我们编写在“添加标注”按钮的单击事件中添加标注的代码:

"Add Caption" 按钮单击事件。

private void btnCaption_Click(object sender, EventArgs e)
{
  // Caption - AcadText object to hold the reference of the AcadText 
  AcadText Caption = default(AcadText);

  // Syntax for creating Text in AutoCAD
  // AcadApp.ActiveDocument.ModelSpace.AddText(TextString, InsertionPoint, Height);
  // 
  // AcadApp        - Reference to the running instance of AutoCAD.
  // ActiveDocument - Represents the current working drawing in AutoCAD.
  // ModelSpace     - Work Area in the current drawing.
  // AddText        - Method, which adds a Text to the modelspace of the current drawing using the TextString, InsertionPoint and Height.
  // TextString     - string, which represents the text to be written.
  // InsertionPoint - 3 Dimensional double array variable holds the insertion point of the circle in the X, Y and Z axis.
  // Height         - Double variable holds the height of text.

  // Text to be written just below the circle as a caption.
  string TextString = "CIRCLE";

  // Definfing the insertion point for the circle, in this example, we are going to place the text just below the circle.
  double[] InsertionPoint = new double[3];

  // Getting Centerpoint of circle we have created earlier to define the insertion point for the Caption.
  double[] CenterOfCircle = (double[])Circle.Center;

  InsertionPoint[0] = CenterOfCircle[0] - Circle.Radius / 2;
  InsertionPoint[1] = CenterOfCircle[1] - (Circle.Radius + 50);
  InsertionPoint[2] = 0;

  // Defining height of the text
  double Height = 20;
            
  // Adding text to the modelspace and getting reference to the text created.
  Caption = AcadApp.ActiveDocument.ModelSpace.AddText(TextString, InsertionPoint, Height);

  // Changing the text color
  Caption.color = ACAD_COLOR.acGreen;
}

步骤 5:添加径向尺寸标注

有许多方法可以为 AutoCAD 对象添加尺寸标注。对于圆,我们需要添加径向尺寸标注,为此,我们将使用 AutoCAD 对象模型中的以下函数:

AddDimRadial

语法:AddDimRadial ( Center, ChordPoint, LeaderLength )

"Add Dimension" 按钮单击事件。

private void btnDimension_Click(object sender, EventArgs e)
{
  // Dimension - AcadDimRadial object to hold the reference of the Radial Dimension of the circle
  AcadDimRadial Dimension = default(AcadDimRadial);

  // Syntax for creating Radial Dimension
  // AcadApp.ActiveDocument.ModelSpace.AddDimRadial(Center, ChordPoint, LeaderLength);
  // 
  // AcadApp        - Reference to the running instance of AutoCAD.
  // ActiveDocument - Represents the current working drawing in AutoCAD.
  // ModelSpace     - Work Area in the current drawing.
  // AddDimRadial   - Method, which adds Radial Dimension to the circle.
  // Center         - 3 Dimensional double array variable holds the center point of the circle in the X, Y and Z axis.
  // ChordPoint     - 3 Dimensional double array variable holds the Chord point of the circle in the X, Y and Z axis.
  // LeaderLength   - Double variable holds the length of the leader line.

  // Assigning Center point of the circle to the center point of radial dimension.
  double[] Center = Circle.Center;

  // Assigning Center Point of circle to ChordPoint
  double[] ChordPoint = Circle.Center;

  // Sets Leader line length
  double LeaderLength = 150;

  Dimension = AcadApp.ActiveDocument.ModelSpace.AddDimRadial(Center, ChordPoint, LeaderLength);

  // Sets the leader line caption
  Dimension.TextOverride = "R" + Circle.Radius.ToString();

  // Sets leader line caption color
  Dimension.TextColor = ACAD_COLOR.acCyan;

  // Sets leader line color
  Dimension.DimensionLineColor = ACAD_COLOR.acMagenta;

  // Sets leader line arrow head size
  Dimension.ArrowheadSize = 15;

  // Sets height of the caption of leader line
  Dimension.TextHeight = 15;
}

结论

根据 AutoCAD 对象模型,每一个 AutoCAD 对象都有其自身的属性和方法。通过使用这些方法,我们可以通过编程来自动化我们的绘图过程。

© . All rights reserved.