.NET

Reply
Active Member
WRonX
Posts: 7
Registered: ‎06-21-2011
Message 1 of 9 (283 Views)
Accepted Solution

Can't insert MText into some DWG files

283 Views, 8 Replies
02-13-2012 09:57 AM

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();
    }
}
 
Active Member
WRonX
Posts: 7
Registered: ‎06-21-2011
Message 2 of 9 (270 Views)

Re: Can't insert MText into some DWG files

02-13-2012 11:11 AM in reply to: WRonX

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?

*Expert Elite*
Alfred.NESWADBA
Posts: 8,880
Registered: ‎06-29-2007
Message 3 of 9 (260 Views)

Re: Can't insert MText into some DWG files

02-13-2012 12:12 PM in reply to: WRonX

Hi,

 

if coordinates are different, could it be you have a coordinate-system (UCS) active that is not equal to WCS (world)?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 9 (259 Views)

Re: Can't insert MText into some DWG files

02-13-2012 12:26 PM in reply to: WRonX

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}

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
FFlix
Posts: 95
Registered: ‎11-15-2011
Message 5 of 9 (249 Views)

Re: Can't insert MText into some DWG files

02-13-2012 02:25 PM in reply to: WRonX

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

Active Member
WRonX
Posts: 7
Registered: ‎06-21-2011
Message 6 of 9 (218 Views)

Re: Can't insert MText into some DWG files

02-15-2012 05:26 AM in reply to: WRonX

Thanks to all of you, maybe you can help me a little more :smileyhappy:

 

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).

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 9 (209 Views)

Re: Can't insert MText into some DWG files

02-15-2012 07:27 AM in reply to: WRonX

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
WRonX
Posts: 7
Registered: ‎06-21-2011
Message 8 of 9 (192 Views)

Re: Can't insert MText into some DWG files

02-16-2012 06:24 AM in reply to: Hallex

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 :smileyhappy: So everyone is happy.

 

Thanks again.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 9 of 9 (179 Views)

Re: Can't insert MText into some DWG files

02-16-2012 06:53 AM in reply to: WRonX

Glad I could help

Cheers :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community