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.