Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
#region Namespaces using System; using System.Collections.Generic; using System.Diagnostics; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.DB.Architecture; #endregion namespace Renumber { [Transaction(TransactionMode.Manual)] public class Command : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; return Result.Succeeded; // list that will contain references to the selected elements IList<Reference> refList = new List<Reference>(); try { // create a loop to repeatedly prompt the user to select an element while (true) refList.Add(uidoc.Selection.PickObject(ObjectType.Element, "Select elements in order to be renumbered. ESC when finished.")); } // When the user hits ESC Revit will throw an OperationCanceledException which will get them out of the while loop catch { } using (Transaction t = new Transaction(doc, "Renumber")) { t.Start(); // need to avoid encountering the error "The name entered is already in use. Enter a unique name." // for example, if there is already a grid 2 we can't renumber some other grid to 2 // therefore, first renumber everny element to a temporary name then to the real one int ctr = 1; int startValue = 0; foreach (Reference r in refList) { Parameter p = getParameterForReference(doc, r); // get the value of the first element to use as the start value for the renumbering in the next loop if (ctr == 1) startValue = Convert.ToInt16(p.AsString()); setParameterToValue(p, ctr + 12345); // hope this # is unused (could use Failure API to make this more robust ctr++; } ctr = startValue; foreach (Reference r in refList) { Parameter p = getParameterForReference(doc, r); setParameterToValue(p, ctr); ctr++; } t.Commit(); } } private Parameter getParameterForReference(Document doc, Reference r) { Element e = doc.GetElement(r); Parameter p = null; if (e is Grid) p = e.LookupParameter("Name"); else if (e is Room) p = e.LookupParameter("Number"); else if (e is FamilyInstance) p = e.LookupParameter("Mark"); else { TaskDialog.Show("Error", "Unsupported element"); return null; } return p; } private void setParameterToValue(Parameter p, int i) { if (p.StorageType == StorageType.Integer) p.Set(i); else if (p.StorageType == StorageType.String) p.Set(i.ToString()); } } }
Hello again,
I am trying to convert a macro to addin
I can compile the code but in revit nothing happens.
I get this error
IList<Reference> refList = new List<Reference>();
warning CS0162: Unreachable code detected
How can i solve this?
Thanks in advance...
Solved! Go to Solution.