Revit二次开发示例:DeleteDimensions

在本例中,创建一个命令,实现删除所选中的尺寸标注。

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

namespace DeleteDimensions
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            ElementSet selections = commandData.Application.ActiveUIDocument.Selection.Elements;
            ElementSet dimsToDelete = new ElementSet();
            
            if (0 == selections.Size)
            {
                message = "Please select dimensions";
                return Result.Failed;
            }

            foreach (Element e in selections)
            {
                Dimension dimensionTemp = e as Dimension;
                if (null != dimensionTemp && !dimensionTemp.Pinned)
                {
                    dimsToDelete.Insert(dimensionTemp);
                }
            }

            if (0 == dimsToDelete.Size)
            {
                message = "There are no unpinned dimensions currently selected";
                return Result.Failed;
            }

            Transaction transation = new Transaction(commandData.Application.ActiveUIDocument.Document, "External Tool");

            transation.Start();

            foreach (Element e in dimsToDelete)
            {
                commandData.Application.ActiveUIDocument.Document.Delete(e.Id);
            }
            transation.Commit();

            return Result.Succeeded;
        }
    }
}
原文地址:https://www.cnblogs.com/xpvincent/p/3605451.html