第 1 条消息(共 10 条)
I am trying to modify Jeremy's great post about Preventing Deleting of elements from the model so that it can actually be saved in the model which elements are being "protected". I tried using a Schema to write an Entity to an Element and then later have the IUpdater retireve that schema to see if element is protected from deletion. However, I am getting null for a doc.GetElement() method in IUpdater and I can't proceed with the rest of the code. Any help would be appreciated.
using System; using System.Collections.Generic; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.DB.ExtensibleStorage; namespace GrimshawRibbon { // Create Elment Filter that passes all elements through public class ElementSelectionFilter : ISelectionFilter { public bool AllowElement(Element e) { return true; } public bool AllowReference(Reference r, XYZ p) { return false; } } [TransactionAttribute(TransactionMode.Manual)] public class PreventDeletion : IExternalCommand { Guid schemaGuid = new Guid("0DC954AE-ADEF-41c1-8D38-EB5B8465D255"); public const string Caption = "PreventDeletion"; public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uiapp.ActiveUIDocument.Document; try { IList<Reference> refs = null; ElementSelectionFilter selFilter = new ElementSelectionFilter(); try { // prompt user to add to selection or remove from it Selection sel = uidoc.Selection; refs = sel.PickObjects(ObjectType.Element, selFilter, "Please pick elements to prevent from deletion."); } // catch an exception if user hits ESC here catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return Result.Cancelled; } using (Transaction trans = new Transaction(doc, "AddElement-PreventDeletion")) { trans.Start(); foreach (Reference r in refs) { Element e = doc.GetElement(r); if (e != null) { SchemaBuilder schemaBuilder = new SchemaBuilder(schemaGuid); // allow anyone to read the object schemaBuilder.SetReadAccessLevel(AccessLevel.Public); // restrict writing to this vendor only schemaBuilder.SetWriteAccessLevel(AccessLevel.Public); // set schema name schemaBuilder.SetSchemaName("PreventDeletionSchema"); // set documentation schemaBuilder.SetDocumentation("A stored boolean value where true means that wall cannot be deleted."); // create a field to store the bool value FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField("PreventDeletionField", typeof(String)); // register the schema Schema schema = schemaBuilder.Finish(); // create an entity object (object) for this schema (class) Entity entity = new Entity(schema); // get the field from schema Field fieldPreventDeletion = schema.GetField("PreventDeletionField"); // set the value for entity entity.Set(fieldPreventDeletion, "Prevent"); // store the entity on the element e.SetEntity(entity); } } trans.Commit(); } return Result.Succeeded; } catch (OperationCanceledException) { return Result.Cancelled; } catch (Exception ex) { message = ex.Message; return Result.Failed; } } } }
using System; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.DB; using Autodesk.Revit.DB.ExtensibleStorage; using System.Collections.Generic; namespace GrimshawRibbon { class PreventDeletionUpdater : IUpdater { static AddInId _appId; UpdaterId _updaterId; FailureDefinitionId _failureId = null; public PreventDeletionUpdater(AddInId addInId) { _appId = addInId; _updaterId = new UpdaterId(_appId, new Guid("6f453eba-4b9a-40df-b637-eb72a9ebf008")); _failureId = new FailureDefinitionId(new Guid("33ba8315-e031-493f-af92-4f417b6ccf70")); FailureDefinition failureDefinition = FailureDefinition.CreateFailureDefinition(_failureId, FailureSeverity.Error, "Prevent Deletion: Sorry, this element cannot be deleted. Please contact your BIM Manager to find out why."); } public void Execute(UpdaterData data) { Document doc = data.GetDocument(); foreach (ElementId id in data.GetDeletedElementIds()) { Element e = doc.GetElement(id) as Element; if (e != null) { IList<Guid> schemaIds = e.GetEntitySchemaGuids(); if (schemaIds.Count != 0) { foreach (Guid schemaId in schemaIds) { Schema pdSchema = Schema.Lookup(schemaId); if (pdSchema != null) { if (pdSchema.SchemaName == "PreventDeletionSchema") { FailureMessage failureMessage = new FailureMessage(_failureId); failureMessage.SetFailingElement(id); doc.PostFailure(failureMessage); } } } } } } } public string GetAdditionalInformation() { return "Prevent deletion of selected elements."; } public ChangePriority GetChangePriority() { return ChangePriority.FloorsRoofsStructuralWalls; } public UpdaterId GetUpdaterId() { return _updaterId; } public string GetUpdaterName() { return PreventDeletion.Caption; } } }
my app is running these updater registers on startup:
public Result OnStartup(UIControlledApplication application) { AddRibbonPanel(application); #region Prevent Deletion Tool // Failure Definition Registry // Implemented in Prevent Deletion Tool PreventDeletionUpdater deletionUpdater = new PreventDeletionUpdater(application.ActiveAddInId); UpdaterRegistry.RegisterUpdater( deletionUpdater); ElementClassFilter filter = new ElementClassFilter( typeof(Dimension), true); UpdaterRegistry.AddTrigger( deletionUpdater.GetUpdaterId(), filter, Element.GetChangeTypeElementDeletion()); #endregion //Prevent Deletion Tool #region DeleteAllViewsSheets Tool m_MyForm = null; // no dialog needed yet; the command will bring it thisApp = this; // static access to this application instance #endregion //DeleteAllViewsSheets Tool return Result.Succeeded; }
Of course huge thanks to Jeremy for posting all of this code. I wouldnt even be able to get to this point without his blog. Thank you!
已解决! 转到解答。