How to add structural connections that have been saved as custom connections

How to add structural connections that have been saved as custom connections

arautio
Advocate Advocate
967 Views
2 Replies
Message 1 of 3

How to add structural connections that have been saved as custom connections

arautio
Advocate
Advocate

I understand that the Revit 2017 sdk samples cover structural connections,  however they appear to be removed from the sdk page.   Does anyone have sample code that covers creating a new connection and applying that to beams in a project?

 

I have the base code in place, I just need a way to get custom connections by name and add them to the selected beams.  I am only adding base/end plates so I only have one member in the connection (though I have a few hundred instances in the project, hence the need for api automation)

 

public void FramingAddConnection()
{    
    UIDocument uidoc = this.ActiveUIDocument;
    Document document = uidoc.Document;
    Autodesk.Revit.DB.View activev = document.ActiveView;

    IList<Reference> felems = uidoc.Selection.PickObjects(ObjectType.Element, "Select Framing Members to Add Connections to");
      
    using (Transaction t = new Transaction(document, "FramingAddConnection"))
    {   
        t.Start();
            
        foreach (Reference eref in felems)
        {
            ElementId elid = eref.ElementId;
            Element el = document.GetElement(elid);
            if (el.Category.Name.Contains("Structural Framing"))
                {
                    //TaskDialog.Show("Revit", el.Name.ToString());
                    FamilyInstance elfi = el as FamilyInstance;

 

                   // ADD CONNECTIONS HERE
                    //StructuralConnectionHandler srcConn = 
                    //StructuralConnectionType stype = 
                    //FramingAddConnection()
                
                }    
            
        }
          
        document.Regenerate();
        t.Commit();
    }
}

0 Likes
Accepted solutions (1)
968 Views
2 Replies
Replies (2)
Message 2 of 3

arautio
Advocate
Advocate

I figured out how to get a list of the connection types in my project, and filtering reduces the list to just the one type that I need to apply to my beams. 

 

FilteredElementCollector con = new FilteredElementCollector(doc);
    con.OfCategory(BuiltInCategory.OST_StructConnections);

    var cons = from element in con where element.Name.Contains("HSS8X4"select element;
    IList<Element> conslist = cons.ToList(); 

 

foreach (Element el in conslist)
    {
        ename = el.Name.ToString();
        eid = el.Id.ToString();
        info = ename + " " + eid;
        TaskDialog.Show("Revit", info);
    }

 

Now I just need to figure out how to make a new connection and apply it (Loop In the previous Post).

0 Likes
Message 3 of 3

arautio
Advocate
Advocate
Accepted solution

Figured it out on my own.

Here's the code if anyone needs something similar.

 

 

public void FramingAddConnection()
{    
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    Autodesk.Revit.DB.View activev = doc.ActiveView;
    

    // Connection name contains
    string conname = "TBP 6X4";

   // Framing Member name contains
    string mname = "HSS6X4";
    
    FilteredElementCollector con = new FilteredElementCollector(doc);
    con.OfCategory(BuiltInCategory.OST_StructConnections);

    var cons = from element in con where element.Name.Contains(conname) select element;
    IList<Element> conslist = cons.ToList(); 
    
    string ename = "";
    string eid = "";
    string etid = "";
    string info = "";
    ElementId telid = null;
    ElementId telidmir = null;
    foreach (Element el in conslist)
    {
        try
        {
            StructuralConnectionHandlerType scht = el as StructuralConnectionHandlerType;
            etid = scht.Id.ToString();
            
            ename = el.Name.ToString();
            eid = el.Id.ToString();
            info = ename + " et= " + etid + " e= " + eid;
            //TaskDialog.Show("Revit", info);
            
            if (ename.Contains("MIR"))
            {
                telidmir = el.Id;
                //TaskDialog.Show("Revit", "Mirror");
            }
            else
            {
                telid = el.Id;
                //TaskDialog.Show("Revit", "Not Mirror");
            }
            
        }
        catch
        {    
        }

    }
    
    IList<Reference> felems = uidoc.Selection.PickObjects(ObjectType.Element, "Select Framing Members to Add Connections to");
    IList<ElementId> felemsids = new List<ElementId>();
    
    using (Transaction t = new Transaction(doc, "FramingAddConnection"))
    {   
        t.Start();
        
        foreach (Reference eref in felems)
        {
            ElementId elid = eref.ElementId;
            Element el = doc.GetElement(elid);
            felemsids.Clear();
            if (el.Category.Name.Contains("Structural Framing"))
                {
                    FamilyInstance elfi = el as FamilyInstance;
                    if (elfi.Name.Contains(mname))
                    {
                        felemsids.Add(elfi.Id);
                        StructuralConnectionHandler.Create(doc,felemsids, telidmir);
                    }
                }    
            
        }
        
        foreach (Reference eref in felems)
        {
            ElementId elid = eref.ElementId;
            Element el = doc.GetElement(elid);
            felemsids.Clear();
            if (el.Category.Name.Contains("Structural Framing"))
                {
                    FamilyInstance elfi = el as FamilyInstance;
                    if (elfi.Name.Contains(mname))
                    {
                        felemsids.Add(elfi.Id);
                        StructuralConnectionHandler.Create(doc,felemsids, telid);
                    }
                }    
            
        }
      
        doc.Regenerate();
        t.Commit();
    }
}

 

Eventually I will change the connection and member name strings to dialogue input boxes,  and check if the beam already has connections, and skip if so.

0 Likes