C# - duplicate view types

In Revit I can duplicate my viewtype (see image)

image

Here I’ve duplicated 3D View and called it, “Here is a lovely new type”.

How do I do this in C#?

I’ve created some views, but I want them as a new 3D view type.

                trans.Start();

                // Retrieve all worksets in the model
                FilteredWorksetCollector worksetCollector = new FilteredWorksetCollector(doc);
                worksetCollector.OfKind(WorksetKind.UserWorkset); // get worksets

                foreach (Workset workset in worksetCollector)
                {
                    // Viewname as per the workset name
                    string viewName = $"3D Views (**DO NOT EDIT**) {workset.Name}";
                    
                    // Check if a view with this name already exists to avoid duplicates
                    if (!ViewExists(doc, viewName))
                    {
                        // Create a 3D view for this workset
                        View3D newView = View3D.CreateIsometric(doc, Find3DViewFamilyType(doc).Id);
                        newView.Name = viewName;

                       
                       
                    }
                }

                trans.Commit();

Each View is an Element.
You need to know the ElementId of your new 3D view type and use the following method:

something like this, to change the viewType after creation:

newView.ChangeTypeId(NewViewType.Id)

or assigning it upon creation

View3D newView = View3D.CreateIsometric(doc, NewViewType.Id)
1 Like

Guessing this is a custom definition elsewhere in your class? Not sure I have seen this before.

Define the base view family type using that, ideally outside of the loop so you can just keep it in memory.

Then in your loop duplicate the family type to ensure a new view family type for each new view. This method should get that done for you: Duplicate Method

After duplicating the view type, set the name as desired (no idea what you’re looking to use there but this will keep you from having ‘duplicate duplicate duplicate’ names).
Now use the new view type in creating the new view, being sure to pull the id as the method above will return the element itself and the method requires an ElementId.

2 Likes

here an example


        public static ViewFamilyType Create3DViewType(string nameOfView)
        {
            Autodesk.Revit.DB.ViewFamilyType view3DFamilyType = null;
            Autodesk.Revit.DB.Document rvt_doc = DocumentManager.Instance.CurrentDBDocument;
            ViewFamilyType view3DType = new FilteredElementCollector(rvt_doc).OfClass(typeof(ViewFamilyType))
                                .Select(x => (ViewFamilyType)x)
                                .FirstOrDefault(x => x.ViewFamily == ViewFamily.ThreeDimensional && x.DefaultTemplateId == ElementId.InvalidElementId);

            TransactionManager.Instance.EnsureInTransaction( rvt_doc );
            try
            {
                view3DFamilyType = (ViewFamilyType)view3DType.Duplicate(nameOfView);
            }
            catch (Exception ex)
            {
                string timestamp = DateTime.Now.ToFileTime().ToString();
                view3DFamilyType = (ViewFamilyType)view3DType.Duplicate(nameOfView + timestamp.Substring(timestamp.Length - 3));
            }
            TransactionManager.Instance.TransactionTaskDone();
            return view3DFamilyType;
        }
2 Likes

Yeah… It looks like this:

        private ViewFamilyType Find3DViewFamilyType(Document doc)
        {
            var collector = new FilteredElementCollector(doc);
            var viewFamilyTypes = collector.OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>();
            return viewFamilyTypes.FirstOrDefault(x => x.ViewFamily == ViewFamily.ThreeDimensional);
        }

image

Finally :partying_face: :mirror_ball: :dancing_women: :tada:

Now I have to work out how to make views using that viewtype :smiley:

            // Start transaction
            using (Transaction trans = new Transaction(doc, "Create Custom 3D View Type"))
            {
                trans.Start();

                // Get 3D view types
                var viewFamilyTypes = new FilteredElementCollector(doc)
                    .OfClass(typeof(ViewFamilyType))
                    .Cast<ViewFamilyType>()
                    .Where(x => x.ViewFamily == ViewFamily.ThreeDimensional);

                // Find the first 3D ViewFamilyType to use as a template for the new 3D view type
                var viewFamilyType = viewFamilyTypes.FirstOrDefault();
                if (viewFamilyType != null)
                {
                    // Duplicate the first 3D view type
                    viewFamilyType = (ViewFamilyType)viewFamilyType.Duplicate("TEST");

                }

                trans.Commit();
            }
1 Like