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;
        }
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值