Duplicate Legend Component

A number of Revit elements can still not be created programmatically, because the API does not provide the appropriate creation methods for them.

Joe Ye just handled a case asking how to create a new legend component, and Harry Mattison suggested an exciting new possibility that we were previously not aware of.

This method allows us to duplicate many kinds of elements with an existing instance inserted. Here is the original question as a starting point:

Question: Is it possible to add a new Legend Component to the Legend programmatically?
I have found no API for the Legend Component.
Is there some workaround – for example invoking a keyboard shortcut for it?

Is it possible to copy an existing Legend Component and change the family it is referring to?

Answer: The Revit API does not currently expose any methods that allow us to add a new legend component.

The good news is that there is a workaround to create a new element by copying an existing one.

The solution is to put the element that you would like to copy into a group, place an instance of the group in the document, and then ungroup the two groups.

The result is that the existing element remains and a new element is created in addition.

Here is a new Building Coder sample command CmdDuplicateElements which demonstrates this, and also provides a first example of a command modifying the Revit model using manual transaction mode, which requires it to start and commit its own transaction:


[Transaction( TransactionMode.Manual )]
[Regeneration( RegenerationOption.Manual )]
class CmdDuplicateElements : IExternalCommand
{
  public Result Execute(
    ExternalCommandData commandData,
    ref string message,
    ElementSet elements )
  {
    UIApplication app = commandData.Application;
    UIDocument uidoc = app.ActiveUIDocument;
    Document doc = uidoc.Document;
 
    Transaction trans = new Transaction( doc,
      "Duplicate Elements" );
 
    trans.Start();
 
    Group group = doc.Create.NewGroup(
      uidoc.Selection.Elements );
 
    LocationPoint location = group.Location
      as LocationPoint;
 
    XYZ p = location.Point;
    XYZ newPoint = new XYZ( p.X, p.Y + 10, p.Z );
 
    Group newGroup = doc.Create.PlaceGroup(
      newPoint, group.GroupType );
 
    group.Ungroup();
 
    ElementSet eSet = newGroup.Ungroup();
 
    // change the property or parameter values 
    // of elements in eSet as required...
 
    trans.Commit();
 
    return Result.Succeeded;
  }
}

To test this command, you can select a legend component and launch the command.
A new legend component will be created.
You can then change the new legend’s parameter
Component Type to change the legend type.

I created three legend components and three other detail objects, selected them, and lauched the command, which produced the six copied elements ten feet higher up like this:

Duplicated elements

Here is
version 2011.0.70.0
of The Building Coder sample source code and Visual Studio solution including the new command.

Many thanks to Joe for handling the case and Harry for suggesting this approach.


Comments

8 responses to “Duplicate Legend Component”

  1. Hi Jeremy Tammik,
    i’m come with another query. i want to create the permanent Dimension. How to create it. In CreateDimension(Api Sample). It describe only the LocationCure(Like Beam, wall). Now i want to create Column also(Familyinstance it represent only locationpoint or any non-structural item).
    And how to locate or identify ReferencePlane in familyinstance. and what is array reference it represent what? (like ReferencePlane or someother). do you have any idea. pls share with me.
    Regards,
    Dharanidharan R

  2. Dear Dharanidharan,
    To create a dimensioning element, you need to determine some references to the geometry being dimensioned.
    The Building Coder sample command CmdNewDimensionLabel provides an example.
    There are a number of ways to identify a reference plane. Two of them are demonstrated by the findElement method in the Revit Family API labs, using the element name and the plane normal vector:
    http://thebuildingcoder.typepad.com/blog/2009/10/revit-family-creation-api-labs.html
    Cheers, Jeremy.

  3. Hi Jeremy Tammik,
     Thanks for your replay. i can create Dimension. But how to assign the Dimension lable.  Ill create the rectangle family and paste family instance in Document (South Elevation). Now select the object and get the information through GeometryObject. its return the four line (two vertical, two horizontal), then i can easily get line refrence and create the Dimension it dont make errors.
     
      Now, Dimension object accept only FamilyParameter. if i create the FamilyParameter using Familymanager it makes error (Family manager only used for Family Editor). then how can i assign the value in label with the familyparameter of familyinstance.
    Thanks in advance
    Dharanidharan R

  4. Dear Dharanidharan,
    Congratulations on the great progress you have made.
    Yes, the family manager only works in the family context, so if you absolutely need to create a family parameter you will have to do so there.
    I cannot tell you off-hand whether that is the way you have to go.
    Cheers, Jeremy.

  5. Dharanidharan has sent you a teddy
    Dharanidharan
    To accept this gift please click the following link:
    Get Gift
    Regards,
    Dharanidharan
    You can opt-out of Shtyle.fm emails.

  6. Dear Jeremy,
    I want to programatically create a legend view for doors and windows. Just learned from your blog, there are no functions exposed to create legend components. We have to create one, copy it and then change Component Type property.
    It is easy to get all windows instances. But how could I get the legend components used in a project? I mean those just listed in the family tree and not manually created in a legend view.
    Regards,
    Eric

  7. Dear Eric,
    You can filter for the BuiltInCategory.OST_LegendComponents.
    Beyond that, there is not much to identify them with, I believe, except their name, if you think you can rely on that.
    Cheers, Jeremy.

  8. Hi Jeremy,
    Any news about this Legend Component API?
    Is there anyway I can do a Symbol Swap at least inside the Legend View?
    I’m trying the following code:
    element1.get_Parameter(BuiltInParameter.LEGEND_COMPONENT).Set(element2.Id);
    But I’m getting the error:
    The component you have selected is not visible in the selected view.
    Any help?
    Thanks in advance,
    Israel

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading