Search code examples
c#revit-apirevitpyrevitbim

Revit API - C# - How to set View Title on Viewport


I'm trying to set a view title to show up on a Revit project using the Revit API however I can't figure out how to access it.

I'm able to place a viewport on the sheet and load the view title family into the project but I'm not able to assign the loaded view title to the viewport. Has anyone had any luck with this?

Here are some pics of what I'm trying to do:

1) The view is placed on the sheet. not a problem View is placed on the sheet. not a problem

2) Edit type of view and change view title use "View Title w sheet" enter image description here

3) Change show extension line to "yes" enter image description here

4) make it look like this. enter image description here

Here are some sources I've been looking at: https://thebuildingcoder.typepad.com/blog/2013/01/changing-viewport-type.html <-- shows how to change viewport type to one already created.

https://forums.autodesk.com/t5/revit-api-forum/move-title-of-a-viewport/td-p/5598602 <-- shows how to move the view title

**************UPDATE**********************

I thought I got it working flawlessly, I didn't.

The first time I click the button, everything works except the "Title" parameter is not set. It still shows <none>.

Clicking the button a second time sends me an InternalDefinition Error when the viewport is created.

If I set the Title manually to the view title family loaded, apply changes, reset it back to <none>, Apply changes, AND THEN hit the button. It works. it's almost like the family isn't recognized as a legitimate view title option until I apply the changes.

here is my code:

// Get an available title block from document
            FilteredElementCollector colTitleBlocks = new FilteredElementCollector(doc);
            colTitleBlocks.OfClass(typeof(FamilySymbol));
            colTitleBlocks.OfCategory(BuiltInCategory.OST_TitleBlocks);


            // Get available viewPlans block from document
            FilteredElementCollector collectorViewPlans = new FilteredElementCollector(doc);
            collectorViewPlans.OfClass(typeof(ViewPlan));
            List<ViewPlan> viewPlansList = collectorViewPlans.Cast<ViewPlan>().ToList();

            // grab first as example
            ViewPlan duplicatedPlan = viewPlansList[0];


            // grab viewport labels
            FilteredElementCollector colViewTitles = new FilteredElementCollector(doc);
            colViewTitles.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_ViewportLabel);

            String colViewTitleFamilyName = null;
            Element colViewTitleFamilyName2 = null;
            ElementId viewTitleIdCommand = null;

using (Transaction t = new Transaction(doc))
            {

                //try
                //{

                t.Start("Create a new ViewSheet");

                // check if any title blocks are loaded. if not, load familly
                if (colTitleBlocks != null)
                {
                    LoadTitleBlocks loadFamily = new LoadTitleBlocks();
                    loadFamily.loadFamily(commandData);
                }
                FamilySymbol firstSheet = colTitleBlocks.FirstElement() as FamilySymbol;


                // Create a sheet view Block
                ViewSheet viewSheet = ViewSheet.Create(doc, firstSheet.Id);
                if (viewSheet == null)
                {
                    throw new Exception("Failed to create new ViewSheet.");
                }
                // End of Create a sheet view Block


                // begin duplication
                ElementId duplicatedPlan2Copy = duplicatedPlan.Duplicate(ViewDuplicateOption.Duplicate);

                // Add passed in view onto the center of the sheet
                UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2,
                                        (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);


                try
                {
                    // Create Viewport
                    newViewPort = Viewport.Create(doc, viewSheet.Id, duplicatedPlan2Copy, new XYZ(location.U, location.V, 0));
                }
                catch (Exception)
                {

                    throw;
                }

                newViewPort.LookupParameter("View Scale").Set(24);
                bool newViewportTypeParameterShowLabel = doc.GetElement(newViewPort.GetTypeId()).get_Parameter(BuiltInParameter.VIEWPORT_ATTR_SHOW_LABEL).Set(1);


if (colViewTitles.Count() > 0)
                {
                    viewTitleIdCommand = colViewTitles.FirstElementId();
                    colViewTitleFamilyName = colViewTitles.FirstElement().ToString();
                    Debug.Print("Count greater than 0. colViewTitleFamilyName: " + colViewTitleFamilyName + " Id: " + viewTitleIdCommand);

                }
                else if (colViewTitles.Count() == 0)
                {
                    LoadViewTitle loadViewTitle = new LoadViewTitle();
                    loadViewTitle.loadFamily(commandData);
                    viewTitleIdCommand = loadViewTitle.viewTitleId;
                    colViewTitleFamilyName = doc.GetElement(loadViewTitle.viewTitleId).Name;
                    colViewTitleFamilyName2 = doc.GetElement(loadViewTitle.viewTitleId) as Family;
                    //Family colViewTitleFamilyName3 = colViewTitleFamilyName2.
                    Debug.Print("Count is 0. colViewTitleFamilyName: " + colViewTitleFamilyName + " Id: " + viewTitleIdCommand);
                }
                doc.GetElement(newViewPort.GetTypeId()).get_Parameter(BuiltInParameter.VIEWPORT_ATTR_LABEL_TAG).Set(viewTitleIdCommand);



Solution

  • I was able to get what I wanted by using these 2 lines of code:

    however, I have to run the button twice. I'm still figuring out how to how to only have to run it once.

    bool newViewportTypeParameterShowLabel = doc.GetElement(newViewPortTypeId).get_Parameter(BuiltInParameter.VIEWPORT_ATTR_SHOW_LABEL).Set(1);
    
    ****Solved*****
    I needed to use a filtered element collector to find the elementId of my TitleView family instead of using the elementId from my `loadFamily` class. A peculiar error. 
    
    
    bool elementType = doc.GetElement(newViewPortTypeId).get_Parameter(BuiltInParameter.VIEWPORT_ATTR_LABEL_TAG).Set(viewTitleIdCommand);