欢迎来到MicroStation Python世界! 使用MicroStation时的常见任务之一是处理围栏内的元素。本文将通过一个Python脚本演示如何管理MicroStation围栅内的元素。
让我们深入探讨在MicroStation中处理围栅内的元素,迭代它们,并对每个元素执行操作。请参阅Python管理器文章以创建和加载Python项目。将您的项目命名为FenceElements.py并将其保存到任意文件夹下。在编辑器中打开该项目以编写Python脚本。
导入模块
该脚本首先导入几个模块,这些模块提供了与MicroStation交互以及操作围栅的函数和类。
from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *
处理围栅内的元素
ProcessFenceElements()函数旨在管理和处理围栅内的元素。让我们一步步分解处理过程:
def ProcessFenceElements():
global ACTIVEMODEL
# Initialize
fenceMgr = FenceManager.GetManager()
selSetManager = SelectionSetManager.GetManager()
elmTypeString = WString()
# Check of Fence is active
if False == fenceMgr.IsFenceActive():
print("No active fence...")
return
# InitFromActiveFence: Populate the supplied FenceParams with information
# for the active fence.
fenceParams = FenceParams(ACTIVEMODEL)
bOverlapMode = fenceMgr.IsOverlapMode()
bClipMode = fenceMgr.IsClipMode()
allowClipFlag = FenceClipMode.eOriginal
fenceMgr.InitFromActiveFence (fenceParams, bOverlapMode, bClipMode, allowClipFlag)
# ElementAgenda: A bvector of EditElementHandle entries to be used for
# operating on groups of elements such as SelectionSet
# and Fences.
eAgenda = ElementAgenda()
modelRefList = DgnFile.GetModelRefList (DgnFile.GetMasterFile())
# BuildAgenda: Collect elements that satisfy the supplied FenceParams/criteria.
fenceMgr.BuildAgenda(fenceParams, eAgenda, modelRefList, False, False, False)
# Clear current selection set
selSetManager.EmptyAll()
# Process Fence elements
for i in range(eAgenda.GetCount()):
# Do Something
eeh = eAgenda.GetEntry(i)
eeh.GetHandler().GetTypeName (elmTypeString, 100)
main函数初始化当前模型并调用ProcessFenceElements()函数来处理围栏元素。
下面是完整的脚本:
from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *
# Get Level Name of Level ID passed as argument
def GetLevelName(lvlId):
global ACTIVEMODEL
levelCache = ACTIVEMODEL.GetDgnFile().GetLevelCache()
it = iter(levelCache)
for level in it:
levelId = level.GetLevelId()
if lvlId == levelId:
return level.GetName()
# Process Fence Elements
def ProcessFenceElements():
global ACTIVEMODEL
# Initialize
fenceMgr = FenceManager.GetManager()
selSetManager = SelectionSetManager.GetManager()
elmTypeString = WString()
# Check of Fence is active
if False == fenceMgr.IsFenceActive():
print("No active fence...")
return
# InitFromActiveFence: Populate the supplied FenceParams with information
# for the active fence.
fenceParams = FenceParams(ACTIVEMODEL)
bOverlapMode = fenceMgr.IsOverlapMode()
bClipMode = fenceMgr.IsClipMode()
allowClipFlag = FenceClipMode.eOriginal
fenceMgr.InitFromActiveFence (fenceParams, bOverlapMode, bClipMode, allowClipFlag)
# ElementAgenda: A bvector of EditElementHandle entries to be used for
# operating on groups of elements such as SelectionSet
# and Fences.
eAgenda = ElementAgenda()
modelRefList = DgnFile.GetModelRefList (DgnFile.GetMasterFile())
# BuildAgenda: Collect elements that satisfy the supplied FenceParams/criteria.
fenceMgr.BuildAgenda(fenceParams, eAgenda, modelRefList, False, False, False)
# Clear current selection set
selSetManager.EmptyAll()
# Process Fence elements
for i in range(eAgenda.GetCount()):
eeh = eAgenda.GetEntry(i)
eeh.GetHandler().GetTypeName (elmTypeString, 100)
msElement = MSElement()
msElement = eeh.GetElement()
levelId = msElement.ehdr.level
levelName = GetLevelName(levelId)
selSetManager.AddElement(eeh.GetElementRef(),ACTIVEMODEL.GetDgnModel())
print("***")
print(f"Element Description: {elmTypeString}")
print(f"Element Type: {eeh.GetElementType()}, Element ID: {eeh.GetElementId()}")
print(f"Level Name: {levelName}, Level ID: {levelId}")
# Main function
def main():
global ACTIVEMODEL
ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
ProcessFenceElements()
# Main
if __name__ == "__main__":
print("********** Fence Elements **********")
main()
运行/执行项目
在当前模型中放置一个围栅框住一些元素,选择所需的围栅类型、模式并运行/执行Python脚本。
与围栅条件匹配的元素将被高亮显示,其信息(描述、类型、Id、层名、层Id)将显示在MicroStation文本窗口中。
此脚本提供了一个使用Python在MicroStation中处理围栅内元素的基本框架。您可以自由定制脚本以满足您的特定需求,并将其集成到您的工作流程中。
祝您编程愉快!
【注】:文中用“围栅内的元素”是为了方便描述,严格来说应该是“符合围栅条件的元素”,因为围栅模式的不同,被选中的元素有可能位于围栅外面的。