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();
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.
Now I have to work out how to make views using that viewtype
// 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();
}