Skip to content

Commit

Permalink
Merge pull request #1260 from mcneel/kike/1.x
Browse files Browse the repository at this point in the history
Fix on TransactionGroup when a Grasshopper document is opened.
  • Loading branch information
kike-garbo authored Dec 20, 2024
2 parents 037b01e + 7295f57 commit 4452716
Showing 1 changed file with 50 additions and 52 deletions.
102 changes: 50 additions & 52 deletions src/RhinoInside.Revit/GH/Guest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,48 @@ GuestResult IGuest.EntryPoint(object sender, EventArgs args)

GuestResult OnCheckIn(CheckInArgs options)
{
Instances.CanvasCreated += EditorLoaded;
Instances.CanvasCreated += Grasshopper_CanvasCreated;

// Register PreviewServer
previewServer = new PreviewServer();
previewServer.Register();

Revit.DocumentChanged += OnDocumentChanged;
Revit.DocumentChanged += Revit_DocumentChanged;

External.ActivationGate.Enter += ActivationGate_Enter;
External.ActivationGate.Exit += ActivationGate_Exit;

RhinoDoc.BeginOpenDocument += BeginOpenDocument;
RhinoDoc.EndOpenDocumentInitialViewUpdate += EndOpenDocumentInitialViewUpdate;
Rhino.Commands.Command.EndCommand += RhinoCommand_EndCommand;
RhinoDoc.BeginOpenDocument += Rhino_BeginOpenDocument;
RhinoDoc.EndOpenDocumentInitialViewUpdate += Rhino_EndOpenDocument;
Rhino.Commands.Command.EndCommand += Rhino_EndCommand;

Instances.CanvasCreatedEventHandler Canvas_Created = default;
Instances.CanvasCreated += Canvas_Created = (canvas) =>
{
Instances.CanvasCreated -= Canvas_Created;
Instances.DocumentEditor.Activated += DocumentEditor_Activated;
canvas.DocumentChanged += ActiveCanvas_DocumentChanged;
canvas.KeyDown += ActiveCanvas_KeyDown;
Instances.DocumentEditor.Activated += Grasshopper_Activated;
canvas.DocumentChanged += Grasshopper_DocumentChanged;
canvas.KeyDown += Grasshopper_KeyDown;
};

Instances.CanvasDestroyedEventHandler Canvas_Destroyed = default;
Instances.CanvasDestroyed += Canvas_Destroyed = (canvas) =>
{
canvas.KeyDown -= ActiveCanvas_KeyDown;
canvas.DocumentChanged -= ActiveCanvas_DocumentChanged;
Instances.DocumentEditor.Activated -= DocumentEditor_Activated;
canvas.KeyDown -= Grasshopper_KeyDown;
canvas.DocumentChanged -= Grasshopper_DocumentChanged;
Instances.DocumentEditor.Activated -= Grasshopper_Activated;
Instances.CanvasDestroyed -= Canvas_Destroyed;
};

Instances.DocumentServer.DocumentAdded += DocumentServer_DocumentAdded;
Instances.DocumentServer.DocumentRemoved += DocumentServer_DocumentRemoved;
Instances.DocumentServer.DocumentAdded += Grasshopper_DocumentAdded;
Instances.DocumentServer.DocumentRemoved += Grasshopper_DocumentRemoved;

return GuestResult.Succeeded;
}

private void EditorLoaded(GH_Canvas canvas)
private void Grasshopper_CanvasCreated(GH_Canvas canvas)
{
Instances.CanvasCreated += EditorLoaded;
Instances.CanvasCreated -= Grasshopper_CanvasCreated;

var message = string.Empty;
try
Expand All @@ -119,16 +119,16 @@ private void EditorLoaded(GH_Canvas canvas)

GuestResult OnCheckOut(CheckOutArgs options)
{
Instances.DocumentServer.DocumentAdded -= DocumentServer_DocumentAdded;
Instances.DocumentServer.DocumentAdded -= Grasshopper_DocumentAdded;

Rhino.Commands.Command.EndCommand -= RhinoCommand_EndCommand;
RhinoDoc.EndOpenDocumentInitialViewUpdate -= EndOpenDocumentInitialViewUpdate;
RhinoDoc.BeginOpenDocument -= BeginOpenDocument;
Rhino.Commands.Command.EndCommand -= Rhino_EndCommand;
RhinoDoc.EndOpenDocumentInitialViewUpdate -= Rhino_EndOpenDocument;
RhinoDoc.BeginOpenDocument -= Rhino_BeginOpenDocument;

External.ActivationGate.Exit -= ActivationGate_Exit;
External.ActivationGate.Enter -= ActivationGate_Enter;

Revit.DocumentChanged -= OnDocumentChanged;
Revit.DocumentChanged -= Revit_DocumentChanged;

// Unregister PreviewServer
previewServer?.Unregister();
Expand Down Expand Up @@ -247,9 +247,11 @@ void ActivationGate_Exit(object sender, EventArgs e)
}

static bool? EnableSolutions;
private void DocumentServer_DocumentAdded(GH_DocumentServer sender, GH_Document doc)
private void Grasshopper_DocumentAdded(GH_DocumentServer sender, GH_Document doc)
{
doc.ObjectsDeleted += Doc_ObjectsDeleted;
doc.SolutionStart += Grasshopper_SolutionStart;
doc.ObjectsDeleted += Grasshopper_ObjectsDeleted;
doc.SolutionEnd += Grasshopper_SolutionEnd;

// If we don't disable the solutions Grasshopper will
// evaluate doc before notifiy us the document is being active.
Expand All @@ -260,13 +262,17 @@ private void DocumentServer_DocumentAdded(GH_DocumentServer sender, GH_Document
}
}

private void DocumentServer_DocumentRemoved(GH_DocumentServer sender, GH_Document doc)
private void Grasshopper_DocumentRemoved(GH_DocumentServer sender, GH_Document doc)
{
doc.ObjectsDeleted -= Doc_ObjectsDeleted;
if (PendingSolution == doc) PendingSolution = null;

doc.SolutionEnd -= Grasshopper_SolutionEnd;
doc.ObjectsDeleted -= Grasshopper_ObjectsDeleted;
doc.SolutionStart -= Grasshopper_SolutionStart;
}

bool activeDefinitionWasEnabled = false;
void BeginOpenDocument(object sender, DocumentOpenEventArgs e)
void Rhino_BeginOpenDocument(object sender, DocumentOpenEventArgs e)
{
if (Instances.ActiveCanvas?.Document is GH_Document definition)
{
Expand All @@ -275,7 +281,7 @@ void BeginOpenDocument(object sender, DocumentOpenEventArgs e)
}
}

void EndOpenDocumentInitialViewUpdate(object sender, DocumentOpenEventArgs e)
void Rhino_EndOpenDocument(object sender, DocumentOpenEventArgs e)
{
if (Instances.ActiveCanvas?.Document is GH_Document definition)
{
Expand All @@ -284,7 +290,7 @@ void EndOpenDocumentInitialViewUpdate(object sender, DocumentOpenEventArgs e)
}
}

private void RhinoCommand_EndCommand(object sender, Rhino.Commands.CommandEventArgs args)
private void Rhino_EndCommand(object sender, Rhino.Commands.CommandEventArgs args)
{
if (args.CommandEnglishName == "GrasshopperBake")
{
Expand Down Expand Up @@ -504,7 +510,8 @@ public static UnitScale ModelUnitScale
private set => modelUnitScale = value;
}

void DocumentEditor_Activated(object sender, EventArgs e) => AuditUnits(Revit.ActiveUIDocument?.Document);
void Grasshopper_Activated(object sender, EventArgs e) => AuditUnits(Revit.ActiveUIDocument?.Document);
void Grasshopper_DocumentChanged(GH_Canvas sender, GH_CanvasDocumentChangedEventArgs e) { }

internal static void AuditUnits(ARDB.Document document)
{
Expand All @@ -526,25 +533,10 @@ internal static void AuditUnits(ARDB.Document document)
}
}
}

void ActiveCanvas_DocumentChanged(GH_Canvas sender, GH_CanvasDocumentChangedEventArgs e)
{
if (e.OldDocument is object)
{
e.OldDocument.SolutionEnd -= ActiveDefinition_SolutionEnd;
e.OldDocument.SolutionStart -= ActiveDefinition_SolutionStart;
}

if (e.NewDocument is object)
{
e.NewDocument.SolutionStart += ActiveDefinition_SolutionStart;
e.NewDocument.SolutionEnd += ActiveDefinition_SolutionEnd;
}
}
#endregion

#region Revit Document Changed
void OnDocumentChanged(object sender, ARDB.Events.DocumentChangedEventArgs e)
void Revit_DocumentChanged(object sender, ARDB.Events.DocumentChangedEventArgs e)
{
#if DEBUG
var transactions = e.GetTransactionNames();
Expand Down Expand Up @@ -759,7 +751,7 @@ internal void CommitTransactionGroups()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ActiveCanvas_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
private void Grasshopper_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (sender is GH_Canvas canvas && canvas.Document is GH_Document document)
{
Expand All @@ -777,7 +769,7 @@ private void ActiveCanvas_KeyDown(object sender, System.Windows.Forms.KeyEventAr
}
}

private void Doc_ObjectsDeleted(object sender, GH_DocObjectEventArgs e) => ObjectsDeleted(sender, (e.Document, e.Objects));
private void Grasshopper_ObjectsDeleted(object sender, GH_DocObjectEventArgs e) => ObjectsDeleted(sender, (e.Document, e.Objects));

internal async void ObjectsDeleted(object sender, (GH_Document Document, IReadOnlyCollection<IGH_DocumentObject> Objects) e)
{
Expand Down Expand Up @@ -946,7 +938,7 @@ internal async void ObjectsDeleted(object sender, (GH_Document Document, IReadOn
}
}

void ActiveDefinition_SolutionStart(object sender, GH_SolutionEventArgs e)
void Grasshopper_SolutionStart(object sender, GH_SolutionEventArgs e)
{
if (e.Document is GH_Document document)
{
Expand All @@ -959,9 +951,12 @@ void ActiveDefinition_SolutionStart(object sender, GH_SolutionEventArgs e)
obj.ExpireSolution(false);
}

GeometryCache.StartKeepAliveRegion();
if (ActiveDocumentStack.Count == 0) GeometryCache.StartKeepAliveRegion();
ActiveDocumentStack.Push(e.Document);
if (document.Enabled) StartTransactionGroups();
if (document.Enabled)
{
if (ActiveDocumentStack.Count == 1) StartTransactionGroups();
}
else if (Instances.ActiveCanvas?.Document == document)
{
if (PendingSolution is object) PendingSolution = document;
Expand All @@ -982,16 +977,19 @@ void ActiveDefinition_SolutionStart(object sender, GH_SolutionEventArgs e)

private static GH_Document PendingSolution;

void ActiveDefinition_SolutionEnd(object sender, GH_SolutionEventArgs e)
void Grasshopper_SolutionEnd(object sender, GH_SolutionEventArgs e)
{
if (e.Document is GH_Document document)
{
if (ActiveDocumentStack.Peek() != e.Document)
throw new InvalidOperationException();

if (document.Enabled) CommitTransactionGroups();
if (document.Enabled)
{
if (ActiveDocumentStack.Count == 1) CommitTransactionGroups();
}
ActiveDocumentStack.Pop();
GeometryCache.EndKeepAliveRegion();
if (ActiveDocumentStack.Count == 0) GeometryCache.EndKeepAliveRegion();

// Warn the user about objects that contain elements modified by Grasshopper.
{
Expand Down

0 comments on commit 4452716

Please sign in to comment.