| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Media;
- using Autodesk.Revit.Attributes;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Plumbing;
- using Autodesk.Revit.UI;
- using Autodesk.Revit.UI.Selection;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Logger;
- using SAGA.MBI.Calc;
- using SAGA.MBI.DataArrange;
- using SAGA.MBI.Interaction;
- using SAGA.MBI.ModeInfoEdit;
- using SAGA.MBI.RevitExport;
- using SAGA.MBI.Tools;
- using SAGA.MBI.WinView.Login;
- using SAGA.MBI.WinView.Upload;
- using SAGA.RevitUtils;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.MEP;
- using WPfPointInfo;
- using Color = Autodesk.Revit.DB.Color;
- namespace SAGA.MBI
- {
- /// <summary>
- /// 上传当前楼层数据
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class ExportMbiModelCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- //WinPointInfoTest win = new WinPointInfoTest();
- //win.ShowDialog();
- var result = MessageShow.Question2("是否是整个项目导出");
- switch (result)
- {
- case System.Windows.Forms.DialogResult.Yes:
- {
- var floors = DalUploadFloor.GetHasFileFloors();
- List<CalcContext> contexts = new List<CalcContext>();
- foreach (UploadFloor floor in floors)
- {
- contexts.Add(new CalcContext(floor.MFloor));
- }
- MBIModelInfoUpload.UpdateMbiInfo(contexts);
- break;
- }
- case System.Windows.Forms.DialogResult.No:
- {
- MbiElementManager.ExecuteExport(ExternalDataWrapper.Current.Doc);
- var floorId = ExternalDataWrapper.Current.Doc.PathName.GetFileName();
- MBIModelInfoUpload.UploadMbiInfo(floorId);
- break;
- }
- }
-
- ////MbiElementManager.ExecuteExport(ExternalDataWrapper.Current.Doc);
- ////var floorId = ExternalDataWrapper.Current.Doc.PathName.GetFileName();
- ////MBIModelInfoUpload.UploadMbiInfo(floorId);
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
-
- //public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- //{
- // return true;
- //}
- }
- /// <summary>
- /// 管道系统类型
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class UpdatePipeSystemCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var pickElement=ExternalDataWrapper.Current.UiApp.PickElement("请选择水管", new PipeFilter());
- if (pickElement == null)
- {
- return Result.Succeeded;
- }
- using (Transaction tran = new Transaction(ExternalDataWrapper.Current.Doc,"连接"))
-
- {
- try
- {
- tran.Start();
- var equioment = ExternalDataWrapper.Current.UiApp.PickElement("选择设备");
- var pipeConnector = pickElement.GetConnectors(Domain.DomainPiping).FirstOrDefault(c => !c.IsConnected);
- var equipmentConnector = equioment.GetConnectors(Domain.DomainPiping).FirstOrDefault(c => !c.IsConnected);
- if (pipeConnector != null && equipmentConnector != null)
- {
- pipeConnector.ConnectTo(equipmentConnector);
- }
- tran.Commit();
- }
- catch (Exception ex)
- {
- MessageShow.Show(ex);
- tran.RollBack();
- }
- }
-
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- //public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- //{
- // return true;
- //}
- public class PipeFilter : ISelectionFilter
- {
- public bool AllowElement(Element elem)
- {
- return elem is Pipe;
- }
- public bool AllowReference(Reference reference, XYZ position)
- {
- return true;
- }
- }
- }
- /// <summary>
- /// 管道系统类型
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class CheckLocationCommand : ExternalCommand, IExternalCommandAvailability
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var pickElement = ExternalDataWrapper.Current.UiApp.PickElement("请选择设备", new EquipmentFilter()) as FamilyInstance;
- if (pickElement == null)
- {
- return Result.Succeeded;
- }
- using (Transaction tran = new Transaction(ExternalDataWrapper.Current.Doc, "显示"))
- {
- try
- {
- tran.Start();
- var doc = ExternalDataWrapper.Current.Doc;
- var s = pickElement.GetLocationPointMBIXYZ();
- DirectShape ds = DirectShape.CreateElement(doc,new ElementId(BuiltInCategory.OST_GenericModel));
- var solid = CreateSphereAt(s, 1);
- ds.AppendShape(new List<GeometryObject>() { solid });
- SetColor(ds,new Color(255, 0, 0));
- tran.Commit();
- ExternalDataWrapper.Current.UiApp.SetShowElements(new List<Element>() { ds });
- }
- catch (Exception ex)
- {
- MessageShow.Show(ex);
- tran.RollBack();
- }
- }
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- #region 静态方法
- /// <summary>
- /// 替换视图中的图形,来修改颜色
- /// </summary>
- public static void SetColor( Element elem, Color color, View view = null,
- int transparency = 0)
- {
- var ogs = new OverrideGraphicSettings();
- #region 表面填充图案
- //可见
- ogs.SetProjectionFillPatternVisible(true);
- //颜色
- ogs.SetProjectionFillColor(color);
- //透明度
- ogs.SetSurfaceTransparency(transparency);
- #endregion
- #region 截面填充图案
- //可见
- ogs.SetCutFillPatternVisible(true);
- //颜色
- ogs.SetCutFillColor(color);
- //填充图案
- #endregion
- if (view != null)
- {
- view.SetElementOverrides(elem.Id, ogs);
- }
- else
- {
- elem.Document.ActiveView.SetElementOverrides(elem.Id, ogs);
- }
- }
- /// <summary>
- /// 使用指定的球心和半径创建球体
- /// </summary>
- static public Solid CreateSphereAt(
- XYZ centre,
- double radius)
- {
- // 使用标准的全局坐标系创建 Frame
- Frame frame = new Frame(centre,
- XYZ.BasisX, XYZ.BasisY, XYZ.BasisZ);
- // 创建一个Z轴方向的半圆闭合曲线(注意所有的坐标都是相对全局坐标系的)
- Arc arc = Arc.Create(
- centre - radius * XYZ.BasisZ,
- centre + radius * XYZ.BasisZ,
- centre + radius * XYZ.BasisX);
- Line line = Line.CreateBound(
- arc.EndPoint(),
- arc.StartPoint());
- CurveLoop halfCircle = new CurveLoop();
- halfCircle.Append(arc);
- halfCircle.Append(line);
- List<CurveLoop> loops = new List<CurveLoop>(1);
- loops.Add(halfCircle);
- return GeometryCreationUtilities
- .CreateRevolvedGeometry(
- frame, loops, 0, 2 * Math.PI);
- }
- #endregion
- public class EquipmentFilter : ISelectionFilter
- {
- public bool AllowElement(Element elem)
- {
- return elem is FamilyInstance;
- }
- public bool AllowReference(Reference reference, XYZ position)
- {
- return true;
- }
- }
- }
- /// <summary>
- /// 测试多项目读取
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class TestMultipleReadDocCommand : ExternalCommand
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- try
- {
- var floors = DalProjectTree.GetAllFloors(false);
- var floor = floors.FirstOrDefault();
- var doc = ExternalDataWrapper.Current.App.OpenDocumentFile(floor.FullPath);
- var eles = doc.GetAllElements();
- string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),$"{floor}_{System.DateTime.Now.Millisecond}.txt");
- //File.Create(path);
- foreach (Element element in eles)
- {
- File.AppendAllText(path,$"{element.Id}:{element.Name}\r\n");
- }
- }
- catch (Exception e)
- {
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- return Result.Succeeded;
- }
- public override bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return true;
- }
- }
- /// <summary>
- /// 掉转阀件方向
- /// </summary>
- [Transaction(TransactionMode.Manual)]
- [Regeneration(RegenerationOption.Manual)]
- public class ReDirectionCommand : ExternalCommand
- {
- public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
- {
- using (Transaction tran = new Transaction(ExternalDataWrapper.Current.Doc, "调整阀门"))
- {
- tran.Start();
- try
- {
- var pickElement = ExternalDataWrapper.Current.UiApp.PickElement("请选择阀件", typeof(FamilyInstance)) as FamilyInstance;
- if (pickElement == null)
- {
- return Result.Succeeded;
- }
- var handling = pickElement.HandOrientation;
- var facing = pickElement.FacingOrientation;
- var location = pickElement.GetLocationPoint();
- var axis = location.NewLine(location + handling.CrossProduct(facing));
- var connectors = pickElement.GetAllConnectors();
- List<Connector> refConnectors = new List<Connector>();
- foreach (var connector in connectors)
- {
- if (!ConnectorType.Physical.HasFlag(connector.ConnectorType))
- {
- continue;
- }
- var tempRefs = connector.GetReferenceConnectors();
- foreach (var tempRef in tempRefs)
- {
- if (ConnectorType.Physical.HasFlag(tempRef.ConnectorType)&& tempRef.IsConnectedTo(connector))
- {
- connector.DisconnectFrom(tempRef);
- refConnectors.Add(tempRef);
- }
- }
- }
- ElementTransformUtils.RotateElement(pickElement.Document, pickElement.Id, axis, Math.PI);
- pickElement.Document.Regenerate();
- connectors = pickElement.GetAllConnectors();
- for (int i = 0; i < refConnectors.Count; i++)
- {
- var refCon = refConnectors[i];
- if (refCon == null)
- continue;
- var maxLength = double.MaxValue;
- Connector bConnector = null;
- foreach (var connector in connectors)
- {
- var newLength = refCon.Origin.Subtract(connector.Origin).GetLength();
- if (newLength < maxLength&&!connector.IsConnected)
- {
- maxLength = newLength;
- bConnector = connector;
- }
- }
- if (bConnector != null)
- {
- bConnector.ConnectTo(refCon);
- }
- }
- tran.Commit();
- }
- catch (Exception e)
- {
- tran.RollBack();
- MessageShow.Show(e);
- return Result.Cancelled;
- }
- }
-
- return Result.Succeeded;
- }
- public override bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
- {
- return true;
- }
- }
- }
|