如何使用 Revit API 从 Revit 元素获取边界框,然后调用该边界框的中心

How to get the bounding box from a Revit Element with Revit API, then call to center of that bounding box

我正在尝试围绕其中心点旋转 Revit 图元。为此,我需要 select 一个 Revit 元素并找到它的中心点,然后用该元素中心点的坐标创建一条线。

实现此目的的最佳想法是将 Revit 元素包裹在边界框中,然后找到该框的中心。我的问题是我不确定如何完成此操作。

我正在使用 pyRevit(神奇的工具),但我一直在研究如何用边界框包裹 selected 元素或检索其现有边界框。

如有任何帮助,我们将不胜感激!我真的很想学习 Revit API 并了解一切是如何工作的。我正在取得进步,但还有很多事情要做。

        def pickobject():
            from Autodesk.Revit.UI.Selection import ObjectType

            #define the active Revit application and document
            app = __revit__.Application
            doc = __revit__.ActiveUIDocument.Document
            uidoc = __revit__.ActiveUIDocument

            #define a transaction variable and describe the transaction
            t = Transaction(doc, 'This is my new transaction')

            # Begin new transaction
            t.Start()

            # Select an element in Revit
            picked = uidoc.Selection.PickObject(ObjectType.Element, "Select something.")


            ### ?????????? ###

            # Get bounding box of selected element.
            picked_bb = BoundingBoxXYZ(picked)  

            # Get max and min points of bounding box.
            picked_bb_max = picked_bb.Max
            picked_bb_min = picked_bb.Min

            # Get center point between max and min points of bounding box.
            picked_bb_center = (picked_bb_max + picked_bb_min) / 2

            ### ?????????? ###    

            # Close the transaction
            t.Commit()

            return picked, picked_bb_center  

在此先感谢您查看我目前拥有的内容。如果有任何需要进一步说明的地方,请告诉我!

编辑:

@CyrilWaechter

我认为你是对的。使用 LocationPoint 可能更有意义。我查看了您链接的脚本(顺便说一句,谢谢!)并尝试在我的代码中实现这一部分。

transform = doc.GetElement(picked.ElementId).GetTransform()

我正在通过此语句传递 ElementId,但出现错误,"Wall" 对象没有属性 'GetTransform'。你能帮我理解一下吗?

编辑 2: 感谢@JeremyTammik 和@CyrilWaechter,您的见解帮助我了解了哪里出了问题。虽然我仍然觉得 Revit API 中的某些属性不明确,但我能够让我的代码正确执行。我将 post 我能够在下面工作的代码。

边界框的中心很容易获得。 picked 是一个 Reference。从中获取 ElementId,使用 doc.GetElement 打开它,并使用 get_BoundingBox 检索边界框,参见。 管道与接线盒相交 :

Element e = Util.SelectSingleElement(
  uidoc, "a junction box" );

BoundingBoxXYZ bb = e.get_BoundingBox( null );

对于某些元素和某些不规则形状,您可能希望使用质心而不是边界框:

以下是我如何使用 pyRevit 解决我的问题。此代码允许您从元素的边界框中心绕其 Z 轴旋转元素。

要使用此代码,select 单个 Revit 元素然后打开 Revit Python Shell。将下面的代码复制并粘贴到 Revit Python Shell 记事本中,然后单击 运行 按钮。这会将元素旋转 45 度,因为当前的 rotateSelectedElement() 参数是 45。您可以在 运行ning.

之前将此数字更改为任何值
# Import the math module to convert user input degrees to radians.
import math

# Get a list of all user selected objects in the Revit Document.
selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]

# Definitions
def rotateSelectedElement(degrees_to_rotate):
    from Autodesk.Revit.UI.Selection import ObjectType

    #define the active Revit application and document
    app = __revit__.Application
    doc = __revit__.ActiveUIDocument.Document
    uidoc = __revit__.ActiveUIDocument

    #define a transaction variable and describe the transaction
    t = Transaction(doc, 'This is my new transaction')

    # Convert the user input from degrees to radians.
    converted_value = float(degrees_to_rotate) * (math.pi / 180.0)

    # Begin new transaction
    t.Start()

    # Get the first selected element from the current Revit doc.
    el = selection[0].Id

    # Get the element from the selected element reference
    el_ID = doc.GetElement(el)      

    # Get the Bounding Box of the selected element.
    el_bb = el_ID.get_BoundingBox(doc.ActiveView)

    # Get the min and max values of the elements bounding box.
    el_bb_max = el_bb.Max
    el_bb_min = el_bb.Min

    # Get the center of the selected elements bounding box.
    el_bb_center = (el_bb_max + el_bb_min) / 2

    #Create a line to use as a vector using the center location of the bounding box.
    p1 = XYZ(el_bb_center[0], el_bb_center[1], 0)
    p2 = XYZ(el_bb_center[0], el_bb_center[1], 1)
    myLine = Line.CreateBound(p1, p2)

    # Rotate the selected element.
    ElementTransformUtils.RotateElement(doc, el, myLine, converted_value)

    # Close the transaction
    t.Commit()


# Execute    
# Add the desired degrees to rotate by as an argument for rotateSelectedElement()
rotateSelectedElement(45)

编辑:使代码更清晰。代码现在无需任何进一步修改即可在 Revit Python Shell 中执行。如果遇到问题,请参阅上面的说明!

由 The Building Coder 为后代编辑和保存:

非常感谢 Christian 的有趣讨论,感谢 Cyril 提供的大量额外信息!