Hi.
I've got a problem with inserting MText into drawings.
The code below works perfectly in new file (AutoCAD 2012 18.2.0.0) and in some files, but in some other files the MText field just doesn't show.
There's no exception thrown, no error messages, debugging shows everything is OK.
How to check, what is wrong?
I mean - what's the difference between those files which I can't insert MText into and those, which I can?
The test code:
[CommandMethod("WTEST3")] public void wtest3() { acApp.Document doc = acApp.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptPointOptions getPointOptions = new PromptPointOptions("Click the point for MText"); PromptPointResult getPointResult = ed.GetPoint(getPointOptions); if (getPointResult.Status != PromptStatus.OK) return; Transaction tr = db.TransactionManager.StartTransaction(); try { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); MText mt = new MText(); mt.Contents = "blah"; mt.Location = new Point3d(getPointResult.Value.X, getPointResult.Value.Y, getPointResult.Value.Z); btr.AppendEntity(mt); tr.AddNewlyCreatedDBObject(mt, true); tr.Commit(); ed.Regen(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { MessageBox.Show("Something went wrong: " + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { tr.Dispose(); } }
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
I don't see the "Edit" option... There's something funny. It appears the MText field in fact is inserted into drawing, but in totally different point.
I added some "debug" messages - just writing to the console XYZ of the clicked point and XYZ of the .Location of the MText after inserting it and commiting transaction. It is the same Point3D.
But when I click for exaple Point like this:
3608.03750820925/-3910.2577842104/0 (X/Y/Z)
(these are coordinates of clicked Point3D *AND* mt.Location.Value)
The MText appears in:
84.3121/-5740.895/0 (X/Y/Z)
It makes me scream "WTF?!" really loudly. Any ideas?
Hi,
if coordinates are different, could it be you have a coordinate-system (UCS) active that is not equal to WCS (world)?
- alfred -
I agreed with alfred
anyway you mihgt be want to try this example
{code}
public static ObjectId DrawMText(Database db, BlockTableRecord btr, Transaction tr, Point3d pt, string txtStr, double hgt, double wid, string txtStyle, string layername,AttachmentPoint atch)
{
MText mtxt = new MText();
mtxt.Attachment = atch;
mtxt.Location = pt;
mtxt.TextHeight = hgt;
mtxt.Width = wid;
mtxt.Contents = txtStr;
mtxt.Layer = layername;
mtxt.ColorIndex = 256;
TextStyleTable txtTbl = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
if (txtTbl.Has(txtStyle))
{
TextStyleTableRecord txtTbr = (TextStyleTableRecord)tr.GetObject(txtTbl[txtStyle], OpenMode.ForRead);
mtxt.TextStyleId = txtTbr.ObjectId;
}
btr.AppendEntity(mtxt);
tr.AddNewlyCreatedDBObject(mtxt, true);
return mtxt.ObjectId;
}
[CommandMethod("mtextDraw", "mxd", CommandFlags.Modal)]
public void TestMtext()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
DrawMText(db, btr, tr, new Point3d(100.04, 200.05, 0.0), "blah\\Pblah", 10.0, 12.0, "Standard", "0", AttachmentPoint.MiddleCenter);
tr.Commit();
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage(ex.StackTrace);
}
finally
{
}
}
}
{code}
hi, just an idea, have you checked the snap settings?
(when working in acad, i've experienced a few times that the snap would pick a block somewhere instead of the point closest to the cursor. the point at which the mtext is inserted is maybe a block extent or even the drawing extent in x or y direction, if that makes sense?)
felix
Thanks to all of you, maybe you can help me a little more 🙂
We checked coordinates systems (thanks, alfred) by doing the experiment:
- make UCS different from WCS by only 1000 on one axis (WCS starting point was 1000 to the left from UCS)
- load DLL, run command, enter the coordinates (0, 0) manually
Yes! It took UCS's coordinates (0, 0) and inserted MText in UCS's (-1000, 0), which was WCS's (0, 0).
So as I understand, clicking or entering coordinates for the textfield is bound with actual UCS, but the field is inserted in the equivalent coordinates in WCS.
I just can't figure out, how to insert MText into UCS's-specific coordinates. Unfortunately, Hallex's code didn't work (worked just like mine).
Try again
{code}
public static ObjectId DrawMText(Database db, BlockTableRecord btr, Transaction tr, Point3d pt, string txtStr, double hgt, double wid, string txtStyle, string layername,AttachmentPoint atch)
{
MText mtxt = new MText();
mtxt.Attachment = atch;
mtxt.Location = pt;
mtxt.TextHeight = hgt;
mtxt.Width = wid;
mtxt.Contents = txtStr;
mtxt.Layer = layername;
mtxt.ColorIndex = 256;
TextStyleTable txtTbl = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);
if (txtTbl.Has(txtStyle))
{
TextStyleTableRecord txtTbr = (TextStyleTableRecord)tr.GetObject(txtTbl[txtStyle], OpenMode.ForRead);
mtxt.TextStyleId = txtTbr.ObjectId;
}
btr.AppendEntity(mtxt);
tr.AddNewlyCreatedDBObject(mtxt, true);
return mtxt.ObjectId;
}
public static void TransformToUCS(Transaction tr,Editor ed, ObjectId id)
{
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
Entity ent = tr.GetObject(id, OpenMode.ForWrite,false) as Entity;
ent.TransformBy(ucs);
}
[CommandMethod("mtextDraw", "mxd", CommandFlags.Modal)]
public void TestMtext()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
ObjectId mtxId = DrawMText(db, btr, tr, new Point3d(1000.0, 0.0, 0.0), "blah\\Pblah", 10.0, 12.0, "Standard", "0", AttachmentPoint.MiddleCenter);
TransformToUCS(tr, ed, mtxId);
tr.Commit();
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
finally
{
Application.ShowAlertDialog("Check insertion point in UCS using command \"ID\"\nin the command line");
}
}
}
{code}
~'J'~
This is it! Using only your TransformToUCS() function solved my problems. Thank you very much - now everything works, my boss is sattisfied and I am not-yet fired 🙂 So everyone is happy.
Thanks again.
Can't find what you're looking for? Ask the community or share your knowledge.