如何检测 uiview 是激活的视口

How can I detect uiview is an activated viewport

我需要检测 Uiview 是标准打开视图还是 sheet 上的激活视口。查询 uiview 的视图 Id returns 激活的视口视图的 Id。我没有找到直接的方法来检测 uiview 实际上是一个具有激活视口的 sheet。

我已经出于另一个目的在视图激活事件中跟踪打开的视图。因此,我考虑将视图 ID 与 uiview 哈希码一起存储,以便稍后在成为激活视图之前检查它确实是一个 sheet 视图。不幸的是,我认为与标准使用相反,uiview 哈希码并不稳定。来自 uiview object return 不同值的多个哈希码请求。

有没有人有办法检测这种情况?我仍然需要能够使用 uiview 上的方法。所以任何帮助找到实际的 child windows 我想与 uiview object 相关。当视图被激活时,视图仍然在标题中显示 "Sheet: ..."。

您可以使用 ViewSheet GetAllViewports 方法来确定给定 sheet 上的所有视口。使用它,您可以将 bi-directional 字典查找系统放在一起,将任何 sheet 映射到它托管的所有视口,反之亦然。那应该有助于解决您的任务。以下是一些示例用法:

http://thebuildingcoder.typepad.com/blog/2014/04/determining-the-size-and-location-of-viewports-on-a-sheet.html

            TaskDialog mainDialog = new TaskDialog("Hello, viewport check!");
            mainDialog.MainInstruction = "Hello, viewport check!";
            mainDialog.MainContent = 
                    "Sadly Revit API doesn't automatically know if the user is in an active viewport. " 
                    + "Please click 'Yes' if your are, or 'No' if your not.";

            mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
                     "Yes, I am in an active viewport on a sheet.");
            mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, 
                            "No, I am just in an ordinary view.");

            mainDialog.CommonButtons = TaskDialogCommonButtons.Close;
            mainDialog.DefaultButton = TaskDialogResult.Close;


            TaskDialogResult tResult = mainDialog.Show();

            bool YesOrNo = true;

            if (TaskDialogResult.CommandLink1 == tResult)
            {
                    YesOrNo = true;

            }

            else if (TaskDialogResult.CommandLink2 == tResult)
            {
                    YesOrNo = false;
            }
                           else{
            return;
               }    

我来晚了 - 但另一种感知用户是否在视口中的方法是调查 Process.MainWindow 标题。像这样的东西(在 RevitPythonShell 中):

import threading, clr
from System.Diagnostics import Process

# need winform libraries for feedback form only
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form, Label

app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
ui = __revit__.ActiveUIDocument

def lookAtWindow(activeView):

    # Looking for one of three conditions:
    # 1. User is on a sheet (ActiveView will be DrawingSheet)
    # 2. User is in an active ViewPort on a sheet (ActiveView will NOT be be DrawingSheet, but MainWindowTitle will contain " - [Sheet: " string)
    # 3. User is on a View (neither of the previous two conditions)

    result = False 

    if str(activeView.ViewType) == 'DrawingSheet':            
        result = 'Youre on a sheet'
    else:
        processes = list(Process.GetProcesses())
        for process in processes:
            window = process.MainWindowTitle
            if window and 'Autodesk Revit '+app.VersionName[-4:] in window and ' - [Sheet: ' in window and ' - '+doc.Title+']' in window:
                result = 'I reckon youre in a Viewport'
    if not result:
        result = 'so you must be in a '+str(activeView.ViewType) 

    form = Form()
    form.Width = 300
    form.Height = 100
    label = Label()
    label.Width = 280
    label.Height = 70
    label.Text = result
    label.Parent = form 
    form.ShowDialog()

# need to close RevitPythonShell console before checking MainWindowTitle, so run on timer
threading.Timer(1, lookAtWindow, [ui.ActiveView]).start()

__window__.Close()