xBIM 插入複製功能

     目錄

     插入複製功能

        在IFC 模型中合併和刪除實體是一項不重要的任務,由於IFC不是一個分層結構。而是具備潛在循環關係的複雜結構,是一個雙向導向。在單個實體上執行這些任務不是問題(STEP21文件中。能夠想象成單行)    

#144= IFCBUILDINGSTOREY('026ajlHVj1HBm_osQm7IDT',#47,'Lower Roof - Slab Level',$,$,#143,$,'Lower Roof - Slab Level',.ELEMENT.,3199.99999999704);

      一旦要隔離定義實體的完整數據島,並但願將其刪除,而不會對數據島以外的其餘實體產生反作用,或者但願將其合併到現有數據中,而不會形成重複和不一致,則會變得愈來愈困難。 因爲這些緣由,咱們更喜歡第三種選擇,即選擇你想要的,並把它複製到一個空的模型中。 這顯然是一個複雜而複雜的任務,但至少讓事情控制在你的控制之下更容易。如今是IModel界面成員的核心功能是InsertCopy()

T InsertCopy<T>(T toCopy, XbimInstanceHandleMap mappings, PropertyTranformDelegate propTransform, bool includeInverses, bool keepLabels);

正如全部參數簡要描述同樣:

  • toCopy: 要複製的實體
  • mappings: 之前插入的映射。對於兩個模型之間的全部插入, 應該始終只有一個實例。
  • propTransform: 可選的委託, 您可使用它來篩選將在複製以前得到 coppied 或轉換它的內容。這是很是棒的一個功能。
  • includeInverses: 選項引入全部反轉實體。這是潛在的危險, 由於若是不受 propTransform 委託的限制, 它可能會輕易地帶來幾乎整個模型。
  • keepLabels: 選項以使實體標籤保持不變。有時保持標籤相同可能會頗有用。若是目標模型不是新模型或從多個模型中插入對象, 則永遠不要使用此選項。

  從全部這些PropertyTranformDelegate看起來有點神祕。可是, 它是上述方法的一個基本部分, 由於它容許控制被複制的數據的範圍。若是你容許逆並不提供任何額外的過濾, 可能會最終與模型包含98% 的原始模型,  要正確使用它,您須要瞭解IFC的結構。 這是一個強大的轉換的簡單例子,它將省去全部的幾何和佈局,只容許描述產品類型和屬性的反向關係。幾何一般須要大約90%的文件,因此若是您對基於幾何的圖形或分析不感興趣,您可使用它來建立僅包含描述性數據的很是小的IFC文件。

PropertyTranformDelegate semanticFilter = (property, parentObject) =>
{
    //幾何和對象位置
    if (parentObject is IIfcProduct &&
        (property.PropertyInfo.Name == nameof(IIfcProduct.Representation) ||
        property.PropertyInfo.Name == nameof(IIfcProduct.ObjectPlacement)))
        return null;

    //映射幾何
    if (parentObject is IIfcTypeProduct && 
        property.PropertyInfo.Name == nameof(IIfcTypeProduct.RepresentationMaps))
        return null;
    // IsDefinedBy 和 IsTypedBy 反向關係   
    if (property.EntityAttribute.Order < 0 && !(
        property.PropertyInfo.Name == nameof(IIfcProduct.IsDefinedBy) ||
        property.PropertyInfo.Name == nameof(IIfcProduct.IsTypedBy)
        ))
        return null;

    return property.PropertyInfo.GetValue(parentObject, null);
};

 PropertyTranformDelegate 採用兩個參數, 其中第一個是 ExpressMetaProperty, 另外一個是表示 IPersistEntity 的對象。ExpressMetaProperty 是一個緩存的對象, 這是咱們本身的反射元模型的一部分, 咱們用於某些數據操做。該委託在使用 c# 反射的其餘代碼中用於檢查數據和複製值。若是未指定委託 InsertCopy (), 將使用實體中的全部屬性並將其複製過來。

using Xbim.Common;
using Xbim.Ifc;
using Xbim.Ifc4.Interfaces;

namespace BasicExamples
{
    class InsertCopy
    {
        public void CopyWallsOver()
        {
            const string original = "SampleHouse.ifc";
            const string inserted = "SampleHouseWalls.ifc";

            PropertyTranformDelegate semanticFilter = (property, parentObject) =>
            {
                //幾何和對象位置
                if (parentObject is IIfcProduct &&
                    (property.PropertyInfo.Name == nameof(IIfcProduct.Representation) ||
                    property.PropertyInfo.Name == nameof(IIfcProduct.ObjectPlacement)))
                    return null;
               //幾何映射
               if (parentObject is IIfcTypeProduct && 
                    property.PropertyInfo.Name == nameof(IIfcTypeProduct.RepresentationMaps))
                    return null;
                if (property.EntityAttribute.Order < 0 && !(
                    property.PropertyInfo.Name == nameof(IIfcProduct.IsDefinedBy) ||
                    property.PropertyInfo.Name == nameof(IIfcProduct.IsTypedBy)
                    ))
                    return null;

                return property.PropertyInfo.GetValue(parentObject, null);
            };

            using (var model = IfcStore.Open(original))
            {
                var walls = model.Instances.OfType<IIfcWall>();
                using (var iModel = IfcStore.Create(model.IfcSchemaVersion, XbimStoreType.InMemoryModel))
                {
                    using (var txn = iModel.BeginTransaction("Insert copy"))
                    {
                        //單個映射應用於兩個模型之間的全部插入
                        var map = new XbimInstanceHandleMap(model, iModel);

                        foreach (var wall in walls)
                        {
                            iModel.InsertCopy(wall, map, semanticFilter, true, false);
                        }

                        txn.Commit();
                    }

                    iModel.SaveAs(inserted);
                }
            }
        }
    }
}
相關文章
相關標籤/搜索