.NET

Reply
Valued Contributor
RamanSBV
Posts: 88
Registered: ‎02-28-2012
Message 1 of 8 (308 Views)

I need do some action just before opening file.

308 Views, 7 Replies
03-01-2013 03:23 AM

Hi,

 

I need to do some action just before drawing file.

 

Please guide me how i can do it.

 


Regards,

Raman

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 2 of 8 (297 Views)

Re: I need do some action just before opening file.

03-01-2013 04:00 AM in reply to: RamanSBV

RamanSBV wrote:
... I need to do some action just before drawing file...

These actions are secret? Can you explain exactly what to do? Which way the file is opened? Maybe you need to replace the command OPEN with own command?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
RamanSBV
Posts: 88
Registered: ‎02-28-2012
Message 3 of 8 (295 Views)

Re: I need do some action just before opening file.

03-01-2013 04:25 AM in reply to: Alexander.Rivilis

Not secrets :smileyhappy:

 

I have to check either this file is valued file (means opening from some particular folder)

if file is opening from somewhere else  other than particular folder then I should not allow user to open the file and show some message.

 

 

 

Regards,

Raman

 

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 4 of 8 (288 Views)

Re: I need do some action just before opening file.

03-01-2013 04:41 AM in reply to: RamanSBV

Veto a particular command in AutoCAD

You can veto command OPEN do some action and open (or not open) file.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
Andrey_Bushman
Posts: 33
Registered: ‎01-18-2009
Message 5 of 8 (281 Views)

Re: I need do some action just before opening file.

03-01-2013 04:49 AM in reply to: RamanSBV
...
App.DocumentCollection docMng = cad.DocumentManager;
docMng.DocumentLockModeChanged += new App.DocumentLockModeChangedEventHandler(docMng_DocumentLockModeChanged);
...


        static void docMng_DocumentLockModeChanged(object sender, App.DocumentLockModeChangedEventArgs e) {

            // Можно переопределить команду OPEN, подсовывая вместо "родного" диалогового окна открытия
            // файлов своё. Такой подход предоставит возможность выполнять предварительную проверку содержимого 
            // каталога открываемого файла и, в случае необходимости, блокировать открытие чертежа: например, если в каталоге 
            // находится доступный только для чтения файл acad*.lsp, или acad*.fas:

            if (settings.DisableDrawingOpeningFromDangerDir && string.Compare(e.GlobalCommandName, "OPEN", true) == 0) {
                e.Veto();
                //ShowOpenFileDialog(); // Так нельзя, иначе после моего диалогового окна почему-то
                // будет открываться "родное" окно выбора файлов AutoCAD.
                // Поэтому делаю так:
                cad.DocumentManager.MdiActiveDocument.SendStringToExecute("bush-open ", true, false, false);
            }

            // В случае необходимости, можно отключить автоматическую загрузку файлов acad*.lsp, или acad*.fas:
            if (settings.DisableAcadLispAutoloading && e.GlobalCommandName.Contains("s::startup"))
                e.Veto();
        }

        [Rtm.CommandMethod("bush-open", Rtm.CommandFlags.Modal)]
        public static void ShowOpenFileDialog() {
            CadWin.OpenFileDialog dialog = new CadWin.OpenFileDialog(
                String.Format("[{0}] {1}", Resource.startTitle, Resource.docOpenTitle),
                "", "dwg;dwt;dws;dxf", "", CadWin.OpenFileDialog.OpenFileDialogFlags.SearchPath);

            WinForms.DialogResult result = dialog.ShowDialog();

            if (result != Win.Forms.DialogResult.OK)
                return;

            if (!dialog.Filename.ToLower().StartsWith(CadSettings.Variables["ac-app"].Value)) {
                DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dialog.Filename));

                if (!dir.Exists)
                    return;

                FileInfo[] files = dir.GetFiles("acad*.lsp", SearchOption.TopDirectoryOnly);
                FileInfo[] files2 = dir.GetFiles("acad*.fas", SearchOption.TopDirectoryOnly);
                FileInfo[] files3 = dir.GetFiles("acad*.mnl", SearchOption.TopDirectoryOnly);

                String fileName = String.Empty;
                try {
                    foreach (FileInfo[] collection in new FileInfo[][] { files, files2, files3 }) {
                        foreach (FileInfo item in collection) {
                            fileName = item.FullName;
                            item.Delete();
                            cad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(Resource.fileDeleted, fileName);
                        }
                    }
                }
                catch (Exception ex) {
                    Win.MessageBox.Show(String.Format(Resource.disableFileOpen, fileName, ex.Message),
                        Resource.warning, Win.MessageBoxButton.OK, Win.MessageBoxImage.Stop);
                    return;
                }
            }
            App.Document doc = cad.DocumentManager.Open(dialog.Filename);
            if (doc != null)
                cad.DocumentManager.MdiActiveDocument = doc;
        }

 

_________________________________________
AutoCAD .Net Laboratory
*Expert Elite*
norman.yuan
Posts: 1,001
Registered: ‎04-27-2009
Message 6 of 8 (257 Views)

Re: I need do some action just before opening file.

03-01-2013 07:46 AM in reply to: RamanSBV

Well, handling DocumentLockModeChanged event and veto the change if the command is "OPEN" only works if the drawing is to be opened with command "OPEN". That is in the code you need to make decisioon by examine the DocumentLockModeChangedEventArgs.GlobalCommandName (say, if the name string contains upcase "OPEN"). However, sometime a drawing can be open without using command OPEN. For example, double-clicking a drawing in Windows Explorer, or click drawing "recent drawing list".

 

You may think hard: if you do not want user to open some drawing why can't you place these drawing file in a folder where the your is not allowed to access? Or maybe give them read-only access is sufficient (so they can still open but cannot save change back)?

 

I'd only consider vetoing open command as last option.

Active Contributor
Andrey_Bushman
Posts: 33
Registered: ‎01-18-2009
Message 7 of 8 (251 Views)

Re: I need do some action just before opening file.

03-01-2013 08:04 AM in reply to: norman.yuan

@norman.yuan

 

Yes, you are right. Now I don't know how to disable drawings opening through an Explorer, or through an "Recent Drawing List". I remember, I have tried at past to close drawings after it was opened (via event's registration), but I couldn't did it (I've got Fatal Error).

 

Regards, Andrey.

 

_________________________________________
AutoCAD .Net Laboratory
*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 8 of 8 (245 Views)

Re: I need do some action just before opening file.

03-01-2013 09:37 AM in reply to: Andrey_Bushman

My Workaround for a smiliar issue was to make a stand alone program that didn't show up in the taskbar. Associate the file format with that new program. It would then open the drawings using COM and make the changes I wanted to the file, then pass the file off to Autocad.

This was all transparent to the user.

User would double click on a file -> File gets checked with COM, AutoCAD is run if needed -> If file passes, it is sent onto AutoCAD.

Just note if you are using COM, I found that the file would be in use by the external program and then it would open read only in AutoCAD, The solution was to copy the file into the users temp folder, with a unique name and then the modifications were done on that file then copied to overright the original. Then the copy is opened in AutoCAD.

This may have changed in 2013, as I have not tried it.

Then the external program waits untill AutoCAD closes and deletes the files in the temp folder, as they are still "in use" from COM.

If there is an easier way I would like to know.

---------------------------



(defun botsbuildbots() (botsbuildbots))
Need installation help?

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