Making a beam associated with an edge (part 1)

Erik asks:

with the UI you can select edges to place a beam but it is not associated to that edge. Can you make it associate?

Yes, we can use the API to solve this problem. In this post I will show how to create the beam and store information about the edge used to create it. In the next post I will show how to update the beam to match the shape of the modified edge.

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

    // find the family symbol for the W-Wide Flange : W12X26 beam
    FamilySymbol beamSymbol = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).Cast<FamilySymbol>()
        .First(q => 
               q.Family.FamilyCategory.Name == "Structural Framing" &&
               q.Family.Name == "W-Wide Flange" && 
               q.Name == "W12X26");

    // find the lowest elevation level in the project (sort levels by elevation, then take the first one)
    Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().OrderBy(q => q.Elevation).First();

    // prompt the user to select an edge
    Reference r = uidoc.Selection.PickObject(ObjectType.Edge,"Pick edge");

    // find the edge from the edge's reference
    Edge edge = doc.GetElement(r).GetGeometryObjectFromReference(r) as Edge;

    using (Transaction t = new Transaction(doc, "Create Beam"))
    {
        t.Start();

        // create a beam on the curve of the edge
        FamilyInstance fi = doc.Create.NewFamilyInstance(edge.AsCurve(), beamSymbol, level, StructuralType.Beam);

        // store a string representation of the edge's reference in a parameter of the beam
        // the value of the parameter will be something like:
        // 99406e0f-efac-4920-9d5f-fe6cea12fc2a-0002cf51:4:LINEAR
        Parameter beamRefParam = fi.get_Parameter("BeamReference");        
        if (beamRefParam == null)
            TaskDialog.Show("Error","Need to create a BeamReference parameter");
        else
            beamRefParam.Set(r.ConvertToStableRepresentation(doc));

        t.Commit();
    }
}
Advertisements

5 thoughts on “Making a beam associated with an edge (part 1)

  1. Hi Harry
    How come I got ” ‘StructuralType’ is inaccessible due to its protection level” error?
    I have these using statements:
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.DB.Architecture;
    using Autodesk.Revit.DB.Events;
    using Autodesk.Revit.UI.Events;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    using Autodesk.Revit.ApplicationServices;

  2. Appreciate for your information Harry
    I’m writing a Macro to flip the beam orientation.I think this code is very useful.
    I’ve tried to use Curve.CreateReversed to do the flip but it only works for beam between columns.

    http://forums.autodesk.com/t5/revit-api/macro-to-change-beam-start-and-end-orientation-is-it-possible/m-p/5524225#M8916

    I have my code to create a new beam by using a line with switched end points from a existing beam. This method would let me hold the beam on a exact position. My next step is to figure it out how to delete the existing beam and obtain the same parameter from the existing beam. Any suggestion?
    Thanks

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