【问题标题】:Revit API python Error:Exception: idx can be only 0, 1, 2Revit API python错误:异常:idx只能是0、1、2
【发布时间】:2019-03-10 10:50:26
【问题描述】:

谁能帮我理解这个错误。我仍在学习 Revit API(和 python)并且搜索没有帮助。我正在尝试获取元素的 xyz 位置点。

这是我的代码:

elements= ui.Selection() 
for d in elements:
for l in d.Parameters:
    for x in d.Location.Point:
        print x

这是输出,注意:它确实返回三个值:

149.412934765
69.7704247908
-3.71628688979

这里是错误信息。我不明白错误消息中对 idx 的引用:

IronPython Traceback:
Traceback (most recent call last):
 File "C\BTS-NY-BETA.extension\BTS-NY-BETA.tab\Beta Tools.panel\BETA3.pushbutton\Get linked docs_script.py", line 32, in 
Exception: idx can be only 0, 1, 2.
Parameter name: idx


Script Executor Traceback:
Autodesk.Revit.Exceptions.ArgumentOutOfRangeException: idx can be only 0, 1, 2.
Parameter name: idx

 at Autodesk.Revit.DB.XYZ.get_Item(Int32 idx)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`3.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
 at IronPython.Runtime.ItemEnumerator.System.Collections.IEnumerator.MoveNext()
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
 at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
 at PyRevitBaseClasses.ScriptExecutor.ExecuteScript(PyRevitCommandRuntime& pyrvtCmd)

【问题讨论】:

  • 脚本Get linked docs_script.py的第32行有什么内容?

标签: revit-api pyrevit


【解决方案1】:

Point 不是一个值数组,访问 X、Y、Z 是正确的方式。

【讨论】:

    【解决方案2】:

    虽然我仍然不理解错误,但我可以通过如下编辑代码来消除错误消息:

    elements= ui.Selection() 
    for d in elements:
    for l in d.Parameters:
        for pt in d.Location.Point:
            print pt.X
            print pt.Y
            print pt.Z
    

    【讨论】:

    • 首先位置不保证是Point。它也可以是Line,在这种情况下你的代码会抛出异常。另外,我想支持 Matt 的 cmets,您不必遍历 Point。要获取 XYZ 值,只需调用 loc = d.Location.Point,然后调用 print loc.X 等。没有理由像这样迭代点属性。
    • @Matt 和 Konrad - 是的,我知道你是对的,对此毫无疑问,有趣的是,迭代它给出了我认为会的结果,然后产生了错误
    • 请务必接受马特的回答。他的建议在这里是正确的。
    猜你喜欢
    • 2022-07-01
    • 2011-07-14
    • 1970-01-01
    • 2021-03-31
    • 2012-05-06
    • 2017-12-13
    • 2019-09-21
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多