Author Topic: MText Stuff  (Read 3712 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
MText Stuff
« on: May 17, 2008, 11:48:28 PM »
I was doing some red-lining for an associate who's learning to push ACAD around from .NET C# , so I thought I'd post the samples.

as indicated here :-
http://www.theswamp.org/index.php?topic=7919.0

The process of adding entities to a drawing Database is/canbe broken into 6 basic stages.

//
//Start a database transaction
//
//Create object
//
//Open modelspace for writing
//
//Add the object to modelspace
//
//Inform the transaction of the new object
//
//Commit the transaction

.. So these are the Mark-ups ...
(note , Using the Mercury theme from here :-
http://www.theswamp.org/index.php?action=theme;sa=pick;u=34;sesc=d5e76a9514c774d888a8c40980bd39ec
ALL the code should be visible)

Code: [Select]
//  CodeHimBelongaKwb
//  [revised kwb 2008-05-18 11:58]
//
#region ... Using Statements

using System;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
//
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
//
using AcCm = Autodesk.AutoCAD.Colors;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;
using AcLy = Autodesk.AutoCAD.LayerManager;
using AcPl = Autodesk.AutoCAD.PlottingServices;
using AcUi = Autodesk.AutoCAD.Windows;

using WinForms = System.Windows.Forms;
#endregion

[assembly: AcRx.CommandClass(typeof(kdub.Testing.Commands.TestCommands))]

namespace kdub.Testing.Commands
{
    /// <summary>
    /// Summary description for TestCommands Class.
    /// </summary>
    public partial class TestCommands
    {
        [AcRx.CommandMethod("HelloMText")]
        public static void HelloMTextCommand()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btrModelSpace = tr.GetObject(bt[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;

                MText oMtext = new MText();
                oMtext.Contents = "Hello World in ModelSPACE !! ";

                btrModelSpace.AppendEntity(oMtext);

                db.TransactionManager.AddNewlyCreatedDBObject(oMtext, true);

                tr.Commit();
            }
        }
        //
        //=================================================================
        //
        [AcRx.CommandMethod("MT-01")]
        public static void HelloMTextCommand_01()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            string mtextContent = "Hello World in ModelSPACE !! from HelloMText-01";
            ObjectId oMtextID = AddMTextToDb(mtextContent, db);

            AcadApp.ShowAlertDialog("ObjectID : " + oMtextID.ToString());

            HelloMTextCommand_02(oMtextID);
        }
        //
        //=================================================================
        //
        public static void HelloMTextCommand_02(ObjectId id)
        {
            MText oMtext = new MText();
            if (!id.IsNull)
            {
                using (Transaction tr = id.Database.TransactionManager.StartTransaction())
                {
                    oMtext = tr.GetObject(id, OpenMode.ForWrite) as MText;
                    Point3d attachPt = new Point3d(10, 20, 0);
                    oMtext.Location = attachPt;
                    oMtext.Attachment = AttachmentPoint.MiddleCenter;
                    oMtext.ColorIndex = 2;
                    //
                    string txtStyle = "T35";
                    ObjectId txtId = ObjectId.Null;
                    TextStyleTableRecord txtTbr = null;
                    TextStyleTable txtTb = tr.GetObject(id.Database.TextStyleTableId, OpenMode.ForRead)
                                                          as TextStyleTable;
                    if (txtTb.Has(txtStyle))
                    {
                        txtTbr = tr.GetObject(txtTb[txtStyle], OpenMode.ForRead) as TextStyleTableRecord;
                    }
                    else
                    {
                        txtTbr = tr.GetObject(txtTb["Standard"], OpenMode.ForRead) as TextStyleTableRecord;
                    }
                    oMtext.TextStyle = txtTbr.ObjectId;

                    tr.Commit();
                }
            }
            AcadApp.ShowAlertDialog("Layer : " + oMtext.Layer.ToString());
        }

        //
        //=================================================================
        //
        public static ObjectId AddMTextToDb(string mtextContent, Database db)
        {
            ObjectId oID = new ObjectId();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btrModelSpace = tr.GetObject(bt[BlockTableRecord.ModelSpace],
                                                    OpenMode.ForWrite) as BlockTableRecord;
                MText oMtext = new MText();

                oMtext.Contents = mtextContent;
                oID = btrModelSpace.AppendEntity(oMtext);
                db.TransactionManager.AddNewlyCreatedDBObject(oMtext, true);

                tr.Commit();
            }
            return oID;
        }
    }
}

and the obligatory piccy :-)

edit: code modified.
« Last Edit: May 18, 2008, 05:39:41 AM by Kerry Brown »
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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8858
  • AKA Daniel
Re: MText Stuff
« Reply #1 on: May 18, 2008, 12:15:00 AM »
Still looking through this, but something caught my eye,  Should you be wrapping your new MText object, oMtext in a using statement?
If I remember correctly, you should not dispose of objects that are added to the database, but you should if they are not.. 

..If you reply to this, I know your computer didn’t explode and I’m wrong  :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Stuff
« Reply #2 on: May 18, 2008, 12:19:57 AM »
I'm responding, there is no smoke, but I could still be incorrect.  :-D
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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8858
  • AKA Daniel
Re: MText Stuff
« Reply #3 on: May 18, 2008, 12:37:03 AM »

No smoke means I’m wrong  :oops:
Very Nice work!  :kewl:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Stuff
« Reply #4 on: May 18, 2008, 12:39:18 AM »
I have an old DOS prog' called NoSmoke.exe that I run memory resident ... that could be coming into play  :lmao:
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 Stuff
« Reply #5 on: May 18, 2008, 05:15:12 AM »
Dan, I've removed the additional redundant using statements.
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 Stuff
« Reply #6 on: May 18, 2008, 05:29:32 AM »
... and this is the StartApp.cs I compile with the sample ... it will Autoexecute when the dll is netloaded
or run when 'AppHelp' is entered at the command line.
Code: [Select]

//  CodeHimBelongaKwb
//  [revised kwb 2008-5-18 19:22]

using System;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcRx = Autodesk.AutoCAD.Runtime;

[assembly: AcRx.ExtensionApplication(typeof(kdub.Testing.Commands.StartApp))]
[assembly: AcRx.CommandClass(typeof(kdub.Testing.Commands.AppHelp))]

namespace kdub.Testing.Commands
{
    //===========================================
    public class StartApp : AcRx.IExtensionApplication
    {
        public void Initialize() {         
            AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
                "\nCommands Loading ...");
            AppHelp.ApplicationHelp();
        }
        //---------------------------------------
        public void Terminate() {
            // Does Nothing Yet, 'cause can't unload NET assembly ...
            //.... so, unloads automagically on close of AutoCad!
        }
        //---------------------------------------
    }
    //===========================================
    public static class AppHelp
    {
        public static void ApplicationHelp() {
            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nCommand Help ...");
            ed.WriteMessage("\nType HelloMText for generic MText, ");
            ed.WriteMessage("\nType MT-01 for MText with properties modified. ");
        }
        //---------------------------------------
        [AcRx.CommandMethod("AppHelp")]
        public static void AppHelpCommand() {
            ApplicationHelp();
        }       
    }
    //===========================================
}

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 Stuff
« Reply #7 on: May 18, 2008, 05:17:37 PM »
I realise this is a contrived example, however, for the sake of completeness...

Kerry, in this function:

HelloMTextCommand_02

The first thing you do is create a new Mtext object even before you get into your main 'if' conditional.
Personally, I would move the creation of the mtext inside your main conditional, therefore it will only be spun up
if your conditional is true...does that make sense?

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: MText Stuff
« Reply #8 on: May 18, 2008, 06:46:10 PM »
I realise this is a contrived example, however, for the sake of completeness...

Kerry, in this function:

HelloMTextCommand_02

The first thing you do is create a new Mtext object even before you get into your main 'if' conditional.
Personally, I would move the creation of the mtext inside your main conditional, therefore it will only be spun up
if your conditional is true...does that make sense?

Cheers,
Glenn.

Yes, it makes sense. The 'AcadApp.ShowAlertDialog( ... " would then need to in the scope of the definition.
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 Stuff
« Reply #9 on: May 19, 2008, 01:03:54 AM »
Ah yes...missed that one at the bottom.