偏移对象会在距原始对象的指定偏移距离处创建一个新对象。可以偏移圆弧、圆、椭圆、直线、轻量折线、折线、样条曲线和x线。 若要偏移对象,请使用为该对象提供的方法。该函数需要距离的正数值或负数值来偏移对象。如果距离为负,AutoCAD 将其解释为偏移以生成“较小”曲线(即,对于弧,它将偏移到小于起始曲线半径的给定距离的半径)。如果“较小”没有意义,则AutoCAD将沿较小的X,Y,ZWCS坐标方向偏移。GetOffsetCurves 对于许多对象,此操作的结果将是一条新曲线(可能与原始曲线的类型不同)。例如,偏移椭圆将产生样条曲线,因为结果确实符合椭圆的方程。在某些情况下,偏移结果可能需要为多条曲线。因此,该函数返回 aobject,其中包含通过偏移曲线创建的所有对象。需要为每个创建的对象迭代返回的对象,然后将其追加到图形数据库中。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 )
GMT+8, 2024-5-19 15:01
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.