Complete CAD file opening function

Complete CAD file opening function

SimonaQQ
Advocate Advocate
2,997 Views
13 Replies
Message 1 of 14

Complete CAD file opening function

SimonaQQ
Advocate
Advocate

When opening a file using CAD, if the file contains proxy graphics, a proxy graphics prompt will pop up; Alternatively, when the file is missing fonts, a prompt for missing fonts will pop up; Alternatively, when opening the same file for the second time, a read-only prompt will pop up.
What I need now is to include these prompts when opening DWG files in the code, and to be able to open DWG files read-only.

Open promptOpen promptRead-only promptRead-only prompt


Here are two ways I tried to open files, each with its own advantages and disadvantages

 

Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension.Open

 

Autodesk.AutoCAD.ApplicationServices.DocumentCollectionExtension.Open Can be opened read-only, but will ignore prompts,And no prompt message will pop up

 

System.Diagnostics.Process.Start 

 

System.Diagnostics.Process.Start Will not ignore the prompt, but cannot open the same file repeatedly and cannot be opened read-only

 

 

So is there a way to open a file that meets these conditions simultaneously: it can pop up a prompt, open the file in read-only mode, and open the same file multiple times

 

 

0 Likes
Accepted solutions (1)
2,998 Views
13 Replies
Replies (13)
Message 2 of 14

norman.yuan
Mentor
Mentor

You may want to rephrase your question as what exactly you want to know/archive.

 

The 2 lines of code are quite different and is not comparable as apple vs banana: first code runs inside AutoCAD to open a drawing; the second code is for external app to lunch AutoCAD, which may or may not have a drawing being opened, and it have no control on what AutoCAD will do after it starts.

 

I assume what you want is whenever a drawing in AutoCAD opens, you want to do something, such as checking if particular conditions are met, particular data status in drawing... and prompt user in some way. If so, you can simply handle DocumentCollection.DocumentCreated event, which provide a chance for you to examine the Document and do something accordingly.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 14

SimonaQQ
Advocate
Advocate
Thank you for your reply. I have revised my question. Do you think it is easier to understand
0 Likes
Message 4 of 14

Ed__Jobe
Mentor
Mentor

If you don't need the user to interact with the dwg, then you can open the dwg as a database instead of opening it as a dwg in the Editor environment. Then you won't get any prompts at all. Often, this is used to copy objects from the database of the readonly file.

 

 


          // Read the DWG into a side database
          Database sourceDb = new Database(false, true);
          sourceDb.ReadDwgFile(Properties.Settings.Default.PlotSettingPath,
                              System.IO.FileShare.Read,
                              true,
                              "");

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 14

SimonaQQ
Advocate
Advocate
What I need is interaction, because there is no interaction when opening DWG documents using APIs, so I asked how to interact.
0 Likes
Message 6 of 14

Ed__Jobe
Mentor
Mentor

It's not easy to understand what you're asking for, but I think this is what you want. AutoCAD automatically takes care of prompting when there are proxy objects and when xrefs, fonts are missing, etc. So all you really need to do is open the file read-only. The DocumentCollectionExtension class isn't used explicitly. It provides extension methods to the DocumentManager class, which is the Open method shown below.

 

using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

Document readOnlyDoc = acApp.DocumentManager.Open("filepath", true);

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 7 of 14

SimonaQQ
Advocate
Advocate
I use the 2016 version of the API to open files without any prompt messages, and there will be no interactive content when opened read-only. Therefore, I have this question. Perhaps my problem has been solved in the updated version?
My English is really poor, what I really want is what is demonstrated in the video.
There are prompt messages or interactive content when opening the file, and it can be opened read-only
0 Likes
Message 8 of 14

Ed__Jobe
Mentor
Mentor

AutoCAD automatically takes care of checking the dwg when opening. It has been that way since I can remember. If you open a dwg with custom objects in it and the plugin that created the objects is loaded, then you won't get the proxy notice dialog. A proxy is a representation of the real thing. If you have a custom object, only the custom app that created it knows how to edit it. If that app is not loaded in AutoCAD, then AutoCAD only know how to show a proxy or a representation of the object. When you open the file, you can choose to view the proxies or not. If font's or xrefs are not able to be found when opening the file, you are shown dialogs to help you locate the missing files. This all happens without you needing to write code for it. All you have to do is open the document. If you use the code I gave you, you can use the second argument as true to open the file readonly. If you leave the second argument off, then acad will attempt to open the document for write.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 9 of 14

SimonaQQ
Advocate
Advocate

However, the actual result is not as expected. I recorded a video where I opened a DWG file using both an API and a manual method. When using the API, there were no prompts, such as proxy notices or the read-only prompt when opening the file a second time. These prompts do appear when opening the file manually. What I want is for these prompts to also appear when opening the file through the API. Could you please help me correct my code?

 

0 Likes
Message 10 of 14

Ed__Jobe
Mentor
Mentor

You might try document.SendCommand method with the OPEN command.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 11 of 14

norman.yuan
Mentor
Mentor
Accepted solution

If the drawing file opened by code is currently in use (i.e. opened in other AutoCAD session, or even in the same AutoCAD session), the DocumentCollection.Open() method will SILENTLY opens the drawing file as read-only. So, it is the opening code's responsibility to decide whether users should be prompted or not. See following code:

        [CommandMethod("OpenTest", CommandFlags.Session)]
        public static void OpenDrawingInAutoCAD()
        {
            var dwgFile = @"E:\Temp\Test.dwg";
            var dwg = CadApp.DocumentManager.Open(dwgFile);
            if (dwg.IsReadOnly)
            {
                var keep = PromptUserForKeepOpenOrClose(dwgFile);
                if ( !keep)
                {
                    dwg.CloseAndDiscard();
                }
            }
        }

        private static bool PromptUserForKeepOpenOrClose(string dwgFile)
        {
            WhoHasInfo whoHas = CadApp.GetWhoHasInfo(dwgFile);

            // show WhoHasInfo to user and ask Keep/Close
            // with a MessageBox with Yes/No button here

            // Assume user want to close
            return false;
        }

 

To test this code. make sure you start another AutoCAD session and open the drawing file there first. Then run the command in different session (likely, your debugging session). After the drawing is opened, you would see the Document.IsReadonly property is true. Then you can prompt user/simulate what AutoCAD does.

 

For other types of prompt, if the code does not trigger the AutoCAD's built-in prompts, you would/could have to do it yourself, say to handle Document.EndDwgOpen event so that you can examine the drawing contents to decide what type of prompts are needed. In order to hook Document.EndDwgOpen event handler, you may have to handle DocumentCollection.DocumentCreated event, I guess (never did that myself).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 12 of 14

SimonaQQ
Advocate
Advocate
It feels quite complicated to determine the file content and provide prompts on my own. Do you have any understanding of the `System.Diagnostics.Process.Start` method? Can this method be used to open the same DWG file twice? I only know that this method controls system processes, and to open the same file, you need support from the corresponding software, such as CAD. But CAD can open the same file, so I still have hope for this method, hoping that I couldn't open the same file because I didn't use it correctly.
0 Likes
Message 13 of 14

SimonaQQ
Advocate
Advocate

I've tried this command, but it brings up a file selection dialog box. In reality, I can directly obtain the file path, so having to select the file again is not very user-friendly. 

 

0 Likes
Message 14 of 14

norman.yuan
Mentor
Mentor
All the user prompts that users see when a drawing is opened in AutoCAD ARE DONE INSIDE AutoCAD, while System.Diagnostics.Process.Start() ONLY STARTS AutoCAD and HAS NOTHING TO DO with what AutoCAD does after it begins running. That is, System.Giagnostics.Process KNOWS NOTHING about AutoCAD, except to get it started on behalf of the OS (Windows).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes