How to open a dwg file and use shortcut commands in new doc

How to open a dwg file and use shortcut commands in new doc

911803692
Explorer Explorer
462 Views
2 Replies
Message 1 of 3

How to open a dwg file and use shortcut commands in new doc

911803692
Explorer
Explorer

911803692_0-1712456206958.png

 

0 Likes
463 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

You should post your code in READ-ABLE manner (using the "</>" button!) and post the Exception picture SEPARATELY (not covering the code!).

 

However, from the partially seeable code, I can see you want to use code to open a drawing and call Editor.Command() against this newly opened drawing. However, you did not show how the code is executed (via a CommandMethod, of course) - application context, or document context.

 

If the code execute in document context (CommandMethod without Session flag set), your code can open a drawing, however, the newly opened drawing will not become MdiActiveDocument, thus the document's Editor is not avaible; on the other hand, if the CommandMethod does has Session flag set (i.e.) the code is executed in application context, then the newly opened document will indeed becomes MdiActiveDocument upon opening, however, Editor.Command() is not allowed to run in application context.

 

You might want to explain what you need to do with more information/and show more relevant code.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

911803692
Explorer
Explorer

this is my code. I try to open a dwg file and use shortcut commands to trim entity.But I can not use ed.Command().Thank you
[CommandMethod("test1")]
public void Open()
{
try
{
string filename = @"D:\0.dwg";
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filename, false);
HostApplicationServices.WorkingDatabase = doc.Database;
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = doc;
Editor ed = doc.Editor;
Database db=doc.Database;
ed.Command("TRIM");
}
catch (System.Exception ex)
{
int a = 5;
}
}

0 Likes