123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /* ==============================================================================
- * 功能描述:CalcContext
- * 创 建 者:Garrett
- * 创建日期:2018/4/1 11:19:24
- * ==============================================================================*/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.Revit.DB;
- using DevExpress.Mvvm.Native;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Data;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Logger;
- using SAGA.MBI.DataArrange;
- using SAGA.MBI.Model;
- using SAGA.MBI.RequestData;
- using SAGA.MBI.Tools;
- using SAGA.MBI.WinView.PositionBeaconModeling;
- using SAGA.MBI.WinView.TraceView;
- using SAGA.MBI.WinView.Upload;
- using SAGA.RevitUtils;
- using SAGA.RevitUtils.Extends;
- namespace SAGA.MBI.Calc
- {
- /// <summary>
- /// CalcContext
- /// </summary>
- public class CalcContext : InitObejct
- {
- public CalcContext(MFloor mFloor) : base()
- {
- MFloor = mFloor;
- }
- public MFloor MFloor { get; set; }
- public string FloorId { get { return MFloor.Id; } }
- public Document RevitDoc { get; set; }
- [InitAttribute]
- public ObservableCollection<MISpace> MSpaces { get; set; }
- [InitAttribute]
- public ObservableCollection<MEquipment> MEquipments { get; set; }
- [InitAttribute]
- public ObservableCollection<MEquipmentPart> MEquipmentParts { get; set; }
- [InitAttribute]
- public List<RltEquipInSpace> RltEquipInSpaces { get; set; }
- /// <summary>
- /// 信标
- /// </summary>
- [InitAttribute]
- public ObservableCollection<MBeacon> MBeacons { get; set; }
- [InitAttribute]
- public List<RltBeaconinElementSp> RltBeaconinElementSps { get; set; }
- /// <summary>
- /// 未分组的待更新的关系数据
- /// </summary>
- [InitAttribute]
- public List<MGraphRelationBase> UnGroupDatas { get; set; }
- /// <summary>
- /// 打开项目
- /// </summary>
- public Document OpenDocument()
- {
- string path = MFloor.FullPath;
- var uiApp = ExternalDataWrapper.Current.UiApp;
- RevitDoc = uiApp.Application.OpenDocumentFile(path);
- return RevitDoc;
- }
- /// <summary>
- /// 关闭项目
- /// </summary>
- public void CloseDocument()
- {
- RevitDoc?.CloseDoc();
- }
- public bool Upload()
- {
- bool result = true;
- Log4Net.Debug("更新楼层数据开始");
- try
- {
- //修改空间
- DalSpace.BatchUpdate(MSpaces);
- //修改设备
- DalEquip.BatchUpdate(MEquipments);
- //修改部件
- DalCommon.BatchUpdate(MEquipmentParts);
- //修改设备所在空间关系
- DalCommon.BetchUpdateRelation(RltEquipInSpaces);
- //修改信标
- DalCommon.BatchUpdate(MBeacons);
- //修改信标所在元空间
- DalCommon.BetchUpdateRelation(RltBeaconinElementSps);
- //更新未分组的数据
- DalCommon.BetchUpdateRelation(UnGroupDatas);
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- result = false;
- }
- Log4Net.Debug("更新楼层数据结束");
- return result;
- }
- /// <summary>
- /// 根据用户选择,进行删除,或者清除BimId的保留
- /// </summary>
- /// <returns></returns>
- public bool Del()
- {
- bool result = true;
- try
- {
- //修改空间
- MSpaces.ForEach(t => t.DelOrClearBIMIDSave());
- //修改设备
- MEquipments.ForEach(t => t.DelOrClearBIMIDSave());
- //修改部件
- MEquipmentParts.ForEach(t => t.DelOrClearBIMIDSave());
- //修改信标
- MBeacons.ForEach(t => t.DelOrClearBIMIDSave());
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- result = false;
- }
- return result;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="mode"></param>
- public void Add(MRevitEquipBase mode)
- {
- if (mode == null) return;
- if (mode is MEquipment equip)
- {
- this.MEquipments.Add(equip);
- }
- else if (mode is MEquipmentPart part)
- {
- this.MEquipmentParts.Add(part);
- }
- else if (mode is MISpace space)
- {
- this.MSpaces.Add(space);
- }
- else if (mode is MBeacon beacon)
- {
- this.MBeacons.Add(beacon);
- }
- }
- /// <summary>
- /// 查找云平台是否有此条数据
- /// </summary>
- /// <param name="bimId"></param>
- /// <returns></returns>
- public MRevitEquipBase FindItem(string bimId)
- {
- MRevitEquipBase mode = null;
- while (true)
- {
- mode = MSpaces.FirstOrDefault(t => t.BimID == bimId);
- if (mode != null) break;
- mode = MEquipments.FirstOrDefault(t => t.BimID == bimId);
- if (mode != null) break;
- mode = MEquipmentParts.FirstOrDefault(t => t.BimID == bimId);
- if (mode != null) break;
- mode = MBeacons.FirstOrDefault(t => t.BimID == bimId);
- if (mode != null) break;
- break;
- }
- return mode;
- }
- /// <summary>
- /// 集合不为null
- /// </summary>
- /// <returns></returns>
- public bool HasItem()
- {
- bool result = false;
- foreach (PropertyInfo info in base.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
- {
- try
- {
- Type type = info.PropertyType;
- if (type.IsClass && !type.IsAbstract)
- {
- InitAttribute[] customAttributes = info.GetCustomAttributes(typeof(InitAttribute), true) as InitAttribute[];
- if (customAttributes.Any<InitAttribute>())
- {
- if (type == typeof(string))
- {
- info.SetValue(this, string.Empty, null);
- }
- else
- {
- var item = info.GetValue(this) as IEnumerable;
- if (item?.GetCount() > 0)
- {
- result = true;
- break;
- }
- }
- }
- }
- }
- catch (Exception)
- {
- }
- }
- return result;
- }
- /// <summary>
- /// 写到临时日志文件
- /// </summary>
- /// <param name="operate"></param>
- public void WriteToTempLogFile(DocumentChangedOperator operate = DocumentChangedOperator.None)
- {
- string pathName = MFloor.FullPath;
- //楼层文件名称,无后缀
- var docName = pathName.GetFileName();
- var doc = this.RevitDoc;
- Action<MRevitEquipBase> writeToLogAction = (t) =>
- {
- DocumentChangedLogMode mode = new DocumentChangedLogMode();
- mode.DataTime = DateTime.Now.ToString();
- mode.DocPath = pathName;
- mode.DocName = docName;
- mode.Operator = t.Operator != DocumentChangedOperator.None ? t.Operator.ToString() : operate.ToString();
- mode.Id = t.BimID.GetBIMID().ToString();
- if (doc?.IsValidObject == true)
- {
- var elem = doc.GetElement(mode.Id.ToInt());
- if (elem != null)
- {
- mode.Type = elem.GetType().Name;
- mode.Family = elem.GetFamily()?.Name;
- mode.Category = elem.GetCategory().ToString();
- }
- }
- DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
- };
- //修改空间
- MSpaces.ForEach(writeToLogAction);
- //修改设备
- MEquipments.ForEach(writeToLogAction);
- //修改部件
- MEquipmentParts.ForEach(writeToLogAction);
- //修改信标
- MBeacons.ForEach(writeToLogAction);
- }
- }
- }
|