.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Random "OK" showing up in MTEXT

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
vince1327
624 Views, 3 Replies

Random "OK" showing up in MTEXT

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();
                    }
                }
            }
        }
    }
}

 

3 REPLIES 3
Message 2 of 4
Hallex
in reply to: vince1327

Just change this line within the code:

 

mt.Contents = pres.StringResult;

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 4
vince1327
in reply to: Hallex

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

Message 4 of 4
Hallex
in reply to: vince1327

Glad I could help,

Cheers, bro 🙂

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost