Author Topic: MText Width Factor  (Read 2640 times)

0 Members and 1 Guest are viewing this topic.

hellios8502

  • Guest
MText Width Factor
« on: February 04, 2011, 04:56:47 PM »
P.S. Is it possible to change a MText width factor?  :roll:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Width Factor
« Reply #1 on: February 04, 2011, 05:10:23 PM »

Do you mean for individual Mtext or for a Text Style ?

And do you mean the Font WIDTH Factor or the MText Width Bounding Box.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

hellios8502

  • Guest
Re: MText Width Factor
« Reply #2 on: February 04, 2011, 06:26:12 PM »
For individual Mtext, not the bounding box but the text it self . I want to change the width factor no matter the text style, but I can't seem to find any reference in the objectarx documentation  :| . I also searched through the autodesk disussion goups but couldn't find any info involving net.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: MText Width Factor
« Reply #3 on: February 04, 2011, 07:16:19 PM »
For the TextStyle the property is XScale

For individual mtext just looking at it real quick with mgddb was to add for example "\W2.5;" to beginning of the mtext contents

If you go this route and the width has already been changed differently from the textstyle property then you need check for it in the contents

This simple example with no error handling or checking if you selected a mtext item lets you enter a width then select a mtext entity.

You might wait to see if anyone replies with a better solution.


Code: [Select]
[CommandMethod("ChangeMtextWidth")]
        public void ChangeMtextWidth()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            double width = 1;

            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                PromptDoubleResult pdr = ed.GetDouble("\nEnter Width");
               
                if (pdr.Status == PromptStatus.OK)
                {
                    width = pdr.Value;
                }

                PromptEntityResult per = ed.GetEntity("\nSelect Entity");
                if (per.Status == PromptStatus.OK)
                {
                    MText mt = trx.GetObject(per.ObjectId, OpenMode.ForWrite) as MText;
                    string mtxtContents = "\\W" + width.ToString() + ";" + mt.Contents;
                    mt.Contents = mtxtContents;
                }

                trx.Commit();       
            }
        }


dan.glassman

  • Guest
Re: MText Width Factor
« Reply #4 on: February 04, 2011, 10:34:35 PM »
As Jeff noted, applying a formatting code directly is fiddly work.  That's always given me a headache.

Here's another solution that uses a TextEditor (restricting its use to 2011), that probably has its own gotchas.

Code: [Select]
public class TextCommands
{
    private static double LastMTextWidth { get; set; }

    static TextCommands() { TextCommands.LastMTextWidth = 1; }

    [CommandMethod("ChangeMtextWidth")]
    public void ChangeMtextWidth()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        PromptDoubleOptions pdo = new PromptDoubleOptions("Enter width: ");
        pdo.AllowNegative = false;
        pdo.AllowZero = false;
        pdo.AllowNone = true;
        pdo.DefaultValue = TextCommands.LastMTextWidth;
        pdo.UseDefaultValue = true;

        PromptDoubleResult pdr = ed.GetDouble(pdo);
        if (pdr.Status != PromptStatus.OK)
        {
            ed.WriteMessage("Cancelled.\n");
            return;
        }
        double width = pdr.Value;

        PromptSelectionOptions pso = new PromptSelectionOptions();
        pso.MessageForAdding = "Select mtexts to add: ";
        pso.MessageForRemoval = "Select mtexts to remove: ";
        pso.RejectObjectsOnLockedLayers = true;
        pso.RejectObjectsFromNonCurrentSpace = true;
        pso.AllowDuplicates = false;

        SelectionFilter sf =
            new SelectionFilter(new TypedValue[] {
                new TypedValue((int)DxfCode.Start, "MTEXT") });

        PromptSelectionResult psr = ed.GetSelection(pso, sf);
        if (psr.Status != PromptStatus.OK)
        {
            ed.WriteMessage("Cancelled.\n");
            return;
        }

        TextCommands.LastMTextWidth = width;    

        using (Transaction trx = db.TransactionManager.StartTransaction())
        {
            foreach (ObjectId mtId in psr.Value.GetObjectIds())
            {
                MText mt = trx.GetObject(mtId, OpenMode.ForWrite) as MText;
                if (mt == null) continue;

                using (TextEditor te = InplaceTextEditor.CreateTextEditor(mt))
                {
                    te.SelectAll();
                    te.Selection.WidthScale = width;
                    te.Close(TextEditor.ExitStatus.ExitSave);
                }
            }
            trx.Commit();
        }
    }
}

« Last Edit: February 04, 2011, 11:19:46 PM by dan.glassman »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: MText Width Factor
« Reply #5 on: February 04, 2011, 10:59:04 PM »
Like I posted previously if it had been formated you would have to check, but Dan's post takes care of that.



Dan,
Nice idea using texteditor. Did not think of that.

Your approach is much better. I hope to see you post more at the Swamp.

Thanks




dan.glassman

  • Guest
Re: MText Width Factor
« Reply #6 on: February 04, 2011, 11:24:02 PM »
Jeff -- I modified my post so that it didn't read so much like a criticism [sorry about that -- wasn't my intent.]

I'm sure I'll get the post count up, eventually; mostly lurking and learning so far.  I need to thank you, btw -- it was your sig @ forums.autodesk.com that led me here.

Cheers!

-drg

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Width Factor
« Reply #7 on: February 04, 2011, 11:32:37 PM »

That looks like a great solution Dan.


Welcome to the Swamp.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: MText Width Factor
« Reply #8 on: February 04, 2011, 11:36:08 PM »
Jeff -- I modified my post so that it didn't read so much like a criticism [sorry about that -- wasn't my intent.]


Not at all.

If I post something stupid please say something and why it is bad. That helps me and maybe others learn. I learn alot from what I have done wrong.

Also,
Just don't say anything bad about my son or Guns N Roses(1996 and earlier) the rest does not matter

hellios8502

  • Guest
Re: MText Width Factor
« Reply #9 on: February 05, 2011, 03:49:53 AM »
Thanks again for the fast reply and the great answers.