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

    .NET

    Reply
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011
    Accepted Solution

    Random "OK" showing up in MTEXT

    90 Views, 3 Replies
    05-29-2012 08:29 AM

    Hey everyone,

     

    I've found some example code on how to create Leaderline and have been playing around with it. So far so good, but when I create my leaderline and the Mtext generates, it is always preceeded by an "OK" and i can't figure out where it's coming from. I've tried using TrimStart on a number of the strings to no avail. I know it must be something insanely simple but it can't seem to get it. Any input would be awesome!

     

    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Geometry;
    
    namespace DimensionLibrary
    {
        public class DimensionCommands
        {
            static ObjectId GetArrowObjectId(string newArrName)
            {
                ObjectId arrObjId = ObjectId.Null;
    
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
    
                // Get the current value of DIMBLK
                string oldArrName = Application.GetSystemVariable("DIMBLK") as string;
    
                // Set DIMBLK to the new style
                // (this action may create a new block)
                Application.SetSystemVariable("DIMBLK", newArrName);
    
                // Reset the previous value of DIMBLK
                if (oldArrName.Length != 0)
                    Application.SetSystemVariable("DIMBLK", oldArrName);
    
                // Now get the objectId of the block
                Transaction tr = db.TransactionManager.StartTransaction();
                using (tr)
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    
                    arrObjId = bt[newArrName];
                    tr.Commit();
                }
                return arrObjId;
            }
    
            [CommandMethod("MLTEST")]
            static public void CreateMultiLeader()
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                string bname = "";
                string bx = "";
                string by = "";
                string bz = "";
    
                const string arrowName = "_DOT";
                ObjectId arrId = GetArrowObjectId(arrowName);
    
                ////////////////////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////////////////////
    
                PromptEntityOptions options = new PromptEntityOptions("\nSelect block reference");
    
                options.SetRejectMessage("\nSelect only block reference");
    
                options.AddAllowedClass(typeof(BlockReference), false);
    
                PromptEntityResult acSSPrompt = ed.GetEntity(options);
    
                using (Transaction tx = db.TransactionManager.StartTransaction())
                {
                    BlockReference blockRef = tx.GetObject(acSSPrompt.ObjectId, OpenMode.ForRead) as BlockReference;
                    BlockTableRecord block = null;
                    if (blockRef.IsDynamicBlock)
                    {
                        block = tx.GetObject(blockRef.BlockTableRecord,
                                    OpenMode.ForRead) as BlockTableRecord;
    
                        if (block.IsAnonymous)
                        {
                            //get the real dynamic block name.
                            block =
                                tx.GetObject(blockRef.DynamicBlockTableRecord,
                                    OpenMode.ForRead) as BlockTableRecord;
                        }
                    }
                    else
                    {
                        block = tx.GetObject(blockRef.BlockTableRecord,
                                    OpenMode.ForRead) as BlockTableRecord;
                    }
    
    
                    if (block != null)
                    {
                        
                        bname = block.Name;
                        bx = acSSPrompt.PickedPoint.X.ToString();
                        by = acSSPrompt.PickedPoint.Y.ToString();
                        bz = acSSPrompt.PickedPoint.Z.ToString();
    
                        ed.WriteMessage("Block name is : " + block.Name + "\n");
                        ed.WriteMessage("X is :" + bx + "\n");
                        ed.WriteMessage("Y is :" + by + "\n");
                        ed.WriteMessage("Z is :" + bz + "\n");
                    }
                    tx.Commit();
                                    
    
                    // Get the start point of the leader
                    PromptPointResult result = ed.GetPoint("\nSpecify leader arrowhead location: ");
    
                    if (result.Status != PromptStatus.OK)
                        return;
    
                    Point3d startPt = result.Value;
    
                    // Get the end point of the leader
                    PromptPointOptions opts = new PromptPointOptions("\nSpecify landing location: ");
                    opts.BasePoint = startPt;
                    opts.UseBasePoint = true;
                    result = ed.GetPoint(opts);
    
                    if (result.Status != PromptStatus.OK)
                        return;
    
                    Point3d endPt = result.Value;
                    ////////////////////////////////////////////////////////////////////////////////
                    ////////////////////////////////////////////////////////////////////////////////
    
    
                    Transaction tr = db.TransactionManager.StartTransaction();
                    using (tr)
                    {
                        try
                        {
                            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    
                            // Create the MLeader
                            MLeader mld = new MLeader();
                            int ldNum = mld.AddLeader();
                            int lnNum = mld.AddLeaderLine(ldNum);
                            mld.AddFirstVertex(lnNum, startPt);
                            mld.AddLastVertex(lnNum, endPt);
                            mld.ArrowSymbolId = arrId;
                            mld.LeaderLineType = LeaderType.SplineLeader;
    
    
                            // Create the MText
                            MText mt = new MText();
                            PromptStringOptions psopts = new PromptStringOptions("Please input a label for your leader line");                     
    
                            psopts.DefaultValue = bname + "\n" + "X:" + bx + "\n" + "Y:" + by + "\n" + "Z:" + bz;
                            PromptResult pres = ed.GetString(psopts);
    
    
                            mt.Contents = pres.ToString();
                            mt.Location = endPt;
                            mld.ContentType = ContentType.MTextContent;
                            mld.MText = mt;
    
                            // Add the MLeader
                            btr.AppendEntity(mld);
                            tr.AddNewlyCreatedDBObject(mld, true);
    
                            //Commit the transaction
                            tr.Commit();
    
                        }
                        catch
                        {
                            // Would also happen automatically
                            // if we didn't commit
                            tr.Abort();
                        }
                    }
                }
            }
        }
    }

     

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

    Re: Random "OK" showing up in MTEXT

    05-29-2012 09:48 AM in reply to: vince1327

    Just change this line within the code:

     

    mt.Contents = pres.StringResult;

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Distinguished Contributor
    vince1327
    Posts: 117
    Registered: ‎11-02-2011

    Re: Random "OK" showing up in MTEXT

    05-29-2012 09:50 AM in reply to: Hallex

    Perfect!!!!!!! Thanks again Hallex, I owe you one

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

    Re: Random "OK" showing up in MTEXT

    05-29-2012 10:04 AM in reply to: vince1327

    Glad I could help,

    Cheers, bro :smileyhappy:

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.