.NET

Reply
Contributor
Pluc.m
Posts: 21
Registered: ‎08-31-2012
Message 1 of 12 (285 Views)

GetFileNameForOpen in command context freeze in 2011

285 Views, 11 Replies
09-13-2012 10:46 AM

Hi,

 

I wrote a little application that redefines the open command. On open, the new command will ask which file to open, check the file state (lock), then ask how the user wants to open the file. If the file is locked by another user, it will ask if you want to open it as read-only or if you want to abord the opening. If it's not locked, it will ask if you want to open it as read-only or full access. Open opening the file as Full Access, it will remove Windows' read-only file attribute and then open the file fully.

 

The other part of the application is a hook on the "DocumentToBeDestroyed" event. Whenever you close a drawing, it will check if another user as that file locked, and if not, it will turn the file into read-only.

 

Everything works fine in AutoCAD 2012 x64 on Windows 7 machines. However, ONE of our machine is running AutoCAD 2011 x86 on Windows XP and the application refuses to work on that machine. The first problem was that I worked with .NET 4.0. So I switched that arround to 3.5 (even had to manually edit projet files for some reason...). Now the DLL loads up. But the nightmare aint over yet.

 

My current problem, in AutoCAD 2011 x86 only, is that when I call "GetFileNameForOpen" to open up the file dialog, AutoCAD straight up freezes. No error, no delay, no nothing. You press CTRL+O or type OPEN in the CLI, and it just stops responding. The weird part is that if I put the EXACT SAME 3 LINES of code in the plugin's initialisation code, it opens it just fine... So I started messing arround with CommandFlags in my command attribute, but couldnt find anything that fixes it.

 

Here is the command

 

[CommandMethod("OPEN", CommandFlags.Session | CommandFlags.NoUndoMarker | CommandFlags.Transparent)]
public void OuvertureProtege()
{
    PromptOpenFileOptions opts = new PromptOpenFileOptions("Sélectionner un fichier");
    opts.Filter = "Dessin (*.dwg)|*.dwg|Normes (*.dws)|*.dws|DXF (*.dxf)|*.dxf|Gabarit de dessin (*.dwt)|*.dwt";
    // The next line doesn't open the file dialog.
    PromptFileNameResult pr =  AcadApplication.DocumentManager.MdiActiveDocument.Editor.GetFileNameForOpen(opts);
    // The next lines are never executed.
    if (pr.Status == PromptStatus.OK)
        OuvertureProtege_Core(pr.StringResult);
}

 Any lead or ideas on what could have an impact on this issue is really appreciated.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 12 (272 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-13-2012 12:47 PM in reply to: Pluc.m

Try to change command name on

"OPENDWG" or something else,

my guess is about you've duplicate core

AutoCAD command "OPEN"

Tested on A2010 only though...

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
Pluc.m
Posts: 21
Registered: ‎08-31-2012
Message 3 of 12 (269 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-13-2012 01:14 PM in reply to: Hallex

If I do that, CTRL+O wont use my dialog anymore. How can I override the CTRL+O shortcut to use my command programatically?

Contributor
Pluc.m
Posts: 21
Registered: ‎08-31-2012
Message 4 of 12 (267 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-13-2012 01:34 PM in reply to: Pluc.m

Changed the command name to "NOROPEN", called it in the command line, same thing happened. So it's not a command name conflict. :smileysad: Any other ideas?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 12 (262 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-13-2012 02:29 PM in reply to: Pluc.m

Try this quick code snip

       [CommandMethod("OPENDWG", CommandFlags.Session | CommandFlags.NoUndoMarker | CommandFlags.Transparent)]
        public void FileNameForOpen()
        {
            try
            {
                PromptOpenFileOptions opts = new PromptOpenFileOptions("Select file to open");
                opts.Filter = "Drawings (*.dwg)|*.dwg|Normes (*.dws)|*.dws|DXF (*.dxf)|*.dxf|Template (*.dwt)|*.dwt";
                // The next line doesn't open the file dialog.
                PromptFileNameResult pr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetFileNameForOpen(opts);
                // The next lines are never executed.
                if (pr.Status == PromptStatus.OK)
                {
                    string dwgname= pr.StringResult;
                    DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager as DocumentCollection;
                    Document doc = docs.Open(dwgname, false) as Document;
                    Database db = doc.Database;
                    Editor ed = doc.Editor;
                    using (DocumentLock doclock = doc.LockDocument())
                    {
                    
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Do your rest work with drawing here");

                    db.SaveAs(dwgname, true, DwgVersion.Current, db.SecurityParameters);
                    
                    }
                    doc.CloseAndDiscard();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
            }
        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
Pluc.m
Posts: 21
Registered: ‎08-31-2012
Message 6 of 12 (243 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-14-2012 04:10 AM in reply to: Hallex

Still does the same thing :smileysad:

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 7 of 12 (236 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-14-2012 07:25 AM in reply to: Pluc.m

Unfortunaly I have just pair of A2010 / Win 7 on my end

so I can't test this code with your settings

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 8 of 12 (232 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-14-2012 07:40 AM in reply to: Pluc.m

I take it you're new to AutoCAD development.

 

Ctrl+O is a shortcut key-combination defined in AutoCAD's CUI. You can customize any shortcut key combination, to execute whatever command you like.

 

You could also expose your .NET command using a different name, and redefine the OPEN command via LISP, something like this:

 

(command "._UNDEFINE" "OPEN")
(defun C:OPEN () (command "MYDOTNETOPEN") (princ))

 

 

Where :"MYDOTNETOPEN" is the name you give your command using the CommandMethod() attribute.

Contributor
Pluc.m
Posts: 21
Registered: ‎08-31-2012
Message 9 of 12 (227 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-14-2012 07:47 AM in reply to: DiningPhilosopher

I know how to manually modify the CTRL+O shortcut, but I was asking how to do it programatically (ex as the DLL loads).

 

I didn't think of redefining the OPEN command to simply call my command. That would probably be cleaner then redefining it with my .NET method. Thanks for the tip.

 

However, this still does not solve my problem as typing the command (even renamed to something unique) still crashes AutoCAD.

 

 

Keep in mind, the crash only occures if the code is ran within the command context. The issue is somewhere between defining the command and the context the command is running in. Also, the File Dialog is the 1st graphical object that occures. Putting MessageBox.Show("Test") anywhere before the specificed line causing the crash will be displayed. Only the File Dialog makes the command crash.

 

Thanks for all the suggestion so far! :smileyhappy:

ADN Support Specialist
fenton.webb
Posts: 352
Registered: ‎07-24-2007
Message 10 of 12 (173 Views)

Re: GetFileNameForOpen in command context freeze in 2011

09-20-2012 10:16 AM in reply to: Pluc.m

If you post working (buildable) sample project I'll debug it for you... (the code snippet isn't enough, I need to see your project settings too)





Fenton Webb

Developer Technical Services

Autodesk Developer Network


Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.