如何获取房间中心的XYZ数据?

发布于 2025-01-15 21:39:14 字数 2798 浏览 3 评论 0原文

我试图将一个元素放置在几个房间的中心。到目前为止,我已经通过使用位置点实现了类似的目标,该位置点将元素放置在靠近中心的位置,但并不精确。

我尝试使用两种我认为有助于完成此任务的方法来解决此问题:GetElementCenter 和 GetRoomCenter,但当我运行该插件时,没有任何反应。

class Class2
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        //Get access to Revit command data, user interface and document
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Document doc = uidoc.Document;

        //Collect all rooms
        FilteredElementCollector roomCollector = new FilteredElementCollector(doc).OfClass(typeof(SpatialElement));

        // Collect element
        FilteredElementCollector element = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_Cameras);

        //Get symbol
        FamilySymbol elementSym = element.FirstElement() as FamilySymbol;

        using (Transaction tx = new Transaction(doc))
        {
            try
            {
                tx.Start("Start");

                //For loop for every room in the roomCollector 
                foreach (SpatialElement oneRoom in roomCollector)
                {
                    //Get area of each room
                    Room room = oneRoom as Room;
                    double area = room.Area;

                    //Location point version
                    Location loc = room.Location;
                    LocationPoint location = loc as LocationPoint;
                    XYZ point = (null == location) ? XYZ.Zero : location.Point;

                    //New version
                    XYZ source = GetRoomCenter(room);

                    double smallRoom = 301;
                    
                    if (area <= smallRoom)
                    {
                        doc.Create.NewFamilyInstance(source, elementSym, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                    }
                }
                tx.Commit();
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                tx.RollBack();
            }
        }
        TaskDialog.Show("Message", "Task completed successfully");
        return Result.Succeeded;
    }

    public XYZ GetElementCenter(Room room)
    {
        BoundingBoxXYZ bounding = room.get_BoundingBox(null);
        XYZ center = (bounding.Max + bounding.Min) * 0.5;
        return center;
    }
    public XYZ GetRoomCenter(Room room)
    {
        XYZ boundCenter = GetElementCenter(room);
        LocationPoint locPt = (LocationPoint)room.Location;
        XYZ roomCenter = new XYZ(boundCenter.X, boundCenter.Y, locPt.Point.Z);
        return roomCenter;
    }
}

任何

关于获取房间中心的 XYZ 数据的帮助将不胜感激。

I am trying to place an element into the center of several rooms. So far I have achieved something similar by using the Location Point which has placed the element close to the center but not exact.

I attempted to fix this by using two methods that I believed would help accomplish this task, GetElementCenter and GetRoomCenter but when I run the plugin, nothing happens.

class Class2
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        //Get access to Revit command data, user interface and document
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Document doc = uidoc.Document;

        //Collect all rooms
        FilteredElementCollector roomCollector = new FilteredElementCollector(doc).OfClass(typeof(SpatialElement));

        // Collect element
        FilteredElementCollector element = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_Cameras);

        //Get symbol
        FamilySymbol elementSym = element.FirstElement() as FamilySymbol;

        using (Transaction tx = new Transaction(doc))
        {
            try
            {
                tx.Start("Start");

                //For loop for every room in the roomCollector 
                foreach (SpatialElement oneRoom in roomCollector)
                {
                    //Get area of each room
                    Room room = oneRoom as Room;
                    double area = room.Area;

                    //Location point version
                    Location loc = room.Location;
                    LocationPoint location = loc as LocationPoint;
                    XYZ point = (null == location) ? XYZ.Zero : location.Point;

                    //New version
                    XYZ source = GetRoomCenter(room);

                    double smallRoom = 301;
                    
                    if (area <= smallRoom)
                    {
                        doc.Create.NewFamilyInstance(source, elementSym, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                    }
                }
                tx.Commit();
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                tx.RollBack();
            }
        }
        TaskDialog.Show("Message", "Task completed successfully");
        return Result.Succeeded;
    }

    public XYZ GetElementCenter(Room room)
    {
        BoundingBoxXYZ bounding = room.get_BoundingBox(null);
        XYZ center = (bounding.Max + bounding.Min) * 0.5;
        return center;
    }
    public XYZ GetRoomCenter(Room room)
    {
        XYZ boundCenter = GetElementCenter(room);
        LocationPoint locPt = (LocationPoint)room.Location;
        XYZ roomCenter = new XYZ(boundCenter.X, boundCenter.Y, locPt.Point.Z);
        return roomCenter;
    }
}

}

Any help on getting the XYZ data of the center of a room would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2025-01-22 21:39:14

Assuming that you have straight walls bounding the room, I would suggest that you take a look at mathematical and geometrical algorithms for determining the center point of a polygon. Something like this new algorithm for finding a visual center of a polygon is probably best suited to your needs, even though you neither state this fact nor probably are yet aware of it :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文