123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663 |
- /* ==============================================================================
- * 功能描述:Command
- * 创 建 者:Garrett
- * 创建日期:2018/3/27 11:42:20
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Windows;
- using Autodesk.Revit.Attributes;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.ExtensibleStorage;
- using Autodesk.Revit.DB.Mechanical;
- using Autodesk.Revit.UI;
- using CEFSharpWpf;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Logger;
- using SAGA.MBI.Calc;
- using SAGA.MBI.Common;
- using SAGA.MBI.DataArrange;
- using SAGA.MBI.Interaction;
- using SAGA.MBI.Login;
- using SAGA.MBI.Model;
- using SAGA.MBI.RequestData;
- using SAGA.MBI.Tools;
- using SAGA.MBI.WinView;
- using SAGA.MBI.WinView.BeModingDutyList;
- using SAGA.MBI.WinView.Login;
- using SAGA.MBI.WinView.ModeInfoMaintenance;
- using SAGA.MBI.WinView.PositionBeaconModeling;
- using SAGA.MBI.WinView.Space;
- using SAGA.MBI.WinView.Upload;
- using SAGA.Models;
- using SAGA.RevitUtils;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.Windows;
- using Application = Autodesk.Revit.Creation.Application;
- using Visibility = System.Windows.Visibility;
- namespace SAGA.MBI
- {
- /// <summary>
- /// 打开楼层模型
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class OpenModeFileCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- WinOpenFile win = new WinOpenFile();
- win.ShowWindow();
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return true;
- }
- }
- /// <summary>
- /// 模型文件管理
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class EditModeFileCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- Log4Net.Debug("开始命令:模型文件管理");
- if (DalUploadFloor.IsAllFloorClosed())
- {
- WinModeFileManage win = new WinModeFileManage();
- win.ShowDialog();
- }
- Log4Net.Debug("结束命令:模型文件管理");
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return true;
- }
- }
- /// <summary>
- /// 申请模型权限
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class LockFloorCommand : ExternalCommand
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var doc = ExternalDataWrapper.Current.Doc;
- if (doc == null) return Result.Cancelled;
- string floorId = doc.PathName.GetFileName();
- DalLock.LockFloor(floorId,true);
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- /// <summary>
- /// 模型信息点值维护
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class EditModeInfoCommand : ExternalCommand//, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- WinModeInfoMaintenance win = WinModeInfoMaintenance.GetWindow();
- if (win.Visibility != Visibility.Visible)
- {
- win.Show();
- IdlingEditInstance.RefrushInstance();
- ExternalDataWrapper.Current.UiApp.Idling += IdlingEditInstance.EditInstance_Idling;
- }
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- ExternalDataWrapper.Current.UiApp.Idling -= IdlingEditInstance.EditInstance_Idling;
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- /// <summary>
- /// 待建模型清单
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class BeModeDutyListCommand : ExternalCommand
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- Log4Net.Debug("开始命令:待建模型清单");
- var doc = ExternalDataWrapper.Current.Doc;
- if (doc == null) return Result.Cancelled;
- VMBeModeDutyList vm=new VMBeModeDutyList();
- WinBeModeDutyList win=new WinBeModeDutyList();
- win.DataContext = vm;
- win.Show();
- Log4Net.Debug("结束命令:待建模型清单");
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- /// <summary>
- /// 未实体化模型清单
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class ShowMissFMListCommand : ExternalCommand//, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- Log4Net.Debug("开始命令:未实体化模型清单");
- var doc = ExternalDataWrapper.Current.Doc;
- if (doc == null) return Result.Cancelled;
- string floorId = doc.PathName.GetFileName();
- WinMissFMList win = new WinMissFMList(floorId);
- win.ShowDialog();
- Log4Net.Debug("结束命令:未实体化模型清单");
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- /// <summary>
- /// 高亮显示空模型
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class HeightLightMissFMListCommand : ExternalCommand//, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var doc = ExternalDataWrapper.Current.Doc;
- if (doc == null) return Result.Cancelled;
- string floorId = doc.PathName.GetFileName();
- WinMissFMList win = new WinMissFMList(floorId);
- List<Element> ids = win.MissFMEquips.Select(t => doc.GetElement(t.Equipment.BimID.GetBIMID())).Where(tt => tt != null).ToList();
- ExternalDataWrapper.Current.UiApp.SetShowElements(ids);
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- /// <summary>
- /// 位置信标建模
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class PositionBeaconModelingCommand : ExternalCommand//, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var doc = ExternalDataWrapper.Current.Doc;
- if (doc == null) return Result.Cancelled;
- string floorId = doc.PathName.GetFileName();
- WinBeacon win = new WinBeacon(floorId);
- win.Show();
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- /// <summary>
- /// 上传模型
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class UploadModeCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- Log4Net.Debug("开始命令:上传模型");
- Log4Net.Debug("开始检查:检查所有的模型是否关闭");
- if (DalUploadFloor.IsAllFloorClosed())
- {
- Log4Net.Debug("结束检查:检查所有的模型是否关闭");
- Log4Net.Debug("开始检查:是否有需要下载的文件");
- if (DalUploadFloor.IsNeedUpdataFile())
- return Result.Succeeded;
- Log4Net.Debug("结束检查:是否有需要下载的文件");
- Log4Net.Debug("开始功能:准备需要上传的数据");
- VMUploadModeManage vm = DalUploadFloor.PrepareUploadFloors();
- Log4Net.Debug("结束功能:准备需要上传的数据");
- WinUploadModeManage win = new WinUploadModeManage();
- win.DataContext = vm;
- win.ShowDialog();
- }
- Log4Net.Debug("结束命令:上传模型");
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- /// <summary>
- /// Onlys show the dialog when a document is open, as Dockable dialogs are only available
- /// when a document is open.
- /// </summary>
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return true;
- }
- }
-
- /// <summary>
- /// 空间管理
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class CreateSpaceCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
-
- var win = new WinCreateSpaces();
- win.Show();
- return Result.Succeeded;
- }
- /// <summary>
- /// 创建空间分隔符
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="virLines"></param>
- /// <param name="deletedPolylines"></param>
- public void CreateSpaceSeqarate(Document doc, List<PointPair> virLines, List<int> deletedPolylines)
- {
- using (Transaction trans = new Transaction(doc, "创建空间分隔符"))
- {
- trans.Start();
- try
- {
- FailureHandlingOptions fho = trans.GetFailureHandlingOptions();
- fho.SetFailuresPreprocessor(new FailuresPreprocessor(false));//这里不对冲突进行捕捉,创建空间的时候会重新捕捉
- trans.SetFailureHandlingOptions(fho);
- //读取空间所在视图
- var view = doc.GetElements<ViewPlan>().FirstOrDefault(t =>
- t.Name?.IndexOf("-saga") > -1 && t.ViewType == ViewType.FloorPlan);
- if (view == null)
- {
- MessageBox.Show("无法找到-saga标志的视图");
- return;
- }
- //创建空间分隔符
- CreateSpaceSeparationLines(view, doc, virLines);
- //删除已有的空间分隔符
- doc.Delete(deletedPolylines.Select(id => new ElementId(id)).ToList());
- trans.Commit();
- }
- catch (Exception e)
- {
- trans.RollBack();
- }
- }
- }
- /// <summary>
- /// 自动创建空间
- /// </summary>
- public void AutoCreateSpaces()
- {
- string SpaceKey = "UKJ";
- string ASpaceKey = "UAKJ";
- IntPtr mainWindowHandle = RevitProcess.GetMainWindowHandle();
- PressHelper.KeyPress(mainWindowHandle, SpaceKey);
- ////发送自动放置空间
- PressHelper.KeyPress(mainWindowHandle, ASpaceKey);
- //ESC
- PressHelper.KeyPress(mainWindowHandle, (char)27);
- PressHelper.KeyPress(mainWindowHandle, (char)27);
- PressHelper.KeyPress(mainWindowHandle, (char)27);
- }
- /// <summary>
- /// 创建空间分隔符
- /// </summary>
- /// <param name="view"></param>
- /// <param name="doc"></param>
- /// <param name="pairs"></param>
- public static void CreateSpaceSeparationLines(ViewPlan view, Document doc, List<PointPair> pairs)
- {
- double z = view.GenLevel.Elevation;
- //创建
- XYZ axis = XYZ.BasisZ;
- XYZ vecX = XYZ.BasisX;
- XYZ normal = vecX.CrossProduct(vecX.VectorRotate(axis, Math.PI / 2));
- Plane p = new Plane(normal, XYZ.Zero);
- var sp = SketchPlane.Create(doc, p);
- foreach (var pair in pairs)
- {
- var curveArray = new CurveArray();
- XYZ start = pair.RPoint[0].NewZ(z);
- XYZ end = pair.RPoint[1].NewZ(z);
- Line line = null;
- try
- {
- line = start.NewLine(end);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.StackTrace);
- }
- if (line != null)
- {
- curveArray.Append(line);
- var newLIneId = doc.Create.NewSpaceBoundaryLines(sp, curveArray, view).get_Item(0).Id;
- pair.ElementId = newLIneId.IntegerValue;
- if (pair.ShowLine != null)
- pair.ShowLine.Tag = newLIneId.IntegerValue;
- }
- }
- }
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return true;
- }
- /// <summary>
- /// 手动创建空间
- /// </summary>
- /// <param name="doc"></param>
- public void CreateSpace(Document doc)
- {
- using (Transaction trans = new Transaction(doc, "创建空间"))
- {
- {
- trans.Start();
- try
- {
- //检测是否有空间标记,没有空间标记加载
- //doc.LoadFamilySymbolExt(@"C:\ProgramData\Autodesk\RVT 2016\Libraries\China\注释\标记\机械\空间标记.rfa");
- doc.LoadFamilySymbolExt(MBIConst.SpaceTagFamilyFilePath);
- FailureHandlingOptions fho = trans.GetFailureHandlingOptions();
- fho.SetFailuresPreprocessor(new FailuresPreprocessor());
- trans.SetFailureHandlingOptions(fho);
- var view = doc.GetElements<ViewPlan>().FirstOrDefault(t =>
- t.GenLevel?.Name != null && t.Name?.IndexOf("-saga") > -1 &&
- t.ViewType == ViewType.FloorPlan);
- if (view != null)
- {
- Parameter para = view.get_Parameter(Autodesk.Revit.DB.BuiltInParameter.VIEW_PHASE);
- Autodesk.Revit.DB.ElementId phaseId = para.AsElementId();
- var phase = doc.GetElement(phaseId) as Phase;
- var level = view.GenLevel;
- //如果视图范围不对,也不能很好的创建空间
- ICollection<ElementId> elements = doc.Create.NewSpaces2(level, phase, view);
- //创建20个未放置的空间
- //var spaces= doc.Create.NewSpaces2(phase, 20);
- //if (elements == null) MessageBox.Show("无法创建空间,请检查视图范围是否正确!");
- }
- else
- {
- Autodesk.Revit.UI.TaskDialog.Show("Revit", "没有找到名称以-saga结尾的平面");
- }
- doc.Regenerate();
- trans.Commit();
- }
- catch (Exception e)
- {
- trans.RollBack();
- MessageShow.Show(e);
- }
- }
- }
- }
- }
- /// <summary>
- /// 存储静态冲突数据
- /// </summary>
- public class StaticData
- {
- public static List<List<ElementId>> FailuresPreprocessorData = new List<List<ElementId>>();
- }
- /// <summary>
- /// 事务冲突处理
- /// </summary>
- public class FailuresPreprocessor : IFailuresPreprocessor
- {
- readonly bool m_isAdd;
- public FailuresPreprocessor(bool isAdd = true)
- {
- m_isAdd = isAdd;
- }
- public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
- {
- IList<FailureMessageAccessor> listFma = failuresAccessor.GetFailureMessages();
- if (listFma.Count == 0)
- return FailureProcessingResult.Continue;
- foreach (FailureMessageAccessor fma in listFma)
- {
- if (fma.GetSeverity() == FailureSeverity.Error)
- {
- if (fma.HasResolutions())
- failuresAccessor.ResolveFailure(fma);
- }
- if (fma.GetSeverity() == FailureSeverity.Warning)
- {
- if (m_isAdd)
- {
- var doc = failuresAccessor.GetDocument();
- //获取所有的冲突element
- var elementIds = fma.GetFailingElementIds();
- StaticData.FailuresPreprocessorData.Add(elementIds.ToList());
- }
- failuresAccessor.DeleteWarning(fma);
- }
- }
- return FailureProcessingResult.ProceedWithCommit;
- }
- }
- /// <summary>
- /// 输出日志,调试用
- /// </summary>
- public class Logs
- {
- public static void Log(string msg)
- {
- var logs = $"{DateTime.Now}======={msg}";
- System.Diagnostics.Debug.WriteLine(logs);
- // WriteLogs(logs);
- }
- }
- /// <summary>
- /// 同步模型本地名称
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class SyncLocalNameCommand : ExternalCommand//, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var floors = DalUploadFloor.GetHasFileFloors();
- List<CalcContext> contexts = new List<CalcContext>();
- foreach (UploadFloor floor in floors)
- {
- contexts.Add(new CalcContext(floor.MFloor));
- }
- MBIModelInfoManager.SyncPlatformToRevit(contexts);
- //var doc = ExternalDataWrapper.Current.Doc;
- //MBIModelInfoManager.SyncPlatformToRevit(doc);
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return false;
- }
- }
- }
|