using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using System;
namespace Zigzag
{
public class Zigzag
{
[CommandMethod("DrawLine")]
public void DrawLine()
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Ellipse el = null;
Ray ray = null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); //получаем ссылку на модель чертежа
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Point3d center = new Point3d(0, 0, 0); //объявляем и задаём параметры для эллипса
Vector3d normal = Vector3d.ZAxis;
Vector3d majorAxis = new Vector3d(1000, 0.0, 0.0);
el = new Ellipse(center, normal, majorAxis, 0.5, 0.0, Math.PI*2.0); //создаём экземпляр эллипса
using (el) //добавление эллипса в бд чертежа
{
btr.AppendEntity(el); //добавляем в пространство модели
tr.AddNewlyCreatedDBObject(el, true); //запускаем транзакцию
}
ray = new Ray(); //объявляем примитив чертежа
ray.BasePoint = center;
ray.SecondPoint = new Point3d(0, 1, 0);
using (ray) //добавление луча в бд чертежа
{
btr.AppendEntity(ray);
tr.AddNewlyCreatedDBObject(ray, true);
}
Point3dCollection collection = new Point3dCollection();
el.IntersectWith(ray, Intersect.ExtendThis, collection, IntPtr.Zero, IntPtr.Zero);
tr.Commit(); //коммитим транзакцию
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage($"\n {ex.Message}");
}
} //end using
}
}
}