Files
CDMUtility/CDMUtil/Utility/FloorUtil.cs
2026-02-23 14:35:54 +08:00

176 lines
7.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using CDM.Interop.Revit.CDMComponent;
using CDM.Interop.Revit.RevitCompoent;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CDM.Interop.Revit.Utility
{
class FloorUtil : Utility
{
public static List<CDMFloor> GetFloorsCDM(Document doc, IList<Element> levels, View3D v3d)
{
List<CDMFloor> cdmFloors = new List<CDMFloor>();
var floors = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors).OfClass(typeof(Floor)).ToElements();
//楼板类型及编号
Dictionary<ElementId, string> dicFloors = new Dictionary<ElementId, string>();
Dictionary<ElementId, string> dictLevelId_LevelCodes = GetDictLevelId_LevelCodes(doc);
int num = 1;
for (int i = 0; i < floors.Count; i++)
{
ElementId floortypeId = floors[i].GetTypeId();
try
{
dicFloors.Add(floortypeId, string.Format("00{0}", num));
num += 1;
}
catch (Exception)
{
continue;
}
}
foreach (Floor floor in floors)
{
CDMFloor cdmfloor = new CDMFloor(doc, floor, v3d);
////通过out将获取到的值复制给cdm的类型id,out参数必须在函数内初始化值
dicFloors.TryGetValue(floor.FloorType.Id, out cdmfloor.CategoryCode);
dictLevelId_LevelCodes.TryGetValue(cdmfloor.LevelId, out cdmfloor.LevelCode);
cdmFloors.Add(cdmfloor);
}
for (int i = 0; i < levels.Count(); i++)
{
for (int j = 0; j < dicFloors.Count; j++)
{
ElementId id = dicFloors.ElementAt(j).Key;
//归类cdm中同标高同类型
IEnumerable<CDMFloor> targetcdm = from d in cdmFloors
where d.LevelId.Equals(levels[i].Id) && d.SymbolId.Equals(id)
select d;
//排序
//var li = targetcdm.ToList();
List<CDMFloor> li = targetcdm.OrderByDescending(p => p.Yw).ThenBy(p => p.Xl).ToList();
for (int k = 0; k < li.Count; k++)
{
if (k > 8)
{
if (k > 98)
{
li[k].LevelComponentCode = string.Format("{0}", k + 1);
}
else
{
li[k].LevelComponentCode = string.Format("0{0}", k + 1);
}
}
else
{
li[k].LevelComponentCode = string.Format("00{0}", k + 1);
}
}
}
}
return cdmFloors;
}
private static CurveArray SetBoundary(XYZ rOrigin, double length, double width)
{
XYZ p1 = new XYZ(rOrigin.X, rOrigin.Y - width / 2, 0);
XYZ p2 = new XYZ(rOrigin.X, rOrigin.Y + width / 2, 0);
XYZ p3 = new XYZ(rOrigin.X + length, rOrigin.Y + width / 2, 0);
XYZ p4 = new XYZ(rOrigin.X + length, rOrigin.Y - width / 2, 0);
Autodesk.Revit.DB.Line line1 = Autodesk.Revit.DB.Line.CreateBound(p1, p2);
Autodesk.Revit.DB.Line line2 = Autodesk.Revit.DB.Line.CreateBound(p2, p3);
Autodesk.Revit.DB.Line line3 = Autodesk.Revit.DB.Line.CreateBound(p3, p4);
Autodesk.Revit.DB.Line line4 = Autodesk.Revit.DB.Line.CreateBound(p4, p1);
CurveArray lines = new CurveArray();
lines.Append(line1);
lines.Append(line2);
lines.Append(line3);
lines.Append(line4);
return lines;
}
public static void PlaceFloors(Document doc, List<RevitFloor> rfloors, Level level)
{
if (rfloors == null || level == null)
{
return;
}
var fts = new FilteredElementCollector(doc).OfClass(typeof(FloorType));
FloorType defft = new FilteredElementCollector(doc).OfClass(typeof(FloorType)).FirstOrDefault() as FloorType;
var floorTypesCol = new FilteredElementCollector(doc).OfClass(typeof(FloorType));
FloorType targetFloorType = null;
using (Transaction trans = new Transaction(doc, "布置板"))
{
trans.Start();
foreach (RevitFloor rfloor in rfloors)
{
Floor fl = null;
//var targetFloorName = rfloor.Name.Substring(rfloor.Name.IndexOf('@') + 1);
var targetFloorName = rfloor.Thickness + "mm";
var floorTypes = from f in floorTypesCol
where f.Name == targetFloorName
select f as FloorType;
if (floorTypes.Count() == 0)
{
targetFloorType = defft.Duplicate(targetFloorName) as FloorType;
CompoundStructure compound = targetFloorType.GetCompoundStructure();
compound.SetLayerWidth(0, rfloor.Thickness / 304.8);
targetFloorType.SetCompoundStructure(compound);
}
else
{
targetFloorType = floorTypes.FirstOrDefault();
}
var arr = SetBoundary(rfloor.rOrigin, rfloor.Length / 304.8, rfloor.Width / 304.8);
fl = doc.Create.NewFloor(arr, targetFloorType, level, true);
fl.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM).Set(rfloor.Offest / 304.8);
doc.Regenerate();//设置完偏移量参数刷新
CreateOpenings(doc, fl, rfloor.Openings);
}
trans.Commit();
}
}
public static void CreateOpenings(Document doc, Floor floor, List<RevitOpening> ropenings)
{
foreach (var opening in ropenings)
{
try
{
XYZ p1 = new XYZ(opening.rOrigin.X, opening.rOrigin.Y - opening.Width / 304.8 / 2, opening.rOrigin.Z);
XYZ p2 = new XYZ(opening.rOrigin.X, opening.rOrigin.Y + opening.Width / 304.8 / 2, opening.rOrigin.Z);
XYZ p3 = new XYZ(opening.rOrigin.X + opening.Length / 304.8, opening.rOrigin.Y + opening.Width / 304.8 / 2, opening.rOrigin.Z);
XYZ p4 = new XYZ(opening.rOrigin.X + opening.Length / 304.8, opening.rOrigin.Y - opening.Width / 304.8 / 2, opening.rOrigin.Z);
CurveArray curves = new CurveArray();
Line l1 = Line.CreateBound(p1, p2);
Line l2 = Line.CreateBound(p2, p3);
Line l3 = Line.CreateBound(p3, p4);
Line l4 = Line.CreateBound(p4, p1);
curves.Append(l1);
curves.Append(l2);
curves.Append(l3);
curves.Append(l4);
doc.Create.NewOpening(floor, curves, true);
}
catch (Exception)
{
continue;
}
}
}
}
}