Quick way to renumber doors, grids, and family instances

A friend asked about a macro to renumber grids in the order they are picked. Here’s how:


public void renumberByPicks()
{
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;

    // 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.get_Parameter("Name");
    else if (e is Room)
        p = e.get_Parameter("Number");
    else if (e is FamilyInstance)
        p = e.get_Parameter("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());    
}

12 thoughts on “Quick way to renumber doors, grids, and family instances

  1. Hi Harry, can you suggest how to improve this code to include the Viewport > Detail Number? I can’t seem to find the string in 2012 API Guide.

    Is it:
    else if (e is ViewSheetSetting)
    p = e.get_Parameter(“Detail Number”);

    ???

    Thanks

    Jeff

  2. Hi Harry,
    Is it possible to add an if else statement before the FamilyInstance that would allow me to select a different shared parameter in the Mechanical Equipment category only before it goes over to all FamilyInstance? I was not able to get it to go beyond BuiltInCategory to select this.

  3. Is it possible to increment numbers that contain other characters? For instance our equipment might be 01-100 or A1111 depending on the client’s standard.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s