xBIM 高级03 更改日志建立

  模型中发生的每个变化都是事务的一部分,这是咱们设计的核心。全部事务都是由 IModel 的实现建立的,而且从中被弱引用,所以当使用 using 语句模型时,只要保留事务,就只保留对该事务的引用。这意味着有一个单一的点,全部的变化都在发生,咱们能够用它们来作一些事情。html

  一件很重要的事情是记录全部的更改、之前的状态和下一个状态。将全部这些结合起来,您能够建立 back-log 或 forward-log。为了简化这个任务,咱们实现了一个 xbim.io.delta.TransactionLog 类。在下面的示例中,咱们将了解如何使用它。app

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会自动为您处理全部者历史记录。ui

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');
 
相关文章
相关标签/搜索