Revitアドイン-底面座標の取得

Revitのアドインコマンドで選択された要素の底面の中心座標を取得するプログラムの作成方法について

はじめに

この記事では、Autodesk Revitのアドインコマンドを使用して、選択された要素の底面の中心座標を取得する方法について解説します。底面の形状は長方形または台形とします。このプログラムを使用することで、Revitモデル内の要素の底面の中心座標を効率的に取得し、設計や解析などの用途に活用することができます。

アドインコマンドの作成

以下に、アドインコマンドのコードを示します。このコードでは、Revitのリボンに新しいボタンを追加し、そのボタンをクリックすると、選択された要素の底面の中心座標がダイアログで表示されます。

using System;
using System.Linq;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;

namespace RevitAddin
{
    public class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication application)
        {
            // リボンに新しいタブを追加
            string tabName = "CustomTab";
            application.CreateRibbonTab(tabName);

            // リボンに新しいパネルを追加
            RibbonPanel ribbonPanel = application.CreateRibbonPanel(tabName, "CustomPanel");

            // ボタンの作成
            PushButtonData buttonData = new PushButtonData(
                "MyButton",
                "Show Bottom Center",
                Assembly.GetExecutingAssembly().Location,
                "RevitAddin.Command");

            PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton;

            // ボタンのプロパティを設定
            pushButton.ToolTip = "選択された要素の底面の中心座標を表示します。";

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
            ExternalCommandData commandData, 
            ref string message, 
            ElementSet elements)
        {
            // UIとドキュメントの取得
            UIDocument uiDoc = commandData.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;

            // 要素を選択
            Selection sel = uiDoc.Selection;
            Reference selectedRef = sel.PickObject(ObjectType.Element);

            // 選択された要素の取得
            Element selectedElement = doc.GetElement(selectedRef.ElementId);

            // 底面の中心座標を取得
            XYZ bottomCenter = GetBottomCenter(selectedElement);

            // ダイアログに座標を表示
            TaskDialog dialog = new TaskDialog("Bottom Center Information");
            dialog.MainInstruction = "底面の中心座標";
            dialog.MainContent = $"X: {bottomCenter.X}\nY: {bottomCenter.Y}\nZ: {bottomCenter.Z}";
            dialog.Show();

            return Result.Succeeded;
        }

        // 底面の中心座標を取得するメソッド
        private XYZ GetBottomCenter(Element element)
        {
            GeometryElement geometryElement = element.get_Geometry(new Options());

            foreach (GeometryObject obj in geometryElement)
            {
                if (obj is Solid solid)
                {
                    Face bottomFace = GetBottomFace(solid);
                    if (bottomFace != null)
                    {
                        XYZ bottomCenter = bottomFace.Evaluate(new UV(0.5, 0.5));
                        return bottomCenter;
                    }
                }
            }

            return null;
        }

        // 底面を取得するメソッド
        private Face GetBottomFace(Solid solid)
        {
            foreach (Face face in solid.Faces)
            {
                if (face.ComputeNormal(new UV()).IsAlmostEqualTo(XYZ.BasisZ.Negate()))
                {
                    return face;
                }
            }

            return null;
        }
    }
}

コードの解説

このコードでは、以下の手順で底面の中心座標を取得しています:

  1. アドインコマンドの作成: アドインコマンドを作成し、Revitのリボンに新しいボタンを追加します。ボタンをクリックすると、選択された要素の底面の中心座標を取得する処理が実行されます。

  2. 底面の中心座標の取得: GetBottomCenterメソッド内で、選択された要素のジオメトリを取得し、その中から底面の中心座標を見つけます。

  3. 底面の中心座標の計算: GetBottomFaceメソッド内で、選択された要素のSolidの各面をチェックし、底面を特定します。底面が見つかった場合、その底面の中心座標を計算します。

  4. ダイアログへの表示: 最終的に、取得した底面の中心座標をダイアログで表示します。

まとめ

この記事では、Revitのアドインコマンドを使用して、選択された要素の底面の中心座標を取得するプログラムの作成方法について解説しました。これにより、Revitモデル内の要素の底面の中心座標を簡単に抽出し、設計や解析などの目的に活用することができます。

この記事が気に入ったらサポートをしてみませんか?