DocumentManager.Open() Is Not the current Document

DocumentManager.Open() Is Not the current Document

Anonymous
Not applicable
3,800 Views
9 Replies
Message 1 of 10

DocumentManager.Open() Is Not the current Document

Anonymous
Not applicable

I used autocad 2012,when I use code to open a drawing like below:

Application.DocumentManager.MdiActiveDocument = Application.DocumentManager.Open(path, false);
Document doc = Application.DocumentManager.MdiActiveDocument;
            doc.LockDocument();

I got a document, but the document is not what i open, it's the previous document.

Anyone can tell me how to get the document I opened right now~?

Any of your help will do me a big favor,

thank you.

0 Likes
Accepted solutions (2)
3,801 Views
9 Replies
Replies (9)
Message 2 of 10

_Tharwat
Advisor
Advisor
Accepted solution

This is what I use:

 

public static void OpenCadDrawing()
        {
            DocumentCollection d = Application.DocumentManager;
            string FileName = "C:\\Test.dwg"; // Add your full path of the drawing
            if (File.Exists(FileName))
            {
                Document doc = d.Open(FileName, false);
                d.MdiActiveDocument = doc;
            }
            else
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nDrawing can not be found !");
            }
        } 
Message 3 of 10

Anonymous
Not applicable

I tried your solution,

but the code can not go on when it goes to 

d.MdiActiveDocument = doc;

the codes after this could not be called..

0 Likes
Message 4 of 10

Anonymous
Not applicable

Actually, I tried your way,and here is what I have problem with

when I tried to call

Application.DocumentManager.MdiActiveDocument.Name

The result is still the previous document,Not the one I Opened it right now...

0 Likes
Message 5 of 10

_Tharwat
Advisor
Advisor

If the problem is only with activating the drawing, try to set the WindowState of the newly opened drawing to Maximized as in the following example.

 

e.g;

 

Document doc = d.Open(FileName, false);
doc.Window.WindowState = DocumentWindow.State.Maximized; 

And you need to add the namespace :

 

using Autodesk.AutoCAD.Windows;

 

EDIT: I made the Window state to Maximized as you wish for.

0 Likes
Message 6 of 10

Anonymous
Not applicable

I can't using the

doc.Window.WindowState = DocumentWindow.State.Maximized; 


the visual studio always remind me to add use of PresentationCore.dll

Even when I add the namespace before the Window will still get the problem.
have you faced it~?

0 Likes
Message 7 of 10

_gile
Consultant
Consultant
Accepted solution

Hi,

 

If your code is called from a CommandMethod attributed class, you have to set the CommandFlags.Session.

 

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


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 10

Anonymous
Not applicable
I just used it in a private function, other place will call the function...
0 Likes
Message 9 of 10

_gile
Consultant
Consultant

When I said "called from", I didn't mean "directly called from", I meant the main calling method.

 

How do you start the process which calls this function? a command or a click on a dialog button? if it's from a dialog, is this dialog modal or modeless? if it's a modal dialog, how is opened this dialog?...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 10

Anonymous
Not applicable

I got it

Thank you...

0 Likes