using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Rivilis.PlineArcToArc))]
#pragma warning disable CS0618
namespace Rivilis
{
public class PlineArcToArc
{
public static bool DivideArc2(Arc arc, out Arc arc1, out Arc arc2)
{
arc1 = arc2 = null;
DoubleCollection parms = new DoubleCollection();
parms.Add(arc.StartParam);
parms.Add((arc.StartParam + arc.EndParam) * 0.5);
parms.Add(arc.EndParam);
DBObjectCollection dbobjs = arc.GetSplitCurves(parms);
if (dbobjs.Count == 2)
{
arc1 = dbobjs[0] as Arc;
arc2 = dbobjs[1] as Arc;
return true;
}
return false;
}
public static CircularArc3d GetGeArc3dFromArc(Arc arc)
{
CircularArc3d arc3d =
new CircularArc3d(
arc.Center,
arc.Normal,
arc.Normal.GetPerpendicularVector(),
arc.Radius,
arc.StartAngle,
arc.EndAngle);
return arc3d;
}
public static Arc GetArcFromGeArc3d(CircularArc3d arc3d)
{
Arc arc = null;
Point3d center = arc3d.Center;
Vector3d normal = arc3d.Normal;
Vector3d refVec = arc3d.ReferenceVector;
using (Plane plane = new Plane(center, normal))
{
double ang = refVec.AngleOnPlane(plane);
arc = new Arc(center, normal, arc3d.Radius,
arc3d.StartAngle + ang, arc3d.EndAngle + ang);
}
return arc;
}
[CommandMethod("PArc2Arc2")]
public void PArc2Arc2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (doc == null) return;
Editor ed = doc.Editor;
PromptEntityOptions peOpt =
new PromptEntityOptions("\nВыберите полилинию с дугами: ");
peOpt.SetRejectMessage("Это не полилиния!");
peOpt.AddAllowedClass(typeof(Polyline), false);
PromptEntityResult peRes = ed.GetEntity(peOpt);
if (peRes.Status != PromptStatus.OK) return;
using (Polyline pl = peRes.ObjectId.Open(OpenMode.ForRead) as Polyline)
{
for (int i = 0; i < pl.NumberOfVertices; i++)
{
if (pl.GetSegmentType(i) == SegmentType.Arc)
{
using (CircularArc3d arc3d = pl.GetArcSegmentAt(i))
{
using (Arc arc = GetArcFromGeArc3d(arc3d))
{
Arc arc1 = null, arc2 = null;
if (DivideArc2(arc, out arc1, out arc2))
{
using (arc1) using (arc2)
using (BlockTableRecord btr = pl.BlockId.Open(OpenMode.ForWrite) as BlockTableRecord)
{
btr.AppendEntity(arc1);
btr.AppendEntity(arc2);
}
}
}
}
}
}
}
}
}
}