Purge Scope Boxes

批量清理未使用的范围框

由于Revit中并没有自带的范围框清理功能,所以我用Dynamo脚本简单的实现了一个。

UI Node from DATA|SHAPES

Dynamo script:

这个脚本的逻辑非常清楚:

取得所有未使用的范围框
(get all unused Scope Boxes)
选择要删除的对象
(select Elements to delete)
执行删除过程
(Delete chosen Elements)
脚本框架

取得所有未使用的范围框:

# Coded by kj.luo
# kj-luo.com
# Get all unused Scope Boxes

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

# Get all Scope Boxes
# Those are categoried as VolumeOfInterest
filter = ElementCategoryFilter(BuiltInCategory.OST_VolumeOfInterest)
allSbIds = [s.Id for s in FilteredElementCollector(doc).WherePasses(filter).WhereElementIsNotElementType().ToElements()]

# Get all used Scope Boxes
usedSbIds = []
views =[x for x in FilteredElementCollector(doc).OfClass(View) if not x.IsTemplate]
for view in views:
	try:
		usedSbIds.append(view.get_Parameter
		(BuiltInParameter.VIEWER_VOLUME_OF_INTEREST_CROP).AsElementId())
	except:
		pass
		
# unused Scoped Boxes and their names
notUsedSbIds = set(allSbIds) - set(usedSbIds)
notUsedSbNames = [doc.GetElement(id).Name for id in notUsedSbIds]

OUT = notUsedSbIds,notUsedSbNames

需要留意的是,范围框在Revit API中并没有被定义成一个明确的类(Category),而是被归到 BuiltIn-Category 中的 VolumeOfInterest。最后则是简单地调用 Revit API 删除这几个对象:

# Coded by kj.luo
# kj-luo.com
# Delete Elements

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

# IN
Ids = [UnwrapElement(x) for x in IN[0]]
# Output
deleted = []
notDeleted = []

TransactionManager.Instance.EnsureInTransaction(doc)

for i in Ids:
	try:
		doc.Delete(i)
		deleted.append(i)
	except:
		notDeleted.append(i)
		
TransactionManager.Instance.TransactionTaskDone()

OUT = deleted,notDeleted

当然,这个简单的功能也能够通过一些免费插件来完成。我做的这些工作只是在完成一个“清理懒人包”的一部分。之后会尝试将好几种清理整合起来,提升工作的自动化层次。可惜 Revit API 并未开放“清除未使用的对象”(Purge Unused Objects) 这个功能。


发表评论

邮箱地址不会被公开。 必填项已用*标注