How to export each assembly to a separate PCF file

Martin Schmid Martin Schmid January 8, 2019

1 min read

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.

Get AEC updates in your inbox

By clicking subscribe, I agree to receive the AEC newsletter and acknowledge the Autodesk Privacy Statement.

______
icon-svg-close-thick

Cookie preferences

Your privacy is important to us and so is an optimal experience. To help us customize information and build applications, we collect data about your use of this site.

May we collect and use your data?

Learn more about the Third Party Services we use and our Privacy Statement.

Strictly necessary – required for our site to work and to provide services to you

These cookies allow us to record your preferences or login information, respond to your requests or fulfill items in your shopping cart.

Improve your experience – allows us to show you what is relevant to you

These cookies enable us to provide enhanced functionality and personalization. They may be set by us or by third party providers whose services we use to deliver information and experiences tailored to you. If you do not allow these cookies, some or all of these services may not be available for you.

Customize your advertising – permits us to offer targeted advertising to you

These cookies collect data about you based on your activities and interests in order to show you relevant ads and to track effectiveness. By collecting this data, the ads you see will be more tailored to your interests. If you do not allow these cookies, you will experience less targeted advertising.

icon-svg-close-thick

THIRD PARTY SERVICES

Learn more about the Third-Party Services we use in each category, and how we use the data we collect from you online.

icon-svg-hide-thick

icon-svg-show-thick

Strictly necessary – required for our site to work and to provide services to you

Qualtrics
W
Akamai mPulse
W
Digital River
W
Dynatrace
W
Khoros
W
Launch Darkly
W
New Relic
W
Salesforce Live Agent
W
Wistia
W
Tealium
W
Upsellit
W
CJ Affiliates
W
Commission Factory
W
Google Analytics (Strictly Necessary)
W
Typepad Stats
W
Geo Targetly
W
SpeedCurve
W
Qualified
#

icon-svg-hide-thick

icon-svg-show-thick

Improve your experience – allows us to show you what is relevant to you

Google Optimize
W
ClickTale
W
OneSignal
W
Optimizely
W
Amplitude
W
Snowplow
W
UserVoice
W
Clearbit
#
YouTube
#

icon-svg-hide-thick

icon-svg-show-thick

Customize your advertising – permits us to offer targeted advertising to you

Adobe Analytics
W
Google Analytics (Web Analytics)
W
AdWords
W
Marketo
W
Doubleclick
W
HubSpot
W
Twitter
W
Facebook
W
LinkedIn
W
Yahoo! Japan
W
Naver
W
Quantcast
W
Call Tracking
W
Wunderkind
W
ADC Media
W
AgrantSEM
W
Bidtellect
W
Bing
W
G2Crowd
W
NMPI Display
W
VK
W
Adobe Target
W
Google Analytics (Advertising)
W
Trendkite
W
Hotjar
W
6 Sense
W
Terminus
W
StackAdapt
W
The Trade Desk
W
RollWorks
W

Are you sure you want a less customized experience?

We can access your data only if you select "yes" for the categories on the previous screen. This lets us tailor our marketing so that it's more relevant for you. You can change your settings at any time by visiting our privacy statement

Your experience. Your choice.

We care about your privacy. The data we collect helps us understand how you use our products, what information you might be interested in, and what we can improve to make your engagement with Autodesk more rewarding.

May we collect and use your data to tailor your experience?

Explore the benefits of a customized experience by managing your privacy settings for this site or visit our Privacy Statement to learn more about your options.