Author Topic: MText Editor - Project Exercise  (Read 9055 times)

0 Members and 1 Guest are viewing this topic.

LE

  • Guest
MText Editor - Project Exercise
« on: November 29, 2006, 11:47:38 PM »
The idea for this alternative MText Editor is to implement new or existing features using an User Interface and mostly to get into the C# world as much as possible...


Code: [Select]
using System;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using MTextEditor;

[assembly: CommandClass(typeof(ClassLibrary.LESQClass))]

namespace ClassLibrary
{
    /// <summary>
    /// Summary description for LESQClass.
    /// </summary>
    public class LESQClass
    {
        //static string x ;

        public LESQClass()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        [CommandMethod("MTED")]
        static public void mtexteditor()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
           
            //doc.BeginDoubleClick += new _DAcadDocumentEvents_BeginDoubleClickEventHandler(m_doc_BeginDoubleClick);

            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptEntityResult res = ed.GetEntity("\nSelect a MText: ");
            if (res.Status != PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                MText text = tr.GetObject(res.ObjectId, OpenMode.ForWrite, false) as MText;
                if (text == null) return;
                string origText = text.Contents;
                using (MTextForm form = new MTextForm())
                {
                    form.SaveMyString(origText);
                    form.Contents = text.Contents;
                    acadApp.ShowModalDialog(form);
                    text.Contents = form.Contents;           
                }
                tr.Commit();
            }
        }

    }
}

Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
using System.Collections;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace MTextEditor
{
    public partial class MTextForm :  Form
    {

        //ArrayList stringList = new ArrayList();

        string[] lista = new string[3];

        public MTextForm()
        {
            InitializeComponent();
        }

        // remove color from the mtext
        private void OnClick_RemoveColor(object sender, EventArgs e)
        {
            //Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;

            string txtstr = textBox1.Text;
            char[] s = txtstr.ToCharArray();
            int cont = 0, flag = 0;
            string txt1 = "";
            for (int j = 0; j < txtstr.Length; j++)
            {
                try
                {
                    char let = s[cont];
                    if (let == '\\')
                    {
                        if (char.ToUpper(s[cont + 1]) == 'C')
                        {
                            int i = (cont + 2);
                            while (s[i] != ';')
                                i++;
                            // remove true and book colors
                            if (s[i + 1] == '\\' && s[i + 2] == 'c')
                            {
                                i = i + 2;
                                while (s[i] != ';')
                                    i++;
                            }
                            txt1 = txt1 + s[i + 1];
                            cont = i + 2;
                        }
                        else
                            flag = 1;
                    }
                    else // color format found
                    {
                        txt1 = txt1 + let;

                        // remove the color blocks
                        int found;
                        found = txt1.IndexOf("{");
                        if (found >= 0)
                            txt1 = txt1.Remove(found, 1);
                        found = txt1.IndexOf("}");
                        if (found >= 0)
                            txt1 = txt1.Remove(found, 1);

                        cont = 1 + cont;
                    }
                    // no color format found
                    if (flag == 1)
                    {
                        txt1 = txt1 + let;
                        cont = 1 + cont;
                        flag = 0;
                    }
                }
                catch
                {
                    //ed.WriteMessage("Error");
                }
            }
            textBox1.Text = txt1; // change the contents
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //this.Close();
        }

        public string Contents
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        private void MTextForm_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.SelectAll(); // onLoad form select all text
        }

        public string GetMyString()
        {
            string s = lista[0];
            return s;
        }

        private void OnClick_Exit(object sender, EventArgs e)
        {
            textBox1.Text = GetMyString();
            Close();
        }

        public void SaveMyString(string s)
        {
            lista[0] = s;
        }

        private void MTextForm_Close(object sender, FormClosedEventArgs e)
        {
            textBox1.Text = GetMyString();
        }

        //public void CopyAllMyText()
        //{
        //    if (textBox1.SelectionLength == 0)
        //        textBox1.SelectAll();
        //    //textBox1.Copy();
        //}

    }
}

Adding:
Included is the VS2005 SLN - in case someone find it useful ... Your comments/feedback are welcome. Have fun

Adding:
User Interface
« Last Edit: November 30, 2006, 12:10:24 AM by LE »

Glenn R

  • Guest
Re: MText Editor - Project Exercise
« Reply #1 on: November 30, 2006, 12:37:32 AM »
Great start on your C# stuff Luis. Attached is your code with some of my changes.
You'll notice I added another constructor to your form.

Anyway, have a look.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Editor - Project Exercise
« Reply #2 on: November 30, 2006, 01:14:29 AM »
great stuff Luis ..

You might want to have a look at one of Tony's samples too ..
he <the master> refers to Dimensions, but I ran something similar against conventional MText
Quote
"Tony Tanzillo" wrote <tony.tanzillo@THE_URL_BELOW.com> wrote in message news:<5401749@discussion.autodesk.com>...
You can strip the formatting info from the
MTEXT object in the dimension block definition.

See http://www.caddzone.com/MTextFragmentSample.cs for an example.

I added this line for the dialog :
Code: [Select]
WinForms.MessageBox.Show("\n" + mtext.Contents + "\n" + s_data );
with this result :


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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Editor - Project Exercise
« Reply #3 on: November 30, 2006, 01:17:39 AM »
oh, the 'WinForms' is defined from this  :

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using WinForms = System.Windows.Forms;
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.

Glenn R

  • Guest
Re: MText Editor - Project Exercise
« Reply #4 on: November 30, 2006, 01:28:31 AM »
Kerry, out of interest, put a few "hard returns" in your mtext string and run Tony's sample over it.
From memory it throws big fat exceptions...

Glenn R

  • Guest
Re: MText Editor - Project Exercise
« Reply #5 on: November 30, 2006, 01:30:40 AM »
A friend and I recently had to look at stripping/replacing formatting codes in MText to get it to go to MicroStation...it's not a particularly easy exercise when you consider there are a LOT of combinations.

I definately think regular expressions would be the way to go for this sort of thing.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Editor - Project Exercise
« Reply #6 on: November 30, 2006, 01:41:29 AM »
Glenn, there are a few issues with the Sample, but it's a start.

Yes,  Formatting translation is a real pain. We wrote some cpp stuff about 7 or 8 years ago ... going to have to do it again for C#. Considering we had a VLisp version running perfectly well at the time there is definite cause to question my sanity.

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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Editor - Project Exercise
« Reply #7 on: November 30, 2006, 01:50:27 AM »
Glenn,

I just edited the Mtext ... added a few hard returns .... NO issues with it exploding  in AC2007.
There is an issue 'cause its doing what it's told .. ie removing the \P 's when I'd expect that they be replaced with a space .. but thats a procedural thing.

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.

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #8 on: November 30, 2006, 09:19:54 AM »
oh, the 'WinForms' is defined from this  :

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using WinForms = System.Windows.Forms;

Thank you Kerry for the url link, I'll check the sample.

When I wrote the first sample using the explodeFragments() - in ObjectARX - I got the fragments in between text lines, for example if one word has two colors let say... (no big deal - and since the idea was to provide different ways to remove the color format from a MText - I moved into C++, Lisp and now C#)

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #9 on: November 30, 2006, 09:36:20 AM »
Great start on your C# stuff Luis. Attached is your code with some of my changes.
You'll notice I added another constructor to your form.

Anyway, have a look.

Cheers,
Glenn.

Great, I am now testing the solution with the new changes... now I do not have the undo in case the x button on the form and another I have to exit without making any changes...

That's why I was using the GetMyString and SaveMyString functions... I know in C# they are the get/set to get or change attributes, I need to read more about them...

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #10 on: November 30, 2006, 12:57:59 PM »
Changes were made to the original code, here is the latest...

Code: [Select]
        [CommandMethod("MTED")]
        static public void mtexteditor()
        {
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            PromptEntityResult res = ed.GetEntity("\nSelect a MText: ");
            if (res.Status != PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                MText text = tr.GetObject(res.ObjectId, OpenMode.ForWrite, false) as MText;
                if (text == null) return;
                string origText = text.Contents;
                using (MTextForm form = new MTextForm(origText))
                {
                    // save original text
                    form.OrigMText = origText;

                    acadApp.ShowModalDialog(form);
                    text.Contents = form.Contents;           
                }
                tr.Commit();
            }
        }

Code: [Select]
namespace MTextEditor
{
    public partial class MTextForm :  Form
    {
        private string orig;
        public string OrigMText
        {
            get { return orig; }
            set { orig = value; }
        }
       
        public MTextForm()
        {
            InitializeComponent();
        }

public MTextForm(string mtextContents) : this()
{
textBox1.Text = mtextContents;
}

        // remove color from the mtext
        private void OnClick_RemoveColor(object sender, EventArgs e)
        {
            string txtstr = textBox1.Text;
            char[] s = txtstr.ToCharArray();
            int cont = 0, flag = 0;
            string txt1 = "";
            for (int j = 0; j < txtstr.Length; j++)
            {
                try
                {
                    char let = s[cont];
                    if (let == '\\')
                    {
                        if (char.ToUpper(s[cont + 1]) == 'C')
                        {
                            int i = (cont + 2);
                            while (s[i] != ';')
                                i++;
                            // remove true and book colors
                            if (s[i + 1] == '\\' && s[i + 2] == 'c')
                            {
                                i = i + 2;
                                while (s[i] != ';')
                                    i++;
                            }
                            txt1 = txt1 + s[i + 1];
                            cont = i + 2;
                        }
                        else
                            flag = 1;
                    }
                    else // color format found
                    {
                        txt1 = txt1 + let;

                        // remove the color blocks
                        int found;
                        found = txt1.IndexOf("{");
                        if (found >= 0)
                            txt1 = txt1.Remove(found, 1);
                        found = txt1.IndexOf("}");
                        if (found >= 0)
                            txt1 = txt1.Remove(found, 1);

                        cont = 1 + cont;
                    }
                    if (flag == 1) // no color format found
                    {
                        txt1 = txt1 + let;
                        cont = 1 + cont;
                        flag = 0;
                    }
                }
                catch{}
            }
            textBox1.Text = txt1; // change the contents
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        public string Contents
        {
            get { return textBox1.Text; }
        }

        private void MTextForm_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.SelectAll(); // onLoad form select all text
        }

        private void MTextForm_Closed(object sender, FormClosedEventArgs e)
        {
            textBox1.Text = OrigMText;
        }

        private void OnClick_ApplyChanges(object sender, EventArgs e)
        {
            textBox1.Text = Contents;
            OrigMText = Contents;
            Close();
        }

    }
}

TO-DO:

1. Add one button to undo the mtext with the original contents (showing the color formatting)
2. Add the Uppercase, Lowercase, Uppercase first characters.

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #11 on: November 30, 2006, 02:26:31 PM »
Here is the function to change to proper case the contents of the mtext.

It goes into the MTextForm class - and by adding an OnClick event into one of the supplied buttons on the form

Code: [Select]
private void OnClick_ProperCase(object sender, EventArgs e)
{
    // get the actual text on the textbox
    string str = Contents;
    string strPCase = str.Substring(0, 1).ToUpper();
    str = str.Substring(1).ToLower();
    string strP = "";
    for (int i=0; i<str.Length; i++)
    {
        if (i > 1)
            strP = str.Substring(i - 1, 1);
        if (strP.Equals(" ") ||
            strP.Equals("\t") ||
            strP.Equals("\n") ||
            strP.Equals("."))
            strPCase += str.Substring(i, 1).ToUpper();
        else
            strPCase += str.Substring(i, 1);
    }
    // set back the new text to the textbox
    textBox1.Text = strPCase;
}

Not fully tested - if a mtext has color formatting or maybe any other format - it will remove that text portion - so requires more tests and update to the function.

To make it work, please first run the removal of color format and then the proper case...

TO-DO:
Work on more types of formatting - not just the color -

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #12 on: November 30, 2006, 03:31:51 PM »
Quote
Not fully tested - if a mtext has color formatting or maybe any other format - it will remove that text portion - so requires more tests and update to the function.

Actually what it does is that changes the color format to lower case and that changes the color - when converting to proper case each word...

I added in the filter condition the comparison for the ";" - that will turn a word to proper case ie:

this is {\C2;only} a test of a {\C6;proper} {\C105;\c5881664;case} mtext

The word proper to Proper (as it does to the whole paragraph... I need to find a way to avoid the turn to lowercase the formatting code....

This is the result on the dialog, when the proper case option is called:

This Is {\c2;Only} A Test Of A {\c6;Proper} {\c105;\c5881664;Case} Mtext

See, it does the job, but changes the color formatting to lowercase to...

Code: [Select]
if (strP.Equals(" ") ||
                    strP.Equals("\t") ||
                    strP.Equals("\n") ||
                    strP.Equals(".") ||
                    strP.Equals(";")) //<<<-----
« Last Edit: November 30, 2006, 03:37:18 PM by LE »

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #13 on: November 30, 2006, 03:51:13 PM »
While I am working on this, does anyone knows why the title bar icon does not show the one applied... (mt.ico)... for some reason - I could not make it to work... it brings the a2007 one...

Other... it will be a good idea to implement a reactor when touching a MText... I have to read more about this.... appart of all the namespace, any one required for that in particular? - I have seen interop or something like that....

Glenn R

  • Guest
Re: MText Editor - Project Exercise
« Reply #14 on: November 30, 2006, 05:47:18 PM »
As far as the form icon goes Luis, I've never gotten it to work in either 2005, 6 or 7. I suspect AutoCAD overrides it...no biggy.

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #15 on: December 01, 2006, 07:58:00 PM »
As far as the form icon goes Luis, I've never gotten it to work in either 2005, 6 or 7. I suspect AutoCAD overrides it...no biggy.

Thanks;

I noticed that it is possible if I put some code in the form_load event.

Code: [Select]
Icon icoMain = new Icon("mt.ico");
this.Icon = icoMain;

It works, but the ico file must be in the same location as the dll... anyway I will look if it is possible if not, as you said no biggy...

le

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #16 on: December 02, 2006, 10:22:02 PM »
Here is what I end up with this command, included is the DLL to work with AutoCAD 2007 - provided AS-IS.

Command: MTED

Select a Mtext and will open the UI.

Features:

1. Highlight ALL the MTEXT contents when loading the dialog.
2. Color formatting removal.
3. Proper Case - foreach first character word.
4. Whitespace stripping to single between words.
5. Exit dialog without changes.

End of this topic.
« Last Edit: December 10, 2006, 12:33:58 PM by LE »

Glenn R

  • Guest
Re: MText Editor - Project Exercise
« Reply #17 on: December 02, 2006, 10:45:10 PM »
Nice one Luis - well done.
C# is quite nice isn't it? :)

Cheers,
Glenn.

LE

  • Guest
Re: MText Editor - Project Exercise
« Reply #18 on: December 03, 2006, 01:13:33 PM »
Nice one Luis - well done.
C# is quite nice isn't it? :)

Thank you.

Yes it is very nice  :-)
« Last Edit: December 04, 2006, 09:42:34 AM by LE »