twitter youtube facebook linkedin email
Connect with:
MEP

How to export each assembly to a separate PCF file

Martin Schmid
January 8, 2019

A comment was recently posted to my previous blog post on Exporting PCF Files from Revit 2018 – Part 2 (Assemblies), asking:

Thanks for the awesome code Martin! Is there a way to make the file name match the Assembly Name?

I’ve provided a revised example below.  The example has been modified to export each assembly to a separate PCF file, naming the file based on the assembly name.  Note that with this modification any element that is NOT in an assembly will NOT be exported to a PCF file… the code for this is shown below.

           public void ExportSelectoinToPCF()
           {
                 // get the selected element ids
                 List<ElementId> elementIdsToPCF = new List<ElementId>();
                 List<ElementId> selectedElementIds =
                 this.ActiveUIDocument.Selection.GetElementIds().ToList();
                 elementIdsToPCF.AddRange(selectedElementIds);
                 
                 // output PCF for each assembly
                 OutputAssemblyPCFs(
                      elementIdsToPCF,
                       this.ActiveUIDocument.Document);
           }  // ExportSelectoinToPCF
      
           
           public void OutputAssemblyPCFs(
                 List<ElementId> selectedElementIds,
                 Document theDocument)
           {
                 foreach (ElementId id in selectedElementIds)
                 {
                      // check if this is an assembly
                      AssemblyInstance assemblyInst =
                            theDocument.GetElement(id) as AssemblyInstance;
                      if (assemblyInst == null)
                            continue;
                      
                      // get the items IDs the assembly
                      List<ElementId> lstElemIds = new List<ElementId>();
                      lstElemIds.AddRange(assemblyInst.GetMemberIds());
                      
                      // get the name of the assembly
                      string assemblyName = assemblyInst.AssemblyTypeName;
                      string fileName = "C:\\temp\\" + assemblyName + ".pcf";
                      
                      // create the PCF file using the assembly name
                 Autodesk.Revit.DB.Fabrication.FabricationUtils.ExportToPCF(
                            this.ActiveUIDocument.Document,
                            lstElemIds,
                            fileName);            
                 } // foreach
           } // OutputAssemblyPCFs
           
           
           // Get the element ids in the selected assemblies
           public  List<ElementId> GetElementIdsInAssembly(
                 List<ElementId> selectedElementIds,
                 Document theDocument)
           {
                 List<ElementId> lstElemIds = new List<ElementId>();
                 foreach (ElementId id in selectedElementIds)
                 {
                      AssemblyInstance assemblyInst =
                            theDocument.GetElement(id) as AssemblyInstance;
                      if (assemblyInst == null)
                            continue;
                      
                      lstElemIds.AddRange(assemblyInst.GetMemberIds());
                 }  // foreach
                 
                 return lstElemIds;
           } // GetElementIdsInAssembly

The same comment asked:

Know of any way to find/replace text in the same export so we don’t have to in Notepad++ etc?   For our .pcf import to work correctly I need to replace the SCH40, SCH80, STD pipe weights with other text.

There are a wide variety of ways to programmatically parse text written to a file, but doing so is beyond the scope of this example.

What is BIM (Building Information Modeling)?

Free Form Rebar - Aligned Distribution

Featured Links

Martin Schmid

Martin Schmid joined Autodesk in 2005 as a project consultant for MEP customers, both for Revit MEP and AutoCAD MEP. Martin holds a BS in Architectural Engineering, and MS in Engineering from Kansas State University, and a Masters in Management of Technology from the University of Texas at San Antonio. He worked as an electrical designer, engineering coordinator, and application developer before joining Autodesk. Martin has co-authored books for Revit and AutoCAD MEP, and is a licensed engineer in the state of New Hampshire. Martin worked in the product globalization and customer success teams prior to joining the product management team, where he now guides direction related to the extended MEP product portfolio, including MEP functionality within Revit, CADmep, ESTmep, CAMduct, and AutoCAD Plant 3D. In his spare time, Martin enjoys reading, travelling, and attending his kids’ activities.

0 Comments