How to export each assembly to a separate PCF file

Uncategorized

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.



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.

Try Revit 2023 Free for 30 Days

Related Articles

  • Uncategorized
    “It’s the most wonderful time of the year!” Ok, no, it is not the holiday season, but it sure does feel like it when the new version of Revit is released! Autodesk recently released Revit 2023 and there were quite a few Revit Ideas that have been implemented in this release. It was hard to […]

  • What’s New
    [Authored by: Ian Molloy, Martin Schmid, Brandon White] With Revit 2023, we’re pleased to provide capabilities that span all aspects of the workflow for electrical and mechanical design; from early conceptual design and analysis, to the creation and documentation of design deliverables, along with improvements to support detailed design for construction. These improvements address a […]

  • What’s New
    What’s New in Revit 2023? Better performance, more productivity features seeded from your requests, and smarter ways to work with data, both on the desktop and in the cloud.  It’s that time of year again! The Revit Factory is very excited to announce that Revit 2023 is in release and rolling out globally. Look for […]

Add comment

There are no comments