#RTCNA wish 9 granted! Place instances of multiple families

Jason asked if the API can be used to “select a bunch of families from a folder and automatically place them in an .rvt

NOTE: For this code to run, you will need to add a reference to the SharpDevelop project as follows: Project – Add Reference – select System.Windows.Forms

public void placeMultipleFamilies()
{
    Document doc = this.ActiveUIDocument.Document;
    
    if (!(doc.ActiveView is ViewPlan))
    {
        TaskDialog.Show("Error","Command must be run from a plan view");
        return;
    }
    
    // use standard Windows File Open dialog to select files
    System.Windows.Forms.OpenFileDialog opendialog = new System.Windows.Forms.OpenFileDialog();
    // let user select multiple files
    opendialog.Multiselect = true;
    // set default directory
    opendialog.InitialDirectory = doc.Application.GetLibraryPaths().Values.First();
    opendialog.Filter = "Revit Families|*.rfa";
    System.Windows.Forms.DialogResult result = opendialog.ShowDialog();
    if (result == System.Windows.Forms.DialogResult.Cancel)
        return;
    
    Level level = doc.ActiveView.GenLevel;
    
    using (Transaction t = new Transaction(doc, "Place families"))
    {
        t.Start();
        double x = 0;
        double y = 0;
        foreach (string file in opendialog.FileNames)
        {
            // load the family
            Family f = null;
            doc.LoadFamily(file, out f);
            
            if (f == null)
                continue;
            
            // get a family symbol (aka family type) from the family
            FamilySymbol fs = f.Symbols.Cast<FamilySymbol>().FirstOrDefault();
            
            doc.Create.NewFamilyInstance(new XYZ(x, y, 0), fs, level, StructuralType.NonStructural);
             
            // increment coordinates for placing next instance
            x = x + 10;
            if (x > 50)
            {
                x = 0;
                y = 10;
            }
        }
        t.Commit();
    }
}
Advertisements

8 thoughts on “#RTCNA wish 9 granted! Place instances of multiple families

  1. Greate code, Harry! When I’m trying to build macro for Revit 2014, I’m getting this error: ‘StructuralType’ is inaccessible due to its protection level (CS0122)
    I have also added the statemen at the top of my file, as you suggested:
    using Autodesk.Revit.UI.Selection;
    But still getting the same arror.
    Aslo is it possible to load all the Family Types inserted into RFA. Would be great to load all Family Types from TXT Types catalog. Is it possible. Thank you!

    • Hi,
      Hi Adam,

      Glad you like it!

      The StructuralType enum is in the Autodesk.Revit.DB.Structure namespace, so if you don’t already have a using statement for that you should try adding one.

      If you want to place instances of all family types, then you iterate through all symbols instead of using FirstOrDefault on this line
      f.Symbols.Cast().FirstOrDefault();

  2. Hello Harry!
    Thank you very much, adding “using Autodesk.Revit.DB.Structure” solved the problem!
    Now I have tested the Macro and I’ve noticed that it place 12 single families separately from each other. But if I have more then 12 files, they are overlapping each other.
    Here is an example: http://i67.tinypic.com/2ep0cqt.jpg
    Is it possible to place all families I have separately from each other?

    Concerning placing instances of all family types, I have modified the code as you suggested:
    FamilySymbol fs = f.Symbols.Cast().All()

    But I get this error: Error CS1501: No overload for method ‘All’ takes 0 arguments
    Build failed.

    I’m really sorry, but I’ve started C# programming 1 week ago. I bought you course: “Learn to program the Revit API by Boost Your BIM” and it helps me very much, but still I have a lot of questions.

    • Ok, I have figured out how to place all the families separatly changing the code:
      // increment coordinates for placing next instance
      x = x + 10;
      if (x > 500)
      Also I have found a way to add all Family Types from TXT.
      Now I’d like to create an addin, but can’t find where in Visual Studio to add the reference: Project – Add Reference – select System.Windows.Forms

      How can I solve this?

  3. Thanks Harry! I’ll try and will let you know if it worked.
    By the way if you are interested I can share the code to place all family types from Revit families and from TXT.

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