学习地址:https://www.bilibili.com/video/BV1mf4y1S72o?p=8
达到目标一
- 从一个元素中得到实体
- 从实体得到面,计算面积与体积
- Solid、Face、Mesh、Triangle之间的关系
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using System.Windows.Forms; using Autodesk.Revit.UI.Selection; namespace RevitDevTV { /// <summary> /// 几何计算 /// </summary> [TransactionAttribute(TransactionMode.Manual)] [RegenerationAttribute(RegenerationOption.Manual)] public class SolidPractise : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { //获取UIDocument UIDocument uiDoc = commandData.Application.ActiveUIDocument; Autodesk.Revit.DB.Document revitDoc = uiDoc.Document; //收集Document的id ICollection<ElementId> ids = uiDoc.Selection.GetElementIds(); //获取选择到的元素的ID Element selElem = uiDoc.Document.GetElement(ids.First()); //获取选择的第一个元素 GeometryElement ge = selElem.get_Geometry(new Options()); double area = 0; //面积 double volume = 0; //体积 int triangleCount = 0; //三角网格数 foreach (GeometryObject gObj in ge) { if (gObj is Solid) //判断是否是实体 { Solid sd = gObj as Solid; //如果是实体,都是sd foreach (Face face in sd.Faces) //获取所有面 { area += face.Area*0.3048*0.3048; Mesh mesh = face.Triangulate(0.5); triangleCount += mesh.NumTriangles; //获取网格数量 } volume += sd.Volume * 0.3048 * 0.3048 * 0.3048; } } TaskDialog.Show("计算", "面积总和为:"+area.ToString()+";平方米"+" "+"体积为:"+volume.ToString("0.000")+"立方米"+" "+"三角网格数:"+triangleCount.ToString()+"个"); return Result.Succeeded; } } }
达到目标二
交点立柱
- 计算轴线间的交点
- 在交点处放置柱子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using System.Windows.Forms; using Autodesk.Revit.UI.Selection; namespace RevitDevTV { /// <summary> /// 交点立柱 /// </summary> [TransactionAttribute(TransactionMode.Manual)] [RegenerationAttribute(RegenerationOption.Manual)] public class GridPractise : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uiDoc = commandData.Application.ActiveUIDocument; Document revitDoc = uiDoc.Document; //创建一个收集器,从uiDoc.Document中获取 FilteredElementCollector coll = new FilteredElementCollector(revitDoc); //创建一个轴网过滤器,从grid(网格)中收集 ElementClassFilter gridFilter = new ElementClassFilter(typeof(Grid)); //将过滤的轴网,放入list List<Element> grid = coll.WherePasses(gridFilter).ToElements().ToList(); //收集线 List<Line> gridLines = new List<Line>(); //创建交点 List<XYZ> intPos = new List<XYZ>(); //找到轴网的交点 foreach (Grid gri in grid) { gridLines.Add(gri.Curve as Line); } foreach (Line ln1 in gridLines) { foreach (Line ln2 in gridLines) { XYZ normal1 = ln1.Direction; XYZ normal2 = ln2.Direction; if (normal1.IsAlmostEqualTo(normal2)) continue; IntersectionResultArray results; SetComparisonResult intRst = ln1.Intersect(ln2, out results); if (intRst==SetComparisonResult.Overlap && results.Size==1) { XYZ tp = results.get_Item(0).XYZPoint; if (intPos.Where(m=>m.IsAlmostEqualTo(tp)).Count()==0) { intPos.Add(tp); } } } } Level level = revitDoc.GetElement(new ElementId(331)) as Level; FamilySymbol familysymb = revitDoc.GetElement(new ElementId(52557)) as FamilySymbol; //在交点处放置52557的柱子 using (Transaction trans=new Transaction(revitDoc)) { trans.Start("dfs"); if (!familysymb.IsActive) { familysymb.Activate(); } foreach (XYZ p in intPos) { //使用创建方法 FamilyInstance familyInstance = revitDoc.Create.NewFamilyInstance(p,familysymb,level,Autodesk.Revit.DB.Structure.StructuralType.NonStructural); } trans.Commit(); } return Result.Succeeded; } } }