Draw text frame (the same as TCIRCLE command from Express tools though)
[CommandMethod("tframe")]
static public void DrawTextFrame()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
using (tr)
{
TypedValue[] tvs = new TypedValue[] { new TypedValue((int)DxfCode.Start, "TEXT") };
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.GetSelection(sf);
SelectionSet sset = psr.Value;
if (sset.Count == 0)
return;
PromptDoubleOptions pdo = new PromptDoubleOptions(
"\nEnter an offset distance of the text frame: ");
pdo.AllowNone = false;
pdo.AllowZero = false;
pdo.DefaultValue = 1.0;
PromptDoubleResult res = ed.GetDouble(pdo);
if (res.Status != PromptStatus.OK)
return;
double offset = res.Value;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (SelectedObject sobj in sset)
{
DBObject dbobj = tr.GetObject(sobj.ObjectId, OpenMode.ForRead);
DBText txtobj = dbobj as DBText;
if (txtobj != null)
{
double ht = txtobj.Height;
double ang = txtobj.Rotation;
Extents3d ext = txtobj.GeometricExtents;
Point3d min = ext.MinPoint;
Point3d max = ext.MaxPoint;
Point3dCollection pts = new Point3dCollection();
DoubleCollection bulges = new DoubleCollection();
double[] bulgelist = { 0, 0, 0, 0 };
bulges.AddRange(bulgelist);
if ((ang > 0) && (ang < Math.PI / 2))
{
Point3d p1 = PolarPoint(min, 0.0, ht * Math.Sin(ang));
Point3d p3 = PolarPoint(max, Math.PI, ht * Math.Sin(ang));
Point3d p2 = PolarPoint(p3, ang - Math.PI / 2, ht);
Point3d p4 = PolarPoint(p1, ang + Math.PI / 2, ht);
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
pts.Add(p4);
Polyline2d pline = Draw2dPoly(
Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
DBObjectCollection dbo = pline.GetOffsetCurves(offset);
foreach (Entity ent in dbo)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
else
{
if ((ang == 0) || (ang == Math.PI))
{
Point3d p1 = new Point3d(min.X, min.Y, min.Z);
Point3d p3 = new Point3d(max.X, max.Y, min.Z);
Point3d p2 = new Point3d(max.X, min.Y, min.Z);
Point3d p4 = new Point3d(min.X, max.Y, min.Z);
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
pts.Add(p4);
Polyline2d pline = Draw2dPoly(
Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
DBObjectCollection dbo = pline.GetOffsetCurves(offset);
foreach (Entity ent in dbo)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
else
{
if ((ang > Math.PI) && (ang < Math.PI * 1.5))
{
Point3d p1 = new Point3d(min.X, min.Y, min.Z);
Point3d p3 = new Point3d(max.X, max.Y, min.Z);
Point3d p2 = new Point3d(max.X, min.Y, min.Z);
Point3d p4 = new Point3d(min.X, max.Y, min.Z);
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
pts.Add(p4);
Polyline2d pline = Draw2dPoly(
Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
DBObjectCollection dbo = pline.GetOffsetCurves(offset);
foreach (Entity ent in dbo)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
else
{
if (ang == Math.PI * 1.5)
{
Point3d p1 = new Point3d(min.X, min.Y, min.Z);
Point3d p4 = PolarPoint(max, Math.PI, ht);
Point3d p3 = new Point3d(max.X, max.Y, max.Z);
Point3d p2 = PolarPoint(p1, 0.0, ht);
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
pts.Add(p4);
Polyline2d pline = Draw2dPoly(
Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
DBObjectCollection dbo = pline.GetOffsetCurves(offset);
foreach (Entity ent in dbo)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
else
{
if ((ang < Math.PI * 1.5) && (ang % Math.PI / 2 != 0))
{
Point3d p1 = PolarPoint(min, 0.0, ht * Math.Sin(ang));
Point3d p3 = PolarPoint(max, Math.PI, ht * Math.Sin(ang));
Point3d p2 = PolarPoint(p3, ang - Math.PI / 2, ht);
Point3d p4 = PolarPoint(p1, ang + Math.PI / 2, ht);
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
pts.Add(p4);
Polyline2d pline = Draw2dPoly(
Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
DBObjectCollection dbo = pline.GetOffsetCurves(offset);
foreach (Entity ent in dbo)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
else
{
if ((ang > Math.PI * 1.5) && (ang < Math.PI * 2))
{
Point3d minp = new Point3d(min.X, max.Y, min.Z);
Point3d maxp = new Point3d(max.X, min.Y, min.Z);
double ang1 = Math.PI * 2 - ang;
Point3d p1 = PolarPoint(minp, 0.0, ht * Math.Sin(ang1));
Point3d p2 = PolarPoint(p1, Math.PI * 1.5 - ang1, ht);
Point3d p3 = PolarPoint(maxp, Math.PI, ht * Math.Sin(ang1));
Point3d p4 = PolarPoint(p3, Math.PI / 2 - ang1, ht);
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
pts.Add(p4);
Polyline2d pline = Draw2dPoly(
Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
DBObjectCollection dbo = pline.GetOffsetCurves(offset);
foreach (Entity ent in dbo)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
}
}
}
}
}
}
}
}
tr.Commit();
}
}
catch (System.Exception ex)
{
acadApp.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
}
}
/// <summary>
/// Draw 2dPolyline
/// </summary>
/// <param name="ptype"></param>
/// <param name="pts"></param>
/// <param name="elev"></param>
/// <param name="isclosed"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="bulges"></param>
/// <returns></returns>
public static Polyline2d Draw2dPoly(Poly2dType ptype, Point3dCollection pts,double elev,bool isclosed,double start, double end, DoubleCollection bulges)
{
Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, pts, 0.0, true, 0.0, 0.0, bulges);
return pline;
}
/// <summary>
/// Polar point // Posted by Tony Tanzillo
/// </summary>
/// <param name="basepoint"></param>
/// <param name="angle"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static Point3d PolarPoint(Point3d basepoint, double angle, double distance)
{
return new Point3d(
basepoint.X + (distance * Math.Cos(angle)),
basepoint.Y + (distance * Math.Sin(angle)),
basepoint.Z);
}
~'J'~