Import a single view template from another file

Import a single view template from another file

DanielKP2Z9V
Advocate Advocate
1,682 Views
6 Replies
Message 1 of 7

Import a single view template from another file

DanielKP2Z9V
Advocate
Advocate

Is it possible to import a single view template from a another project file without downloading all the others? The only builtin tool I could find TransferProjectStandars operates on entire categories and not individual types.

0 Likes
1,683 Views
6 Replies
Replies (6)
Message 2 of 7

noah_AvatarMetaverse
Enthusiast
Enthusiast

No, I used to import all the view template in new project then delete all unwanted view template than import only specific view template to my project 

Message 3 of 7

DanielKP2Z9V
Advocate
Advocate

Thanks, I'm writing a script in C# now to export only selected viewtemplates into a new .rvt file, but got stuck.

0 Likes
Message 4 of 7

DanielKP2Z9V
Advocate
Advocate

As per that linked thread above the script is now finished and seems to work fine exporting only the selected view templates to an empty .rvt file (CustomGUI not included - I'll try to open source the full codebase later)

 

 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace scripts
{
    [Transaction(TransactionMode.Manual)]
    public class ExportViewTemplatesToRvt : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Document doc = uiDoc.Document;

            // Get all view templates in the document
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            List<Autodesk.Revit.DB.View> viewTemplates = collector.OfClass(typeof(Autodesk.Revit.DB.View))
                                                        .Cast<Autodesk.Revit.DB.View>()
                                                        .Where(v => v.IsTemplate)
                                                        .ToList();

            List<string> properties = new List<string> { "Title" };

            var selectedViews = CustomGUIs.DataGrid<Autodesk.Revit.DB.View>(viewTemplates, properties);

            if (selectedViews.Count == 0)
                return Result.Cancelled;

            // Prompt user to choose location and file name
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Revit File (*.rvt)|*.rvt";
            saveFileDialog.Title = "Save As";
            saveFileDialog.ShowDialog();

            // Check if the user cancelled the operation
            if (saveFileDialog.FileName == "")
                return Result.Cancelled;

            // Create a new Revit document
            var revitApp = uiApp.Application;
            Document newDoc = revitApp.NewProjectDocument(UnitSystem.Metric);

            // Start a transaction
            using (Transaction transaction = new Transaction(newDoc, "Export View Templates"))
            {
                transaction.Start();

                // Copy the selected elements to the new document
                List<ElementId> copiedIds = new List<ElementId>();
                foreach (Autodesk.Revit.DB.View selectedViewTemplate in selectedViews)
                {
                    CopyPasteOptions copyOptions = new CopyPasteOptions();
                    copyOptions.SetDuplicateTypeNamesHandler(new DuplicateTypesHandler());
                    ElementId copiedId = ElementTransformUtils.CopyElements(doc, new List<ElementId> { selectedViewTemplate.Id }, newDoc, Transform.Identity, copyOptions).FirstOrDefault();
                    copiedIds.Add(copiedId);
                }

                // Commit the transaction
                transaction.Commit();
            }

            // Save the new document as .rvt
            string filePath = saveFileDialog.FileName;
            SaveAsOptions saveAsOptions = new SaveAsOptions { OverwriteExistingFile = true };
            newDoc.SaveAs(filePath, saveAsOptions);
            newDoc.Close(false); // Pass false to indicate that changes should not be saved

            return Result.Succeeded;
        }
    }
}

 

 

 

0 Likes
Message 5 of 7

DanielKP2Z9V
Advocate
Advocate

And for the sake of completeness, the equivalent code to import from that temporary .rvt file (my project browser freezes when I have more than one document opened at the same time in a single revit process, so can't use TransferProjectStandards dialog):

 

 

 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace scripts
{
    [Transaction(TransactionMode.Manual)]
    public class ImportViewTemplatesFromRvt : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Document doc = uiDoc.Document;

            // Prompt user to select the .rvt file containing view templates
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Revit File (*.rvt)|*.rvt";
            openFileDialog.Title = "Select .rvt File";
            openFileDialog.ShowDialog();

            // Check if the user cancelled the operation
            if (openFileDialog.FileName == "")
                return Result.Cancelled;

            // Open the selected .rvt file
            string filePath = openFileDialog.FileName;
            Document sourceDoc = uiApp.Application.OpenDocumentFile(filePath);

            // Get all view templates from the source document
            FilteredElementCollector collector = new FilteredElementCollector(sourceDoc);
            List<Autodesk.Revit.DB.View> viewTemplates = collector.OfClass(typeof(Autodesk.Revit.DB.View))
                                                        .Cast<Autodesk.Revit.DB.View>()
                                                        .Where(v => v.IsTemplate)
                                                        .ToList();
            
            if (viewTemplates.Count == 0)
            {
                TaskDialog.Show("No View Templates", "The selected .rvt file does not contain any view templates.");
                return Result.Failed;
            }

            // Select view templates to import
            List<string> properties = new List<string> { "Title" };
            var selectedViews = CustomGUIs.DataGrid<Autodesk.Revit.DB.View>(viewTemplates, properties);

            if (selectedViews.Count == 0)
                return Result.Cancelled;

            // Start a transaction to import view templates
            using (Transaction transaction = new Transaction(doc, "Import View Templates"))
            {
                transaction.Start();

                // Copy the selected view templates into the active document
                foreach (Autodesk.Revit.DB.View selectedViewTemplate in selectedViews)
                {
                    CopyPasteOptions copyOptions = new CopyPasteOptions();
                    copyOptions.SetDuplicateTypeNamesHandler(new DuplicateTypesHandler());
                    ElementTransformUtils.CopyElements(sourceDoc, new List<ElementId> { selectedViewTemplate.Id }, doc, Transform.Identity, copyOptions);
                }

                // Commit the transaction
                transaction.Commit();
            }

            // Close the source document
            sourceDoc.Close(false); // Pass false to indicate that changes should not be saved

            return Result.Succeeded;
        }
    }
}

 

 

0 Likes
Message 6 of 7

noah_AvatarMetaverse
Enthusiast
Enthusiast

great you can mark this toped as solved, if you already got it works 

0 Likes
Message 7 of 7

Simon_Weel
Advisor
Advisor

TransferSingle does a decent job.

Transfer Template is free but only transfers templates.