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

GetFileNameForOpen in command context freeze in 2011

11 REPLIES 11
Reply
Message 1 of 12
Pluc.m
629 Views, 11 Replies

GetFileNameForOpen in command context freeze in 2011

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.

11 REPLIES 11
Message 2 of 12
Hallex
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
Message 3 of 12
Pluc.m
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?

Message 4 of 12
Pluc.m
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. 😞 Any other ideas?

Message 5 of 12
Hallex
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
Message 6 of 12
Pluc.m
in reply to: Hallex

Still does the same thing 😞

Message 7 of 12
Hallex
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
Message 8 of 12
DiningPhilosopher
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.

Message 9 of 12
Pluc.m
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! 🙂

Message 10 of 12
fenton.webb
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
AutoCAD Engineering
Autodesk

Message 11 of 12
Pluc.m
in reply to: fenton.webb

There you go. Sorry there's a lot of French in there. 🙂 I translated the comments at least.

Message 12 of 12
fenton.webb
in reply to: Pluc.m

It seems to work well for me... Ok, I'm on 64bit, but I wanted to verify the code...

 

A couple of things though...

 

1) defining a Command called 'Open' is not the correct way to override the Open command. You shoukd use the redefine command.

2) I changed the csproj ProjectToolsVersion="3.5"




Fenton Webb
AutoCAD Engineering
Autodesk

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