浏览代码

Merge branch 'master' of http://39.106.8.246:3003/revit/RevitExport

mengxiangge 5 年之前
父节点
当前提交
8fea5ac8c4

+ 1 - 2
JBIM/ExportStart/Program.cs

@@ -42,8 +42,7 @@ namespace ExportStart
             try
             {
                 Console.WriteLine("Start Export");
-                RevitToJBim.TestExport.Document = doc;
-                RevitToJBim.TestExport.Export();
+                RevitToJBim.MbiExport.Export(doc);
 
                 Console.WriteLine("End Export");
             }

+ 0 - 6
JBIM/RevitToJBim/ExportDataBuilder.cs

@@ -31,21 +31,15 @@ namespace RevitToJBim
             List<ElementWrapper> wrappers = new List<ElementWrapper>();
             //应对传入元素,不是document全集的情况
             //FilteredElementCollector collector = new FilteredElementCollector(doc, elements.Select(e => e.Id).ToList());
-
             #region Unit
-
             var formatOptions = doc.GetUnits().GetFormatOptions(UnitType.UT_Length);
             ElementWrapper wrapper = new ElementWrapper(formatOptions, null);
             wrappers.Add(wrapper);
-
             #endregion
             FilteredElementCollector collector = new FilteredElementCollector(doc);
             wrappers.AddRange(collector.Clone().FilterElements<Wall>().Select(e => new ElementWrapper(e)));
             wrappers.AddRange(collector.Clone().FilterElements<CurveElement>(BuiltInCategory.OST_MEPSpaceSeparationLines).Select(e => new ElementWrapper(e)));
             var sourceSpaces = collector.Clone().FilterElements<SpatialElement>(BuiltInCategory.OST_MEPSpaces).OfType<Space>().Where(t=>t.IsSpace()).ToList();
-            //附加逻辑判断
-            //  var originSpaces = collector.Clone().GetUseSpaces();
-
             wrappers.AddRange(sourceSpaces.Select(e => new ElementWrapper(e)));
 
             wrappers.AddRange(collector.Clone().FilterElements<Pipe>().Select(e => new ElementWrapper(e)));

+ 18 - 11
JBIM/RevitToJBim/JBimParseContext.cs

@@ -32,7 +32,7 @@ namespace RevitToJBim
             RelationShips = new List<ElementRelationShip>();
         }
         public RevitToJBimParser Parser { get; private set; }
-        public BimDocument Document { get;private set; }
+        protected BimDocument Document { get;private set; }
         
         #region 数据缓存相关
 
@@ -85,11 +85,9 @@ namespace RevitToJBim
         }
         #endregion
 
-        #region 序列化相关信息
-
-        public string Serialize()
+        #region 整理document数据
+        private void ArrangeBimDocument()
         {
-
             #region 关系数据处理
             /*
              * 当前关系数据是一个临时数据,需要不全信息之后才能真实使用
@@ -108,8 +106,6 @@ namespace RevitToJBim
                 }
             }
             #endregion
-
-            return BimJsonUtil.Serializer(Document);
         }
         /// <summary>
         /// 获取关联对象的BimId
@@ -132,7 +128,7 @@ namespace RevitToJBim
         }
         private RelationshipBase ConvertRelationShipInfo(ElementRelationShip ship)
         {
-            BimId relatingid = GetRelatingBimId(ship);    
+            BimId relatingid = GetRelatingBimId(ship);
             RelationshipBase useShip = null;
             if (relatingid == null)
             {
@@ -145,10 +141,10 @@ namespace RevitToJBim
                 {
                     useShip = new OneToOneRel(relatingid, relatedid);
                 }
-                
+
             }
             else if (ship is ElementOneToManyRel manyRel)
-            {              
+            {
                 var relatedIds = GetBimIds(manyRel.RelatedObjects);
                 if (relatedIds != null)
                 {
@@ -174,7 +170,7 @@ namespace RevitToJBim
             if (ship is ElementOneToOneRel singleRel)
             {
                 var relatedid = GetBimId(singleRel.RelatedObject);
-                
+
                 if (relatedid != null)
                 {
                     BimObjectUtil.AcceptRelation(bimObject, ship.PropertyName, relatedid);
@@ -192,5 +188,16 @@ namespace RevitToJBim
             return;
         }
         #endregion
+
+        /// <summary>
+        /// 获取结果Document
+        /// </summary>
+        /// <returns></returns>
+        public BimDocument GetResultDocument()
+        {
+            //没有经过整理的document是半成品,所以经document对应的属性取消公开
+            ArrangeBimDocument();
+            return Document;
+        }
     }
 }

+ 4 - 40
JBIM/RevitToJBim/JsonConverter/BimJsonUtil.cs

@@ -14,30 +14,18 @@ using JBIM;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Converters;
 using Newtonsoft.Json.Linq;
+using RevitToJBim.MBI;
 
 namespace RevitToJBim.JsonConverter
 {
     public static class BimJsonUtil
     {
-        public static string Serializer(BimDocument document)
+        public static string Serializer(MBIDocument document)
         {
             JsonSerializerSettings jsetting = CreateDefaultSettings();
-            var bimObjects = document.BimObjects;
-            var group = bimObjects.GroupBy(bim => bim.ElementType);
-            Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
-            foreach (var collection in group)
-            {
-                dic[collection.Key + "s"] = collection.ToList();
-            }
-
-            var documentObject = new {MBIName = "PjXXX_001", PlanName = "F1-saga", Elements = dic};
-            //var serializer = JsonSerializer.Create(jsetting);
-            //var jobject=JObject.FromObject(dic, serializer);
-            //jobject.ToString();
-            string dataStr =JsonConvert.SerializeObject(documentObject, jsetting);
+            string dataStr =JsonConvert.SerializeObject(document, jsetting);
             return dataStr;
         }
-
         private static JsonSerializerSettings CreateDefaultSettings()
         {
             JsonSerializerSettings jsetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
@@ -48,30 +36,6 @@ namespace RevitToJBim.JsonConverter
             jsetting.Converters.Add(new ParameterConverter());
             return jsetting;
         }
-        private static DocumentDto ConverToDto(BimDocument document)
-        {
-            DocumentDto dto = new DocumentDto();
-            var bimObjects = document.BimObjects;
-            var group = bimObjects.GroupBy(bim => bim.ElementType);
-            Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
-            foreach (var collection in group)
-            {
-                dic[collection.Key + "s"] = collection.ToList();
-            }
-
-            dto.MBIName = "PjXXX_001";//待读取
-            dto.PlanName = "F1-saga";
-            dto.Elements = dic;
-            return dto;
-        }
-    }
-    /// <summary>
-    /// 项目传输数据结构
-    /// </summary>
-    public class DocumentDto
-    {
-        public string MBIName { get; set; }
-        public string PlanName { get; set; }
-        public Dictionary<string, List<BimObject>> Elements { get; set; }
     }
+    
 }

+ 51 - 0
JBIM/RevitToJBim/MBI/MBIDocument.cs

@@ -0,0 +1,51 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:MBIDocument
+ * 作者:xulisong
+ * 创建时间: 2019/6/28 10:57:07
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Autodesk.Revit.DB;
+using JBIM;
+using SAGA.RevitUtils;
+
+namespace RevitToJBim.MBI
+{
+    /// <summary>
+    /// 项目传输数据结构
+    /// </summary>
+    public class MBIDocument
+    {
+        public string MBIName { get; set; }
+        public string PlanName { get; set; }
+        public Dictionary<string, List<BimObject>> Elements { get;private set; }
+        /// <summary>
+        /// 附加元素Elements信息
+        /// </summary>
+        /// <param name="document"></param>
+        public void AttachElements(BimDocument document)
+        {
+            var bimObjects = document.BimObjects;
+            var group = bimObjects.GroupBy(bim => bim.ElementType);
+            Dictionary<string, List<BimObject>> dic = new Dictionary<string, List<BimObject>>();
+            foreach (var collection in group)
+            {
+                dic[collection.Key + "s"] = collection.ToList();
+            }
+            Elements = dic;
+        }
+
+        public static MBIDocument CreateDocument(Document document)
+        {
+            MBIDocument dto = new MBIDocument();
+            dto.MBIName = "PjXXX_001";//待读取
+            dto.PlanName = document.GetUseView()?.Name;
+            return dto;
+        }
+    }
+}

+ 54 - 0
JBIM/RevitToJBim/MbiExport.cs

@@ -0,0 +1,54 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:TestExport
+ * 作者:xulisong
+ * 创建时间: 2019/6/18 15:43:54
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Autodesk.Revit.Attributes;
+using Autodesk.Revit.DB;
+using Autodesk.Revit.DB.Mechanical;
+using Autodesk.Revit.DB.Plumbing;
+using Autodesk.Revit.UI;
+using RevitExport;
+using RevitExport.Export;
+using RevitToJBim.ComponentParse;
+using RevitToJBim.JsonConverter;
+using RevitToJBim.MBI;
+using SAGA.RevitUtils;
+using SAGA.RevitUtils.Extends;
+
+namespace RevitToJBim
+{
+    public class MbiExport
+    {
+        public static void Export(Document document)
+        {
+            //导出类
+            var wrappers = ExportDataBuilder.CreateExportData(document);
+            ExportInstance report = new ExportInstance(wrappers);
+            //解析类
+            JBimParseContext context = new JBimParseContext(ParseCore.GetUseParsers());
+            context.Parser.Parse(report);
+
+            //构建需要结果
+            var mbiDocument = MBIDocument.CreateDocument(document);
+            mbiDocument.AttachElements(context.GetResultDocument());
+            /*
+             * 序列化的变化封装在这里,增加MBIDocument传输数据对象,
+             * 增加一些和JBIM无关,又和当前业务逻辑相关的属性
+             */
+            var result = BimJsonUtil.Serializer(mbiDocument);
+            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
+            string path = Path.Combine(@"D:\", $"{fileName}.json");
+            File.WriteAllText(path, result);
+            System.Diagnostics.Process.Start("notepad.exe", path);
+        }
+    }
+}

+ 57 - 0
JBIM/RevitToJBim/MbiExportCommand.cs

@@ -0,0 +1,57 @@
+/*-------------------------------------------------------------------------
+ * 功能描述:MbiExportCommand
+ * 作者:xulisong
+ * 创建时间: 2019/6/28 10:52:24
+ * 版本号:v1.0
+ *  -------------------------------------------------------------------------*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Autodesk.Revit.Attributes;
+using Autodesk.Revit.DB;
+using Autodesk.Revit.UI;
+
+namespace RevitToJBim
+{
+    /// <summary>
+    /// 提取数据
+    /// </summary>
+    [Transaction(TransactionMode.Manual)]
+    [Regeneration(RegenerationOption.Manual)]
+    public class MbiExportCommand : IExternalCommand, IExternalCommandAvailability
+    {
+
+        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
+        {
+            try
+            {
+                var document = commandData.Application.ActiveUIDocument.Document;
+                MbiExport.Export(document);
+                TaskDialog dialog = new TaskDialog("导出");
+                dialog.MainInstruction = "导出成功";
+                dialog.Show();
+            }
+            catch (Exception e)
+            {
+                TaskDialog dialog = new TaskDialog("导出");
+                dialog.MainInstruction = e.Message + "\n\t" + e.StackTrace.ToString();
+                dialog.Show();
+                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;
+        }
+    }
+}

+ 3 - 1
JBIM/RevitToJBim/RevitToJBim.csproj

@@ -75,7 +75,9 @@
     <Compile Include="Extension\GeometryElementExtension.cs" />
     <Compile Include="Extension\WallExtension.cs" />
     <Compile Include="JsonConverter\ParameterConverter.cs" />
+    <Compile Include="MbiExportCommand.cs" />
     <Compile Include="MBI\MBIBuiltInParameterName.cs" />
+    <Compile Include="MBI\MBIDocument.cs" />
     <Compile Include="MBI\MBIRegexPattern.cs" />
     <Compile Include="Common\RevitIdGenerator.cs" />
     <Compile Include="Common\RevitUtil.cs" />
@@ -105,7 +107,7 @@
     <Compile Include="RevitToJBimParser.cs" />
     <Compile Include="JBimParseContext.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="TestExport.cs" />
+    <Compile Include="MbiExport.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\JBIM\JBIM.csproj">

+ 0 - 87
JBIM/RevitToJBim/TestExport.cs

@@ -1,87 +0,0 @@
-/*-------------------------------------------------------------------------
- * 功能描述:TestExport
- * 作者:xulisong
- * 创建时间: 2019/6/18 15:43:54
- * 版本号:v1.0
- *  -------------------------------------------------------------------------*/
-
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Autodesk.Revit.Attributes;
-using Autodesk.Revit.DB;
-using Autodesk.Revit.DB.Mechanical;
-using Autodesk.Revit.DB.Plumbing;
-using Autodesk.Revit.UI;
-using RevitExport;
-using RevitExport.Export;
-using RevitToJBim.ComponentParse;
-using SAGA.RevitUtils;
-using SAGA.RevitUtils.Extends;
-
-namespace RevitToJBim
-{
-    public class TestExport
-    {
-        public static Document Document { get; set; }
-        public static void Export()
-        {
-            var elements = Document.GetElements(typeof(Pipe));
-            var elements2 = Document.GetElements(typeof(SpatialElement)).OfType<Space>().ToList();
-            var elements3 = Document.GetElements(typeof(Duct)).OfType<Duct>().ToList();
-            elements.AddRange(elements2);
-            elements.AddRange(elements3);
-            var wrappers = ExportDataBuilder.CreateExportData(Document);// elements.Select(e => new ElementWrapper(e)).ToList();
-            ExportInstance report = new ExportInstance(wrappers);
-            JBimParseContext context = new JBimParseContext(ParseCore.GetUseParsers());
-            var dd = false;
-            context.Parser.Parse(report);
-            var result = context.Serialize();
-            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
-            string path = Path.Combine(@"D:\", $"{fileName}.json");
-            File.WriteAllText(path, result);
-            System.Diagnostics.Process.Start("notepad.exe", path);
-        }
-    }
-    ///// <summary>
-    ///// 测试提取数据
-    ///// </summary>
-    //[Transaction(TransactionMode.Manual)]
-    //[Regeneration(RegenerationOption.Manual)]
-    //public class PickDataCommand : IExternalCommand, IExternalCommandAvailability
-    //{
-
-    //    public  Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
-    //    {
-    //        try
-    //        {
-    //            TestExport.Document = commandData.Application.ActiveUIDocument.Document;
-    //            TestExport.Export();
-    //            TaskDialog dialog = new TaskDialog("导出");
-    //            dialog.MainInstruction ="导出成功";
-    //            dialog.Show();
-    //        }
-    //        catch (Exception e)
-    //        {
-    //            TaskDialog dialog = new TaskDialog("导出");
-    //            dialog.MainInstruction =e.Message+"\n\t"+ e.StackTrace.ToString();
-    //            dialog.Show();
-    //            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;
-    //    }
-    //}
-}