Author Topic: To lock drawing opening  (Read 3753 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
To lock drawing opening
« on: March 12, 2013, 07:00:57 AM »
I need to get a notification about the drawing opening, and to close this document at once, if it is necessary for me. How can I do it?

P.S. In an ideal I want to have opportunity to cancel drawing opening, instead of to close after opening.

Regards, Andrey.

BlackBox

  • King Gator
  • Posts: 3770
Re: To lock drawing opening
« Reply #1 on: March 12, 2013, 06:09:07 PM »
I'm not knowledgeable enough to suggest anything other than an asynchronous call to Close Command via IExtensionApplication.Initialize()

Code - C#: [Select]
  1.         public void Initialize()
  2.         {
  3.             Document doc = acApp.DocumentManager.MdiActiveDocument;
  4.  
  5.             if (doc.Name.Contains("TheDawgNameYouDontWantOpened"))
  6.                     doc.SendStringToExecute("Close\n", false, false, true);
  7.         }
  8.  



** Edit - Perhaps it would be better to call that when the DocumentCreated Event is raised instead.
« Last Edit: March 12, 2013, 06:12:29 PM by BlackBox »
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3770
Re: To lock drawing opening
« Reply #2 on: March 12, 2013, 06:19:09 PM »
** Edit - Perhaps it would be better to call that when the DocumentCreated Event is raised instead.

Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.Runtime;
  4.  
  5. using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
  6.  
  7. [assembly: ExtensionApplication(typeof(TheSwamp.Sample.Events.CloseDwgAtOpen))]
  8.  
  9. namespace TheSwamp.Sample.Events
  10. {
  11.     public class CloseDwgAtOpen : IExtensionApplication
  12.     {
  13.         public void Initialize()
  14.         {
  15.             DocumentCollection acDocs = acApp.DocumentManager;
  16.  
  17.             acDocs.DocumentCreated += OnDocumentCreated;
  18.  
  19.             CloseDwgIf(acDocs.MdiActiveDocument);
  20.         }
  21.  
  22.         public void Terminate()
  23.         {
  24.         }
  25.  
  26.         public void OnDocumentCreated(object sender, DocumentCollectionEventArgs e)
  27.         {
  28.             Document doc = e.Document;
  29.  
  30.             if (doc != null)
  31.                 CloseDwgIf(doc);
  32.         }
  33.  
  34.         public void CloseDwgIf(Document doc)
  35.         {
  36.             if (doc.Name.Contains("TheDawgNameYouDontWantOpened"))
  37.                 doc.SendStringToExecute("Close\n", false, false, true);
  38.         }
  39.     }
  40. }
  41.  
« Last Edit: March 13, 2013, 10:30:41 AM by BlackBox »
"How we think determines what we do, and what we do determines what we get."

TheMaster

  • Guest
Re: To lock drawing opening
« Reply #3 on: March 12, 2013, 09:45:13 PM »
I need to get a notification about the drawing opening, and to close this document at once, if it is necessary for me. How can I do it?

P.S. In an ideal I want to have opportunity to cancel drawing opening, instead of to close after opening.

Regards, Andrey.

Can you tell why you need this?

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #4 on: March 13, 2013, 12:44:09 AM »
Can you tell why you need this?
My users are use the AutoCAD 2009. They often infect the network and local  computers through acad*.lsp, or acad*.fas. This problem is not only for my company. I blocked automatic loading of files acad*.lsp, and acad*.fas (through .NET). But I want not only to block danger, but also to remove that already extended...

If the user has enough rights, my plug-in automatically deletes these files if they are not where it is necessary. But sometimes the user doesn't have enough rights for removal. For example, if he opens for read only the drawing from someone else's directory. In that case it is necessary to block opening of the drawing and to show the message about the danger. The user must to contact with the owner, and that must to remove itself from the catalog the dangerous lisp files.
« Last Edit: March 13, 2013, 04:01:26 AM by Andrey »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: To lock drawing opening
« Reply #5 on: March 13, 2013, 01:38:35 AM »
Is it the virus mentioned here CAD Virus?

Other links from link above.
McAfee info and description
Autodesk solution for removal


Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #6 on: March 13, 2013, 04:26:07 AM »
Is it the virus mentioned here CAD Virus?
Are exist many types of this infection. I didn't compare them. One of types of this virus writes itself into all directories of drawings, and into all directories of "support files search path". it also writes itself into all lsp and mnl files.
Other links from link above.
McAfee info and description
Autodesk solution for removal
Our company used the Kaspersky antivirus. I have sent the mail about this virus them. But often it not help, and antivirus corrupts the autocad files (other lsp and mnl files).

I wrote the my own "antivirus" (autocad .net plugin) which almost solved this problem. The first version of this program successfully works, but now it is necessary to make some changes, and problem will solved completely.

If it is interesting to someone, I can share this program and its source code with people.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #7 on: March 13, 2013, 04:38:38 AM »
I'm not knowledgeable enough to suggest anything other than an asynchronous call to Close Command via IExtensionApplication.Initialize()

Code - C#: [Select]
  1.         public void Initialize()
  2.         {
  3.             Document doc = acApp.DocumentManager.MdiActiveDocument;
  4.  
  5.             if (doc.Name.Contains("TheDawgNameYouDontWantOpened"))
  6.                     doc.SendStringToExecute("Close\n", false, false, true);
  7.         }
  8.  
The Initialize method doesn't suit for this task.

** Edit - Perhaps it would be better to call that when the DocumentCreated Event is raised instead.
I already tried this method earlier, but received Fatal Error:
Code - C#: [Select]
  1. static void docMng_DocumentCreated(object sender, App.DocumentCollectionEventArgs e) {
  2.     if (e.Document != null)
  3.         e.Document.CloseAndDiscard();
  4. }
  5.  
And...
Quote from: BlackBox
Code - C#: [Select]
  1.        public void CloseDwgIf(Document doc)
  2.         {
  3.             if (doc.Name.Contains("TheDawgNameYouDontWantOpened"))
  4.                 doc.SendStringToExecute("Close\n", false, false, true);
  5.         }
  6.  
I already tried this method earlier too, but received Fatal Error again:
Code - C#: [Select]
  1. static void docMng_DocumentCreated(object sender, App.DocumentCollectionEventArgs e) {
  2.     if (e.Document != null)
  3.         e.Document.SendStringToExecute("Close\n", false, false, true);        
  4. }
  5.  
BlackBox, try the your code before advising, please.  :-(
« Last Edit: March 13, 2013, 04:55:13 AM by Andrey »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #8 on: March 13, 2013, 05:50:58 AM »
In addition I tried such method:
Code - C#: [Select]
  1. // Static constrictor of class
  2. static Extention() {
  3.   ...
  4.     App.DocumentCollection docMng = cad.DocumentManager;
  5.     cad.DocumentManager.DocumentCreated += docMng_DocumentCreated;
  6.     cad.DocumentManager.DocumentActivated += docMng_DocumentActivated;
  7.     docMng.DocumentLockModeChanged += new App.DocumentLockModeChangedEventHandler(
  8.                 docMng_DocumentLockModeChanged); // Here I try to close the drawing...
  9.   ...
  10. }
  11.  
  12. static Boolean mark = false; // Mark about the document opening
  13. static Boolean activated = false; // Mark about the document activation
  14. static String createdFileName = default(String); // Opened document name              
  15.  
  16. static void docMng_DocumentCreated(object sender, App.DocumentCollectionEventArgs e) {
  17.     mark = true;
  18.     createdFileName = e.Document.Name;
  19. }
  20.  
  21. static void docMng_DocumentActivated(object sender, App.DocumentCollectionEventArgs e) {
  22.     if (e.Document.Name == createdFileName)
  23.         activated = true;
  24. }
  25.  
  26. static void docMng_DocumentLockModeChanged(object sender,
  27.     App.DocumentLockModeChangedEventArgs e) {
  28.     if (mark && e.CurrentMode == App.DocumentLockMode.NotLocked && e.Document != null &&
  29.         createdFileName == e.Document.Name && activated) {                
  30.         mark = false;
  31.         activated = false;
  32.         createdFileName = default(String);
  33.  
  34.         // e.Document.CloseAndDiscard(); // Fatal Error
  35.         // or:
  36.         e.Document.SendStringToExecute("_.Close ", true, false, true); // Fatal Error
  37.     }
  38. }
  39.  
But this code creates the Fatal Error too.  :-(
« Last Edit: March 13, 2013, 05:55:30 AM by Andrey »

BlackBox

  • King Gator
  • Posts: 3770
Re: To lock drawing opening
« Reply #9 on: March 13, 2013, 11:09:33 AM »
I'm not knowledgeable enough to suggest anything other than an asynchronous call to Close Command via IExtensionApplication.Initialize()

Code - C#: [Select]
  1.         public void Initialize()
  2.         {
  3.             Document doc = acApp.DocumentManager.MdiActiveDocument;
  4.  
  5.             if (doc.Name.Contains("TheDawgNameYouDontWantOpened"))
  6.                     doc.SendStringToExecute("Close\n", false, false, true);
  7.         }
  8.  
The Initialize method doesn't suit for this task.

** Edit - Perhaps it would be better to call that when the DocumentCreated Event is raised instead.
I already tried this method earlier, but received Fatal Error:
Code - C#: [Select]
  1. static void docMng_DocumentCreated(object sender, App.DocumentCollectionEventArgs e) {
  2.     if (e.Document != null)
  3.         e.Document.CloseAndDiscard();
  4. }
  5.  
And...
Quote from: BlackBox
Code - C#: [Select]
  1.        public void CloseDwgIf(Document doc)
  2.         {
  3.             if (doc.Name.Contains("TheDawgNameYouDontWantOpened"))
  4.                 doc.SendStringToExecute("Close\n", false, false, true);
  5.         }
  6.  
I already tried this method earlier too, but received Fatal Error again:
Code - C#: [Select]
  1. static void docMng_DocumentCreated(object sender, App.DocumentCollectionEventArgs e) {
  2.     if (e.Document != null)
  3.         e.Document.SendStringToExecute("Close\n", false, false, true);        
  4. }
  5.  
BlackBox, try the your code before advising, please.  :-(

I mistakenly named the Class the same as one of the Methods within (which has since been corrected, I changed TheSwamp.Sample.Events.CloseDwgIf to TheSwamp.Sample.Events.CloseDwgAtOpen)... I'll try to be more clear about when posting code written with Notepad++ in lieu of Visual Studio, Andrey.

My Class-name-corrected code does properly Close Drawing1.dwg at Netload; however, I suspect this is due to all necessary initialization of said drawing being completed prior to Netload, as subsequent Qnew calls result in a new drawing, with Close at the command line, but no fatal error(s) result as you claim.

My code may not ultimately work for your needs here (I was quite explicit in my initial post), but if fatal errors persist on your end, then there is something wrong with your code, my friend.



Not sure what version you're currently using, but you might consider AutoLISP and VBA Security Controls in AutoCAD 2013 SP1.

As for your issue of the original suspect files, rather than attempting to 'veto' a drawing from opening (if that's even possible from .NET API?), methinks your effort would be better served renaming the suspect file(s) if found via FindFile() call(s) when the DocumentCreated Event is raised which happens to be prior to any built-in user defined files are loaded in the startup sequence.

Once the malicious code has soiled your own .LSP, and .MNL files though, the only alternative is to be 'highly reactive' I'm afraid :|. I sure hope you have good, nightly backups to restore from.
"How we think determines what we do, and what we do determines what we get."

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #10 on: March 13, 2013, 11:55:44 AM »
Quote from: BlackBox
there is something wrong with your code, my friend.
I have created the new Visual Studio 2012 project for AutoCAD 2009 SP3 x64 Enu.
I copied your last modified code into project and run it. In your code I replaced the "TheDawgNameYouDontWantOpened" on the "Field.dwg". When I try to open the "Field.dwg" I get the Fatal Error.
Then I changed the project for AutoCAD 2013 SP1.1 x64 Enu. When I try to open the "Field.dwg", I don't get the Fatal Error, but the "Field.dwg" was opened.

Both results are wrong.

Quote from: BlackBox
Not sure what version you're currently using, but you might consider AutoLISP and VBA Security Controls in AutoCAD 2013 SP1.
I know this info, and I think it was added by my ADN Case 06921325. My users use the AutoCAD 2009 SP3 Enu.
Quote from: BlackBox
rather than attempting to 'veto'...
By means of the prohibition I try to force users to be more responsible.
Quote from: BlackBox
Once the malicious code has soiled your own .LSP, and .MNL files though, the only alternative is to be 'highly reactive' I'm afraid . I sure hope you have good, nightly backups to restore from.
It is not problem. In the some server's directory are located all lsp, fas, and mnl files as samples. This directory are read only for all users. My "antivirus" use this files for the quick recovery of local files.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #11 on: March 13, 2013, 12:02:25 PM »
I found methods how to lock opening of unwanted files inside in AutoCAD (through "Recent Files", or "OpenFile" dialog), and just as to lock files which are dragged in application by a mouse (Drag & Drop).
But it ain't working when the drawing opened through Explorer. How can I solve this problem?

BlackBox

  • King Gator
  • Posts: 3770
Re: To lock drawing opening
« Reply #12 on: March 13, 2013, 01:13:43 PM »
Quote from: BlackBox
there is something wrong with your code, my friend.
I have created the new Visual Studio 2012 project for AutoCAD 2009 SP3 x64 Enu.
I copied your last modified code into project and run it. In your code I replaced the "TheDawgNameYouDontWantOpened" on the "Field.dwg". When I try to open the "Field.dwg" I get the Fatal Error.
Then I changed the project for AutoCAD 2013 SP1.1 x64 Enu. When I try to open the "Field.dwg", I don't get the Fatal Error, but the "Field.dwg" was opened.

Both results are wrong.

While obviously not successful in terms of preventing a drawing from being opened for reasons already discussed... My code above loads, and runs without fatal error. Compiled using Visual Studio 2010 Express (C#), and tested on Civil 3D 2011, 2012 (.NET 3.5, any CPU). I cannot offer any suggestion for issues with earlier release, and framework versions.

Hopefully someone will be able to assist you, where I have been unsuccessful.

Cheers
"How we think determines what we do, and what we do determines what we get."

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: To lock drawing opening
« Reply #13 on: March 20, 2013, 05:45:11 AM »
up.