Open a drawing with .net?

 Does anyone know if it's possible to open a drawing from within bricscad via c# similar to what is described here - 

Specifically, the line - 
acDocMgr.Open(strFileName, false);    

Comments

  • You can do this via COM, also you can create an extension method so it will be more code compatible

           public static class Commands 
        {
            public static void Open(this DocumentCollection manager, string path)
            {
                AcadApplication app = AcAp.Application.AcadApplication as AcadApplication;
                AcadDocument doc = app.Documents.Open(path,false,null);
                if (doc != null)
                {
                    app.ActiveDocument = doc;
                }
            }         [CommandMethod("doit")]
            public static void doit()
            {
                try
                {
                    string doctoOpen = @C:\Drawings\Drawing1.dwg;
                    AcAp.Application.DocumentManager.Open(doctoOpen);
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(
                        String.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                }
            }
        }
  • sorry for the code format : (
  • sorry for the code format : (

    That's alright.  Thanks for the tip!  I'll have to give it a try.

  • That's alright.  Thanks for the tip!  I'll have to give it a try.

    Did it help you?
    I have similar problem, but I have to close the drawing afterwards.
    This is sample code for Autocad and it would be nice to get it work on BricsCad

                For i = 0 To FailsForOpening.Count - 1
                    Dim Drawing As ApplicationServices.Document = ApplicationServices.Application.DocumentManager.Open(FailsForOpening(i), True)
                   
                   'some actions with drawing
                    'and now the drawing has to be closed
                    Drawing .CloseAndDiscard()
                Next
  •  I didn't get a chance to try it yet, but I'm hoping to this week.  I'll be needing to close the drawings also, so I'll try to post the solution here if all goes well.
  • Okay, so I got a chance to try the COM libraries in conjunction with the .NET libraries.  It all works fine, but can be a little confusing figuring out which method or class to use from which library so I'll post some code with fully-qualified classes and methods.

    First, you have to make sure you have all the pertinent .NET and COM libraries referenced.  The key .NET reference is BrxMgd.dll  The key COM reference is BricscadApp.dll

    [code]
    using Bricscad.ApplicationServices;    // .NET library
    using BricscadApp;                         // COM library

    ///
    /// Open the drawing specified by the filename string.
    ///

    /// String that fully qualifies the desired file.
    public void OpenDrawing(string fileName)
    {
        BricscadApp.AcadApplication app = Bricscad.ApplicationServices.Application.AcadApplication as BricscadApp.AcadApplication;

        BricscadApp.AcadDocument doc = app.Documents.Open(fileName, false, null);

        if (doc != null) {
            app.ActiveDocument = doc;
        }
    }

    ///
    /// Close the current (active) drawing.
    ///

    public void CloseDrawing()
    {
        BricscadApp.AcadApplication app = Bricscad.ApplicationServices.Application.AcadApplication as BricscadApp.AcadApplication;
        app.ActiveDocument.Close();
    }
    [/code]

    Hope that helps.
  • Good!,

    Also, you can get the COM instance of the document, in the same way you do for the application

    [code]
        public static class Commands
        {
            //overload for read only and password
            public static void Open(this DocumentCollection manager, string path)
            {
                AcadApplication app = AcAp.Application.AcadApplication as AcadApplication;
                AcadDocument doc = app.Documents.Open(path,false,null);
                if (doc != null)
                {
                    app.ActiveDocument = doc;
                }
            }

            public static void CloseAndDiscard(this Document doc)
            {
                AcadDocument _doc = doc.AcadDocument as AcadDocument;
                if (_doc != null)
                    _doc.Close(false, null);
            }

            [CommandMethod("openit")]
            public static void openit()
            {
                try
                {
                    string doctoOpen = @C:\Drawings\Drawing1.dwg;
                    AcAp.Application.DocumentManager.Open(doctoOpen);
                    System.Windows.Forms.MessageBox.Show(AcAp.Application.DocumentManager.Count.ToString());
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(
                        String.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                }
            }

            [CommandMethod("closeit")]
            public static void closeit()
            {
                try
                {
                    AcAp.Application.DocumentManager.MdiActiveDocument.CloseAndDiscard();
                    System.Windows.Forms.MessageBox.Show(AcAp.Application.DocumentManager.Count.ToString());
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(
                        String.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                }
            }
        }[/code]
  • guess copy/paste from VS does not work for me : (
  • [code]

    public static class Commands
        {
            //overload for read only and password
            public static void Open(this DocumentCollection manager, string path)
            {
                AcadApplication app = AcAp.Application.AcadApplication as AcadApplication;
                AcadDocument doc = app.Documents.Open(path,false,null);
                if (doc != null)
                {
                    app.ActiveDocument = doc;
                }
            }

            public static void CloseAndDiscard(this Document doc)
            {
                AcadDocument _doc = doc.AcadDocument as AcadDocument;
                if (_doc != null)
                    _doc.Close(false, null);
            }

            [CommandMethod("openit")]
            public static void openit()
            {
                try
                {
                    string doctoOpen = @C:\Drawings\Drawing1.dwg;
                    AcAp.Application.DocumentManager.Open(doctoOpen);
                    System.Windows.Forms.MessageBox.Show(AcAp.Application.DocumentManager.Count.ToString());
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(
                        String.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                }
            }

            [CommandMethod("closeit")]
            public static void closeit()
            {
                try
                {
                    AcAp.Application.DocumentManager.MdiActiveDocument.CloseAndDiscard();
                    System.Windows.Forms.MessageBox.Show(AcAp.Application.DocumentManager.Count.ToString());
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(
                        String.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                }
            }
        }

    [/code]

  • Thanks a lot. It helped me much. It just works.

    Okay, so I got a chance to try the COM libraries in conjunction with the .NET libraries.  It all works fine, but can be a little confusing figuring out which method or class to use from which library so I'll post some code with fully-qualified classes and methods.

    First, you have to make sure you have all the pertinent .NET and COM libraries referenced.  The key .NET reference is BrxMgd.dll  The key COM reference is BricscadApp.dll

    using Bricscad.ApplicationServices;    // .NET libraryusing BricscadApp;                         // COM library/// /// Open the drawing specified by the filename string./// /// String that fully qualifies the desired file.public void OpenDrawing(string fileName){    BricscadApp.AcadApplication app = Bricscad.ApplicationServices.Application.AcadApplication as BricscadApp.AcadApplication;    BricscadApp.AcadDocument doc = app.Documents.Open(fileName, false, null);    if (doc != null) {        app.ActiveDocument = doc;    }}/// /// Close the current (active) drawing./// public void CloseDrawing(){    BricscadApp.AcadApplication app = Bricscad.ApplicationServices.Application.AcadApplication as BricscadApp.AcadApplication;    app.ActiveDocument.Close();}  


    Hope that helps.
This discussion has been closed.