How to create a door & tag

Continuing the theme of element creation from the last post, here is how to create a door and tag it.

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

    // Get the door type
    // FamilySymbol corresponds to the Type of a Family
    // In this case Door - Single-Flush - 34" x 80"
    FamilySymbol familySymbol = (from fs in new FilteredElementCollector(doc).
         OfClass(typeof(FamilySymbol)).
         Cast<FamilySymbol>()
         // need to put the \ before the " in the name of the door type so it isn't
         // treaded as the " that ends the string
         where (fs.Family.Name == "Single-Flush" && fs.Name == "34\" x 80\"")
         select fs).First();

    // Get the door tag
    FamilySymbol doorTagType = (from tag in new FilteredElementCollector(doc)
                                .OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_DoorTags)
                                .Cast<FamilySymbol>() where tag.Name == "Door Tag" select tag).First();

    // Prompt the user to select a wall
    Reference r = uidoc.Selection.PickObject(ObjectType.Element, "Select the wall that will host the door");
    // Get the wall element from the selected reference
    Wall wall = doc.GetElement(r) as Wall;
    // Get the point on the wall where the selection was made
    XYZ globalPoint = r.GlobalPoint;
    // But we don't want to place the door at this "global point", because the Z of that point is at the
    // cut plane height of the plan view.

    // Instead, create a new XYZ using the X and Y from the GlobalPoint and the Z of the plan view's level
    ViewPlan viewPlan = doc.ActiveView as ViewPlan;
    Level level = viewPlan.GenLevel;
    XYZ levelPoint = new XYZ(globalPoint.X, globalPoint.Y, level.Elevation);

    using (Transaction t = new Transaction(doc,"Create door"))
    {
        t.Start();
        // Create door
        FamilyInstance door = doc.Create.NewFamilyInstance(levelPoint, familySymbol, e, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
        // Create tag.
        // Unlike room tags, which have a dedicated NewRoomTag method, the generic NewFamilyInstance is used to tag doors and other elements.
        FamilyInstance doorTag = doc.Create.NewFamilyInstance(levelPoint, doorTagType, doc.ActiveView);
        t.Commit();
    }
}
Advertisements

10 thoughts on “How to create a door & tag

  1. Hi………..
    I am getting problem to get a real time view(like in case inserting door on a wall,after selecting the door, i am not getting the door view of moving along with the mouse cursor only getting cursor and if click on wall directly it will inserting.) before placing family inserting.

    my code is like this:

    public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
    ……….
    try
    {
    trans.Start();
    Family family = null;
    FamilySymbol symbol = null;
    if (doc.LoadFamily(@h4, out family))
    {
    foreach (FamilySymbol s in family.Symbols)
    {
    symbol = s;
    }
    View view = uidoc.ActiveView;
    Reference r = sel.PickObject(ObjectType.PointOnElement, “Select”);
    Element elem = doc.GetElement(r);
    Wall wall = elem as Wall;
    XYZ globalPoint = r.GlobalPoint;
    XYZ levelPoint = new XYZ(10, 10, 1);
    Wall wall = doc.GetElement(r) as Wall;
    FamilyInstance instance = doc.Create.NewFamilyInstance(levelPoint, symbol, wall,level,StructuralType.NonStructural);
    }
    trans.Commit();
    }
    catch (Exception)
    {

    }
    return Result.Succeeded;
    }

    plzz help me……

  2. Hi Harry,
    How can I tag a ceiling. I tried using the independent tag method in an active view based on the revit api developer’s guide wall tag example, when taggaing walls this works however even though i tried to get the locationPoint and then the point from this, I get a null reference exception. Any pointers as to how this is done?
    cheers,
    Fabs

  3. sry i should have given you all the code. I cant see any difference between my two methods i am getting an element from the collector the same as with the wall i am just using a point instead of a location line or curve.
    //add tag to centre of ceiling?
    LocationPoint p = wall.Location as LocationPoint;
    p.Point = new XYZ(p.Point.X / 2, p.Point.Y / 2, p.Point.Z);
    XYZ ceilingCentre = p.Point;

    • Hi Frank,

      I don’t think the ceiling has a location point. What point would that be? It might work better to use its bounding box or get the geometry of its faces & their edges to find the point for the tag.

      Harry

  4. haha thanx harry,
    I think you are correct that the ceiling doesn’t have a point as the null exception states. I will try the bounding box option next. A further sort of related question to my addin, i have noticed that the second floor or floor finishes slab that is used for finishes tagging confuses the create view section method and i end up with erroneous crop boundaries for my elevations. The top crop height is usually just above the top slab. Do you know of a solution to this issue?

  5. Guys i want to know if there is any option or API to add level to a door tag..
    i mean when a door is tagged it should also show the level at which it is placed..
    please let me know if there is anything could be done….!!!!

    thank you……………………..!!

    • You could create a shared parameter “Door Level” to the Door category and add it to your tag. Then use Dynamic Model Update to automatically set the value for this parameter anytime a door is created.

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