.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to offset pline?

4 REPLIES 4
Reply
Message 1 of 5
wesbird
863 Views, 4 Replies

How to offset pline?

Hi
I am new of .net. In VBA, RT = oPl.Offset(dist) will return the offset result of oPl. How do I do it in .net?
I want get the offset polyline point list. I found this in ObjectARX help file, AcGeOffsetCurve2d class. Am I on the right track? is there any sample about it?


Thank you
Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
4 REPLIES 4
Message 2 of 5
ChrisArps
in reply to: wesbird

Best would be to use the Autodesk.AutoCAD.Geometry.OffsetCurve2d method If you want to stay with dot net. This is the same as the AcGeOffsetCurve2d class.

You can also do it with COM interop with the
Autodesk.AutoCAD.Interop.Common.IAcadPolyline.Offset method. This is the same as VBA.

Chris Arps
Message 3 of 5
wesbird
in reply to: wesbird

Thank you.
when I working in the code, I found I have problem to create a object as Curve2d. Could you what's wrong in my code? Thank you very much.

here is the code:
using System ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[assembly: CommandClass(typeof(ClassLibrary.RSClass))]

namespace ClassLibrary
{
///
/// Summary description for RSClass.
///

public class RSClass
{

// Define Command "AsdkCmd1"
[CommandMethod("OffsetEnt")]
static public void test() // This method can have any name
{
// Put your command code here
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Transaction trans = db.TransactionManager.StartTransaction();

try
{
PromptEntityOptions prEnt = new PromptEntityOptions("Select a polyline:");
PromptEntityResult prEntRes = ed.GetEntity(prEnt);

if (prEntRes.Status != PromptStatus.OK )
{
return;
}
ObjectId id = prEntRes.ObjectId;
Entity opl = (Entity)trans.GetObject(id,OpenMode.ForWrite,true);
Curve2d cvPl = (Curve2d)opl; // <<<--- here is my problem.
OffsetCurve2d oOff = new OffsetCurve2d(cvPl, 2);
}
finally
{
}
}

}
} Message was edited by: weslleywang
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
Message 4 of 5
ChrisArps
in reply to: wesbird

I tried using the curve2d class until I saw the methods on the polyline object itself. It is probably not as efficient as getting the AcGe objects to work, but this code does work.

Notice the transaction manager code, you must commit your modifications when done, you must abort if an exception is done, and it is good to call dispose in the finally clause to clean up.

Excuse the vb.net reply, you can always translate it online.


Try
Dim prEnt As New PromptEntityOptions("Select a polyline:")
Dim prEntRes As PromptEntityResult = editor.GetEntity(prEnt)
If prEntRes.Status <> PromptStatus.OK Then Return

Dim id As ObjectId = prEntRes.ObjectId
' get input polyline
Dim ent As Entity = DirectCast(trans.GetObject(id, OpenMode.ForRead), Entity)
Dim pl As Polyline = DirectCast(ent, Polyline)

Dim curves As DBObjectCollection = pl.GetOffsetCurves(2.0)
Dim i As Integer
Dim s As String
For i = 0 To curves.Count - 1
Dim newPl As Polyline = DirectCast(curves.Item(i), Polyline)
Dim isNew As Boolean = newPl.IsNewObject
Dim numSegs As Integer = newPl.NumberOfVertices
If newPl.Closed = False Then numSegs -= 1
Dim j As Integer
For j = 0 To numSegs - 1
Dim segType As SegmentType = newPl.GetSegmentType(j)
If segType = SegmentType.Line Then
Dim seg2 As LineSegment2d
Dim seg3 As LineSegment3d
seg2 = newPl.GetLineSegment2dAt(j)
seg3 = newPl.GetLineSegmentAt(j)
ElseIf segType = SegmentType.Arc Then
Dim a2 As CircularArc2d = newPl.GetArcSegment2dAt(j)
Dim a3 As CircularArc3d = newPl.GetArcSegmentAt(j)
End If
Next
Next
curves.Dispose()
trans.Commit()
Catch ex As Exception
trans.Abort()
MsgBox(ex.Message)
Finally
trans.Dispose()
End Try
Message 5 of 5
wesbird
in reply to: wesbird

Thank you very much. it works like a clock.



Wes
Windows 10 64 bit, AutoCAD (ACA, Map) 2023

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost