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:
- Set a sheet view active with two viewports on it – could be two floor plans on a sheet.
- Change one of the viewports to another type – see below.
- Select both viewports and run sample project – the last selected viewport will be changed to have the same type as the first.
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!
Hello, thanks for the useful infos, do you know if it is possible to change the view FOV from the API? I have been looking into that without success..
Posted by: teocomi | January 18, 2013 at 12:12
Use ChangeTypeId-Method instead:
// To change ViewType:
Viewport vp = Autodesk.Revit.DB.Viewport.Create(...)
ElementId id = getViewTypeId();
vp.ChangeTypeId(id);
// To get your ViewTypeId:
ElementId getViewTypeId()
{
FilteredElementCollector c = new FilteredElementCollector(doc);
c.WhereElementIsElementType();
c.Where( ..someRuleToIdentifyYourViewType.. );
return c.FirstElementId();
}
// To change properties of a ViewType:
Parameter withLine = viewType.get_Parameter(BuiltInParameter.VIEWPORT_ATTR_SHOW_EXTENSION_LINE);
withLine.Set(0); // No Line
withLine.Set(1); // Has Line
Parameter withLabel = viewType.get_Parameter(BuiltInParameter.VIEWPORT_ATTR_SHOW_LABEL);
withLabel .Set(0); // No Label
withLabel .Set(1); // Has Label
Use ElementType.Duplicate to create a ViewType if your ViewType is not already defined in the Revit Project.
Posted by: Alexander Buschmann | January 21, 2013 at 08:35
Dear Alexander,
Thank you for pointing out this obvious and important must-know method.
I added a pointer to your note in the main post to ensure nobody misses it.
Cheers, Jeremy.
Posted by: Jeremy Tammik | January 21, 2013 at 09:09
Dear Matteo,
Well, depends on the exact circumstances. There are several posts that describe how to set up a view and zoom to an object. Have you looked at Steve's presentation mentioned above? Here are some other posts that may help:
http://thebuildingcoder.typepad.com/blog/2009/12/crop-3d-view-to-room.html
http://thebuildingcoder.typepad.com/blog/2011/07/section-view-creation.html
http://thebuildingcoder.typepad.com/blog/2012/06/create-section-view-parallel-to-wall.html
http://thebuildingcoder.typepad.com/blog/2012/08/set-view-section-box-to-match-scope-box.html
Please let us know if that is not what you are after, and also if it is, to ensure we have a useful answer to this very valid question. Thank you!
Cheers, Jeremy.
Posted by: Jeremy Tammik | January 21, 2013 at 09:32
Thanks Jeremy for your nice answer.
Probably I didn't express myself properly and at your links I couldn't find the answer.
http://imgur.com/a/EPnTb
In these two images I just dragged the pink dot of the camera view from the site plan and it affects the distortion of the wall. Is it possible to set this distance/distortion (I was calling it FOV) when I create the perspective view?
Thanks again,
Matteo
Posted by: teocomi | January 22, 2013 at 16:03
Dear Matteo,
Thank you for your appreciation, clarification and helpful sample images.
I searched my archives and found an old comment by Jeff on the same topic:
http://thebuildingcoder.typepad.com/blog/2009/04/dwf-view-definition.html?cid=6a00e553e1689788330133ee54ea4c970b#comment-6a00e553e1689788330133ee54ea4c970b
At the time it was not possible, and we had an open wish list item #145414 [API request: The API to Get and Set the Zoom factor for view is needed] for that functionality.
Looking into that record again, I see that the development team now state that this can be achieved using the UIView ZoomAndCenterRectangle and GetZoomExtents methods.
I hope this solves your issue as well.
Please do let me know how it goes, since I am sure this is of great interest to others also, and would love to publish a snippet of working sample code to set the desired FOV. Thank you!
Cheers, Jeremy.
Posted by: Jeremy Tammik | January 23, 2013 at 06:28
Thanks again,
I'm still working on this when I have time, and so far I found out that apparently the only parameter affecting the "distortion" of the view is the XYZ eyePosition parameter of the ViewOrientation3D. In fact by increasing it of 3.2 times (each of the XYZ coordinates) you get a FOV of around 60.
Then of course Zoom can be adjusted via UIView ZoomAndCenterRectangle and GetZoomExtents methods.
I'll keep posting here as a have new findings and maybe a more "technical" approach ;)
Cheers!
Posted by: teocomi | January 26, 2013 at 12:44
Dear Alexander,
Actually, due to another question that just arose, I added your note about the extension line toggling parameter to a main blog post as well:
http://thebuildingcoder.typepad.com/blog/2013/02/parameters-versus-properties.html#2
It also includes some observations on the interesting topic you bring up on properties versus parameters.
Cheers, Jeremy.
Posted by: Jeremy Tammik | February 01, 2013 at 05:16
Is there a way to change the location of the View title that is used for the Viewport? Also, I could not get Alexander's getViewTypeId method to work. This is what I did:
public static ElementId getViewTypeId(Document doc, string s)
{
ElementId viewPortType = null;
foreach (Element elem in new FilteredElementCollector(doc).WhereElementIsElementType())
{
if (elem.Name == s && !(elem is FamilySymbol))
{
viewPortType = elem.Id;
break;
}
}
return viewPortType;
}
Thanks.
Posted by: Michael | April 26, 2014 at 17:05
Dear Michael,
1. change the location of the View title?
How would you achieve that through the user interface, please?
2. The logic WhereElementIsElementType AND elem.Name == s ANDÂ !(elem is FamilySymbol) will return the first element encountered in the entire Revit database that has the name 's' and is not a family symbol.
It is extremely improbable that such an element will happen to be a view type.
Cheers, Jeremy.
Posted by: Jeremy Tammik | April 27, 2014 at 15:36