123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /* ==============================================================================
- * 功能描述:检查模型中那些设备没有在空间中
- * 创 建 者:Garrett
- * 创建日期:2018/7/11 14:34:26
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SAGA.MBI.Calc;
- using SAGA.MBI.Model;
- using SAGA.MBI.WinView.Upload;
- using Autodesk.Revit.DB;
- using Microsoft.Win32;
- using SAGA.DotNetUtils.Logger;
- using SAGA.MBI.DataArrange;
- using SAGA.MBI.Tools;
- using SAGA.RevitUtils.Extends;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Others;
- using ICell= NPOI.SS.UserModel.ICell;
- using CellType= NPOI.SS.UserModel.CellType;
- namespace SAGA.MBI.ToolsData
- {
- /// <summary>
- /// CheckEquipInSpace
- /// </summary>
- public class CheckEquipInSpace
- {
- /// <summary>
- /// 导出没有在空间中的设备和部件
- /// </summary>
- public static void Export()
- {
- var floors = DalUploadFloor.GetHasFileFloors();
- List<CalcContext> contexts = new List<CalcContext>();
- foreach (UploadFloor floor in floors)
- {
- var context = GetEquipNotInSpaceCalcContext(floor.MFloor);
- contexts.Add(context);
- }
- ExportToExcel(contexts);
- }
- /// <summary>
- /// 重新上传设备所在空间关系数据
- /// </summary>
- public static void ReuploadEquipInElementSp()
- {
- var doc = ExternalDataWrapper.Current.Doc;
- var floorid=doc.PathName.GetFileName();
- MFloor floor = DalProjectTree.GetFloorByFloorId(floorid);
- CalcContext context = DalCommon.DownLoadFloorDataByBIMFloorInfo(floor);
- context.OpenDocument();
- var document = context.RevitDoc;
- var spaces = context.MSpaces;
- var rspaces = context.RevitDoc.GetSpaces().Where(t => t.IsValidObject).ToList();
- foreach (var equipment in context.MEquipments)
- {
- var bimId = equipment.BimID;
- Element fi=document.GetElement(bimId.GetBIMID());
- if (fi != null)
- {
- var space = fi.GetReferenceSpace(rspaces);
- if (space != null)
- {
- var spaceBimid = space.GetCloudBIMId();
- var cspace = spaces.FirstOrDefault(t => spaceBimid == t.BimID);
- if (cspace != null)
- {
- var relation=new RltEquipInSpace(cspace,equipment){Operator = DocumentChangedOperator.Add};
- context.UnGroupDatas.Add(relation);
- }
- }
- }
- }
- DalCommon.BetchUpdateRelation(context.UnGroupDatas);
- }
- /// <summary>
- /// 获取没有在空间中的设备
- /// </summary>
- /// <param name="floor"></param>
- /// <returns></returns>
- private static CalcContext GetEquipNotInSpaceCalcContext(MFloor floor)
- {
- CalcContext context = new CalcContext(floor);
- context.OpenDocument();
- try
- {
- CalcEquipPartRlt(context);
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- finally
- {
- context.RevitDoc.CloseDoc();
- }
- return context;
- }
- /// <summary>
- /// 读取空间中的设备、部件、设备所在空间的关系
- /// </summary>
- /// <param name="context"></param>
- private static void CalcEquipPartRlt(CalcContext context)
- {
- var linkElements = context.RevitDoc.GetMbiElements();
- var rspaces = context.RevitDoc.GetSpaces();
- //过滤出的所有实体
- foreach (Element fi in linkElements)
- {
- if (fi.IsEquipment())
- {
- MEquipment equipment = DalEquip.GetEquipment(fi);
- if (equipment == null) continue;
- //识别设备所在空间
- var space = fi.GetReferenceSpace(rspaces);
- if (space == null)
- {
- string familyName = fi.GetFamilyName();
- var id = fi.Id.ToString();
- equipment.EquipClassCode = familyName;
- equipment.BimID = id;
- context.MEquipments.Add(equipment);
- }
- }
- // 在这里处理部件
- if (fi.IsEquipmentPart())
- {
- MEquipmentPart part = DalEquipPart.GetEquipPart(fi);
- if (part == null) continue;
- context.MEquipmentParts.Add(part);
- //识别设备所在空间
- var space = fi.GetReferenceSpace(rspaces);
- if (space == null)
- {
- string familyName = fi.GetFamilyName();
- var id = fi.Id.ToString();
- part.EquipClassCode = familyName;
- part.BimID = id;
- context.MEquipmentParts.Add(part);
- }
- }
- }
- }
- /// <summary>
- /// 导出数据
- /// </summary>
- /// <param name="xyzsList"></param>
- public static void ExportToExcel(List<CalcContext> contexts)
- {
- SaveFileDialog sflg = new SaveFileDialog();
- sflg.Filter = "Excel(*.xlsx)|*.xlsx";
- if (sflg.ShowDialog() != true) return;
- ExportToExcel(contexts, sflg.FileName, sflg.FilterIndex);
- }
- /// <summary>
- /// 导出数据到指定目录
- /// </summary>
- /// <param name="xyzsList"></param>
- /// <param name="fileName"></param>
- private static void ExportToExcel(List<CalcContext> contexts, string fileName, int filterIndex = 0)
- {
- try
- {
- IWorkbook book = new XSSFWorkbook();
- ISheet sheet = book.CreateSheet("Summary");
- #region 添加表头
- IRow row = sheet.CreateRow(0);
- NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
- cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
- cell0.SetCellValue("楼层名称");
- ICell cell1 = row.CreateCell(1);
- cell1.SetCellType(CellType.String);
- cell1.SetCellValue("楼层所在位置");
- ICell cell2 = row.CreateCell(2);
- cell2.SetCellType(CellType.String);
- cell2.SetCellValue("族名称");
- ICell cell3 = row.CreateCell(3);
- cell3.SetCellType(CellType.String);
- cell3.SetCellValue("设备id");
- #endregion
- #region 添加数据
- int index = 0;
- foreach (var context in contexts)
- {
- var floorName = context.MFloor.ToString();
- var floorPath = context.MFloor.FullPath;
- foreach (var equip in context.MEquipments)
- {
- var family = equip.EquipClassCode;
- var id = equip.BimID;
- index++;
- row = sheet.CreateRow(index);
- ICell cell = row.CreateCell(0, CellType.String);
- cell.SetCellValue(floorName);
- cell = row.CreateCell(1, CellType.String);
- cell.SetCellValue(floorPath);
- cell = row.CreateCell(2, CellType.String);
- cell.SetCellValue(family);
- cell = row.CreateCell(3, CellType.String);
- cell.SetCellValue(id);
- }
- }
- #endregion
- #region 写入
- MemoryStream ms = new MemoryStream();
- book.Write(ms);
- book = null;
- using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- byte[] data = ms.ToArray();
- fs.Write(data, 0, data.Length);
- fs.Flush();
- }
- ms.Close();
- ms.Dispose();
- #endregion
- }
- catch (Exception e)
- {
- MessageShowBase.Show(e);
- }
- }
- }
- }
|