欢迎来到MicroStation Python世界! 通过键入命令增强应用程序的功能,允许用户执行自定义命令。本文将指导您使用Python给MicroStation添加键入(keyin)命令。
支持添加键入命令
让我们深入探讨在MicroStation中创建和注册键入命令。请参阅Python管理器文章以创建和加载Python项目。将您的项目命名为MyReport.py并将其保存到任意文件夹下。在编辑器中打开该项目以编写Python脚本。
构建XML命令表
首先,我们需要创建一个名为MyReport.commands.XML的XML命令表文件。此XML文件将定义我们要添加到MicroStation中的命令。XML文件的格式在“给Addin添加命令”链接中有很好的说明。
下面的XML文件定义了三个命令:ReportLevels、ReportTextStyles和ReportFonts,每个命令都与一个键入字符串相关联。
<?xml version="1.0" encoding="utf-8" ?>
<KeyinTree xmlns="http://www.bentley.com/schemas/1.0/MicroStation/AddIn/KeyinTree.xsd">
<RootKeyinTable ID="root">
<Keyword SubtableRef="MyDgnReport" CommandClass="MacroCommand"
CommandWord="MYREPORT">
<Options Required="true" />
</Keyword>
</RootKeyinTable>
<SubKeyinTables>
<KeyinTable ID="MyDgnReport">
<Keyword CommandWord="LEVELS" />
<Keyword CommandWord="TEXTSTYLES" />
<Keyword CommandWord="FONTS" />
</KeyinTable>
</SubKeyinTables>
<KeyinHandlers>
<KeyinHandler Keyin="MYREPORT LEVELS" Function="ReportLevels" />
<KeyinHandler Keyin="MYREPORT TEXTSTYLES" Function="ReportTextStyles" />
<KeyinHandler Keyin="MYREPORT FONTS" Function="ReportFonts" />
</KeyinHandlers>
</KeyinTree>
在Python中加载命令表
接下来,我们将把命令表加载到Python程序中。MSPython中提供有一个PythonKeyinManager类,其下的LoadCommandTableFromXml函数用来注册命令表。
导入模块
该脚本首先导入几个模块,这些模块提供了与MicroStation交互的函数和类。
from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyMstnPlatform import *
from MSPyDgnView import *
import os
import ctypes
消息框函数
MsgBox函数是一个显示消息框的实用程序。它使用ctypes库调用Windows API来显示消息框。
def MsgBox (title, text, style):
return ctypes.windll.user32.MessageBoxW (0, text, title, style)
报告函数
ReportLevels、ReportTextStyles和ReportFonts函数是占位符(树桩),当前仅显示一个消息框,指示该函数是一个正在进行的工作(WIP)。但这不影响本主题的内容。
def ReportFonts ():
print ("Report Fonts stub...")
MsgBox ('MyReport', 'WIP: This function will report existing Fonts...', 0)
def ReportTextStyles ():
print ("Report Text Styles stub...")
MsgBox ('MyReport', 'WIP: This function will report existing Text Styles...', 0)
def ReportLevels ():
print ("Report Levels stub...")
MsgBox ('MyReport', 'WIP: This function will report existing Levels...', 0)
将以上代码集成在一起:main函数
main函数构造XML命令表的路径,并使用PythonKeyinManager将命令表加载到MicroStation中。WString函数用于将文件路径转换为API所需的适当字符串格式。
下面是完整的脚本:
from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyMstnPlatform import *
from MSPyDgnView import *
import os
import ctypes
# Message box function
def MsgBox (title, text, style):
## Styles:
## 0 : OK
## 1 : OK | Cancel
## 2 : Abort | Retry | Ignore
## 3 : Yes | No | Cancel
## 4 : Yes | No
## 5 : Retry | Cancel
## 6 : Cancel | Try Again | Continue
## To also change icon, add these values to previous number
# 16 Stop-sign icon
# 32 Question-mark icon
# 48 Exclamation-point icon
# 64 Information-sign icon consisting of an 'i' in a circle
return ctypes.windll.user32.MessageBoxW (0, text, title, style)
# Report fonts function
def ReportFonts ():
print ("Report Fonts stub...")
MsgBox ('MyReport', 'WIP: This function will report existing Fonts...', 0)
# Report text styles function
def ReportTextStyles ():
print ("Report Text Styles stub...")
MsgBox ('MyReport', 'WIP: This function will report existing Text Styles...', 0)
# Report levels function
def ReportLevels ():
print ("Report Levels stub...")
MsgBox ('MyReport', 'WIP: This function will report existing Levels...', 0)
# Main function
def main():
keyinXml = os.path.dirname(__file__) + '/MyReport.commands.xml'
PythonKeyinManager.GetManager ().LoadCommandTableFromXml (WString (__file__), WString (keyinXml))
# main
if __name__ == "__main__":
main()
运行/执行项目
从Python管理器对话框中选择项目MyReport.py并运行/执行Python脚本。
在MicroStation键入窗口中,键入MyReport Levels,将显示以下消息框。
扩展上述代码以添加报告功能。请参阅交付的示例和文档。
祝您编码愉快!