偏移对象
 
 

Offsetting an object creates a new object at a specified offset distance from the original object. You can offset arcs, circles, ellipses, lines, lightweight polylines, polylines, splines, and xlines.

偏移对象可以在距原始对象的指定偏移距离处创建新的对象。可以偏移圆弧、圆、椭圆、直线、优化多段线、多段线、样条曲线和构造线。

To offset an object, use the GetOffsetCurves method provided for that object. The function requires a positive or negative numeric value for the distance to offset the object. If the distance is negative, it is interpreted by AutoCAD as being an offset to make a “smaller” curve (that is, for an arc it would offset to a radius that is the given distance less than the starting curve's radius). If “smaller” has no meaning, then AutoCAD would offset in the direction of smaller X,Y,Z WCS coordinates.

要偏移对象,请使用该对象的 GetOffsetCurves 方法。这个函数需要一个正的或负的数值作为偏移对象的距离。如果此距离为负,则 AutoCAD 会将其解释为一段用于生成“更小”曲线的偏移(即对于圆弧来说,将偏移到小于起始曲线的半径给定距离的半径)。如果“更小”没有意义,则 AutoCAD 将在更小的 X,Y,Z WCS 坐标方向上偏移。

For many objects, the result of this operation will be a single new curve (which may not be of the same type as the original curve). For example, offsetting an ellipse will result in a spline because the result does fit the equation of an ellipse. In some cases it may be necessary for the offset result to be several curves. Because of this, the function returns a DBObjectCollection object, which contains all the objects that are created by offsetting the curve. The returned DBObjectCollection object needs to be iterated for each object created and then be appended to the drawing database.

对于许多对象,此操作的结果是一条新的曲线(可能与原始曲线的类型不同)。例如,偏移椭圆会形成样条曲线,因为结果的确符合椭圆的表达式。在某些情况下,偏移结果可能会形成若干条曲线。因此,该函数会返回 DBObjectCollection 对象,它包含通过偏移曲线所创建的所有对象。返回的 DBObjectCollection 对象需要循环每一个被创建的对象然后追加到图形数据库中。

For more information about offsetting objects, see “Copy, Offset, or Mirror Objects” in the AutoCAD User's Guide.

有关偏移对象的详细信息,请参见《AutoCAD 用户手册》中的“复制、偏移或镜像对象”。

偏移多段线

This example creates a lightweight polyline and then offsets it.

本例创建一条优化多段线,然后偏移该多段线。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("OffsetObject")> _
  Public Sub OffsetObject()
  '' 获得当前文档和数据库   Get the current document and 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 Block table for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                   OpenMode.ForRead)
 
      '' 以写方式打开模型空间块表记录   Open the Block table record Model space for write
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                      OpenMode.ForWrite)
 
      '' 创建一条轻量多段线     Create a lightweight polyline
      Dim acPoly As Polyline = New Polyline()
      acPoly.SetDatabaseDefaults()
      acPoly.AddVertexAt(0, New Point2d(1, 1), 0, 0, 0)
      acPoly.AddVertexAt(1, New Point2d(1, 2), 0, 0, 0)
      acPoly.AddVertexAt(2, New Point2d(2, 2), 0, 0, 0)
      acPoly.AddVertexAt(3, New Point2d(3, 2), 0, 0, 0)
      acPoly.AddVertexAt(4, New Point2d(4, 4), 0, 0, 0)
      acPoly.AddVertexAt(5, New Point2d(4, 1), 0, 0, 0)
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acPoly)
      acTrans.AddNewlyCreatedDBObject(acPoly, True)
 
      '' 将多段线偏移一定距离   Offset the polyline a given distance
      Dim acDbObjColl As DBObjectCollection = acPoly.GetOffsetCurves(0.25)
 
      '' 遍历新创建的对象    Step through the new objects created
      For Each acEnt As Entity In acDbObjColl
          '' 添加每个偏移的对象    Add each offset object
          acBlkTblRec.AppendEntity(acEnt)
          acTrans.AddNewlyCreatedDBObject(acEnt, True)
      Next
 
      '' 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit()
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("OffsetObject")]
public static void OffsetObject()
{
  // 获得当前文档和数据库   Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 启动一个事务  Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 以只读方式打开块表   Open the Block table for read
      BlockTable acBlkTbl;
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                   OpenMode.ForRead) as BlockTable;
 
      // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
      BlockTableRecord acBlkTblRec;
      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                      OpenMode.ForWrite) as BlockTableRecord;
 
      // 创建一条轻量多段线     Create a lightweight polyline
      Polyline acPoly = new Polyline();
      acPoly.SetDatabaseDefaults();
      acPoly.AddVertexAt(0, new Point2d(1, 1), 0, 0, 0);
      acPoly.AddVertexAt(1, new Point2d(1, 2), 0, 0, 0);
      acPoly.AddVertexAt(2, new Point2d(2, 2), 0, 0, 0);
      acPoly.AddVertexAt(3, new Point2d(3, 2), 0, 0, 0);
      acPoly.AddVertexAt(4, new Point2d(4, 4), 0, 0, 0);
      acPoly.AddVertexAt(5, new Point2d(4, 1), 0, 0, 0);
 
      // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acPoly);
      acTrans.AddNewlyCreatedDBObject(acPoly, true);
 
      // 将多段线偏移一定距离   Offset the polyline a given distance
      DBObjectCollection acDbObjColl = acPoly.GetOffsetCurves(0.25);
 
      // 遍历新创建的对象    Step through the new objects created
      foreach (Entity acEnt in acDbObjColl)
      {
          // 添加每个偏移的对象    Add each offset object
          acBlkTblRec.AppendEntity(acEnt);
          acTrans.AddNewlyCreatedDBObject(acEnt, true);
      }
 
      // 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考