using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using CivDb = Autodesk.Civil.DatabaseServices;
namespace Civil2015Test
{
public class TestStructureAddEvent
{
// Коллекция для хранения ID колодцев между методами.
private ObjectIdCollection structureIds = new ObjectIdCollection();
[CommandMethod("TestAddReactor")]
public void AddReactor()
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
// На случай повторного запуска
adoc.CommandWillStart -= adoc_CommandWillStart;
adoc.CommandEnded -= adoc_CommandEnded;
adoc.CommandCancelled -= adoc_CommandEnded;
adoc.CommandFailed -= adoc_CommandEnded;
adoc.CommandWillStart += adoc_CommandWillStart;
adoc.CommandEnded += adoc_CommandEnded;
adoc.CommandCancelled += adoc_CommandEnded;
adoc.CommandFailed += adoc_CommandEnded;
}
[CommandMethod("TestRemoveReactor")]
public void DelReactor()
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
adoc.CommandWillStart -= adoc_CommandWillStart;
adoc.CommandEnded -= adoc_CommandEnded;
adoc.CommandCancelled -= adoc_CommandEnded;
adoc.CommandFailed -= adoc_CommandEnded;
}
void adoc_CommandEnded(object sender, CommandEventArgs e)
{
if (e.GlobalCommandName.Equals("AECCLAYOUTPIPEANDSTRUCTURE"))
{
Document adoc = (Document)sender;
adoc.Database.ObjectAppended -= Database_ObjectAppended;
// Если коллекция колодцев непустая
if (structureIds.Count > 0)
{
// Производим ее обработку
using (Transaction tr = adoc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId structureId in structureIds)
{
CivDb.Structure structure = tr.GetObject(structureId, OpenMode.ForRead) as CivDb.Structure;
adoc.Editor.WriteMessage("\nДобавлен колодец {0} в сеть {1}", structure.Name, structure.NetworkName);
}
tr.Commit();
}
// И очищаем ее
structureIds.Clear();
}
}
}
void adoc_CommandWillStart(object sender, CommandEventArgs e)
{
if (e.GlobalCommandName.Equals("AECCLAYOUTPIPEANDSTRUCTURE"))
{
Document adoc = (Document)sender;
// На всякий случай
adoc.Database.ObjectAppended -= Database_ObjectAppended;
adoc.Database.ObjectAppended += Database_ObjectAppended;
}
}
void Database_ObjectAppended(object sender, ObjectEventArgs e)
{
if (e.DBObject is CivDb.Structure)
{
// Добавляем ID колодца во коллекцию
structureIds.Add(e.DBObject.Id);
}
}
}
}