#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.
#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.
Solved by arnostlobel. Go to Solution.
Hi destinova:
I am sure that after you read this, you will be going duh! 🙂
The problem is within your Execute method. You return from the method's line #5. Since it is a non-conditional return, any code after that is effectivelly unreachable. The first code affected is the line at which you create the new List. To fix it you need to return from the end of the method. You should return either Succeeded or Failed depending on the outcome of your Execute method.
For the record, I have not reviewed the rest of the code (beyond the line # 5), thus I do not know if the rest is all right. Dealing with the premature return should get you going, though.
Cheers
Hi destinova:
I am sure that after you read this, you will be going duh! 🙂
The problem is within your Execute method. You return from the method's line #5. Since it is a non-conditional return, any code after that is effectivelly unreachable. The first code affected is the line at which you create the new List. To fix it you need to return from the end of the method. You should return either Succeeded or Failed depending on the outcome of your Execute method.
For the record, I have not reviewed the rest of the code (beyond the line # 5), thus I do not know if the rest is all right. Dealing with the premature return should get you going, though.
Cheers
Can't find what you're looking for? Ask the community or share your knowledge.