• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Member
    WRonX
    Posts: 7
    Registered: ‎06-21-2011
    Accepted Solution

    Can't insert MText into some DWG files

    220 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();
        }
    }
     
    Please use plain text.
    Active Member
    WRonX
    Posts: 7
    Registered: ‎06-21-2011

    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?

    Please use plain text.
    *Expert Elite*
    Posts: 6,635
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Valued Contributor
    FFlix
    Posts: 90
    Registered: ‎11-15-2011

    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

    Please use plain text.
    Active Member
    WRonX
    Posts: 7
    Registered: ‎06-21-2011

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

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    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
    Please use plain text.
    Active Member
    WRonX
    Posts: 7
    Registered: ‎06-21-2011

    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.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    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
    Please use plain text.