public class Crossection
{
public static string sectionName;
public static Point3d pL;
public static Point3d pR;
public static Point3d pointer;
public static void CreateSectionMark(Point3d pL, Point3d pR, string sectionName, Point3d pointer, int drawingScale = 200)
{
//--------------------------------------------------------------------Mandatory variables
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
//--------------------------------------------------------------------Mandatory variables
ObjectIdCollection collection = new ObjectIdCollection();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
Polyline directrix = new Polyline();
directrix.SetDatabaseDefaults();
directrix.AddVertexAt(0, new Point2d(pL.X, pL.Y), 0, 0, 0);
directrix.AddVertexAt(1, new Point2d(pR.X, pR.Y), 0, 0, 0);
directrix.LineWeight = LineWeight.ByLineWeightDefault;
directrix.Layer = "0";
Vector3d directrixDirection = directrix.GetFirstDerivative(directrix.EndPoint);
double angleToRotate = Vector3d.XAxis.GetAngleTo(directrixDirection, Vector3d.ZAxis);
double stripeLength = 7 * drawingScale;
int arrowLength = 7;
Polyline stripe = new Polyline();
stripe.SetDatabaseDefaults();
double thickness = 2.5;
stripe.AddVertexAt(0, new Point2d(pL.X, pL.Y), 0, thickness, thickness);
stripe.AddVertexAt(1, new Point2d(pL.X + stripeLength, pL.Y), 0, thickness, thickness);
stripe.LineWeight = LineWeight.ByLineWeightDefault;
stripe.Layer = "0";
stripe.ColorIndex = 2;
btr.AppendEntity(stripe);
tr.AddNewlyCreatedDBObject(stripe, true);
collection.Add(stripe.ObjectId);
stripe.TransformBy(Matrix3d.Rotation(angleToRotate, Vector3d.ZAxis, stripe.StartPoint));
Point3d startPoint = stripe.GetPointAtDist(0.33 * stripe.Length);
Vector3d perpDirection = stripe.GetFirstDerivative(stripe.GetParameterAtPoint(startPoint));
perpDirection = perpDirection.GetNormal();
perpDirection = perpDirection.TransformBy(Matrix3d.Rotation(Math.PI / 2, stripe.Normal, Point3d.Origin));
Polyline arrow = new Polyline();
arrow.SetDatabaseDefaults();
Point2d p1 = new Point2d(startPoint.X, startPoint.Y);
Point2d p2 = p1 + new Vector2d(perpDirection.X, perpDirection.Y) * arrowLength / 2.5 /* 2.0*/;
Point2d p3 = p1 + new Vector2d(perpDirection.X, perpDirection.Y) * arrowLength;
arrow.AddVertexAt(0, p3, 0, thickness / 6, thickness / 6);
arrow.AddVertexAt(1, p2, 0, thickness, 0);
arrow.AddVertexAt(2, p1, 0, 0, 0);
arrow.LineWeight = LineWeight.ByLineWeightDefault;
arrow.Layer = "0";
arrow.ColorIndex = 1;
btr.AppendEntity(arrow);
tr.AddNewlyCreatedDBObject(arrow, true);
collection.Add(arrow.ObjectId);
arrow.TransformBy(Matrix3d.Scaling(drawingScale, new Point3d(p1.X, p1.Y, 0)));
DBObjectCollection mirrorLine = arrow.GetOffsetCurves(-directrix.Length / 2 + 0.33 * stripe.Length);
Polyline ii = (Polyline)mirrorLine[0];
Line3d l3d = new Line3d(ii.StartPoint, ii.EndPoint);
Polyline arrow2 = arrow.Clone() as Polyline;
Polyline stripe2 = stripe.Clone() as Polyline;
arrow2.TransformBy(Matrix3d.Mirroring(l3d));
stripe2.TransformBy(Matrix3d.Mirroring(l3d));
btr.AppendEntity(arrow2);
tr.AddNewlyCreatedDBObject(arrow2, true);
btr.AppendEntity(stripe2);
tr.AddNewlyCreatedDBObject(stripe2, true);
Vector3d perpDirection2 = arrow.GetFirstDerivative(arrow.GetParameterAtPoint(arrow.StartPoint));
perpDirection2 = perpDirection2.GetNormal();
perpDirection2 = perpDirection2.TransformBy(Matrix3d.Rotation(Math.PI / 2, arrow.Normal, arrow.StartPoint));
Point3d textPoint = arrow.StartPoint - new Vector3d(perpDirection2.X, perpDirection2.Y, 0) * 5 * drawingScale;
MText mtext = new MText();
mtext.Location = textPoint;
mtext.TextHeight = ESKDValues.SectionTextHeight * drawingScale;
mtext.Attachment = AttachmentPoint.MiddleCenter;
mtext.Width = 10;
mtext.Contents = sectionName;
mtext.Layer = "0";
AddRegAppTableRecord("ITAT");
ResultBuffer rb = new ResultBuffer();
rb.Add(new TypedValue((int) DxfCode.ExtendedDataRegAppName, "ITAT"));
rb.Add(new TypedValue((int)DxfCode.Text, "sec"));
btr.AppendEntity(mtext);
tr.AddNewlyCreatedDBObject(mtext, true);
mtext.XData = rb;
rb.Dispose();
MText mtext2 = mtext.Clone() as MText;
mtext2.TransformBy(Matrix3d.Mirroring(l3d));
mtext2.Normal = new Vector3d(0, 0, 1);
btr.AppendEntity(mtext2);
tr.AddNewlyCreatedDBObject(mtext2, true);
tr.Commit();
}
}
[CommandMethod("0sec")]
public static void DoSection()
{
sectionName = GetStringParameter("Set section name");
pL =GetPointFromUser("Set left point");
pR = GetPointFromUser("Set right point");
CreateSectionMark(pL, pR, sectionName, pointer);
}
public static Point3d GetPointFromUser(string message)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
PromptPointOptions pickPoint = new PromptPointOptions(message);
PromptPointResult insertPoint = ed.GetPoint(pickPoint);
return insertPoint.Value;
}
public static string GetStringParameter(string message)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
PromptStringOptions pso1 = new PromptStringOptions($"\n{message}");
PromptResult stringParameter = doc.Editor.GetString(pso1);
string parameter = stringParameter.StringResult;
return parameter;
}
public static void AddRegAppTableRecord(string regAppName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
RegAppTable rat = (RegAppTable)tr.GetObject(db.RegAppTableId, OpenMode.ForRead, false);
if (!rat.Has(regAppName))
{
rat.UpgradeOpen();
RegAppTableRecord ratr = new RegAppTableRecord();
ratr.Name = regAppName;
rat.Add(ratr);
tr.AddNewlyCreatedDBObject(ratr, true);
}
tr.Commit();
}
}
}