Message 1 of 6
preselected elements highlighting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have been looking at Jeremy's post about locking down elements from Deleting. Here's the link: http://thebuildingcoder.typepad.com/blog/2011/11/lock-the-model-eg-prevent-deletion.html
I wanted to modify it a little bit. Right now it stores selected Ids and you can only ADD new ones to the selection but never remove any. My idea is to use the previously "protected" elemets to predefine the selection and then either add to that or remove from it before it gets stored back in _ProtectedIds. Here's my attempt, and currently it works as expected except that when prompted to select elements there is nothing highlighted. I am basically clearing _ProtectedIds and defning them again every time which is a little cumbersome. Any ideas will help:
#region Namespaces using System; using System.Collections.Generic; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; #endregion namespace PreventDeletion { /// <summary> /// External command to select elements /// to protect from deletion. /// </summary> [TransactionAttribute(TransactionMode.Manual)] [RegenerationAttribute(RegenerationOption.Manual)] public class Command : IExternalCommand { public const string Caption = "Prevent Deletion"; static List<ElementId> _protectedIds = new List<ElementId>(); static public bool IsProtected( ElementId id ) { return _protectedIds.Contains( id ); } public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements ) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uiapp.ActiveUIDocument.Document; IList<Reference> refs = null; try { // 1. get current contents of the _ProtectedIds. // 2. set current selection to those elements and highlight in view // 3. clear _Protected ids // 4. Prompt user selection // 5. Add re-defined user selection to _ProtectedIds. using (Transaction trans = new Transaction(doc)) { trans.Start("test"); uidoc.Selection.SetElementIds(_protectedIds); doc.Regenerate(); } _protectedIds.Clear(); // prompt user to add to selection or remove from it Selection sel = uidoc.Selection; refs = sel.PickObjects( ObjectType.Element, "Please pick elements to prevent from deletion. " ); } catch( OperationCanceledException ) { return Result.Cancelled; } if( null != refs && 0 < refs.Count ) { foreach( Reference r in refs ) { ElementId id = r.ElementId; if( !_protectedIds.Contains( id ) ) { _protectedIds.Add( id ); } } int n = refs.Count; TaskDialog.Show( Caption, string.Format( "{0} new element{1} selected and protected " + " from deletion, {2} in total.", n, ( 1 == n ? "" : "s" ), _protectedIds.Count ) ); } return Result.Succeeded; } } }