Resolving duplicate tags Step 4 – transactions and wrapping up

We’re almost done!

The last thing to mention is that any time we use the API to modify the document, we need to first create and start a transaction. When we are finished making changes we commit the transaction. Here is the final code with the transactions added:

public void MakeMarksUnique()
{
    Document document = this.ActiveUIDocument.Document;
    FilteredElementCollector collector = new FilteredElementCollector(document);
    IList<Element> doorList = collector.OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance)).ToList();
    if (doorList.Count > 0)
    {
        IList<string> markValues = new List<string>();
        using (Transaction t = new Transaction(document,"Modify Mark values"))
        {
            t.Start();           
            foreach (Element e in doorList)
            {
                Parameter p = e.get_Parameter("Mark");
                if (markValues.Contains(p.AsString()))
                    p.Set(p.AsString() + "*");
                else
                    markValues.Add(p.AsString());
            }
            t.Commit();
        }
    }
}

And here is what the Revit model looks like after running the command. Note that three of the doors have new mark values and the Undo menu includes the “Modify Mark Values” transaction that was created by the macro.

noduplicates

I hope you enjoyed this first little project and found it interesting and useful. Please leave comments about what you think I should do next!

Advertisements

10 thoughts on “Resolving duplicate tags Step 4 – transactions and wrapping up

  1. If you don’t mind, I would like to better understand some of the code.

    Transaction t = new Transaction(document,”Modify Mark values”)

    When you initiate a transaction, you’re creating a variable t. That variable (weither t, trans, etc.) would always equal “new Transaction(document, “definition or explaination of transaction?” )”? with “Modify Mark values” (in your instance) as a descriptor of what the transaction is doing? Is this ‘discriptor’ or ‘definition’ ever seen by the end user?

  2. As a basic question, I’ve never used the Macro Manager to create code as we’re doing here. I’ve always thought it needing to be done as an addin accessible from the add-in’s tab (using visual studio or similar). What’s the difference between what we’re doing here, and one done that way? The code seems the same. Once we create the code for a task, as we’ve done here, would you then create an addin so it’s accessible a different way? What’s the process?

    Thanks!
    David

  3. Hi Harry, is it possible to make this more generic to include all model categories since they all contain “Mark” parameter as well as Room “Number” parameter?

      • Harry, it is related to get_Parameter. I think it is an easy fix? I am trying to get a fix-all solution to remove the ‘duplicate mark’ warning. Ideally, it would work across an entire category (or multiple categories) and ensure the Mark parameter was unique for that Category. The actual value itself is unimportant. Here is the full build warning:

        ‘Autodesk.Revit.DB.Element.get_Parameter(string)’ is obsolete: ‘This property is obsolete in Revit 2015, as more than one parameter can have the same name on a given element. Use Element.Parameters to obtain a complete list of parameters on this Element, or Element.GetParameters(String) to get a list of all parameters by name, or Element.LookupParameter(String) to return the first available parameter with the given name.’ (CS0618) – C:\ProgramData\Autodesk\Revit\Macros\2015\Revit\AppHookup\duplicateRemover\Source\duplicateRemover\ThisApplication.cs:53,31

          • Ok cool, I was able to get it to run for Pipe Fittings OST_PipeFitting, but I just end up with Marks appended with *. I think I need to modify the collector to grab every instance of an element in a given Category?

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