1

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

完成此操作的最佳方法是将 Revit 元素包装在边界框中,然后找到该框的中心。我的问题是我不确定如何做到这一点。

我正在使用 pyRevit (惊人的工具),我被困在如何使用边界框包装选定元素或检索其现有边界框。

任何帮助将不胜感激!我真的很想学习 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 中的某些属性不明确,但我能够让我的代码正确执行。我将在下面发布我能够工作的代码。

4

3 回答 3

2

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

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

BoundingBoxXYZ bb = e.get_BoundingBox( null );

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

于 2018-12-13T18:42:33.467 回答
1

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

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

于 2018-12-18T10:24:32.883 回答
1

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

要使用此代码,请选择一个 Revit 元素,然后打开 Revit Python Shell。将以下代码复制并粘贴到 Revit Python Shell 记事本中,然后单击运行按钮。这会将元素旋转 45 度,因为当前 rotateSelectedElement() 参数是 45。您可以在运行之前将此数字更改为任何值。

# 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 中执行。如果您遇到问题,请参阅上面的说明!

于 2018-12-17T01:12:42.997 回答