pyRevit WPF非模态问题

2024-02-03

所以我刚刚开始涉足 pyRevit 中的 WPF。我尝试像这样实现pyrevit.forms.WPFWindow 类:

# -*- coding: UTF-8 -*-
"""
Third-Party software credits:
pyRevit: repository at https://github.com/eirannejad/pyRevit
"""

import System, clr, json, sys
clr.AddReference("System.Windows.Forms")
clr.AddReference('IronPython.Wpf')
import wpf
from Autodesk.Revit.DB import *
from pyrevit import revit, script, forms

class FactorySettings(forms.WPFWindow):

    def __init__(self):
        forms.WPFWindow.__init__(self, script.get_bundle_file('settings.xaml'))

    def something_click(self, A, B):
        plantings = FilteredElementCollector(revit.doc) \
            .WhereElementIsElementType() \
            .OfCategory(BuiltInCategory.OST_Planting)

ui = FactorySettings()
ui.show()

这是我的 xaml:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d"
        ShowInTaskbar="False" ResizeMode="CanResize"
        WindowStartupLocation="CenterScreen"
        HorizontalContentAlignment="Center"
        Title="Set worksets" Width="300" SizeToContent="Height"
        >
    <StackPanel Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Button Content="Do something" Name="something" Click="something_click"/>
    </StackPanel>

</Window>

这将打开一个非模式窗口,因为def show(self, modal=False):类函数。 对我来说问题是即使我的点击功能只调用FilteredElementCollector对象,Revit 崩溃。如果我做ui.show(modal=True)它会起作用,但我无法在 Revit UI 中执行任何操作。 我真正想要的是这样做:

def something_click(self, A, B):
    if self.PHfamSymbol != None:
         with forms.WarningBar(title='Place an instance of the placeholder object.'):
            revit.uidoc.PromptForFamilyInstancePlacement(self.PHfamSymbol)

这是行不通的,因为焦点仍然在 UI 上。 我试过这个:

def something_click(self, A, B):
    if self.PHfamSymbol != None:
        self.Close()
         with forms.WarningBar(title='Place an instance of the placeholder object.'):
            try:
                revit.uidoc.PromptForFamilyInstancePlacement(self.PHfamSymbol)
            except:
                pass

这可行,但完成后我需要创建一个新的 UI 实例。 pyRevit 是否有可能拥有非模态 UI?


你需要使用一个外部事件 https://www.revitapidocs.com/2020/05089477-4612-35b2-81a2-89c4f44370ea.htm在非模态窗口中与 Revit 进行交互。你有很多样本pyRevitMEP https://github.com/CyrilWaechter/pyRevitMEP。我制造了一个可重用的类在这里 https://github.com/CyrilWaechter/pyRevitMEP/blob/master/lib/pyrevitmep/event.py。我在许多 pyRevitMEP 脚本中使用它,例如元素三维旋转 https://github.com/CyrilWaechter/pyRevitMEP/tree/master/pyRevitMEP.tab/Modify.panel/Element3DRotation.pushbutton。你也可以看看我的旧博客[Revit API] pyRevit 中的简单无模式表单(外部事件处理程序) https://pythoncvc.net/?p=247帖子引用了更多资源并有更多评论。

Sample

script

"""
Copyright (c) 2017 Cyril Waechter
Python scripts for Autodesk Revit

This file is part of pypevitmep repository at https://github.com/CyrilWaechter/pypevitmep

pypevitmep is an extension for pyRevit. It contain free set of scripts for Autodesk Revit:
you can redistribute it and/or modify it under the terms of the GNU General Public License
version 3, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

See this link for a copy of the GNU General Public License protecting this package.
https://github.com/CyrilWaechter/pypevitmep/blob/master/LICENSE
"""
from Autodesk.Revit.UI import IExternalEventHandler, ExternalEvent
from Autodesk.Revit.DB import Transaction
from Autodesk.Revit.Exceptions import InvalidOperationException
import rpw
from pyrevit.forms import WPFWindow
doc = rpw.revit.doc
uidoc = rpw.revit.uidoc

__doc__ = "A simple modeless form sample"
__title__ = "Modeless Form"
__author__ = "Cyril Waechter"
__persistentengine__ = True

# Simple function we want to run
def delete_elements():
    with rpw.db.Transaction("Delete selection"):
        for elid in uidoc.Selection.GetElementIds():
            doc.Delete(elid)

# Create a subclass of IExternalEventHandler
class SimpleEventHandler(IExternalEventHandler):
    """
    Simple IExternalEventHandler sample
    """

    # __init__ is used to make function from outside of the class to be executed by the handler. \
    # Instructions could be simply written under Execute method only
    def __init__(self, do_this):
        self.do_this = do_this

    # Execute method run in Revit API environment.
    def Execute(self, uiapp):
        try:
            self.do_this()
        except InvalidOperationException:
            # If you don't catch this exeption Revit may crash.
            print "InvalidOperationException catched"

    def GetName(self):
        return "simple function executed by an IExternalEventHandler in a Form"


# Now we need to make an instance of this handler. Moreover, it shows that the same class could be used to for
# different functions using different handler class instances
simple_event_handler = SimpleEventHandler(delete_elements)
# We now need to create the ExternalEvent
ext_event = ExternalEvent.Create(simple_event_handler)

# A simple WPF form used to call the ExternalEvent
class ModelessForm(WPFWindow):
    """
    Simple modeless form sample
    """

    def __init__(self, xaml_file_name):
        WPFWindow.__init__(self, xaml_file_name)
        self.simple_text.Text = "Hello World"
        self.Show()

    def delete_click(self, sender, e):
        # This Raise() method launch a signal to Revit to tell him you want to do something in the API context
        ext_event.Raise()

# Let's launch our beautiful and useful form !
modeless_form = ModelessForm("ModelessForm.xaml")

xaml文件

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Delete things:" Height="150" Width="300" ShowInTaskbar="False" Topmost="True"
        WindowStartupLocation="CenterScreen" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalContentAlignment="Center">
    <StackPanel Margin="20" HorizontalAlignment="Stretch">
        <TextBlock x:Name="simple_text" Text="" Grid.Column="0" HorizontalAlignment="Center" FontWeight="Bold"/>
        <Button Content="Delete selected elements" Height="30" Margin="10,10" Click="delete_click"/>
    </StackPanel>
</Window>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pyRevit WPF非模态问题 的相关文章

  • 在 C++/CX 中解析 JSON ISO8601 日期

    我有一个来自 JSON 2012 08 01T15 42 06Z 的日期字符串 并且想要在 Windows 运行时中解析它 据我所知 只有COle日期时间可以处理这个问题 当我取出 T 和 Z 字符时 我只能让它正确解析字符串 但这增加了一
  • Pycharm - 在远程解释器中配置 PYTHONPATH

    我在 Windows 上安装了 PyCharm 2 7 3 并且正在尝试在 Linux 计算机上远程开发应用程序 到目前为止 我可以运行简单的程序 但是我正在尝试设置我的 PYTHONPATH 并且 PyCharm 似乎特别忽略了此配置 在
  • 获取 Windows Phone 上 ScrollViewer 的滚动事件

    问题 获取 Windows Phone 上 ScrollViewer 的滚动事件 我有一个像这样的滚动查看器
  • 可以将 crossorigin 属性添加到 Angular cli 生成的脚本标签中吗?

    是否可以将 crossorigin 属性添加到 Angular cli 生成的脚本标签中 运行我的角度应用程序时 脚本标签被添加到我的index html的末尾 是否可以配置 angular cli 以便当这些标签包含在构建 index h
  • 仅从类和接口静态导入

    我的代码在 Eclipse 中编译得很好 但是当我尝试从命令行编译 通过我们基于 ruby 的构建系统 时 我收到以下错误消息 static import only from classes and interfaces 建议不允许静态导入
  • 如何从 WAMP 恢复 MySQL 数据库?

    上个月我不得不重新安装 Windows 并且我已经将WAMP http en wikipedia org wiki WAMP文件夹到另一个分区 现在我再次安装了 WAMP 但我需要旧数据库 如何从安全分区上保存的 WAMP 文件夹中获取旧的
  • 系统托盘应用程序如何在其他平台上完成?

    Windows 有一个 系统托盘 其中包含时钟和始终运行的服务 例如 MSN Steam 等 我想开发一个 wxPython 托盘应用程序 但我想知道它移植到其他平台的效果如何 每个平台上的托盘等效项是什么 以及支持 Windows OSX

随机推荐

  • CGAffineTransformMakeScale 动画不起作用

    我有一个视图控制器 其中有一个视图 在删除它之前 我使用 UIView 动画将其缩小到 0 我的驳回它的代码是 UIView animateWithDuration dismissAnimationDuration delay 0 0 op
  • 如何将 GameLift 与 Unity3d 集成作为游戏客户端

    我正在尝试使用 Unity3d 游戏作为 GameList 客户端 根据GameLift 论坛 https gamedev amazon com forums questions 13771 create client in unity h
  • 如何在绘图中使用多个组,但仅使用定义数量的图例组

    假设我有多个不同细胞的时间序列 我可以根据它们是否接受治疗来分割它们 我如何绘制所有单独的时间序列 不平均 但根据绘图中的治疗对它们进行分组 它与 ggplot 完美配合 我知道我可以从那里使用 ggplotly 但有完整的情节方式吗 以下
  • python 组合范围和数字列表

    range 5 15 1 1 5 6 10 10 10 11 17 28 range 6 24 4 10 10 10 15 16 18 20 24 30 range 7 41 9 18 19 23 23 26 28 40 42 44 ran
  • 获取OpenCV当前的FPS

    我正在编写一个 OpenCV 应用程序 FPS 非常重要 如何计算主循环的处理时间以获得当前和平均 FPS 这样 我就可以知道我的应用程序运行速度有多快 顺便说一句 我在 SSD 上使用 imread 所以处理器是这 里的瓶颈 你可以做这样
  • 单击锚标记时,将 HTML 文本输入的 readonly 属性设置为 false

    My HTML div class profileForm fieldset fieldset div
  • 网关未出现在 jhipster 注册表中

    我已经为网关应用程序创建了一个 docker 映像 但是当我运行命令时 docker compose up只有微服务和注册表是UP的 但网关甚至没有出现在实例中 22 08 25 10 57 23 661 ERROR 1 restarted
  • 强制完全重绘 Jpanel Java2D

    我的问题是 我需要制作一个不断更新的 GUI 因为我从数据库获取可以更改的值 并且在图形区域中遇到了一些问题 我使用 Graphics2D 中的 Drawline 和 Drawstring 打印在数据库中找到的值 这些字符串和线条移动并更改
  • Google脚本DriveApp.getFolders().hasNext()错误

    我想在驱动器中创建一个目录 如果该目录尚不存在 function CreateDirectory var folderName Example var Directory var fi DriveApp getFoldersByName f
  • WEKA 生成的模型似乎无法预测给定属性索引的类别和分布

    Overview 我正在使用 WEKA API 3 7 10 开发者版本 来使用我预制的 model files 我制作了 25 个模型 五种算法的五个结果变量 J48决策树 http weka sourceforge net doc de
  • Magento:(大多数)国家/地区在结账时扣除税费 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我在一家英国时装店工作 客户有一个特
  • 用图像替换 d3.js 符号

    参考这个fiddle example http jsfiddle net andycooper a7as6 我需要用图像替换符号 或者可能首先用单个图像替换 例如 此图像 https github com favicon ico 我在代码中
  • Haskell——如何在同一个文件中使用多个模块?

    抱歉 这是一个愚蠢的问题 但我无法弄清楚如何将多个模块放在同一个文件中 假设文件名为A hs 如果我把模块B首先 即 module B where module A where 它抱怨说它期望A当我运行 ghci A 时 它不是顶级的 所以
  • 通过 Docker 主机名在两个微服务之间进行通信

    现在如何运作 微服务 X 使用静态 ip 向微服务 Y 发出 REST API 请求 http ip address port doSomething 问题 问题是我不能长期保证静态ip 我不想通过使用 docker 主机名来解决这个问题
  • 复制 Xcode 4 项目

    基本上我想为我的 Xcode 项目和所有文件制作一个独立的副本 我怎样才能做到这一点 我正在研究图形框架 我想为每个框架使用相同的 UI 借调 zoul https stackoverflow com users 17279 zoul的评论
  • GWT CellTable 列调整大小/排序

    有没有人找到一种方法使 GWT CellTable 允许用户调整列大小 我们正在放弃旧的 gwt incubator 小部件 因为它们似乎与 GWT 2 1 存在一些兼容性问题 并且仍然需要以前具有的此功能 另外 如果我们能够像孵化器那样进
  • 我可以禁用对已弃用的方法和类的 CheckStyle 投诉吗?

    我正在维护一个已弃用某些公共静态字段的 API CheckStyle 大声抱怨这些 但我宁愿让它完全忽略它们 因为我已经通过将字段标记为已弃用来处理问题 具体来说 该库具有用于枚举的常量 公共静态最终 但它们没有标记为最终的 CheckSt
  • 如何将 R 脚本加载到 JRI 并从 Java 执行?

    我正在使用 JRI 从 Java 执行 R 我看到 JRI 使用eval 方法来执行R命令 我有一个用于执行的 R 脚本 如何在 JRI 中加载此脚本并执行它 您可以使用 R 命令运行整个脚本source
  • jQuery 验证 - 相同的规则取决于值

    我在输入字段验证中获取 2 个范围值的语法时遇到问题 我的表单有 2 个选择字段和 1 个文本输入字段 If select1 1 and select2 A 我希望文本字段上的范围值是1 to 120 If select1 1 and se
  • pyRevit WPF非模态问题

    所以我刚刚开始涉足 pyRevit 中的 WPF 我尝试像这样实现pyrevit forms WPFWindow 类 coding UTF 8 Third Party software credits pyRevit repository