【发布时间】:2016-06-27 11:36:03
【问题描述】:
下面的 sn-p 应该删除任何不在工作表上的视图,或者在名为“视图所有者”的项目视图参数中不存在任何值。我已经在一个空白项目上对此进行了测试,它似乎按计划工作。然而,在“真正的”项目上,在搅动和搅动之后返回以下错误....
Autodesk.Revit.Exceptions.InvalidObjectException:引用的对象无效,可能是因为它已从数据库中删除,或者它的创建已撤消。在 validateNativeInstance(Void* ptr) 在 Autodesk.Revit.RevitAPIManagedWeakPointer.getValidPtr() 在 Autodesk.Revit.DB.Element.get_Id() 在 Microsoft.Scripting.Interpreter.FuncCallInstruction2.Invoke(Object arg0) at IronPython.Runtime.Binding.PythonGetMemberBinder.FastPropertyGet1.GetProperty(CallSite 站点,TSelfType 目标, CodeContext 上下文)在 Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame) 在 Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 在 Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,Tret] (T0 arg0, T1 arg1) 在 IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 在 Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) 在 RevitPythonShell.RpsRuntime.ScriptExecutor.ExecuteScript(String source)...强>
我真的不知道该怎么做。首先,它是什么,它意味着什么?其次 - 我将如何“捕捉”这个并防止它抛出错误?我假设收集的元素之一是“无效的”?有没有办法确定一个对象是否无效并忽略它?有没有办法摆脱无效对象?什么使对象无效?
__window__.Width = 1100
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, View, Transaction
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
selection = [ doc.GetElement( elId ) for elId in __revit__.ActiveUIDocument.Selection.GetElementIds() ]
views = []
viewstodelete = []
#Build the list full of views
if len(selection) == 0:
cl_views = FilteredElementCollector(doc)
views = cl_views.OfCategory( BuiltInCategory.OST_Views ).WhereElementIsNotElementType().ToElements()
else:
for sel in selection:
if isinstance(sel, View):
views.append(sel)
count = 0
#Get all views with a view owner
for v in views:
if (v.LookupParameter("Sheet Number") is None or v.LookupParameter("Sheet Number").AsString() == "---") and (v.LookupParameter("View Owner").AsString() is None or v.LookupParameter("View Owner").AsString() == ""):
if v.LookupParameter("View Name") is not None:
vOwner = v.LookupParameter("View Name").AsString()
count= count+1
viewstodelete.append(v)
else:
vOwner = "[View Template] - Not Deleted"
print(vOwner)
t = Transaction(doc, 'Delete Views')
t.Start()
for el in viewstodelete:
doc.Delete(el.Id)
t.Commit()
print "Views in Project: %s" % len(views)
print "Deleted views: %s" % count
我进行了以下编辑,允许脚本继续运行,但是,这些“可能从数据库中删除”错误中的每一个都非常耗时...
for el in viewstodelete:
t.Start()
try:
doc.Delete(el.Id)
except Exception as e:
print("Error: %s" %str(e))
t.Commit()
【问题讨论】: