Hi zoltan and Jeff H,
Thank you for helping. This is what I have so far. (I left out some obvious code lines to keep the code compact) Like mentioned I tried the CommandWillStart and CommandEnded events and the LispEnded Events. Both approaches cause autocad to freeze in the while (this.acadIsBusy) loop. I am quite new to Autocad programming so maybe I overlooked something obvious for you guys. I use the program class to define the CommandMethods which I start from the autocad command line. I am not sure what the Commandflags.Session thing is doing. Maybe the problem is related with that?
using System;
using System.Threading;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace XX
{
public class YE_AutoCad
{
// Add drawing
public void AddDrawing(string DrawingName)
{
Document acDoc = Application.DocumentManager.Add(this.template);
acDoc.Database.SaveAs(this.ProjectDirectory + DrawingName, DwgVersion.Current);
this.drawingName = DrawingName;
if (this.electrical)
{
this.AddDrawingToProject();
}
}
// Close active drawing
public void CloseDrawing()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
acDoc.CloseAndSave(acDoc.Name);
}
// Add drawing to AutoCad Electrical project
private void AddDrawingToProject()
{
char chrQuote = (char)34;
char chrSingQuote = (char)39;
string str = "(c:ace_add_dwg_to_project '"
+ this.drawingName
+ "' (list 'Section' 'Sub section' 'Page Description' nil))\r";
str = str.Replace(chrSingQuote, chrQuote); // Replace ' for "
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//doc.LispWillStart += new LispWillStartEventHandler(doc_LispWillStart);
doc.LispEnded += new EventHandler(doc_LispEnded);
//doc.CommandWillStart += new CommandEventHandler(doc_CommandWillStart);
//doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
this.aCadIsBusy = true;
doc.SendStringToExecute(str, true, false, false);
while (this.aCadIsBusy) ;
//doc.CommandWillStart -= new CommandEventHandler(doc_CommandWillStart);
//doc.CommandEnded -= new CommandEventHandler(doc_CommandEnded);
//doc.LispWillStart -= new LispWillStartEventHandler(doc_LispWillStart);
doc.LispEnded -= new EventHandler(doc_LispEnded);
}
void doc_CommandEnded(object sender, CommandEventArgs e)
{
this.aCadIsBusy = false;
}
void doc_CommandWillStart(object sender, CommandEventArgs e)
{
this.aCadIsBusy = true;
}
void doc_LispEnded(object sender, System.EventArgs e)
{
this.aCadIsBusy = false;
}
void doc_LispWillStart(object sender, System.EventArgs e)
{
this.aCadIsBusy = true;
}
private Boolean electrical;
private Boolean aCadIsBusy;
private string drawingName;
private string template;
private string eProjectDirectory;
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Autodesk.AutoCAD.Runtime;
namespace XX
{
public class Program
{
[CommandMethod("TestAdd", CommandFlags.Session)]
public void TestAdd()
{
yeCad.ProjectDirectory = @"C:\";
yeCad.AddDrawing(@"Werner3.dwg");
}
[CommandMethod("TestAdds", CommandFlags.Session)]
public void TestAdds()
{
yeCad.ProjectDirectory = @"C:\";
yeCad.AddDrawing(@"Werner3.dwg");
yeCad.CloseDrawing();
yeCad.AddDrawing(@"Werner4.dwg");
yeCad.CloseDrawing();
}
XX.YE_AutoCad yeCad = new XX.YE_AutoCad();
}
}