Revit二次开发——自动标注轴网

 

引言:在自动标注大主题下,自动标注轴网;利弊等望读者自行改进(仅限平面视图)

开始:

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 

namespace AutoDimention
{
    [TransactionAttribute(TransactionMode.Manual)]
    public class AutoDimentionGrid_Cmd : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            FilteredElementCollector collectorGrid = new FilteredElementCollector(doc);
            collectorGrid.OfClass(typeof(Grid)).OfCategory(BuiltInCategory.OST_Grids);
            IList listGrids = collectorGrid.ToElements().ToList();
            List ListGridY = new List();
            List ListGridX = new List();
            Line lineGridX = null;
            Line lineGridY = null;
            XYZ Xvec = new XYZ(-1, 0, 0);//*-X单位向量
            XYZ Xvec2 = new XYZ(1, 0, 0);//*X单位向量
            XYZ Yvec = new XYZ(0, -1, 0);//*-Y单位向量
            XYZ Yvec2 = new XYZ(0, 1, 0);//*Y单位向量
            ReferenceArray refArryGridY = new ReferenceArray();
            ReferenceArray refArryGridX = new ReferenceArray();

            foreach (Element TemElement in listGrids)
            {
                Grid grid = TemElement as Grid;
                Line line = grid.Curve as Line;
                if (line.Direction.IsAlmostEqualTo(Yvec) || line.Direction.IsAlmostEqualTo(Yvec2))
                {
                    ListGridY.Add(line);
                    refArryGridY.Append(new Reference(grid));
                }
                if (line.Direction.IsAlmostEqualTo(Xvec) || line.Direction.IsAlmostEqualTo(Xvec2))
                {
                    ListGridX.Add(line);
                    refArryGridX.Append(new Reference(grid));
                }

            }
            if (ListGridX.Count >= 2)
            {
                 lineGridX = Line.CreateBound(ListGridX[0].Origin, ListGridX[1].Origin);

            }
            if (ListGridY.Count >= 2)
            {
                lineGridY = Line.CreateBound(ListGridY[0].Origin, ListGridY[1].Origin);
            }
            using (Transaction transaction = new Transaction(doc, "AutoDimenionGrid"))
            {
                try
                {
                    transaction.Start();
                    doc.Create.NewDimension(doc.ActiveView, lineGridY, refArryGridY);
                    doc.Create.NewDimension(doc.ActiveView, lineGridX, refArryGridX);
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Commit();
                }
            }
 
            return Result.Succeeded;
        }
    }
}

Revit二次开发——自动标注轴网_第1张图片

技术群:1090519856

你可能感兴趣的:(Revit二次开发,Revit二次开发,轴网标注,自动标注)