Here is a question on setting the type of an element by using the built-in ELEM_FAMILY_AND_TYPE_PARAM parameter value, explored by Bettina Zimmermann of NTI Cadcenter A/S:
Question: I'm inserting viewports on sheets and I'd like to change the viewport type to my own defined type.
How can I this programmatically?
By default, the API call creates a new viewport with type "Title w Line". I'd like to change that to e.g. my own type "Test".
Here is my own self-defined viewport type "Test":
I created it manually by pressing Duplicate.
The API method to create viewports does not support any viewport type parameter:
Autodesk.Revit.DB.Viewport.Create( Document, viewSheet.Id, View.Id, zero )
I hope the viewport type can be changed in some other way.
Answer: When dealing with views and viewports, one of the first places to take a look is Steve Mycynek's AU class CP3133 Using the Revit Schedule and View APIs, which demonstrates just about everything you can do with the Revit View API.
Further, I would suggest that you explore the elements of interest in depth using RevitLookup.
The Element.GetTypeId method may provide read access to the data you seek. Unfortunately, of course, it is read-only. Maybe you can find some parameter that also provides access to this data?
Response: I did indeed find a parameter, well hidden in an unexpected location.
My first thought when searching for it is that the viewport is a system family, just like a wall, and walls allow you to change their type by setting wall.WallType = newWallType.
Walls also expect a type argument when a new wall is created:
DB.Wall.Create( Document, Line, WallType.Id, Level.Id, 11, 0, False, IsStructural );
However, as said, the Viewport Create method does not take any type argument:
Autodesk.Revit.DB.Viewport.Create( Document, viewSheet.Id, View.Id, zero );
In spite of this, the parameter I found that I can use for this is BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM.
I created a VB.NET sample add-in ChangeViewPortType to demonstrate its use. Here is how to use it:
Here is a sheet with a viewport.
The default type of a newly created viewport is 'Title w Line'; I made a new type called 'Test' that has no 'Title' and 'Extension Line'. 'Title' and 'Extension Line' is the circle with a line and some text:
I made the type by clicking 'Edit Type' and duplicate.
Many thanks to Bettina for her exploration and thorough documentation!
Addendum: As pointed out below by Alexander Buschmann, the Revit API also provides the more user-friendly Element.ChangeTypeId method to directly change the type of any element having a type, with no need to go through the parameter to access it. Thanks, Alexander, for adding that!