public void SetSelection(IEnumerable <IObject3D> items) { if (items.Count() == 1) { // Add a single item directly as the SelectedItem SelectedItem = items.First(); } else { // Add a range of items wrapped with a new SelectionGroup var SelectionGroup = new SelectionGroupObject3D(items) { Name = "Selection".Localize() }; // Move selected items into a new SelectionGroup this.Children.Modify(list => { foreach (var item in items) { list.Remove(item); } // Add the SeletionGroup as the first child list.Insert(0, SelectionGroup); }); SelectedItem = SelectionGroup; } }
public void AddToSelection(IObject3D itemToAdd) { var selectedItem = SelectedItem; if (itemToAdd == selectedItem || selectedItem?.Children?.Contains(itemToAdd) == true) { return; } if (selectedItem != null) { if (selectedItem is SelectionGroupObject3D) { // Remove from the scene root this.Children.Modify(list => list.Remove(itemToAdd)); // Move into the SelectionGroup selectedItem.Children.Modify(list => list.Add(itemToAdd)); } else // add a new selection group and add to its children { // We're adding a new item to the selection. To do so we wrap the selected item // in a new group and with the new item. The selection will continue to grow in this // way until it's applied, due to a loss of focus or until a group operation occurs var newSelectionGroup = new SelectionGroupObject3D(new[] { selectedItem, itemToAdd }) { Name = "Selection".Localize() }; this.Children.Modify(list => { list.Remove(itemToAdd); list.Remove(selectedItem); // add the seletionGroup as the first item so we can hit it first list.Insert(0, newSelectionGroup); }); SelectedItem = newSelectionGroup; } } else if (Children.Contains(itemToAdd)) { SelectedItem = itemToAdd; } else { throw new Exception("Unable to select external object. Item must be in the scene to be selected."); } }