using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace DimFinal
{
public class Class1
{
[CommandMethod("GetPointsFromUser")]
public static void GetPointsFromUser()
{
// Get the current database and start the Transaction Manager
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Editor ed = acDoc.Editor;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("")
{
// Prompt for the start point
Message = "\nEnter the start point of the line: "
};
pPtRes = ed.GetPoint(pPtOpts);
// Exit if the user presses ESC or cancels the command
if (pPtRes.Status != PromptStatus.OK)
{
if (pPtRes.Status == PromptStatus.Cancel)
{
ed.WriteMessage("\nInterrupted by user"); return;
}
else
{
ed.WriteMessage("\nError on specifying a point"); return;
}
}
Point3d ptStart = pPtRes.Value;
// Prompt for the end point
pPtOpts.Message = "\nEnter the end point of the line: ";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = ptStart;
pPtRes = ed.GetPoint(pPtOpts);
if (pPtRes.Status != PromptStatus.OK)
{
if (pPtRes.Status == PromptStatus.Cancel)
{
ed.WriteMessage("\nInterrupted by user"); return;
}
else
{
ed.WriteMessage("\nError on specifying a point"); return;
}
}
Point3d ptEnd = pPtRes.Value;
// Prompt for the 3d point
pPtOpts.Message = "\nEnter the 3d point of the line: ";
pPtRes = ed.GetPoint(pPtOpts);
Point3d pt3 = pPtRes.Value;
if (pPtRes.Status == PromptStatus.Cancel) return;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
BlockTableRecord acBlkTblRec, acBlkTblRec1;
// Open Model space for write
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
acBlkTblRec1 = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
AlignedDimension alDim = new AlignedDimension();
using (alDim)
{
alDim.XLine1Point = ptStart;
alDim.XLine2Point = ptEnd;
alDim.DimLinePoint = pt3;
alDim.DimensionStyle = acCurDb.Dimstyle;
// Add the line to the block table
acBlkTblRec1.AppendEntity(alDim);
string text = alDim.DimensionText;
acTrans.AddNewlyCreatedDBObject(alDim, false);
ObjectId mtextId = ObjectId.Null;
MText mtx = new MText()
{
//mtx.SetDatabaseDefaults();
Contents = text,
Location = ptStart
};
mtextId = acBlkTblRec.AppendEntity(mtx);
acTrans.AddNewlyCreatedDBObject(mtx, true);
ObjectId leaderId = ObjectId.Null;
Leader ld = new Leader();
ld.AppendVertex(ptStart);
ld.AppendVertex(pt3);
ld.SetDatabaseDefaults();
leaderId = acBlkTblRec.AppendEntity(ld);
ld.Annotation = mtextId;
ld.EvaluateLeader();
acTrans.AddNewlyCreatedDBObject(ld, true);
}
// Commit the changes and dispose of the transaction
acTrans.Commit();
}
}
}
}