RevitAPI(五) 建筑建模

一  标高和轴网

        

轴网 Grid

创建轴网
曲线:Grid Create(Document,Arc);
直线:Grid Create(Document,Line);
常用属性:
Curve:拿到轴网的线
IsCurved:true说明是Arc对象,否则是Line对象
Name:改轴网的名

注意:
1.轴网创建后名字会按照上一次的规则递增命名
2.传入的直线或弧线的平面必须是一个水平的
 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Application = Autodesk.Revit.ApplicationServices.Application;

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class GridTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //删除
            Transaction transaction = new Transaction(doc, "轴网");
            transaction.Start();

            XYZ start=uIDocument.Selection.PickPoint();
            XYZ end=uIDocument.Selection.PickPoint();

            Line line = Line.CreateBound(start,end);

            var a=Grid.Create(doc,line);


            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
}

 标高Level

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class LevelTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //删除
            Transaction transaction = new Transaction(doc, "标高");
            transaction.Start();
            Level level = Level.Create(doc,10000.0.Tofoot());
            var a=(doc.GetElement(new ElementId(53969))) as ViewFamilyType;
            ViewPlan.Create(doc,a.Id,level.Id);
            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
}

 二 宿主元素HostObject

  宿主元素(HostObject)子类有Wall、Floor、Ceiling、Roof      

        


1.获取复合结构通过宿主元素的类型如WalIType、FloorType得到如WallType.GetCompoundStructure()

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI.Selection;

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class CompoundStructureTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //删除
            Transaction transaction = new Transaction(doc, "获取复合结构");
            transaction.Start();

            Reference reference= uIDocument.Selection.PickObject(ObjectType.Element);
            Wall wall=doc.GetElement(reference) as Wall;
            WallType wallType = wall.WallType;

            CompoundStructure compoundStructure = wallType.GetCompoundStructure();

            IList<CompoundStructureLayer> compoundStructures = compoundStructure.GetLayers();
            StringBuilder stringBuilder = new StringBuilder();
            foreach(var compoundStructureLayer in compoundStructures)
            {
                ElementId a =compoundStructureLayer.MaterialId;
                stringBuilder.AppendLine("序号:\t\t"+ compoundStructureLayer.LayerId.ToString());
                stringBuilder.AppendLine("材质:\t\t" + doc.GetElement(a).Name.ToString());
                stringBuilder.AppendLine("宽度:\t\t" + compoundStructureLayer.Width.ToString());
            }
            TaskDialog.Show("a", stringBuilder.ToString());

            Reference reference1 = uIDocument.Selection.PickObject(ObjectType.Element);
            Wall wall1 = doc.GetElement(reference1) as Wall;
            IList<ElementId> elementIds = wall1.FindInserts(true, true, true, true);

            StringBuilder stringBuilder1= new StringBuilder();
            foreach (var elementId in elementIds)
            {
                Element element = doc.GetElement(elementId) ;
                stringBuilder1.AppendLine("Id:\t\t" + elementId.ToString());
                stringBuilder1.AppendLine("类型名称:\t\t" + element.Name.ToString());
                
            }
            TaskDialog.Show("a", stringBuilder1.ToString());
            transaction.Commit();

            //

            return Result.Succeeded;
        }
    }
}


2.获取宿主元素上的其它元素如wall.FindInserts()

使用HostObject.FindInserts方法可以用来获取插入到宿主元素上的元素的集合:
IList<ElementId>FindInserts(bool addRectOpenings,bool includeShadows,boolincludeEmbeddedWalls,bool includeSharedEmbeddedInserts)


3.获取宿主的面,HostObjectUtils类的方法


4.创建墙

using Autodesk.Revit.DB;
using System.Collections.Generic;
using System.Linq;

namespace Revit_CreatInstane
{
    public static class WallFactory
    {
       
        public static ElementId CreateWall(Document document) 
        {
            //创建线
            XYZ start=new XYZ(0,0,0);
            XYZ end=new XYZ(10000/304.8, 0,0);
            Line line=Line.CreateBound(start, end);
            //创建标高
            List<Level> levels = document.SpeedFilter<Level>(BuiltInCategory.OST_Levels);

            Level level=levels.FirstOrDefault(x => x.Name == "标高 1");
            //高度
            double height = 4000.0.Unit();
            //is structural
            bool isStruct = true;
            //偏移量
            double offset = 0;

            //获取类型

            List<WallType> wallTypes = document.SpeedFilter<WallType>(BuiltInCategory.OST_Walls);

            WallType wallType = wallTypes.FirstOrDefault( x => x.Name == "常规 - 300mm");

            Wall wall = Wall.Create(document, line, wallType.Id, level.Id, height, offset, false, isStruct);
            ElementId elementId = wall.Id;
            return elementId;

            
        }
        
        
    }
}

5.创建楼板

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI.Selection;

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class FloorTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //删除
            Transaction transaction = new Transaction(doc, "创建楼板");
            transaction.Start();

            Reference reference =uIDocument.Selection.PickObject(ObjectType.Element, new FloorSelectionFilter());

            Room room=doc.GetElement(reference) as Room;


            IList<IList<BoundarySegment>> a = room.GetBoundarySegments(new SpatialElementBoundaryOptions());

            CurveArray curveArray = new CurveArray();
            foreach (IList<BoundarySegment> boundarySegments in a)
            {
                foreach(BoundarySegment boundarySegment in boundarySegments)
                {
                    curveArray.Append(boundarySegment.GetCurve());

                }

            }
            
            FloorType floorType = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors).OfClass(typeof(FloorType)).FirstOrDefault() as FloorType;
            Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).FirstOrDefault() as Level;
            doc.Create.NewFloor(curveArray, floorType, level,false);

            TaskDialog.Show("测试",room.Name);
            
            transaction.Commit();

            //

            return Result.Succeeded;
        }

        public  class FloorSelectionFilter : ISelectionFilter
        {
            public bool AllowElement(Element elem)
            {
                
                if(elem is Room&&(!elem.Equals(null)))
                
                    return true;
                else
                    return false;
                
            }

            public bool AllowReference(Reference reference, XYZ position)
            {
                return true;
            }
        }
    }
}

三 载入族和创建族实例

        载入族

        

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI.Selection;

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class LoadFamiltTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //删除
            Transaction transaction = new Transaction(doc, "载入族");
            transaction.Start();

            
            string path= @"C:\Users\zmd\Desktop\混凝土梁-矩形砼梁-有标记.rfa";
            Family family = LoadFamily(path, doc);


            TaskDialog.Show("测试", family.Name);
            transaction.Commit();

            //

            return Result.Succeeded;
        }
        public Family LoadFamily(string path,Document document) 
        {
            bool result= document.LoadFamily(path, out Family family);
            if (!result) 
            {
                FileInfo fileInfo = new FileInfo(path);
                string filename = fileInfo.Name;
                int num =filename.IndexOf(".");

                filename=filename.Substring(0, num);

                FamilySymbol symbol= new FilteredElementCollector(document).OfClass(typeof(FamilySymbol)).FirstOrDefault(x => (x as FamilySymbol).FamilyName == filename) as FamilySymbol;
               family = symbol.Family;

            }
            return family;
        }
    }
}

创建族实例

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;
using System.Collections.Generic;
using System.Linq;

namespace Revit_CreatInstane
{
    public static class  FamilyInstanceFactory
    {
        public static ElementId CreateDoor(Document document,ElementId hostElementId)
        {
            //坐标
            XYZ location= new XYZ(3000/304.8,0,0);

            //宿主元素
            Element element = document.GetElement(hostElementId);

            //标高
            Level level = document.GetElement(element.LevelId) as Level;

            //类型
            List<FamilySymbol> elements = document.SpeedFilter<FamilySymbol>(BuiltInCategory.OST_Doors);

            FamilySymbol familySymbol = elements.FirstOrDefault(x=>x.Name=="1730 x 2134mm");
            familySymbol.Activate();

            Autodesk.Revit.DB.FamilyInstance familyInstance= document.Create.NewFamilyInstance(location, familySymbol, element, level, StructuralType.NonStructural);

            return familyInstance.Id;
            

        }
        public static ElementId CreateWindows(Document document, ElementId hostElementId)
        {
            //坐标
            XYZ location = new XYZ(7000 / 304.8, 0, 0);

            //宿主元素
            Element element = document.GetElement(hostElementId);

            //标高
            Level level = document.GetElement(element.LevelId) as Level;

            //类型
            List<FamilySymbol> elements = document.SpeedFilter<FamilySymbol>(BuiltInCategory.OST_Windows);

            FamilySymbol familySymbol = elements.FirstOrDefault(x => x.Name == "Standard");
            familySymbol.Activate();

            Autodesk.Revit.DB.FamilyInstance familyInstance = document.Create.NewFamilyInstance(location, familySymbol, element, level, StructuralType.NonStructural);

            return familyInstance.Id;


        }
    }
}

四 创建房间

        

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI.Selection;

namespace Selection
{
    [Transaction(TransactionMode.Manual)]
    public class RoomTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //UIApplication表示一个活动的Revit@会话,提供了对UI定制、事件以及活动文档的访问。
            UIApplication uIApplication = commandData.Application;
            //Application类表示一个Revit应用,提供对文档、选项以及其他应用范围的数据的访问和设置。
            Application app = uIApplication.Application;
            //主要包含了跟UI相关的文档信息和操作,比如Active View和ShowElement
            UIDocument uIDocument = uIApplication.ActiveUIDocument;
            //用与存储Revit的元素、管理视图和数据
            Document doc = uIDocument.Document;

            //创建房间
            Transaction transaction = new Transaction(doc, "创建房间");
            transaction.Start();

           
            
            Level level=new FilteredElementCollector(doc).OfClass(typeof(Level)).FirstOrDefault(x=>x.Name=="标高 1") as Level;

            System.Collections.Generic.ICollection<ElementId> elementIds = doc.Create.NewRooms2(level);
            transaction.Commit();

            transaction.Start();
            Reference reference = uIDocument.Selection.PickObject(ObjectType.Element, new Isroom());
            Room room = doc.GetElement(reference) as Room;
           
           


            System.Collections.Generic.List<FamilyInstance> doors = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance)).Cast<FamilyInstance>().ToList();

            foreach (FamilyInstance familyInstance in doors)
            {
                if (familyInstance != null && familyInstance.ToRoom!= null && familyInstance.ToRoom.Id==room.Id)
                    TaskDialog.Show("测试", familyInstance.Name.ToString());
                else if (familyInstance != null && familyInstance.Room != null && familyInstance.Room.Id == room.Id)
                    TaskDialog.Show("测试", familyInstance.Name.ToString());
                else if(familyInstance != null && familyInstance.FromRoom != null && familyInstance.FromRoom.Id == room.Id)
                    TaskDialog.Show("测试", familyInstance.Name.ToString());
                else TaskDialog.Show("测试2", "测试2");
            }

            transaction.Commit();

            //

            return Result.Succeeded;
        }

       
    }
     public  class Isroom : ISelectionFilter
    {
        public bool AllowElement(Element elem)
        {
            if (elem != null && elem is Room) return true;
            else return false;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值