// (C) Copyright 2019 by
//
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(CrossectionMark.MyCommands))]
namespace CrossectionMark
{
public class MyCommands
{
[CommandMethod("0sec", CommandFlags.UsePickSet)]
public static void DoSection()
{
Crossection cs = new Crossection();
cs.CreateSectionMark();
}
}
public class Crossection
{
private static int stripeLength = 500;
private static int thickness = 5;
public void CreateSectionMark()
{
//--------------------------------------------------------------------Mandatory variables
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Point3d pL = GetPointFromUser("Set left point");
Point3d pR = GetPointFromUser("Set right point");
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 stripe = new Polyline();
stripe.SetDatabaseDefaults();
stripe.AddVertexAt(0, new Point2d(pL.X, pL.Y), 0, thickness, thickness);
stripe.AddVertexAt(1, new Point2d(pR.X, pR.Y), 0, thickness, thickness);
stripe.LineWeight = LineWeight.ByLineWeightDefault;
stripe.Layer = "0";
btr.AppendEntity(stripe);
tr.AddNewlyCreatedDBObject(stripe, true);
collection.Add(stripe.ObjectId);
// Point2d yy= new Point2d( stripe.Normal.X, new Vector2d().GetPerpendicularVector().Y);
Point3d startPoint = stripe.GetPointAtDist(0.33 * stripe.Length);
Vector3d perpDir = stripe.GetFirstDerivative(stripe.GetParameterAtPoint(startPoint));
perpDir = perpDir.GetNormal();
perpDir = perpDir.TransformBy(Matrix3d.Rotation(Math.PI / 2, stripe.Normal, Point3d.Origin));
// Line arrow = new Line(startPoint, startPoint + 500*angle);
Polyline arrow = new Polyline();
arrow.SetDatabaseDefaults();
Point2d p1 = new Point2d(startPoint.X, startPoint.Y);
Point2d p2 = p1 + new Vector2d(perpDir.X, perpDir.Y) * stripeLength / 3.0 * 2.0;
Point2d p3 = p1 + new Vector2d(perpDir.X, perpDir.Y) * stripeLength;
arrow.AddVertexAt(0, p1, 0, 0, 0);
arrow.AddVertexAt(1, p2, 0, thickness, 0);
arrow.AddVertexAt(2, p3, 0, 0, 0);
arrow.LineWeight = LineWeight.ByLineWeightDefault;
arrow.Layer = "0";
btr.AppendEntity(arrow);
tr.AddNewlyCreatedDBObject(arrow, true);
collection.Add(arrow.ObjectId);
tr.Commit();
}
}
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;
}
}
}