Author Topic: Programming ObjectArx NET (1) - A tool class for document, editor and database  (Read 2734 times)

0 Members and 1 Guest are viewing this topic.

csharpbird

  • Newt
  • Posts: 64
I've been learning ObjectArx Net programming for AutoCAD for a while. As an environment of development, AutoCAD platform is more powerful than we thought. During the learning period I realized there were several programming skills / or issues which were valuable to discuss with others, so I decided to write down my experience, step by step with one sample project which is named “Project Ben”.




In this article, let’s focus on the document, editor and database. We are very familiar with below code, in the beginning of each function,


      Document doc = Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;
      Editor ed = doc.Editor;


To avoid the duplicated code in every function, A global tool class could be created,


using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace Test1
{
    static class G
    {
        public static Document Doc
        {
            get
            {
                if (_doc == null)
                    _doc = AcadApp.DocumentManager.MdiActiveDocument;
                return _doc;
            }
        }
        public static Editor Ed
        {
            get
            {
                if (_ed == null)
                    _ed = Doc.Editor;
                return _ed;
            }
        }
        public static Database Db
        {
            get
            {
                if (_db == null)
                    _db = Doc.Database;
                return _db;
            }
        }
        private static Document _doc;
        private static Editor _ed;
        private static Database _db;
    }
}

And this is the test code:


    [CommandMethod("Test")]
    public void Test()
    {
        G.Ed.WriteMessage("Hello World!");
    }

However, there is an error when running the test in a new drawing document, because after switching to another document, the reference to document, editor and database become invalid.

So it is needed to notify the class G to invalid the static members,

    [assembly: ExtensionApplication(typeof(Test1.App))]
    class App : IExtensionApplication
    {
        public void Initialize()
        {
            AcadApp.DocumentManager.DocumentActivated += new
                DocumentCollectionEventHandler(
                DocumentManager_DocumentActivated);
        }
        public void Terminate()
        {
            AcadApp.DocumentManager.DocumentActivated -= new
                DocumentCollectionEventHandler(
                DocumentManager_DocumentActivated);
        }
        void DocumentManager_DocumentActivated(object sender,
            DocumentCollectionEventArgs e)
        {
            G.InvalidDocument();
        }
    }
    static class G
    {
        ...
        public static void InvalidDocument()
        {
            _doc = null;
            _ed = null;
            _db = null;
        }
    }


Now G.Doc, G.Ed and G.Db do support for multiple documents.


(by Youzi from objectarx.net)


Bryco

  • Water Moccasin
  • Posts: 1864
I like that, hope C# will end up with that.