void OnIdle(object sender, IdlingEventArgs args) { // 1. Do Rhino pending OnIdle tasks if (rhinoCore.OnIdle()) { args.SetRaiseWithoutDelay(); return; } // Load this assembly as a Grasshopper assembly if (!LoadedAsGHA && PlugIn.GetPlugInInfo(new Guid(0xB45A29B1, 0x4343, 0x4035, 0x98, 0x9E, 0x04, 0x4E, 0x85, 0x80, 0xD9, 0xCF)).IsLoaded) { LoadedAsGHA = LoadGrasshopperComponents(); } // Document dependant tasks need a document ActiveUIApplication = (sender as UIApplication); if (ActiveDBDocument != null) { // 1. Do all document read actions if (ProcessReadActions()) { args.SetRaiseWithoutDelay(); return; } // 2. Do all document write actions if (!ActiveDBDocument.IsReadOnly) { ProcessWriteActions(); } // 3. Refresh Active View if necesary bool regenComplete = DirectContext3DServer.RegenComplete(); if (pendingRefreshActiveView || !regenComplete || GH.PreviewServer.PreviewChanged()) { pendingRefreshActiveView = false; var RefreshTime = new Stopwatch(); RefreshTime.Start(); ActiveUIApplication.ActiveUIDocument.RefreshActiveView(); RefreshTime.Stop(); DirectContext3DServer.RegenThreshold = Math.Min(RefreshTime.ElapsedMilliseconds, 200); } if (!regenComplete) { args.SetRaiseWithoutDelay(); } } }
public void OnIdle(object sender, IdlingEventArgs args) { // 1. Do Rhino pending OnIdle tasks if (rhinoCore.OnIdle()) { args.SetRaiseWithoutDelay(); return; } // Load this assembly as a Grasshopper assembly if (!LoadedAsGHA && PlugIn.GetPlugInInfo(new Guid(0xB45A29B1, 0x4343, 0x4035, 0x98, 0x9E, 0x04, 0x4E, 0x85, 0x80, 0xD9, 0xCF)).IsLoaded) { LoadedAsGHA = LoadGrasshopperComponents(); } // Document dependant tasks need a document ActiveUIApplication = (sender as UIApplication); if (ActiveDBDocument != null) { // 2. Do all BakeGeometry pending tasks lock (bakeRecipeQueue) { if (bakeRecipeQueue.Count > 0) { using (var trans = new Transaction(ActiveDBDocument)) { if (trans.Start("BakeGeometry") == TransactionStatus.Started) { while (bakeRecipeQueue.Count > 0) { var recipe = bakeRecipeQueue.Dequeue(); if (recipe.geometryToBake != null && recipe.categoryToBakeInto != BuiltInCategory.INVALID) { try { var geometryList = new List <GeometryObject>(); // DirectShape only accepts those types and no nulls foreach (var g in recipe.geometryToBake) { switch (g) { case Point p: geometryList.Add(p); break; case Curve c: geometryList.Add(c); break; case Solid s: geometryList.Add(s); break; case Mesh m: geometryList.Add(m); break; } } if (geometryList.Count > 0) { var ds = DirectShape.CreateElement(ActiveDBDocument, new ElementId(recipe.categoryToBakeInto)); ds.SetShape(geometryList); } } catch (Exception e) { Debug.Fail(e.Source, e.Message); } } } } trans.Commit(); } } } // 3. Do all document actions lock (documentActions) { if (documentActions.Count > 0) { using (var trans = new Transaction(ActiveDBDocument)) { try { if (trans.Start("RhinoInside") == TransactionStatus.Started) { while (documentActions.Count > 0) { documentActions.Dequeue().Invoke(ActiveDBDocument); } Committing = true; trans.Commit(); Committing = false; foreach (GH_Document definition in Grasshopper.Instances.DocumentServer) { if (definition.Enabled) { definition.NewSolution(false); } } } } catch (Exception e) { Debug.Fail(e.Source, e.Message); if (trans.HasStarted()) { trans.RollBack(); } } finally { documentActions.Clear(); } } } } } }
public void OnIdle(object sender, IdlingEventArgs args) { // 1. Do Rhino pending OnIdle tasks if (rhinoCore.OnIdle()) { args.SetRaiseWithoutDelay(); return; } // Document dependant tasks need a document var doc = (sender as UIApplication)?.ActiveUIDocument?.Document; if (doc == null) { return; } // 2. Do all BakeGeometry pending tasks lock (bakeRecipeQueue) { if (bakeRecipeQueue.Count > 0) { using (var trans = new Transaction(doc)) { if (trans.Start("BakeGeometry") == TransactionStatus.Started) { while (bakeRecipeQueue.Count > 0) { BakeRecipe recipe = bakeRecipeQueue.Dequeue(); if (recipe.geometryToBake != null && recipe.categoryToBakeInto != BuiltInCategory.INVALID) { try { var geometryList = new List <GeometryObject>(); // DirectShape only accepts those types and no nulls foreach (var g in recipe.geometryToBake) { switch (g) { case Point p: geometryList.Add(p); break; case Curve c: geometryList.Add(c); break; case Solid s: geometryList.Add(s); break; case Mesh m: geometryList.Add(m); break; } } if (geometryList.Count > 0) { var ds = DirectShape.CreateElement(doc, new ElementId(recipe.categoryToBakeInto)); ds.SetShape(geometryList); } } catch (Exception e) { Debug.Fail(e.Source, e.Message); } } } } trans.Commit(); } } } // 3. Do all document actions lock (documentActions) { if (documentActions.Count > 0) { using (var trans = new Transaction(doc)) { var action = documentActions.Peek(); if (trans.Start(action.GetMethodInfo().Name) == TransactionStatus.Started) { try { documentActions.Dequeue().Invoke(doc); trans.Commit(); } catch (Exception e) { Debug.Fail(e.Source, e.Message); trans.RollBack(); } } } } if (documentActions.Count > 0) { args.SetRaiseWithoutDelay(); } } }
public void OnIdle(object sender, IdlingEventArgs args) { // 1. Do Rhino pending OnIdle tasks if (_rhinoCore.OnIdle()) { args.SetRaiseWithoutDelay(); return; } // Document dependant tasks need a document var doc = (sender as UIApplication)?.ActiveUIDocument?.Document; if (doc == null) { return; } // 2. Do all BakeGeometry pending tasks lock (_bakeQueue) { if (_bakeQueue.Count > 0) { using (var trans = new Transaction(doc)) { if (trans.Start("BakeGeometry") == TransactionStatus.Started) { var categoryId = new ElementId(BuiltInCategory.OST_GenericModel); while (_bakeQueue.Count > 0) { var geometryList = _bakeQueue.Dequeue(); if (geometryList != null) { var ds = DirectShape.CreateElement(doc, categoryId); ds.SetShape(geometryList); } } } trans.Commit(); } } } // 3. Do all document actions lock (_documentActions) { if (_documentActions.Count > 0) { using (var trans = new Transaction(doc)) { var a = _documentActions.Peek(); var name = a.GetMethodInfo().Name; var s = trans.Start(name); if (s == TransactionStatus.Started) { try { _documentActions.Dequeue().Invoke(doc); trans.Commit(); } catch (Exception e) { Debug.Fail(e.Source, e.Message); trans.RollBack(); } } } } if (_documentActions.Count > 0) { args.SetRaiseWithoutDelay(); } } }