Accessing Discipline and Duplicating View Template

I am back again in the land of the living... working, blogging.

I'll jump right in with two recent cases related to disciplines and views:

Accessing View and Project Browser Disciplines

Question: When changing the Revit project browser organization in Revit to 'Discipline', the project browser shows different subfolders depending on the document template.

For example, in a project created from an architectural template, it shows ‘Architectural':

Project browser discipline label Architecture

If the project was created from a structural template, it shows ‘Structural':

Project browser discipline label Structural

Is it possible to retrieve this discipline name programmatically?

Answer: My understanding is that this is for organization. If you choose 'discipline', it shows discipline as a top categorization.

If you change the discipline of a view, say from 'Architecture' to 'Structure' for an elevation, it will show up under 'Structure':

View discipline

This depends on the current setting of the View.Discipline property providing a ViewDiscipline enumeration value.

Response: Yes, View.Discipline returns the discipline for a given view.

The returned discipline label is English text, even in a localised (e.g., Japanese) version of Revit.

I implemented this test code to list the disciplines of all views in the current document:

  FilteredElementCollector collector
    = new FilteredElementCollector( doc )
      .OfClass( typeof( View ) );
 
  string s = "";
 
  foreach( View v in collector )
  {
    if( ( v.ViewType == ViewType.Elevation ||
      v.ViewType == ViewType.FloorPlan ||
      v.ViewType == ViewType.ThreeD ) &&
      v.CanBePrinted )
    {
      if( !s.Contains( v.Discipline.ToString() ) )
      {
        s = s + "\n" + v.Discipline.ToString();
      }
    }
  }
  TaskDialog.Show( "専門分野名", s );

Is there any other more direct way to retrieve the discipline label displayed in the project browser?

Answer: The following code retrieves the string '構造' (Structural) for the current view (Level 1, in this case) in the structural discipline:

Project browser discipline label
  BrowserOrganization bo = BrowserOrganization
    .GetCurrentBrowserOrganizationForViews( doc );
 
  IList<FolderItemInfo> folderItems
    = bo.GetFolderItems( doc.ActiveView.Id );
 
  foreach( FolderItemInfo folder in folderItems )
  {
    string name = folder.Name;
  }

Please note that the BrowserOrganization class was added in Revit 2015.

Duplicating a View Template

Madhu Das raised the issue of duplicating a view template in a comment on the discussion on filtering for views and the IsTemplate predicate:

Question: How can I programmatically duplicate a view template?

The following code returns False if v1 is a view template:

  v1.CanViewBeDuplicated(ViewDuplicateOption.Duplicate)

Answer: We currently have an open wish list item CF-1509 [API wish: create view template -- 09693106] for programmatic creation of a view template.

For duplication of an existing one, you may be able to make use of the copy and paste API.

Look at the numerous copy and paste API usage examples.

Response: Thank you very much for the help.

It is possible by using the Copy and Paste API.

I successfully tried the following code:

  Dim elementIds As New List(Of ElementId)
 
  elementIds.Add(v.Id)
 
  copiedIds = ElementTransformUtils.CopyElements( _
    activeDoc, elementIds, activeDoc, _
    Transform.Identity, Options)
 
  vNew = activeDoc.GetElement(copiedIds(0))
 
  vNew.Name = "New View Template"