Hallo Ihr,
ich habe folgendes vor:
Ich möchte über die API mittels c# (verwende die ACAD-.Net-API und ACAD 2010) alle offenen DWG durchgehen und diese bei Bedarf speichern (wenn noch nicht auf Platte gespeichert, dann unter neuem Namen speichern).
Mein Problem besteht nun, wenn ein DWG vorhanden ist, welches noch nicht auf Platte gespeichert wurde (also beispielsweise die Vorlage direkt nach dem Starten von ACAD).
Reproduzierbar mit folgender Vorgehensweise:
- ACAD-Starten (dann ja bereits eine DWG-Vorlage offen)
- Eine vorhandene DWG von Platte öffnen (ist jetzt das aktive Dokument)
- Meinen Code ausführen!
Jetzt passiert folgendes:
- Erstes Element in der Schleife (ist aber wohl Zufall) is bei mir immer das neu erzeugte Dokument. Da dieses nicht aktiv ist, will mein Code das Dokument aktiv setzen. Das führt aber dazu, das ACAD zwar das entsprechende Dokument aktiv setzt, dann aber ohne Fehler die Abbarbeitung abbricht.
Ich habe folgendes zu diesem Thema gefunden:
Zitat:
You can only change the active document from the application
context. If try to do it from the document context (e.g.,
from the handler of a command method that's registered without the
CommandFlags.Session flag), as soon as the
active document changes, your code's execution
is suspended, and will not resume until the document it was running in is
active again.
Um das zu umgehen habe ich aber die Anweisung "[CommandMethod("CD", CommandFlags.Session)]" verwendet. Was mache ich falsch, gibt es einen besseren Weg?
Nachfolgend mein abgespeckter Code (habe ich aus einem Beispiel zum Schliessen aller offenen Dokumente gekl....):
Code:
[CommandMethod("CD", CommandFlags.Session)]
static public bool Test()
{
try
{
DocumentCollection docs = Application.DocumentManager;
foreach (Document nDoc in docs)
{
// First cancel any running command
if (nDoc.CommandInProgress != "" && nDoc.CommandInProgress != "CD")
{
AcadDocument oDoc = (AcadDocument)nDoc.AcadDocument;
oDoc.SendCommand("\x03\x03");
}
//Activate next Document
if (Application.DocumentManager.MdiActiveDocument != nDoc)
{
Application.DocumentManager.MdiActiveDocument = nDoc;
}
}
return true;
}
catch (System.Exception e)
{
return false;
}
}
Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP