xBIM 高級03 更改日誌創建

  • 2019 年 10 月 4 日
  • 筆記

  模型中發生的每一個變化都是事務的一部分,這是我們設計的核心。所有事務都是由 IModel 的實現創建的,並且從中被弱引用,因此當使用 using 語句模型時,只要保留事務,就只保留對該事務的引用。這意味著有一個單一的點,所有的變化都在發生,我們可以用它們來做一些事情。

  一件很重要的事情是記錄所有的更改、以前的狀態和下一個狀態。將所有這些結合起來,您可以創建 back-log 或 forward-log。為了簡化這個任務,我們實現了一個 xbim.io.delta.TransactionLog 類。在下面的示例中,我們將了解如何使用它。

using System;  using Xbim.Common;  using Xbim.Ifc;  using Xbim.Ifc4.Interfaces;  using Xbim.IO.Delta;  using Xbim.IO.Step21;      var editor = new XbimEditorCredentials  {      ApplicationDevelopersName = "You",      ApplicationFullName = "Your app",      ApplicationIdentifier = "Your app ID",      ApplicationVersion = "4.0",        EditorsFamilyName = "Santini Aichel",      EditorsGivenName = "Johann Blasius",      EditorsOrganisationName = "Independent Architecture"  };    using (var model = IfcStore.Open("SampleHouse.ifc", editor, true))  {      using (var txn = model.BeginTransaction("Modification"))      {          using (var log = new TransactionLog(txn))          {              // 修改一個已經存在的 牆 對象              var wall = model.Instances.FirstOrDefault<IIfcWall>();              wall.Name = "Unexpected name";              wall.GlobalId = Guid.NewGuid().ToPart21();              wall.Description = "New and more descriptive description";                // 列印所有由此引起的更改              PrintChanges(log);              txn.Commit();          }          Console.WriteLine();      }  }
private static void PrintChanges(TransactionLog log)  {      foreach (var change in log.Changes)      {          switch (change.ChangeType)          {              case ChangeType.New:                  Console.WriteLine(@"New entity: {0}", change.CurrentEntity);                  break;              case ChangeType.Deleted:                  Console.WriteLine(@"Deleted entity: {0}", change.OriginalEntity);                  break;              case ChangeType.Modified:                  Console.WriteLine(@"Changed Entity: #{0}={1}", change.Entity.EntityLabel, change.Entity.ExpressType.ExpressNameUpper);                  foreach (var prop in change.ChangedProperties)                      Console.WriteLine(@"        Property '{0}' changed from {1} to {2}", prop.Name, prop.OriginalValue, prop.CurrentValue);                  break;              default:                  break;          }      }  }

生成的更改日誌將如下所示。它包含更多的更改,因為當您更改或創建任何ifcroot實體時,xbim會自動為您處理所有者歷史記錄。

Changed Entity: #1229=IFCWALL          Property 'Name' changed from 'Basic Wall:Wall-Ext_102Bwk-75Ins-100LBlk-12P:285330' to 'Unexpected name'          Property 'OwnerHistory' changed from #42 to #83873          Property 'GlobalId' changed from '3cUkl32yn9qRSPvBJVyWw5' to '0zxW3$9z95n8U_H9YOcyiE'          Property 'Description' changed from $ to 'New and more descriptive description'  New entity: #83873=IFCOWNERHISTORY(#83876,#83877,$,.MODIFIED.,$,$,$,0);  New entity: #83874=IFCPERSON($,'Santini Aichel','Johann Blasius',$,$,$,$,$);  New entity: #83875=IFCORGANIZATION($,'Independent Architecture',$,$,$);  New entity: #83876=IFCPERSONANDORGANIZATION(#83874,#83875,$);  New entity: #83878=IFCORGANIZATION($,'You',$,$,$);  New entity: #83877=IFCAPPLICATION(#83878,$,'Your app','Your app ID');