Creating a line and displaying angle

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Windows;
using System.Windows.Forms;



[assembly: CommandClass(typeof(ClassLibrary.Class))]

namespace ClassLibrary
{
///
/// Summary description for Class.
///

public class Class :IExtensionApplication
{
private static Editor pEditor;
public static Line pLine;
public Class()
{

}

[CommandMethod("lian")]
static public void test()
{
Database pDatabase = HostApplicationServices.WorkingDatabase;
Editor pEditor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Autodesk.AutoCAD.DatabaseServices.TransactionManager pTransactionManager = pDatabase.TransactionManager;
Transaction pTransaction = pTransactionManager.StartTransaction();
try
{
//for creating a line
Autodesk.AutoCAD.DatabaseServices.Line pLine = new Line();
PromptPointOptions pPromptPointOptions1 = new PromptPointOptions("enter first point of line");
PromptPointResult pPromptPointResult1 = pEditor.GetPoint(pPromptPointOptions1);
PromptPointOptions pPromptPointOptions2 = new PromptPointOptions("enter second point of line");
PromptPointResult pPromptPointResult2 = pEditor.GetPoint(pPromptPointOptions2);
pLine.StartPoint = pPromptPointResult1.Value;
pLine.EndPoint = pPromptPointResult2.Value;

BlockTable pBlockTable = pTransaction.GetObject(pDatabase.BlockTableId, OpenMode.ForRead, false) as BlockTable;
BlockTableRecord pBlockTableRecord = pTransaction.GetObject(pBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false) as BlockTableRecord;
ObjectId objId = pBlockTableRecord.AppendEntity(pLine);
pTransaction.AddNewlyCreatedDBObject(pLine, true);

//to get text
Point3d pPoint3d = new Point3d((pLine.StartPoint.X + pLine.EndPoint.X) / 2, (pLine.StartPoint.Y + pLine.EndPoint.Y ) / 2, 0.0);
DBText pDBText = new DBText();
pDBText.VerticalMode = TextVerticalMode.TextBottom;
pDBText.HorizontalMode = TextHorizontalMode.TextCenter;
pDBText.Height = 0.5;

//to find angle btwn to points
double angle = Angle(pLine.StartPoint, pLine.EndPoint);
//for getting angle in degrees upto 4 decimals
pDBText.TextString = "Angle = " + Converter.DistanceToString(angle * (180.0 / Math.PI), DistanceUnitFormat.Decimal, 4);

pDBText.Rotation = angle;
pDBText.AlignmentPoint = pPoint3d;
objId = pBlockTableRecord.AppendEntity(pDBText);
pTransaction.AddNewlyCreatedDBObject(pDBText, true);

}
catch (System.Exception pException)
{
pEditor.WriteMessage(pException.ToString());
}
finally
{

pTransaction.Commit();
}
pTransaction.Dispose();
}

public static double Angle(Point3d firPnt, Point3d secPnt)
{
double lineAngle;
if (firPnt.X == secPnt.X)
{
if (firPnt.Y < secPnt.Y)
lineAngle = Math.PI / 2.0;
else
lineAngle = (Math.PI / 2.0) * 3.0;
}
else
{
lineAngle = Math.Atan((secPnt.Y - firPnt.Y) / (secPnt.X - firPnt.X));
if (firPnt.X > secPnt.X)
lineAngle = Math.PI + lineAngle;
else
if (lineAngle < 0.0)
lineAngle = (Math.PI * 2.0) + lineAngle;
}
return (lineAngle);
}

#region IExtensionApplication Members

public void Initialize()
{
MessageBox.Show("application loaded");
}

public void Terminate()
{

}

#endregion
}
}

0 comments:

Post a Comment