分享

02控制AutoCAD环境(七) 精确制图

 Jcstone 2012-09-09

Draw with Precision  精确绘图

With AutoCAD you can create your drawings with precise geometry without performing tedious calculations. Often you can specify precise points without knowing the coordinates. Without leaving the drawing screen, you can perform calculations on your drawing and display various types of status information.

 使用AutoCAD,我们可以进行精确的几何制图而无需进行繁琐的计算。通常我们无需知道坐标就能精确指定点。不用离开制图屏幕,我们就可以对图形执行计算并显示各种状态信息。

Topics in this section  本节主题

·         Adjust Snap and Grid Alignment 调整捕捉和栅格对齐

·         Use Ortho Mode 使用正交模式

·         Calculate Points and Values 计算点和值

·         Calculate Areas 计算面积

1、Adjust Snap and Grid Alignment调整捕捉和栅格对齐

The grid is a visual guideline to measure distances, while Snap mode is used to restrict cursor movement. In addition to setting the spacing for the grid and Snap mode, you can adjust the rotation and type of snap used.

栅格是可以度量距离的视觉化导线,捕捉模式用来限制光标移动。除了设置栅格间距和捕捉模式,我们还可以调整捕捉旋转角度和捕捉类型。

If you need to draw along a specific alignment or angle, you can rotate the snap angle. The center point of the snap angle rotation is the snap base point.

如果需要沿某一基线或角度绘图,我们可以旋转捕捉角度。捕捉角度旋转的中心点就是捕捉的基点。

NoteAfter changing the snap and grid settings for the active viewport, you should use the UpdateTiledViewportsFromDatabase method of the Editor object to update the display of the drawing area.

注意:修改了活动视口的捕捉和栅格设置后,应调用Editor对象的UpdateTiledViewportsFromDatabase方法更新一下绘图区域的显示。

Snap and grid do not affect points specified through the .NET API, but do affect points specified in the drawing area by the user if they are requested to enter input using methods such as GetPoint or GetEntity. See “Adjust Grid and Grid Snap” in the AutoCAD User's Guide for more information on using and setting snaps and grids.

捕捉和栅格不影响通过.NET API指定的点,但确实会影响到用户在绘图区域指定的点,如果这些点是使用GetPoint或GetEntity这样的方法要求用户输入的点的话。关于使用和设置捕捉和栅格的更多信息,见AutoCAD用户指南中的“调整栅格及栅格捕捉”一节。

Change the grid and snap settings 修改栅格和捕捉设置

This example changes the snap base point to (1,1) and the snap rotation angle to 30 degrees. The grid is turned on the spacing is adjusted so that the changes are visible.

本例修改捕捉基点到(1,1)并修改捕捉旋转角为30度。打开栅格并调整间距使修改可见。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.Geometry

<CommandMethod("ChangeGridAndSnap")> _

Public Sub ChangeGridAndSnap()

  '' Get the current database

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

  Dim acCurDb As Database = acDoc.Database

   '' Start a transaction

  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

      '' Open the active viewport

      Dim acVportTblRec As ViewportTableRecord

      acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _

                                        OpenMode.ForWrite)

      '' Turn on the grid for the active viewport

      acVportTblRec.GridEnabled = True

      '' Adjust the spacing of the grid to 1, 1

      acVportTblRec.GridIncrements = New Point2d(1, 1)

      '' Turn on the snap mode for the active viewport

      acVportTblRec.SnapEnabled = True

      '' Adjust the snap spacing to 0.5, 0.5

      acVportTblRec.SnapIncrements = New Point2d(0.5, 0.5)

      '' Change the snap base point to 1, 1

      acVportTblRec.SnapBase = New Point2d(1, 1)

      '' Change the snap rotation angle to 30 degrees (0.524 radians)

      acVportTblRec.SnapAngle = 0.524

      '' Update the display of the tiled viewport

      acDoc.Editor.UpdateTiledViewportsFromDatabase()

      '' Commit the changes and dispose of the transaction

      acTrans.Commit()

  End Using

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Runtime;

namespace FunGridSnap

{

    public class Class1

    {

        [CommandMethod("ChangeGridAndSnap")]

        public static void ChangeGridAndSnap()

        {

            // Get the current database

            //获取当前数据库

            Document acDoc = Application.DocumentManager.MdiActiveDocument;

            Database acCurDb = acDoc.Database;

            // Start a transaction启动事务;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())

            {

                // Open the active viewport;

                //打开活动视口;

                ViewportTableRecord acVportTblRec;

                acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,

                                                  OpenMode.ForWrite) as ViewportTableRecord;

                // Turn on the grid for the active viewport;

                //活动视口栅格开;

                acVportTblRec.GridEnabled = true;

                // Adjust the spacing of the grid to 1, 1

                //调整栅格间距为1,1;

                acVportTblRec.GridIncrements = new Point2d(1, 1);

                // Turn on the snap mode for the active viewport;

                //打开活动视口的捕捉模式;

                acVportTblRec.SnapEnabled = true;

                // Adjust the snap spacing to 0.5, 0.5

                //调整捕捉间距为0.5,0.5;

                acVportTblRec.SnapIncrements = new Point2d(0.5, 0.5);

                // Change the snap base point to 1, 1

                //修改捕捉基点为1,1;

                acVportTblRec.SnapBase = new Point2d(1, 1);

                // Change the snap rotation angle to 30 degrees (0.524 radians)

                //修改捕捉旋转角为30读(0.524弧度)

                acVportTblRec.SnapAngle = 0.524;

                // Update the display of the tiled viewport;

                //更新平铺视口的显示;

                acDoc.Editor.UpdateTiledViewportsFromDatabase();

                // Commit the changes and dispose of the transaction

                //提交修改,关闭事务;

                acTrans.Commit();

            }

        }

    }

}

 

VBA/ActiveX Code Reference

Sub ChangeGridAndSnap()

    ' Turn on the grid for the active viewport

    ThisDrawing.ActiveViewport.GridOn = True

    ' Adjust the spacing of the grid to 1, 1

    ThisDrawing.ActiveViewport.SetGridSpacing 1, 1

    ' Turn on the snap mode for the active viewport

    ThisDrawing.ActiveViewport.SnapOn = True

    ' Adjust the snap spacing to 0.5, 0.5

    ThisDrawing.ActiveViewport.SetSnapSpacing 0.5, 0.5

    ' Change the snap base point to 1, 1

    Dim newBasePoint(0 To 1) As Double

    newBasePoint(0) = 1: newBasePoint(1) = 1

    ThisDrawing.ActiveViewport.SnapBasePoint = newBasePoint

    ' Change the snap rotation angle to 30 degrees (0.524 radians)

    Dim rotationAngle As Double

    rotationAngle = 0.524

    ThisDrawing.ActiveViewport.SnapRotationAngle = rotationAngle

    ' Reset the viewport

    ThisDrawing.ActiveViewport = ThisDrawing.ActiveViewport

End Sub

 

 2、Use Ortho Mode使用正交模式

As you draw lines or move objects, you can use Ortho mode to restrict the cursor to the horizontal or vertical axis. The orthogonal alignment is dependent on the current snap angle and UCS. Ortho mode works with activities that require you to specify a second point, such as when using the GetDistance or GetAngle methods. You can use Ortho not only to establish vertical or horizontal alignment but also to enforce parallelism or create regular offsets.

绘制直线或移动对象时,我们可以使用正交模式将光标限制在水平方向或垂直方向。正交对齐依赖于当前捕捉角和用户坐标系。正交模式应用于需要指定第2个点的情形下,比如当使用GetDistance方法或GetAngle方法时。使用正交,不仅可以建立垂直或水平对齐,还可以强制平行或创建端正的偏移。

By allowing AutoCAD to impose orthogonal restraints, you can draw more quickly. For example, you can create a series of perpendicular lines by turning on Ortho mode before you start drawing. Because the lines are constrained to the horizontal and vertical axes, you can draw faster, knowing that the lines are perpendicular.

通过让AutoCAD强制使用正交约束,我们可以更快地绘制图形。例如,通过在开始绘图前打开正交模式,我们可以创建一系列垂直直线。由于直线被限制在水平或垂直方向上,我们知道绘出来的线肯定是水平或垂直的,所以可以绘得更快。

         

The following statements turn Ortho mode on. Unlike the grid and snap settings, Ortho mode is maintained in the Database object instead of the active viewport.

下列代码打开正交模式,不像栅格和捕捉设置,正交模式在Database对象中维护,而不是在活动视口维护。

VB.NET

Application.DocumentManager.MdiActiveDocument.Database.Orthomode = True

C#

Application.DocumentManager.MdiActiveDocument.Database.Orthomode = true;

VBA/ActiveX Code Reference

ThisDrawing.ActiveViewport.OrthoOn = True

 

 3、Calculate Points and Values计算点和值

By using the methods provided by the Editor object and the Geometry and Runtime namespaces, you can quickly solve a mathematical problem or locate points in your drawing. Some of the available methods are:

通过使用Editor对象及Geometry和Runtime命名空间提供的方法,我们可以快速解决数学问题或定位图形中的点。可用的方法举例如下:

·         Get the distance between two 2D or 3D points using the GetDistanceTo and DistanceTo methods 使用GetDistanceTo方法和DistanceTo方法获取两个2D点或3D点间的距离;

·         Get the angle from the X-axis using two 2D points using the GetVectorTo method with the Angle property of the returned value 使用GetVectorTo方法的返回值的Angle属性获取两个2D点对x轴的夹角;

·         Convert an angle as a string to a real (double) value with the StringToAngle method 使用StringToAngle方法将字串型角度值转换为实数值(双精度);

·         Convert an angle from a real (double) value to a string with the AngleToString method 使用AngleToString方法将角度从实数值(双精度)转换为字串值;

·         Convert a distance from a string to a real (double) value with the StringToDistance method 使用StringToDistance方法将距离从字串转换为实数值(双精度);

·         Find the distance between two points entered by the user with the GetDistance method 使用GetDistance方法求用户输入的两点之间的距离;

NoteThe .NET API does not contain methods to calculate a point based on a distance and angle (polar point) and for translating coordinates between different coordinate systems. If you need these utilities, you will want to utilize the PolarPoint and TranslateCoordinates methods from the ActiveX Automation library.

注意:.NET API没有提供根据距离和角(极轴点)求点的方法,及在不同坐标系间转换坐标的方法。如果我们需要这些工具,要从ActiveX Automation库调用PolarPoint和TranslateCoordinates方法。

Get angle from X-axis 获取对x轴的角度

This example calculates a vector between two points and determines the angle from the X-axis.

本例计算两点间的向量,以及该向量对x轴的角度。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.Geometry

<CommandMethod("AngleFromXAxis")> _

Public Sub AngleFromXAxis()

  Dim pt1 As Point2d = New Point2d(2, 5)

  Dim pt2 As Point2d = New Point2d(5, 2)

  Application.ShowAlertDialog("Angle from XAxis: " & _

                              pt1.GetVectorTo(pt2).Angle.ToString())

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Geometry;

[CommandMethod("AngleFromXAxis")]

public static void AngleFromXAxis()

{

  Point2d pt1 = new Point2d(2, 5);

  Point2d pt2 = new Point2d(5, 2);

  Application.ShowAlertDialog("Angle from XAxis: " +

                              pt1.GetVectorTo(pt2).Angle.ToString());

}

VBA/ActiveX Code Reference

Sub AngleFromXAxis()

    ' This example finds the angle, in radians, between the X axis

    ' and a line defined by two points.

    Dim pt1(0 To 2) As Double

    Dim pt2(0 To 2) As Double

    Dim retAngle As Double

    pt1(0) = 2: pt1(1) = 5: pt1(2) = 0

    pt2(0) = 5: pt2(1) = 2: pt2(2) = 0

    ' Return the angle

    retAngle = ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)

    ' Display the angle found

    MsgBox "The angle in radians between the X axis is " & retAngle

End Sub

Calculate Polar Point 计算极轴点

This example calculates a point based on a base point, an angle and distance.

本例基于基点、角度、距离求一个点。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.Geometry

Public Shared Function PolarPoints(ByVal pPt As Point2d, _

                                   ByVal dAng As Double, _

                                   ByVal dDist As Double)

   Return New Point2d(pPt.X + dDist * Math.Cos(dAng), _

                     pPt.Y + dDist * Math.Sin(dAng))

End Function

Public Shared Function PolarPoints(ByVal pPt As Point3d, _

                                   ByVal dAng As Double, _

                                   ByVal dDist As Double)

  Return New Point3d(pPt.X + dDist * Math.Cos(dAng), _

                     pPt.Y + dDist * Math.Sin(dAng), _

                     pPt.Z)

End Function

 <CommandMethod("PolarPoints")> _

Public Sub PolarPoints()

  Dim pt1 As Point2d

  pt1 = PolarPoints(New Point2d(5, 2), 0.785398, 12)

  Application.ShowAlertDialog(vbLf & "PolarPoint: " & _

                              vbLf & "X = " & pt1.X & _

                              vbLf & "Y = " & pt1.Y)

  Dim pt2 As Point3d

  pt2 = PolarPoints(New Point3d(5, 2, 0), 0.785398, 12)

  Application.ShowAlertDialog(vbLf & "PolarPoint: " & _

                              vbLf & "X = " & pt2.X & _

                              vbLf & "Y = " & pt2.Y & _

                              vbLf & "Z = " & pt2.Z)

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Geometry;

 static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)

{

  return new Point2d(pPt.X + dDist * Math.Cos(dAng),

                     pPt.Y + dDist * Math.Sin(dAng));

}

static Point3d PolarPoints(Point3d pPt, double dAng, double dDist)

{

  return new Point3d(pPt.X + dDist * Math.Cos(dAng),

                     pPt.Y + dDist * Math.Sin(dAng),

                     pPt.Z);

}

 [CommandMethod("PolarPoints")]

public static void PolarPoints()

{

  Point2d pt1 = PolarPoints(new Point2d(5, 2), 0.785398, 12);

   Application.ShowAlertDialog("\nPolarPoint: " +

                              "\nX = " + pt1.X +

                              "\nY = " + pt1.Y);

   Point3d pt2 = PolarPoints(new Point3d(5, 2, 0), 0.785398, 12);

   Application.ShowAlertDialog("\nPolarPoint: " +

                              "\nX = " + pt2.X +

                              "\nY = " + pt2.Y +

                              "\nZ = " + pt2.Z);

}

VBA/ActiveX Code Reference

Sub PolarPoints()

    ' This example finds the coordinate of a point that is a given

    ' distance and angle from a base point.

    Dim polarPnt As Variant

    Dim basePnt(0 To 2) As Double

    Dim angle As Double

    Dim distance As Double

    basePnt(0) = 2#: basePnt(1) = 2#: basePnt(2) = 0#

    angle = 0.785398

    distance = 6

    polarPnt = ThisDrawing.Utility.PolarPoint(basePnt, angle, distance)

    MsgBox vbLf + "PolarPoint: " + _

           vbLf + "X = " + CStr(polarPnt(0)) + _

           vbLf + "Y = " + CStr(polarPnt(1)) + _

           vbLf + "Z = " + CStr(polarPnt(2))

End Sub

Find the distance between two points with the GetDistance method 用GetDistance方法计算两点间距离

This example uses the GetDistance method to obtain two points and displays the calculated distance.

本例使用GetDistance方法获得两个点,计算并显示两点间距离。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

 <CommandMethod("GetDistanceBetweenTwoPoints")> _

Public Sub GetDistanceBetweenTwoPoints()

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

   Dim pDblRes As PromptDoubleResult

  pDblRes = acDoc.Editor.GetDistance(vbLf & "Pick two points: ")

   Application.ShowAlertDialog(vbLf & "Distance between points: " &  pDblRes.Value.ToString())

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

 [CommandMethod("GetDistanceBetweenTwoPoints")]

public static void GetDistanceBetweenTwoPoints()

{

  Document acDoc = Application.DocumentManager.MdiActiveDocument;

   PromptDoubleResult pDblRes;

  pDblRes = acDoc.Editor.GetDistance("\nPick two points: ");

   Application.ShowAlertDialog("\nDistance between points: " +  pDblRes.Value.ToString());

}

VBA/ActiveX Code Reference

Sub GetDistanceBetweenTwoPoints()

    Dim returnDist As Double

     ' Return the value entered by user. A prompt is provided.

    returnDist = ThisDrawing.Utility.GetDistance(, "Pick two points.")

    MsgBox "The distance between the two points is: " & returnDist

End Sub

 

 

4、Calculate Areas计算面积

You can find the area of an arc, circle, ellipse, lightweight polyline, polyline, region, hatch, planar-closed spline or any other entity that is derived from the base type of Curve by using the Area property.

我们可以使用Area属性计算下面这些实体的面积:圆弧、圆、椭圆、优化多段线、多段线、面域、填充、平面闭合样条曲线、及其他派生自基类Curve的实体。

If you need to calculate the combined area of more than one object, you can keep a running total as you add or use the Boolean method on a series of regions to obtain a single region representing the desired area. From this single region you can use the Area property to obtain its area.

如果需要计算不止一个对象的组合面积,我们可以求出每个对象的面积再汇总,或者,对一系列面域使用Boolean方法获得代表所求面积的单一面域。对这个单一面域,我们可以用Area属性求出其面积。

The calculated area differs according to the type of object you query. For an explanation of how area is calculated for each object type, see “Obtain Area and Mass Properties Information” in the AutoCAD User's Guide.

计算所得面积因所查询对象的类型不同而有所不同。有关每种类型对象面积的计算方法的解释,见AutoCAD用户指南“获取Area属性及Mass属性信息”一节。

Topics in this section本小节主题

·         Calculate a Defined Area 计算给定面积

 

 

4.1、Calculate a Defined Area计算给定面积

If the area you want to calculate is based on user specified points, you might consider creating an in memory object such as a lightweight polyline, and then query of the area of the object before discarding it. The following steps explain how you might accomplish this:

如果需要计算的面积是基于用户给定点构成的,我们应考虑创建一个内存对象,比如轻量多段线等,然后在放弃这个对象前查询其面积。下列步骤解释实现方法:

1.     Use the GetPoint method in a loop to obtain the points from the user. 使用GetPoint方法循环获取用户输入的点;

2.     Create a lightweight polyline from the points provided by the user. Create a new Polyline object. Specify the number of vertices and the points they should be at. 用这些点创建轻量多段线。新建一个Polyline对象,指定顶点数及各点位置;

3.     Use the Area property to obtain the area of the newly created polyline. 使用Area属性获取新建的多段线的面积;

4.     Dispose of the polyline using its Dispose method. 使用多段线的Dispose方法销毁多段线(释放内存)。

Calculate the area defined by points entered from the user 计算有用户输入点定义的面积

This example prompts the user to enter five points. A polyline is then created out of the points entered. The polyline is closed, and the area of the polyline is displayed in a message box. Since the polyline is not added to a block, it needs to be disposed before the command ends.

本例提示用户输入5个点,然后用这5个点创建一个多段线,闭合多段线,查询多段线面积并显示在消息框内。因为无需将多段线添加到块中,因此命令结束前要将其销毁以释放内存。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.Geometry

Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.AutoCAD.Runtime

<CommandMethod("CalculateDefinedArea")> _

Public Sub CalculateDefinedArea()

  '' Prompt the user for 5 points

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

  Dim pPtRes As PromptPointResult

  Dim colPt As Point2dCollection = New Point2dCollection

  Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")

  '' Prompt for the first point

  pPtOpts.Message = vbLf & "Specify first point: "

  pPtRes = acDoc.Editor.GetPoint(pPtOpts)

  colPt.Add(New Point2d(pPtRes.Value.X, pPtRes.Value.Y))

  '' Exit if the user presses ESC or cancels the command

  If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

  Dim nCounter As Integer = 1

  While (nCounter <= 4)

      '' Prompt for the next points

      Select Case nCounter

          Case 1

              pPtOpts.Message = vbLf & "Specify second point: "

          Case 2

              pPtOpts.Message = vbLf & "Specify third point: "

          Case 3

              pPtOpts.Message = vbLf & "Specify fourth point: "

          Case 4

              pPtOpts.Message = vbLf & "Specify fifth point: "

      End Select

      '' Use the previous point as the base point

      pPtOpts.UseBasePoint = True

      pPtOpts.BasePoint = pPtRes.Value

       pPtRes = acDoc.Editor.GetPoint(pPtOpts)

      colPt.Add(New Point2d(pPtRes.Value.X, pPtRes.Value.Y))

      If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

       '' Increment the counter

      nCounter = nCounter + 1

  End While

   '' Create a polyline with 5 points

  Using acPoly As Polyline = New Polyline()

      acPoly.AddVertexAt(0, colPt(0), 0, 0, 0)

      acPoly.AddVertexAt(1, colPt(1), 0, 0, 0)

      acPoly.AddVertexAt(2, colPt(2), 0, 0, 0)

      acPoly.AddVertexAt(3, colPt(3), 0, 0, 0)

      acPoly.AddVertexAt(4, colPt(4), 0, 0, 0)

      '' Close the polyline

      acPoly.Closed = True

       '' Query the area of the polyline

      Application.ShowAlertDialog("Area of polyline: " & _

                                  acPoly.Area.ToString())

       '' Dispose of the polyline

  End Using

End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

 [CommandMethod("CalculateDefinedArea")]

public static void CalculateDefinedArea()

{

  // Prompt the user for 5 points提示用户输入5个点

  Document acDoc = Application.DocumentManager.MdiActiveDocument;

   PromptPointResult pPtRes;

  Point2dCollection colPt = new Point2dCollection();

  PromptPointOptions pPtOpts = new PromptPointOptions("");

   // Prompt for the first point提示第1个点

  pPtOpts.Message = "\nSpecify first point: ";

  pPtRes = acDoc.Editor.GetPoint(pPtOpts);

  colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));

   // Exit if the user presses ESC or cancels the command

  //如果用户按ESC键或取消命令就退出

  if (pPtRes.Status == PromptStatus.Cancel) return;

   int nCounter = 1;

  while (nCounter <= 4)

  {

      // Prompt for the next points提示下一个点

      switch(nCounter)

      {

          case 1:

              pPtOpts.Message = "\nSpecify second point: ";

              break;

          case 2:

              pPtOpts.Message = "\nSpecify third point: ";

              break;

          case 3:

              pPtOpts.Message = "\nSpecify fourth point: ";

              break;

          case 4:

              pPtOpts.Message = "\nSpecify fifth point: ";

              break;

      }

      // Use the previous point as the base point用前一个点为基点

      pPtOpts.UseBasePoint = true;

      pPtOpts.BasePoint = pPtRes.Value;

      pPtRes = acDoc.Editor.GetPoint(pPtOpts);

      colPt.Add(new Point2d(pPtRes.Value.X, pPtRes.Value.Y));

      if (pPtRes.Status == PromptStatus.Cancel) return;

      // Increment the counter计数加1

      nCounter = nCounter + 1;

  }

   // Create a polyline with 5 points用5个点创建多段线

  using (Polyline acPoly = new Polyline())

  {

      acPoly.AddVertexAt(0, colPt[0], 0, 0, 0);

      acPoly.AddVertexAt(1, colPt[1], 0, 0, 0);

      acPoly.AddVertexAt(2, colPt[2], 0, 0, 0);

      acPoly.AddVertexAt(3, colPt[3], 0, 0, 0);

      acPoly.AddVertexAt(4, colPt[4], 0, 0, 0);

       // Close the polyline闭合多段线

      acPoly.Closed = true;

       // Query the area of the polyline查询多段线面积

      Application.ShowAlertDialog("Area of polyline: " + acPoly.Area.ToString());

      // Dispose of the polyline 销毁多段线

  }

}

 

VBA/ActiveX Code Reference

  VBA/ActiveX代码参考

This example prompts the user to enter five points. A polyline is then created out of the points entered. The polyline is closed, and the area of the polyline is displayed in a message box. Unlike the .NET API examples, the polyline is not created in memory but as a database resident object and added to Model space. So after the area of the polyline is obtained, it is removed.

本例与上面的.NET API例子过程相同,所不同的是,本例里多段线不是创建在内存中,而是作为数据库驻留对象添加到模型空间,因此获取多段线面积后,将其删除。

 

Sub CalculateDefinedArea()

    Dim p1 As Variant

    Dim p2 As Variant

    Dim p3 As Variant

    Dim p4 As Variant

    Dim p5 As Variant

     ' Get the points from the user

    p1 = ThisDrawing.Utility.GetPoint(, vbCrLf & "Specify first point: ")

    p2 = ThisDrawing.Utility.GetPoint(p1, vbCrLf & "Specify second point: ")

    p3 = ThisDrawing.Utility.GetPoint(p2, vbCrLf & "Specify third point: ")

    p4 = ThisDrawing.Utility.GetPoint(p3, vbCrLf & "Specify fourth point: ")

    p5 = ThisDrawing.Utility.GetPoint(p4, vbCrLf & "Specify fifth point: ")

     ' Create the 2D polyline from the points

    Dim polyObj As AcadLWPolyline

    Dim vertices(0 To 9) As Double

    vertices(0) = p1(0): vertices(1) = p1(1)

    vertices(2) = p2(0): vertices(3) = p2(1)

    vertices(4) = p3(0): vertices(5) = p3(1)

    vertices(6) = p4(0): vertices(7) = p4(1)

    vertices(8) = p5(0): vertices(9) = p5(1)

    Set polyObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(vertices)

    polyObj.Closed = True

    ' Display the area for the polyline

    MsgBox "The area defined by the points is " & _

           polyObj.Area, , "Calculate Defined Area"

     ' Remove the polyline

    polyObj.Delete

End Sub

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多