top of page
Search
  • Writer's picturePaul

Embed a Revit family in a c# addin

Recently ive been looking into how to embed a Revit family in a c# addin as a resource.


The plan was to be able to deliver a single .ddl file that would be a nice neat package for an addin and not have a user worry about having to load the latest family files or maintain different versions.


Autodesk are imfamous for not allowing revit families to be backwards compatible.

So make sure you embed a family made with the oldest version of revit that you plan to mantain compatibility with - 2018 in my case.


Drop your family file in your assembly folder.

Right click and change to embedded resource.


To load the family file use the following code (replacing where needed):


using (Transaction transaction = new Transaction(doc, "Creating Routing View"))
{
    transaction.Start();

    string filePath = null;
    FamilySymbol familySymbol = null;

    //Checking if the family exists already
    familySymbol = new filteredElementCollector(doc).
    OfClass(typeof(FamilySymbol)).
    FirstOrDefault(x => x.Name == "AutoCT Default") as FamilySymbol;

    if (familySymbol == null)
        {
            string[] resourceNames =    Assembly.
            GetExecutingAssembly().GetManifestResourceNames();

            //replace the namespace, folder and 
            //family name here with your own

            Stream stream = Assembly.
              GetExecutingAssembly().
              GetManifestResourceStream("AutoElec.
              Revit_Families.AutoCT Default.rfa");

            filePath = Path.
            GetDirectoryName(Assembly.
            GetExecutingAssembly().
            Location) + @"\AutoCT Default.rfa";

            using (FileStream fs = File.Create(filePath))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(fs);
                fs.Flush();
            }

            string familyName = "AutoCT Default";

            try
            {
                if (!doc.
                LoadFamilySymbol(filePath, 
                familyName, out familySymbol))
                {
                    TaskDialog.Show("Revit", 
                    "Can't load the family file.");
                    return false;
                  }
                }
            catch (Exception ex)
               {
                TaskDialog.Show("Revit", 
                "Can't load the family file.");
                return false;
              }
            }

            if (!familySymbol.IsActive)
            {
                familySymbol.Activate();
                doc.Regenerate();
            }

            transaction.Commit();
            if (filePath != null)
            {
                File.Delete(filePath);
            }
        }
 }

Revit will automatically upgrade the family on load (even if youre in the middle of a transaction). So no worries there.

Also, good to keep the folders neat by removing the file after loading it at the end. It will be held in model anyway.


From here, you can then go on to use the FamilySymbol in your custom code.


Hope that helps some people out.

24 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page