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

How a DWG is opened

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
StormyC
946 Views, 11 Replies

How a DWG is opened

Im hoping to issue a warning if a drawing is double clicked to open it as it sometimes causes AutoCAD to falter.

 

Is there a way to identify how a drawing is being opened?  ie was it by the open menu, double clicking or drag and drop. 

 

Any help would be appreciated.

11 REPLIES 11
Message 2 of 12
fenton.webb
in reply to: StormyC

I'm sure it's possible to detect the open style, but it will take a lot of work - rewinding, how is autocad faltering? Maybe we should address that first?




Fenton Webb
AutoCAD Engineering
Autodesk

Message 3 of 12
StormyC
in reply to: fenton.webb

Thanks for the reply;

 

Its to do with the order of execution.  I have a startup lisp routine that loads .NET modules once per AutoCAD session (acad.lsp / defun-q the_startup).  One of the .NET functions is executed from a Lisp routine that loads and runs with every document (acaddoc).

 

Due to the order of execution when drawings are double clicked the .NET function isn't available when the lisp is run so the function fails and an error message is issued.  Hence my term "AutoCAD is faltering."

 

This isn't usually a huge problem because its one of my drawing integrity routines that checks our custom data. (Sometimes an ACAD session will crash because it hangs while trying to load the .NET functions if multiple drawings are 'right clicked' and opened without an existing ACAD App running easily avoided....) However, at some point I will need that routine to run always, so, if I could issue a warning when a drawing is double clicked it would be very useful.

 

If I check that the function is loaded by trapping errors I could issue a warning with the possible reason that a drawing is double clicked.  It would be better to know how it was loaded though.

 

Many thanks for your attention.

 

 

 

 

Message 4 of 12
fenton.webb
in reply to: StormyC

Oh, you don't want to netload from LISP anymore. Best way it to do it from the new Autoloader bundle format. Best thing is to sign up for Autodesk Exchange Apps http://apps.exchange.autodesk.com, download some trial apps, check out their bundle folders that get installed and then modify one to suit your own needs.

 

Also, the detailed documentation on the Autoloader and how it works can be found here http://adndevblog.typepad.com/autocad/2013/01/autodesk-autoloader-white-paper.html




Fenton Webb
AutoCAD Engineering
Autodesk

Message 5 of 12
StormyC
in reply to: fenton.webb

I've tried that..

 

However, our systems are old, XP pro, the .NET install is shaky at best. For example installing dwf viewer causes AutoCAD to crash at every startup requiring a complete strip of .NET and reinstall.. Sometimes a complete op system rebuild.....  Its something to do with the way certain rights are removed - not being able to edit the registry etc...

 

SO when I did try I try it with AutoCAD 2013 I was getting problems with the custom Ribbon / toolbar loading/unloading.  ie it wasn't working as expected.

 

Oh and while we are on that topic changing the autoloader so that a plugin doesn't load up e.g. for Sketchbook Designer doesn't work on our systems either - I've had to rename the XML so that it gets ignored.........

 

Thanks for your help thus far.

 

(Would still like to know if there is a way to detect how AutoCAD was started.....)

 

Attached is the XML I was using to load up our app.

Message 6 of 12
norman.yuan
in reply to: StormyC

If you are not ready to AutoLoader, loading your .ENY code with acad.lsp is fine. You can skip using acaddoc.lsp to run a command defined iin your .NET assembly, though.

 

You can make your .NET assembly implement IExtensionApplication, and in its Initialize() procedure, you hook up to one of the DocumentCollection event, such as DocumentCreated, DocumaneActivated, and/or DocumentBecameCurrent. Since acad.lsp always gets run regardless how AutoCAD get started, thus, your IExtensionApplication is always loaded, therefore your code can do things with drawing in that AutoCAD session no matter how the drawing is opened. For example, if your code is hooked up with DocumentCreated event, then whenever an drawing is opened in AutoCAD (during the period of drawing being opened in AutoCAD, event DocumentCreated event will fire), you get chance to do something in the event handler. So, indeed, you do not have to care about how drawing is opened (via "Open" command, or user double-click a DWG file in Windows Explorer). Just make sure your code is hooked up to the appropriate DocumentCollection event that suits to your exact need.

 

Message 7 of 12
StormyC
in reply to: norman.yuan

Many thanks for your input;

 

As I understood it those kind of events are not guaranteed to fire at the point you would expect and I need the drawing to be fully initialised so I have access to all its objects.  I did play with this before and found it to be a little unreliable.

 

From the Help Pages......

Do not rely on the sequence of events.

When writing event handlers, do not rely on the sequence of events to happen in the exact order you think they occur. For example, if you issue an OPEN command, the events CommandWillStart, DocumentCreateStarted, DocumentCreated, and CommandEnded will all be triggered. However, they may not occur in that exact order each and everytime. The only thing you can rely on is that a most events occur in pairs, a beginning and ending event.

 

When I tried it with DocumentCreated AutoCAD would sometimes hang.  I think the above was the reason.

 

 

Message 8 of 12
norman.yuan
in reply to: StormyC

Well, the document says "Do not rely on the sequence of events.", not "Do not handle the envent correctly".

 

If your goal is to run some code when a drawing is opened in AutoCAD, regardless how it opens, you certainly can use one of the event as the trigger to run the code. Not knowing what exactly you wna to do, I'd most likely use DocuemtnBecameCurrent event in most cases, because when opening a drawing with "Open" command or double-click a DWG file, the drawing is opened in AutoCAD editor and should become the active drawing, which means, DocumentBecameCurrent is guranteed to be fired with a drawing at least once on it is opened in AutoCAD. of course this event will fire when user switch drawings in AutoCAD. So, you may want to maintain a static list of drawing name to flag which drawing has already be processed with your code, or you can bury the flag with Document.UserData.

 

As matter of fact, I have my code runs against every single drawing opened in AutoCAD, as long as the Acad is set up with our .NET IExtensionApplication preloaded, in my office for years, no matter how the drawing is opened: "Open" command, double-click drawing file name, or even opened by automation application.

 

 

Message 9 of 12
fenton.webb
in reply to: StormyC

About your XP system kicking and screeming with everything that Autodesk does... You do need to keep your system up-to-date with Windows services packs, otherwise what you say is happening will happen.




Fenton Webb
AutoCAD Engineering
Autodesk

Message 10 of 12
StormyC
in reply to: norman.yuan

Thanks Fenton.

 

Thanks Norman;  I will see if I can get my code to work using events;

 

The code will need to access object's extended data in the drawing database of the opened drawing.  I certainly dont want this to happen everytime a user makes a drawing current, only when the drawing is 1st opened.

 

Any advice is appreciated....

Message 11 of 12
norman.yuan
in reply to: StormyC

If you only want to run some code once on each drawing, you can hook up DoucmentActivated or DocumentBecameCurrent event when your IExtesionApplication assenbly is loaded on AutoCAD startup. Then, I'd use Document.UserData to flag it. Something like:

 

void DocumentBecameCurrent (....)

{

    bool processd=false;

    try

    {

        process=(bool)e.Document.UserData["MyDwgOpenProcess"];

    }

    catch{}

 

    if (!processed)

    {

        e.Document.UserData["MyDwgOpenProcess"]=true;

        e.Document.SendStringToExecute("MyCustomCommand "....)

    }

}

Message 12 of 12
StormyC
in reply to: norman.yuan

Again many thanks for taking the time to reply.

I will try that out and post back, will not be for a while though...

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