/// <summary> /// A simple way to put urgent / non-urgent requests into the request queue /// </summary> /// <param name="request"></param> /// <param name="urgent"></param> private void PutInRequestQueue(Request request, bool urgent) { if (urgent) { // If request is urgent, have it 'skip' the line and set it up to be served ASAP this.ImmediateRequest(request); } else { // If request isn't urgent, have it put on the end of the line and process it through like normal this.AddRequest(request); } }
/// <summary> /// Have the given request be put at the 'back' of the queue, where it will be served orderly /// </summary> /// <param name="request"></param> public void AddRequest(Request request) { this.requestQueue.Add(request); }
/// <summary> /// Have the given request be put at the 'front' of the queue, where it will be served immediately /// </summary> /// <param name="request"></param> public void ImmediateRequest(Request request) { this.requestQueue.Insert(0, request); }
/// <summary> /// Handles the form's request to execute instructions within a revit API context /// </summary> private void HandleRequest_IdlingHandler(Request request) { FilterMode filter = requestHandler.FilterBy; switch (request) { case Request.UpdateTreeView: // debug.printText(dataController.SelElementIds, "SelElementIds Before", 1); // Step 1: Update all viewable elements for the current view bool changed = dataController.SetMode(filter); changed = true; // Step 1.1: Exit if dataController doesn't detect a change in viewable elements if (!changed) { return; } Dictionary <Common.Depth, HashSet <string> > blackList; HashSet <string> bl = new HashSet <string>(); blackList = filterController.FieldBlackList; bl.Clear(); if (blackList.ContainsKey(Common.Depth.CategoryType)) { bl.UnionWith(blackList[Common.Depth.CategoryType]); } debug.printText(bl, "CategoryType temporary", 1); blackList = filterController.PersistentBlackList; bl.Clear(); if (blackList.ContainsKey(Common.Depth.CategoryType)) { bl.UnionWith(blackList[Common.Depth.CategoryType]); } debug.printText(bl, "CategoryType persistent", 2); // Get the new elements by filtering all the elements reported by dataController HashSet <ElementId> newElementIds = filterController.FilterS(dataController.AllElements); // Get the old elements obtaining all the elements reported by selectionController HashSet <ElementId> oldElementIds = selectionController.AllElementIds; // Make a LINQ statement for addList var addList = from ElementId id in newElementIds where (!oldElementIds.Contains(id)) select id; // Make a LINQ statement for delList var delList = from ElementId id in oldElementIds where (!newElementIds.Contains(id)) select id; // Set nodes to add and nodes to delete selectionController.NodesToAdd = addList.ToList(); selectionController.NodesToDel = delList.ToList(); // Step 2: Update the treeView element outside of the API context this.BeginInvoke(new Action(() => { // Commit the changes to the TreeView selectionController.CommitTree(dataController.ElementTree); // selectionController.CommitTree(dataController.ElementTree, newElementIds); // Refresh all node counters since the TreeView's nodes are mostly going to be changed selectionController.RefreshAllNodeCounters(dataController); // Update the selection counter label selectionController.UpdateSelectionCounter(); // Update the HideNodeList this.optionController.UpdateHideNodeList(dataController.ElementTree.SetTree, this.filterController); })); // Step 3: Update visibility state of the view Autodesk.Revit.DB.View view = dataController.View; HashSet <ElementId> hidden = new HashSet <ElementId>(); if (optionController.GetVisibilityState()) { // Step 3.1a: Get hidden elements by the formula: hidden = AllElements - SelElements hidden = new HashSet <ElementId>(dataController.AllElements.Except(dataController.SelElementIds)); } else { // Step 3.1b: To show all elements, we need to remove the previously hidden ids from the records (if there is any) if (dataController.ElementTree.HiddenNodes.ContainsKey(view.Id)) { dataController.ElementTree.HiddenNodes.Remove(view.Id); } } // Step 4: Set dataController's ids to hide with hidden dataController.IdsToHide = hidden; // Step 5: Request ChangeElementVisibility requestHandler.AddRequest(Request.ChangeElementVisibility); requestHandler.AddRequest(Request.UpdateTreeViewSelection); break; case Request.UpdateTreeViewSelection: // Step 1: Update dataController's select element list HashSet <ElementId> newSelection = new HashSet <ElementId>(revitController.GetElementIdsFromSelection()); // newSelection.IntersectWith(dataController.AllElements); newSelection.IntersectWith(selectionController.AllElementIds); HashSet <ElementId> oldSelection = dataController.SelElementIds; oldSelection.IntersectWith(selectionController.AllElementIds); // Step 2: Construct add and remove hashsets HashSet <ElementId> addSelection = new HashSet <ElementId>(newSelection.Except(oldSelection)); HashSet <ElementId> remSelection = new HashSet <ElementId>(oldSelection.Except(newSelection)); // DEBUG: Print out addSelection //StringBuilder sb = new StringBuilder(); //sb.AppendLine("RemSelection"); //foreach (ElementId id in remSelection) //{ // sb.AppendLine(id.ToString()); //} //MessageBox.Show(sb.ToString()); // Branch nodes to update // Step 3: Update selection by 'adding' and 'removing' selected treeNodes by checking and unchecking them this.BeginInvoke(new Action(() => { selectionController.UpdateSelectionByElementId(addSelection, true); selectionController.UpdateSelectionByElementId(remSelection, false); // selectionController.RefreshAllNodeCounters(dataController); selectionController.UpdateAffectedCounter(addSelection.Union(remSelection), dataController); selectionController.UpdateSelectionCounter(); })); // Step 4: Update dataController efficiently // Mathematical visualization: SelElements = (currentSelection ^ revitSelection) U additionalSelection dataController.SelElementIds.IntersectWith(newSelection); dataController.SelElementIds.UnionWith(addSelection); // Step 3: (OPTIONAL) Hide the remaining elementIds if (optionController.GetVisibilityState()) { // Step 3.1: Get selected and all elemnts from dataController HashSet <ElementId> sel = dataController.SelElementIds; HashSet <ElementId> all = dataController.AllElements; // Step 3.2: Get the elementIds that are supposed to be hidden by (hid = all - sel) HashSet <ElementId> hid = new HashSet <ElementId>(all.Except(sel)); // Step 3.3: Set dataController's requested ids to hide (NOT ACTUAL HIDDEN ELEMENTIDS) to hid dataController.IdsToHide = hid; // Step 3.4: Immediately override the next request by Requesting ChangeElementVisibility requestHandler.ImmediateRequest(Request.ChangeElementVisibility); } break; case Request.SelectElementIds: // Step 1: Make a new selection in Revit revitController.MakeNewSelection(dataController.SelElementIds.ToList()); // Step 2: Update the selection's counter this.BeginInvoke(new Action(() => { // selectionController.RefreshAllNodeCounters(dataController); selectionController.UpdateSelectionCounter(); })); // Step 2: (OPTIONAL) Hide the remaining elementIds if (optionController.GetVisibilityState()) { // Step 2.1: Get selected and all elemnts from dataController HashSet <ElementId> sel = dataController.SelElementIds; HashSet <ElementId> all = dataController.AllElements; // Step 2.2: Get the elementIds that are supposed to be hidden by (hid = all - sel) HashSet <ElementId> hid = new HashSet <ElementId>(all.Except(sel)); // Step 2.3: Set dataController's requested ids to hide (NOT ACTUAL HIDDEN ELEMENTIDS) to hid dataController.IdsToHide = hid; // Step 2.4: Immediately override the next request by Requesting ChangeElementVisibility requestHandler.ImmediateRequest(Request.ChangeElementVisibility); } break; case Request.ChangeElementVisibility: // Step 1: Get the previously hidden ids HashSet <ElementId> prevHidden = new HashSet <ElementId>(); Autodesk.Revit.DB.View v = dataController.View; if (v != null) { if (dataController.ElementTree.HiddenNodes.ContainsKey(v.Id)) { prevHidden.UnionWith(dataController.ElementTree.HiddenNodes[v.Id]); } } // Step 2: Get ids to Hide and Show // idsToHide = IdsToHide HashSet <ElementId> idsToHide = dataController.IdsToHide; // idsToShow = AllElements - prevHidden HashSet <ElementId> idsToShow = new HashSet <ElementId>(dataController.AllElements.Union(prevHidden)); // Step 3: idsToShow = idsToShow - idsToHide idsToShow.ExceptWith(idsToHide); // Step 4: Hide elements if there is any if (idsToHide.Count != 0) { revitController.HideElementIds(idsToHide, dataController.ElementTree); } // Step 5: Show elements if there is any if (idsToShow.Count != 0) { revitController.ShowElementIds(idsToShow, dataController.ElementTree); } break; case Request.ShiftElements: // Step 1: Get all the needed information within dataController List <ElementId> idsToMove = revitController.GetMovableElementIds(dataController.IdsToMove.ToList()); List <int> coords = dataController.Coords; bool copyAndShift = dataController.CopyAndShift; if (idsToMove.Count == 0) { this.actionController.PromptNotValidElements(); } // Step 2: Copy and move the elements via the Revit Controller using the given information and get back the affected elementIds // revitController.CopyAndMoveElements(idsToMove, coords, copyAndShift); List <ElementId> elementsAffected = revitController.CopyAndShiftElements(idsToMove, coords, copyAndShift); if (copyAndShift) { this.dataController.AddToAllElements(elementsAffected); this.selectionController.NodesToAdd = elementsAffected; requestHandler.ImmediateRequest(Request.UpdateTreeView); } break; case Request.Nothing: if (RevitSelectionChanged()) { requestHandler.AddRequest(Request.UpdateTreeViewSelection); } break; default: break; } }
/// <summary> /// The event while the app is idling (checks what is selected) /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void UIAppEvent_IdlingEventHandler(object sender, IdlingEventArgs args) { try { args.SetRaiseWithoutDelay(); // If a thread has halted the idling handler, this if statement will // make sure that the process does not pass through any further. if (this.haltIdlingHandler) { return; } // Initializer for everything the form needs within the Revit API context FirstStartup_IdlingHandler(); Request request = GetNextRequest_IdlingHandler(); HandleRequest_IdlingHandler(request); #region Stow /* * if (!this.haltIdlingHandler) * { * // This is to get the essential objects to use the Revit API * UIApplication uiApp = sender as UIApplication; * UIDocument uiDoc = uiApp.ActiveUIDocument; * Document activeDoc = uiDoc.Document; * * if (this.firstStartup) * { * this.firstStartup = false; * * List<ElementId> AllElementIds = revitController.GetAllElementIds(FilterMode.Project); * dataController.SetAllElements(AllElementIds); * } * * Request request; * FilterMode filter; * * filter = requestHandler.FilterBy; * * int viewChanged = revitController.UpdateView(); * if (viewChanged == 1) * { * request = Request.UpdateTreeView; * } * else * { * request = requestHandler.GetRequest(); * } * * request = requestHandler.ProcessRequest(request); * * switch (request) * { * case Request.UpdateTreeView: * * dataController.SetMode(filter); * * // Update the treeView element within the form * this.BeginInvoke(new Action(() => * { * selectionController.UpdateTreeViewStructure(dataController.AllElements, dataController.ElementTree); * // selectionController.UpdateTreeViewStructure_New(dataController.ElementTree.SubSet, dataController.ElementTree); * })); * * break; * case Request.UpdateTreeViewSelection: * * ICollection<ElementId> currentSelected = this.uiDoc.Selection.GetElementIds(); * this.BeginInvoke(new Action(() => * { * bool updateSucceeded = selectionController.UpdateSelectedLeaves(currentSelected); * if (updateSucceeded) * { * requestHandler.FailureListRemove(request); * } * else * { * requestHandler.AttemptRecovery(request); * } * })); * * break; * case Request.SelectElementIds: * * revitController.MakeNewSelection(dataController.SelElements); * * //List<ElementId> elementIds * // = revitController.GetAllElementIds(filter); * //dataController.UpdateAllElements(elementIds); * * dataController.SetMode(filter); * * // Hide all elements * if (requestHandler.UnselectedHidden) * { * revitController.HideUnselectedElementIds( * dataController.SelElements, * dataController.AllElements); * } * else * { * revitController.ShowSelectedElementIds(dataController.SelElements); * } * * break; * case Request.ShiftSelected: * * List<ElementId> movElementIds = dataController.MovElements; * List<int> coords = dataController.Coords; * bool copyAndShift = dataController.CopyAndShift; * * // Copy and Move the Elements * revitController.CopyAndMoveElements(movElementIds, coords, copyAndShift); * * break; * case Request.Nothing: * // Do absolutely nothing * break; * default: * * // If the request isn't any other request (even Nothing), then prompt user with warning message * TaskDialog.Show("Debug - SelectionChanged_UIAppEvent_WhileIdling", * "Warning: Handler given invalid request"); * * break; * } * } */ #endregion STOW } catch (Exception ex) { ErrorReport.Report(ex); } }