偏移对象会创建一个与原始对象具有指定偏移距离的新对象。您可以偏移圆弧、圆、椭圆、直线、轻量级折线、折线、样条和 x线。 若要偏移对象,请使用为该对象提供的方法。该函数需要偏移对象距离的正数值或负数值。如果距离为负数,AutoCAD 会将其解释为偏移以生成“较小”曲线(即,对于圆弧,它将偏移到小于起始曲线半径的给定距离的半径)。如果“较小”没有含义,则AutoCAD将沿较小的X,Y,Z WCS坐标方向偏移。GetOffsetCurves 对于许多对象,此操作的结果将是一条新曲线(可能与原始曲线的类型不同)。例如,偏移椭圆将导致样条曲线,因为结果确实符合椭圆的方程。在某些情况下,偏移结果可能需要是几条曲线。因此,该函数返回一个对象,该对象包含通过偏移曲线创建的所有对象。对于创建的每个对象,需要对返回的对象进行迭代,然后将其追加到图形数据库中。DBObjectCollectionDBObjectCollection 偏移折线本示例创建一条轻量级折线,然后对其进行偏移。 VB.NETImports 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 Using acPoly As Polyline = New Polyline() 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 End Using '' 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 using (Polyline acPoly = new Polyline()) { 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 代码参考Sub OffsetObject() ' Create the polyline Dim plineObj As AcadLWPolyline Dim points(0 To 11) As Double points(0) = 1: points(1) = 1 points(2) = 1: points(3) = 2 points(4) = 2: points(5) = 2 points(6) = 3: points(7) = 2 points(8) = 4: points(9) = 4 points(10) = 4: points(11) = 1 Set plineObj = ThisDrawing.ModelSpace. _ AddLightWeightPolyline(points) plineObj.Closed = True ZoomAll ' Offset the polyline Dim offsetObj As Variant offsetObj = plineObj.Offset(0.25) ZoomAll End Sub 相关概念父主题: |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-1-8 19:24
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.