Making a beam associated with an edge (part 2)

In the previous post, I showed how to create a beam using the geometry of an edge, and how to store parameter data in the beam that can be used to identify the edge used to create it.

Here is how to update the beams when the edge geometry has changed.

public void UpdateAllBeamsOnEdges()
{
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = this.ActiveUIDocument;
    SelElementSet selSet = SelElementSet.Create();

    int updatedCtr = 0;
    int skippedCtr = 0;

    using (Transaction t = new Transaction(doc, "Update beams on edges"))
    {
        t.Start();
        // find all beams that have a value for the BeamReference parameter
        // the lambda expression (inside the Where statement) defines a local variable named stableRefParam
        // to help check that both the parameter is not null and that the value of the parameter is not null
        foreach (FamilyInstance fi in new FilteredElementCollector(doc)
                 .OfClass(typeof(FamilyInstance))
                 .OfCategory(BuiltInCategory.OST_StructuralFraming)
                 .Cast<FamilyInstance>()
                 .Where(q => {
                            Parameter stableRefParam = q.get_Parameter("BeamReference"); 
                            return stableRefParam != null &&
                                stableRefParam.AsString() != null;}))
        {
            // get the string from the parameter
            string stableRef = fi.get_Parameter("BeamReference").AsString();

            // get the reference and edge from the stable reference string
            Reference r = Reference.ParseFromStableRepresentation(doc, stableRef);                
            Edge edge = doc.GetElement(r).GetGeometryObjectFromReference(r) as Edge;

            if (edge == null)
            {
                selSet.Add(fi);
                skippedCtr++;
                continue;
            }
            // get the location curve of the beam
            LocationCurve lc = fi.Location as LocationCurve;

            // set the location curve of the beam to the geometry curve of the edge
            lc.Curve = edge.AsCurve();

            updatedCtr++;
        }
        t.Commit();
    }
    uidoc.Selection.Elements = selSet;
    TaskDialog.Show("Info","Updated " + updatedCtr + " beams\n" + "Skipped " + skippedCtr + " highlighted beams (references may no longer exist)");
}
Advertisements

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