static bool valid = false;
static Document original;
static HashSet<Document> docs;
static IEnumerator<Document> enumer;
static bool updating = false;
static bool regen = false;
static ViewTableRecord syncView;
[CommandMethod("retoggle")]
public void toggleRegen()
{
regen = !regen;
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nRegen is {0}", regen ? "enabled" : "disabled");
}
[CommandMethod("TestView")]
public void testView()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
if (valid)
{
ed.WriteMessage("\nDisabling doc_ViewChanged");
foreach (Document d in docs)
{
d.ViewChanged -= doc_ViewChanged;
}
original = null;
docs = null;
enumer = null;
syncView = null;
valid = !valid;
}
else
{
ed.WriteMessage("\nEnabling doc_ViewChanged");
docs
= new HashSet
<Document
>(); docs.Add(doc);
doc.ViewChanged += doc_ViewChanged;
valid = !valid;
}
}
[CommandMethod("TestSync")]
public void testViewSync()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
if (valid)
{
if (doc == original)
{
ed.WriteMessage("\nCan't sync original document");
}
else if (docs.Add(doc))
{
using (ViewTableRecord vtr = (ViewTableRecord)ed.GetCurrentView())
{
syncView
= new ViewTableRecord
(); syncView.SetUcs(vtr.UcsOrthographic);
syncView.Target = vtr.Target;
syncView.ViewDirection = vtr.ViewDirection;
syncView.Elevation = vtr.Elevation;
syncView.CenterPoint = vtr.CenterPoint;
syncView.Height = vtr.Height;
syncView.Width = vtr.Width;
}
original = doc;
doc.ViewChanged += doc_ViewChanged;
enumer = docs.GetEnumerator();
enumer.Reset();
Application.Idle += SetViews;
updating = true;
enumer.MoveNext();
Application.DocumentManager.MdiActiveDocument = enumer.Current;
}
else
{
ed.WriteMessage("\nDoc already sync'd");
}
}
else
{
ed.WriteMessage("\nMust call 'TestView' first");
}
}
private void SetViews(object sender, EventArgs e)
{
Application.Idle -= SetViews;
if (enumer.Current != original)
{
using (DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument())
{
using (ViewTable vt = (ViewTable)Application.DocumentManager.MdiActiveDocument.Database.ViewTableId.Open(OpenMode.ForWrite))
{
using (ViewTableRecord vtr = Application.DocumentManager.MdiActiveDocument.Editor.GetCurrentView())
{
vtr.SetUcs(syncView.ViewOrthographic);
vtr.Target = syncView.Target;
vtr.ViewDirection = syncView.ViewDirection;
vtr.Elevation = syncView.Elevation;
vtr.CenterPoint = syncView.CenterPoint;
vtr.Height = syncView.Height;
vtr.Width = syncView.Width;
Application.DocumentManager.MdiActiveDocument.Editor.SetCurrentView(vtr);
if (regen)
Application.DocumentManager.MdiActiveDocument.Editor.Regen();
}
}
}
}
if (enumer.MoveNext())
{
Application.Idle += SetViews;
Application.DocumentManager.MdiActiveDocument = enumer.Current;
}
else
{
enumer = null;
updating = false;
syncView.Dispose();
syncView = null;
Application.DocumentManager.MdiActiveDocument = original;
original = null;
}
}
void doc_ViewChanged(object sender, EventArgs e)
{
if (!updating)
{
using (ViewTableRecord vtr = (ViewTableRecord)Application.DocumentManager.MdiActiveDocument.Editor.GetCurrentView())
{
syncView
= new ViewTableRecord
(); syncView.SetUcs(vtr.UcsOrthographic);
syncView.Target = vtr.Target;
syncView.ViewDirection = vtr.ViewDirection;
syncView.Elevation = vtr.Elevation;
syncView.CenterPoint = vtr.CenterPoint;
syncView.Height = vtr.Height;
syncView.Width = vtr.Width;
}
original = (Document)sender;
enumer = docs.GetEnumerator();
enumer.Reset();
Application.Idle += SetViews;
updating = true;
enumer.MoveNext();
Application.DocumentManager.MdiActiveDocument = enumer.Current;
}
}