Setting the Document and Editor via Database

Setting the Document and Editor via Database

dennis
Advisor Advisor
1,131 Views
2 Replies
Message 1 of 3

Setting the Document and Editor via Database

dennis
Advisor
Advisor

I generally define the AutoCAD three as shown in my modules:

 

using acApp = Autodesk.AutoCAD.ApplicationServices.Application;

private Document doc;
private Database db;
private Editor ed;
private void InitializeAutoCAD()
{
     doc = acApp.DocumentManager.MdiActiveDocument;
     db = doc.Database;
     ed = doc.Editor;

}

However, I have been writing some Entity Extensions, and was passing the AutoCAD database as an argument.  But, I discovered that I could set the database via Entity.Database.  

Which now has me thinking and looking, but couldn't find a way to get the Document (and consequently the Editor) from the Database nor the Entity.

What I would like to do is lock the document, without having to pass the doc as an argument.  And, do Editor.WriteMessage in my catch, again without passing the argument.

Any one have a means to this end that will speed me along?

0 Likes
Accepted solutions (1)
1,132 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

If I write Entity extension method, in general, I'd leave Document and/or Editor out of the extension methods' work scope, that is, not trying to lock document/write message with Editor... within extension method. This way, the logic in the extension method would only focus on entity itself. It would be the extension methods' calling process that should take the responsibility to call the extension method in proper context (locking document if necessary, write message of success/failure to the command line...).

 

I assume that you do know that an entity accessible in AutoCAD memory may not be database residing (created, but not being added to DB yet); When an entity is from a Database, the database could be a side database that does not have corresponding Document; When an entity from a Database of a Document, the Document's Editor may not be accessible (when the document is not the current active one... All these cases would limit your extension methods very limited applicable scope, if you use Document and/or Editor inside.

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

dennis
Advisor
Advisor

Good points to consider Norman.  These specific extensions are being written with "my understanding" that the entities do indeed already exist in the drawing.  But your point of keeping extensions clean to the scope intended and having the calling handle the locking and messaging makes sense.  You have thought this out a little deeper than I have.  Thanks.

0 Likes