Revit開發之軸線交點創建柱子

Revit開發之軸線交點創建柱子

這個demo實現了軸線的交點創建柱子功能,純屬娛樂和學習。不足之處請各路大神指教

using System;
using System.Collections.Generic;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Electrical;
using System.Threading;
using System.Linq;
using System.Runtime.InteropServices;

namespace 插件1
{
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(JournalingMode.UsingCommandData)]
    public class Command : IExternalCommand
    {
        public readonly double unit = 304.8;

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            UIApplication uiapp = new UIApplication(commandData.Application.Application);
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection selection = uidoc.Selection;

            List<Grid> xGrid = new List<Grid>();
            List<Grid> yGrid = new List<Grid>();
            List<XYZ> points = new List<XYZ>();
            List<Reference> reffs = (selection.PickObjects(ObjectType.Element,new GridFilter(),"請選擇所有的軸線")).ToList();
            
            foreach(Reference item in reffs)
            {
                Grid grid = doc.GetElement(item) as Grid;
                if(grid!=null)
                {
                    Line line = grid.Curve as Line;
                    if(line!=null)
                    {
                        XYZ dir = line.Direction;
                        if(dir.Y.Equals(-1)||dir.Y.Equals(1))
                        {
                            yGrid.Add(grid);
                        }
                        
                        else if(dir.X.Equals(-1)||dir.X.Equals(1))
                        {
                            xGrid.Add(grid);
                        }
                    }
                }
                else
                {
                    continue;
                }
            }
            foreach(Grid grx in xGrid)
            {
                foreach(Grid gry in yGrid)
                {
                    XYZ res = Command.GetIntersectPoint(grx, gry);
                    if (res == null) continue;
                    points.Add(res);
                }
            }
            // 過濾出一個柱子的族類型
            ElementId columnTypeId = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Columns).OfClass(typeof(FamilySymbol)).ToElementIds().First();
            FamilySymbol fmSymbol = doc.GetElement(columnTypeId) as FamilySymbol;
            // 過濾出一個默認的標高
            Level level = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).OfClass(typeof(Level)).ToList<Element>().FirstOrDefault() as Level;

            using (Transaction ts = new Transaction(doc,"創建柱子"))
            {
                ts.Start();
                if(!fmSymbol.IsActive)
                {
                    fmSymbol.Activate();
                }
                foreach(XYZ ptn in points)
                {

                    doc.Create.NewFamilyInstance(ptn, fmSymbol, level, Autodesk.Revit.DB.Structure.StructuralType.NonStructural);
                }

                ts.Commit();
            }


                return Result.Succeeded;
        }
        // 獲得兩個軸線的交點
        public static XYZ GetIntersectPoint(Grid grid1,Grid grid2)
        {
            Line line1 = grid1.Curve as Line;
            Line line2 = grid2.Curve as Line;
            IntersectionResultArray res;
            line1.Intersect(line2, out res);
            XYZ point = res.get_Item(0).XYZPoint;


            return point;
        }
        
    }
    
    // 用戶只能選擇軸線
    public class GridFilter : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            return elem is Grid ? true : false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return false;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章