简体   繁体   English

如何使用Python存储Revit加载项设置?

[英]How to store Revit Add-in Settings with Python?

My Revit Add-in reads at some point a text file, that could be located anywhere. 我的Revit加载项有时会读取一个文本文件,该文件可以位于任何地方。 In my current implementation, the path to the text file is hardcoded. 在我当前的实现中,文本文件的路径是硬编码的。 I'd like to avoid that, so that when I distribute the Add-in to other people, it doesn't simply crash. 我想避免这种情况,以便在将外接程序分发给其他人时,它不会只是崩溃。

Ideally I'd like to give them the ability of specifying their own location for that file on their computer, and that they don't need to re-specify it every time they re-launch the Add-in ! 理想情况下,我想让他们能够在计算机上为该文件指定其自己的位置, 并且他们不必在每次重新启动外接程序时都重新指定该文件的位置! In other words, I'd like to store once and for all this information. 换句话说,我想一劳永逸地存储所有这些信息。 And if you close and re-open Revit, the location is still stored somewhere when you re-use the Addin. 而且,如果您关闭并重新打开Revit,则在您重新使用Addin时,该位置仍存储在某个位置。

This question is actually similar to this one , except that I'd need a solution when developing in Python ( pyRevit ). 这个问题实际上类似于这一个 ,除了在Python(pyRevit)进行开发时,我需要一个解决方案。 Any help? 有什么帮助吗?

I implemented two other ways to store Revit add-in settings in the HoloLens Escape Path Waypoint JSON Exporter : 我实现了另外两种将Revit加载项设置存储在HoloLens Escape Path Waypoint JSON Exporter中的方法

  • Store add-in option settings in XML using the .NET System.Configuration.ApplicationSettingsBase class 使用.NET System.Configuration.ApplicationSettingsBase类将加载项选项设置存储为XML
  • Store add-in option settings in JSON using custom solution and JavaScriptSerializer class 使用自定义解决方案和JavaScriptSerializer类将加载项选项设置存储在JSON中

Both solutions are well suited for what you need. 两种解决方案都非常适合您的需求。

Check them out in the ExportWaypointsJson GitHub repository . ExportWaypointsJson GitHub存储库中检出它们。

if you're developing you addon in pyRevit, then you can use the pyrevit.script module to get the configuration for that script. 如果您正在pyRevit中开发插件,则可以使用pyrevit.script模块获取该脚本的配置。

Ask user for the file location ( pyrevit.forms.save_file helps) and then save the file path in the script configuration. 向用户询问文件位置( pyrevit.forms.save_file帮助),然后在脚本配置中保存文件路径。 pyRevit handles this automatically and saves the information inside its master configuration file at %appdata%/pyRevit pyRevit自动处理此问题,并将信息保存在其主配置文件中的%appdata%/pyRevit

from pyrevit import script
config = script.get_config()
config.filelocation = 'path/to/your/file'
script.save_config()

And then later, read the configuration like this: 然后,再阅读如下配置:

from pyrevit import script
config = script.get_config()
print(config.filelocation)
# or to get the config safely
print(config.get_option('filelocation', None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM