Revit API - C# - 如何在视口上设置视图标题

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

我正在尝试使用 Revit API 设置视图标题以显示在 Revit 项目中,但我不知道如何访问它。

我可以在 sheet 上放置一个视口并将视图标题系列加载到项目中,但我无法将加载的视图标题分配给视口。有人对此有好运吗?

这是我正在尝试做的一些照片:

1) 视图放在sheet上。没问题

2) 编辑视图类型并更改视图标题使用 "View Title w sheet"

3) 将显示延长线更改为 "yes"

4) 让它看起来像这样。

以下是我一直在查看的一些资源: https://thebuildingcoder.typepad.com/blog/2013/01/changing-viewport-type.html <-- 显示如何将视口类型更改为已创建的类型。

https://forums.autodesk.com/t5/revit-api-forum/move-title-of-a-viewport/td-p/5598602 <-- 显示如何移动视图标题

**************更新************************

我以为我可以完美地工作,但我没有。

第一次单击按钮时,除了未设置 "Title" 参数外,一切正常。它仍然显示 <none>

创建视口时,第二次单击该按钮会向我发送内部定义错误。

如果我手动将 Title 设置为加载的视图标题系列,应用更改,将其重置回 <none>,应用更改,然后点击按钮。有用。在我应用更改之前,这几乎就像家庭不被识别为合法的视图标题选项一样。

这是我的代码:

// 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);


这是最近关于 Title Block Data Access 的解释,应该有所帮助。

我可以通过使用这两行代码得到我想要的东西:

但是,我必须 运行 按钮两次。我仍在弄清楚如何只需要 运行 一次。

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);