TheSwamp
Code Red => .NET => Topic started by: Kerry on January 27, 2010, 03:06:45 AM
-
LIBRARY THREAD for AutoCAD TEXT, MTEXT
Members are encouraged to post any functions, methods, snips regarding
AutoCAD TEXT, MTEXT in .NET : C# , VB , F# , Python , etc
Feel free to include comments, descriptive notes, limitations, and images to document your post.
Please post questions in a regular thread.
-
Change textstyle of texts/mtexts either
on locked layers as well
~'J'~
-
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'~
-
Hi,
STRIMT a command to strip the selected mtexts from every formatting
gc-GetStrippedMtextString a LISP function which returns the stripped mtext string (argument: the mtext ename)
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace StripMtextSample
{
public class StripMtext
{
private StringBuilder m_sb = null;
private string GetStrippedMtextContents(MText mt)
{
m_sb = new StringBuilder();
mt.ExplodeFragments(new MTextFragmentCallback(FragmentCallback));
return m_sb.ToString();
}
private MTextFragmentCallbackStatus FragmentCallback(MTextFragment fragment, object obj)
{
m_sb.Append(fragment.Text);
return MTextFragmentCallbackStatus.Continue;
}
[CommandMethod("STRIPMT", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void StripSelSet()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "MTEXT") });
ed.WriteMessage("\nSelect mtexts or hit Enter for all.");
PromptSelectionResult psr = ed.GetSelection(filter);
if (psr.Status == PromptStatus.Error)
psr = ed.SelectAll(filter);
if (psr.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in psr.Value.GetObjectIds())
{
MText mt = (MText)tr.GetObject(id, OpenMode.ForWrite);
mt.Contents = GetStrippedMtextContents(mt);
}
tr.Commit();
}
}
[LispFunction("gc-GetStrippedMtextString")]
public string GetStrippedMtextString(ResultBuffer resbuf)
{
string result = null;
if (resbuf == null)
{
LispErrorMsg.TooFew();
return result;
}
TypedValue[] arg = resbuf.AsArray();
if (arg.Length > 1)
{
LispErrorMsg.TooMuch();
return result;
}
if (arg[0].TypeCode != (short)LispDataType.ObjectId)
{
LispErrorMsg.BadType("lentityp", arg[0]);
return result;
}
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
MText mt = tr.GetObject(((ObjectId)arg[0].Value), OpenMode.ForRead) as MText;
if (mt == null)
{
LispErrorMsg.Message("incorrect argument type: not a mtext");
return result;
}
return GetStrippedMtextContents(mt);
}
}
}
public static class LispErrorMsg
{
static Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
internal static void TooFew()
{
ed.WriteMessage("\nError: too few arguments\n");
}
internal static void TooMuch()
{
ed.WriteMessage("\nError: too many arguments\n");
}
internal static void BadType(string T, TypedValue tV)
{
string msg = tV.TypeCode == (int)LispDataType.Nil ? "nil" : tV.Value.ToString();
ed.WriteMessage("\nError: incorrect argument type: {0} {1}\n", T, msg);
}
internal static void Message(string msg)
{
ed.WriteMessage("\nError: {0}\n", msg);
}
}
}
-
This has been working fine for me for drawings we keep getting where they keep embedding font types, etc.. in mtext.
But for stripping the formatting
foreach (MText mtxt in mtextToFix)
{
mtxt.Contents = mtxt.Text;
}
-
Nice trick Jeff.
Works since 2010, I think.