using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Platform = Autodesk.AutoCAD;
using PlatformDb = Autodesk.AutoCAD;
[assembly: CommandClass(typeof(testtool.PolyNum))]
namespace testtool
{
public class PolyNum
{
[CommandMethod("enumtest")]
static public void enumer()
{
ObjectIdCollection mtexts = new ObjectIdCollection();
Document doc = Platform.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
int totalporyadcount = 0;
double dist = 3;
bool smartalg = true;
double fontheight = 3;
bool poryadkn = true;
TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE") };
SelectionFilter filter = new SelectionFilter(filList);
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\nВыбирете объекты в порядке очередности один за одним и нажмите Enter для экспорта: ";
PromptSelectionResult per = ed.GetSelection(pso, filter);
if (per.Status == PromptStatus.OK)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
SelectionSet acSSet = per.Value;
int objcount = 0;
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
foreach (SelectedObject acSSObj in acSSet)
{
DBObject obj = tr.GetObject(acSSObj.ObjectId, OpenMode.ForRead);
Polyline lwp = obj as Polyline;
Curve curv = obj as Curve;
DBObjectCollection ids = new DBObjectCollection();
if (curv.GetOffsetCurves(dist).Count != 0) ids = curv.GetOffsetCurves(dist);
else ids.Add(curv);
Polyline lwpclone = lwp.Clone() as Polyline;
Extents3d ext = lwpclone.GeometricExtents;
Point3d p_center = new Point3d(
(ext.MinPoint.X + ext.MaxPoint.X) * 0.5,
(ext.MinPoint.Y + ext.MaxPoint.Y) * 0.5,
(ext.MinPoint.Z + ext.MaxPoint.Z) * 0.5
);
double scaledist = 1 + dist / 100;
lwpclone.TransformBy(Matrix3d.Scaling(scaledist, p_center));
Polyline lwp2 = ids[0] as Polyline;
int vn2 = 0;
if (lwp.NumberOfVertices != lwp2.NumberOfVertices)
{
lwp2 = lwpclone;
}
vn2 = lwp2.NumberOfVertices;
if (lwp != null)
{
objcount++;
int vn = lwp.NumberOfVertices;
Point3d[] smartalgpt = new Point3d[vn];
string vertnum = "";
for (int i = 0; i < vn; i++)
{
Point2d pt = lwp.GetPoint2dAt(i);
Point2d ptclone = lwp2.GetPoint2dAt(i);
// / / / / / / / / / / /SMARTALG
if (smartalg)
{
DBObjectCollection objectsforintersectcheck = new DBObjectCollection();
PromptSelectionResult res = SelectByPolyline(ed, lwp2, true, new TypedValue((int)DxfCode.Start, "MTEXT,LWPOLYLINE,TEXT,INSERT"));
if (res.Status == PromptStatus.OK)
{
foreach (ObjectId oid in res.Value.GetObjectIds())
{
if (oid != null)
{
DBObject objfi = tr.GetObject(oid, OpenMode.ForRead);
if (objfi is BlockReference)
{
BlockReference br = objfi as BlockReference;
br.Explode(objectsforintersectcheck);
}
objectsforintersectcheck.Add(objfi);
}
}
}
foreach (ObjectId oid in mtexts)
{
DBObject objfi = tr.GetObject(oid, OpenMode.ForRead);
if (!objectsforintersectcheck.Contains(objfi)) objectsforintersectcheck.Add(objfi);
}
if (!objectsforintersectcheck.Contains(lwp)) objectsforintersectcheck.Add(lwp);
bool allintersect = false;
MText mt2 = new MText();
mt2.TextHeight = fontheight;
mt2.Location = new Point3d(ptclone.X, ptclone.Y, 0);
mt2.Attachment = AttachmentPoint.MiddleCenter;
if (vertnum == "") mt2.Contents = "0";
else mt2.Contents = vertnum;
ms.AppendEntity(mt2);
tr.AddNewlyCreatedDBObject(mt2, true);
Point3dCollection pts = new Point3dCollection();
foreach (DBObject dbobj in objectsforintersectcheck)
{
Entity entfi = dbobj as Entity;
mt2.IntersectWith(entfi, Intersect.OnBothOperands, pts, 0, 0);
if (pts.Count > 0)
{
allintersect = true;
}
}
mt2.Erase();
if (!allintersect)
{
smartalgpt[i] = lwp2.GetPoint3dAt(i);
ptclone = new Point2d(smartalgpt[i].X, smartalgpt[i].Y);////
}
else
{
bool happened = false;
Line[] lineList = new Line[17];
double gradus = 0;
DBObjectCollection approvedlines = new DBObjectCollection();
for (int k = 0; k < 17; k++)
{
Vector3d vec3d = new Vector3d();
try
{
vec3d = lwp.GetFirstDerivative(lwp.GetParameterAtPoint(lwp.GetPoint3dAt(i)));
}
catch (PlatformDb.Runtime.Exception ex)
{
vec3d = new Vector3d(lwp.GetPoint3dAt(i).X, lwp.GetPoint3dAt(i).Y, lwp.GetPoint3dAt(i).Z);
ed.WriteMessage("\n" + ex.Message);
}
vec3d = vec3d.GetNormal() * 999999.0;
Vector3d vec3dshort = vec3d.GetNormal() * dist;
vec3d = vec3d.TransformBy(Matrix3d.Rotation(gradus / 180.0 * Math.PI, doc.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d.Zaxis, Point3d.Origin));
vec3dshort = vec3dshort.TransformBy(Matrix3d.Rotation(gradus / 180.0 * Math.PI, doc.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d.Zaxis, Point3d.Origin));
Line line = new Line(lwp.GetPoint3dAt(i), lwp.GetPoint3dAt(i) + vec3d);
Line lineshort = new Line(lwp.GetPoint3dAt(i), lwp.GetPoint3dAt(i) + vec3dshort);
lineList[k] = line;
Point3dCollection points = new Point3dCollection();
line.IntersectWith(lwp, Intersect.OnBothOperands, points, 0, 0);
if (points.Count == 1)
{
approvedlines.Add(lineshort);
}
gradus = gradus + 22.5;
}
foreach (Line appline in approvedlines)
{
ms.AppendEntity(appline);
tr.AddNewlyCreatedDBObject(appline, true);
MText mt3 = new MText();
mt3.TextHeight = fontheight;
mt3.Location = appline.EndPoint;
mt3.Attachment = AttachmentPoint.MiddleCenter;
if (vertnum == "") mt3.Contents = "0";
else mt3.Contents = vertnum;
ms.AppendEntity(mt3);
tr.AddNewlyCreatedDBObject(mt3, true);
pts = new Point3dCollection();
bool intersect = false;
foreach (DBObject dbobj in objectsforintersectcheck)
{
Entity entfi = dbobj as Entity;
mt3.IntersectWith(entfi, Intersect.OnBothOperands, pts, 0, 0);
if (pts.Count > 0)
{
intersect = true;
}
}
mt3.Erase();
if (!intersect)
{
smartalgpt[i] = appline.EndPoint;
ptclone = new Point2d(smartalgpt[i].X, smartalgpt[i].Y);////*/
happened = true;
break;
}
}
if (happened == false)
{
ed.WriteMessage("\nСвободное место для вершины не найдено: " + vertnum);
}
}
}
int nextpoint = i + 1;
if (nextpoint >= vn2) nextpoint = 0;
Point2d pt2 = lwp2.GetPoint2dAt(nextpoint);
if (smartalg)
{
pt2 = new Point2d(smartalgpt[nextpoint].X, smartalgpt[nextpoint].Y);
}
if (!poryadkn) vertnum = (i + 1).ToString();
else vertnum = totalporyadcount.ToString();
ObjectId mtId;
MText mt = new MText();
mt.TextHeight = fontheight;
mt.Location = new Point3d(ptclone.X, ptclone.Y, 0);
mt.Attachment = AttachmentPoint.MiddleCenter;
mt.Contents = vertnum;
mtId = ms.AppendEntity(mt);
tr.AddNewlyCreatedDBObject(mt, true);
totalporyadcount++;
mtexts.Add(mtId);
}
}
}
tr.Commit();
}
}
}
public static PromptSelectionResult SelectByPolyline(Editor ed, Polyline pline, bool mode, params TypedValue[] filter)
{
Point3dCollection polygon = new Point3dCollection();
for (int i = 0; i < pline.NumberOfVertices; i++)
{
polygon.Add(pline.GetPoint3dAt(i));
}
PromptSelectionResult result;
ViewTableRecord view = ed.GetCurrentView();
Zoom(ed, pline.GeometricExtents);
if (mode)
result = ed.SelectCrossingPolygon(polygon, new SelectionFilter(filter));
else
result = ed.SelectWindowPolygon(polygon, new SelectionFilter(filter));
ed.SetCurrentView(view);
return result;
}
public static void Zoom(Editor ed, Extents3d extents)
{
using (ViewTableRecord view = ed.GetCurrentView())
{
Matrix3d worldToEye =
Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
Matrix3d.Displacement(view.Target - Point3d.Origin) *
Matrix3d.PlaneToWorld(view.ViewDirection)
.Inverse();
extents.TransformBy(worldToEye);
view.Width = extents.MaxPoint.X - extents.MinPoint.X;
view.Height = extents.MaxPoint.Y - extents.MinPoint.Y;
view.CenterPoint = new Point2d(
(extents.MaxPoint.X + extents.MinPoint.X) / 2.0,
(extents.MaxPoint.Y + extents.MinPoint.Y) / 2.0);
ed.SetCurrentView(view);
}
}
}
}