.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Make a new drawing or open one

9 REPLIES 9
Reply
Message 1 of 10
Juergen.Becker
3586 Views, 9 Replies

Make a new drawing or open one

Hi,

 

there is something thats makes me crazy.

In one of my application I want to open a drawing and make a new one.
That doesnt work. So I copy following code from the .Net help.

 

        [CommandMethod("NewDrawing", CommandFlags.Session)]

        public static void NewDrawing()
        {
            string strTemplatePath = "acadiso.dwt";
            DocumentCollection acDocMgr  = Application.DocumentManager;
            acDocMgr.DocumentActivationEnabled = true;
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = DocumentCollectionExtension.Add(acDocMgr, strTemplatePath);

// Alternatively: Autodesk.AutoCAD.ApplicationServices.Document acDoc = Application.DocumentManager.Add(strTemplatePath);
            acDocMgr.MdiActiveDocument = acDoc;
        }

 

At the end the MdiActiveDocument is still the drawing which where open before a new document is created.

BTW: I'm using 2013 and 2014. This method works fine in 2010.

 

Whats the different between 2013/ 2014 and 2010.

 

Thanks for help.

 

Best Regards Jürgen

 

9 REPLIES 9
Message 2 of 10
mzakiralam
in reply to: Juergen.Becker

Message 3 of 10

Hi,

 

thanks for reply.

That is what I did all the time. The MdiActiveDocument object is always the old one.

Therefore AutoCAD didnt switch or actived the new drawing.

 

This is my actual code (really simple just for testing):

 

 

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension

Public ClassClass1

    < CommandMethod("NewDrawing", CommandFlags.Session)>

   Public Sub NewDrawing()

      '' Specify the template to use, if the template is not found 

   '' the default settings are used.

       

   Dim strTemplatePath AsString = "acad.dwt"

   Dim acDocMgr AsDocumentCollection = Application.DocumentManager

   Dim acDoc AsDocument = acDocMgr.Add(strTemplatePath)

       acDocMgr.MdiActiveDocument = acDoc

 End Sub

End Class

 

The problem is now that no editor works probably.

 

Best Regards Jürgen

Message 4 of 10

Hi,

 

does that work (for me it does):

<Runtime.CommandMethod("NewDrawing", Runtime.CommandFlags.Session)> _
Public Sub NewDrawing()
   Dim strTemplatePath As String = "acad.dwt"
   Dim acDocMgr As ApplicationServices.DocumentCollection = ApplicationServices.Application.DocumentManager
			Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension.Add(acDocMgr, strTemplatePath)

End Sub

When I copied your code into my VS2012 project (that is prepared for AutoCAD 2013) I looked to that syntax:

    acDocMgr.Add(strTemplatePath)

...if you try to type    acDocMgr.    after the dot you should get available methods/properties and there is no .Add available (or I missed some Import?)

 

- alfred -

 

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 10

Hi,

 

I did what you wrote but that doesnt solved that problem.

Please try this and you will see what I mean.

    <CommandMethod("NewDrawing", CommandFlags.Session)>

   

PublicSub NewDrawing()

       

Dim strTemplatePath AsString = "acad.dwt"

       

Dim acDocMgr As Autodesk.AutoCAD.ApplicationServices.DocumentCollection =

        ApplicationServices.

Application.DocumentManager

       

Dim acDoc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension.Add(acDocMgr, strTemplatePath)

        acDocMgr.MdiActiveDocument = acDoc

       

Dim hED As EditorInput.Editor = acDoc.Editor

       

Dim hPr AsPromptSelectionOptions

       

Dim hResult AsPromptSelectionResult = hED.SelectAll()

   

EndSub

 

The line with hED.SelectAll() will crash with an eNotApplicable Error. That because the editor shows at the old drawing not into the new one. I know that line makes no sence because there are no elements in the drawing. I just wanted to show you what I mean.

 

Best Regards Jürgen

Message 6 of 10

Hi,

 

>> The line with hED.SelectAll() will crash with an eNotApplicable Error

No, your code does not crash (testing currently with AutoCAD 2013 German with .English language pack (G.114.0.0), 64bit on Win7.

I have made (and attached) a project for you with the content so you can compare your project settings with mine.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 10
Hallex
in reply to: Juergen.Becker

Hi, Jürgen

You may want to try this code as well,

idea is not mine though

  public bool is_template = true;

  [CommandMethod("od", CommandFlags.Modal)]
  public void testNewDocOpen()
  {
      // possible using examples:
     // is_template = true;
     // OpenDocInAppContext("acad.dwt");// <-- or acadiso.dwt

      // Or
       is_template = false;
       OpenDocInAppContext(@"C:\Test\WorkingDrawing.dwg");// <-- your file name here
  }

  public void OpenDocInAppContext(string filename)
  {
      DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
      docs.ExecuteInApplicationContext(OpenDocCallback, filename);
  }

  // The callback that will run in the application
  // execution context:


  public void OpenDocCallback(object Data)
  {
      // Open the document here, whose name was passed in
      // the Data parameter
      DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
      if (is_template)
      {
          docs.Add(Data.ToString());
      }
      else
      {
          docs.Open(Data.ToString(), false, "");

          // documents is already open and activated, pass your utility here:
          //Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.RunCommand("my cool command");
      }
  }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 8 of 10
_gile
in reply to: Hallex

Hi,

 

Just a little precision about extension methods using with VB.

 

With AutoCAD 2013 some methods have been moved to another class (see the Migration Guide topic in the ObjectARX 2013 \Docs\arxmgd.chm file).

For exmple the DocumentCollection.Add() method became an extension method defined in the DocumentCollectionExtension class (or module in VB).

 

Extension methods can be called both way (assuming docCol = Application.DocumentManager):

 

- as static (Shared) methods:

doc = Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExension.Add(docCol, "acad.dwt")

 

- as instance methods of the extended type:

doc = docCol.Add("acad.dwt")

 

The second one will directly work with C# because DocumentCollectionExtension is a class of the ApplicationServices namespace.

But with VB extension methods have to be defined in a Module, so, the DocumentcollectionExtension module have to be imported first for this behavior to be available.

 

Imports Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 10
Juergen.Becker
in reply to: _gile

Many, many thanks to all of you.

 

I think I solved that problem.

The problem was my AutoCAD. After reinstall and installing the SP2 it works fine.

 

But I didnt test it in my application enviroment, I will see if it works there.

Nice Weekend.

 

Best Regards Jürgen.

 

 

Message 10 of 10
Hallex
in reply to: Juergen.Becker

Jürgen, you can use my code in any public class

without creating extension static class

Have a nice weekend

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost