如何用xBIM Explorer修改IFC文件并重用默认IfcOwnerHistory
解决方案:重用IFC模型的OwnerHistory,避免重复生成
核心问题分析
- 现有代码仅从已加载的IFC文件中读取第一个
IfcOwnerHistory,但MemoryModel初始状态下没有任何OwnerHistory实例,导致无法重用,xbim会自动创建新的OwnerHistory、Person及Organization。 - 所有新创建的IFC实体(包括任务、任务时间、关联关系)如果不手动指定OwnerHistory,xbim都会自动生成新的实例。
修改后的实现代码
1. 调整OwnerHistory的初始化逻辑(兼容IfcStore和MemoryModel)
首先定义私有字段存储可重用的OwnerHistory,然后新增初始化方法:
private IIfcOwnerHistory _reusableOwnerHistory; public bool LoadIFC() { var model = IfcStore.Open(currentFileName); SetContext(model); InitializeReusableOwnerHistory(model); } // 初始化可重用OwnerHistory,兼容已有文件和空MemoryModel private void InitializeReusableOwnerHistory(IModel model) { // 优先读取模型中已有的第一个OwnerHistory _reusableOwnerHistory = model.Instances.OfType<IIfcOwnerHistory>().FirstOrDefault(); // 如果模型中没有(比如MemoryModel刚创建),则创建一个可重用的实例 if (_reusableOwnerHistory == null) { using (var txn = model.BeginTransaction("Create reusable OwnerHistory")) { // 创建关联的Person和Organization(可根据需求修改信息) var person = model.Instances.New<IfcPerson>(p => p.FamilyName = "DefaultUser"); var organization = model.Instances.New<IfcOrganization>(o => o.Name = "DefaultOrg"); var personOrg = model.Instances.New<IfcPersonAndOrganization>(po => { po.ThePerson = person; po.TheOrganization = organization; }); // 创建可重用的OwnerHistory _reusableOwnerHistory = model.Instances.New<IfcOwnerHistory>(oh => { oh.OwningUser = personOrg; oh.OwningApplication = model.Instances.New<IfcApplication>(app => { app.ApplicationDeveloper = organization; app.Version = "1.0"; app.ApplicationFullName = "YourApplication"; app.ApplicationIdentifier = "YourAppID"; }); oh.ChangeAction = IfcChangeActionEnum.ADDED; oh.LastModifiedDate = DateTime.Now; oh.CreationDate = DateTime.Now; }); txn.Commit(); } } } // 若使用MemoryModel,初始化时也要调用上述方法 // 示例: // var memoryModel = new IfcStore(IfcSchemaVersion.Ifc4, XbimStoreType.InMemoryModel); // InitializeReusableOwnerHistory(memoryModel);
2. 修改AddTask方法,确保所有新实体都重用OwnerHistory
private void AddTask() { if (_currentProduct == null || _reusableOwnerHistory == null) return; using (var txn = Model.BeginTransaction("Ajout IfcTask a un produit")) { try { // 创建IfcTask并指定重用的OwnerHistory var newIfcTask = Model.Instances.New<IfcTask>(task => { task.Name = "Test"; task.Description = "TaskDescription"; task.OwnerHistory = _reusableOwnerHistory as IfcOwnerHistory; }); // 创建IfcTaskTime并指定OwnerHistory var taskTime = Model.Instances.New<IfcTaskTime>(tt => { tt.EarlyStart = new IfcDateTime(StartDate.ToString()); tt.EarlyFinish = new IfcDateTime(EndDate.ToString()); tt.OwnerHistory = _reusableOwnerHistory as IfcOwnerHistory; }); newIfcTask.TaskTime = taskTime; // 创建关联关系并指定OwnerHistory var relAssignsToProduct = Model.Instances.New<IfcRelAssignsToProduct>(rel => { rel.RelatingProduct = _currentProduct; rel.RelatedObjects.Add(newIfcTask); rel.OwnerHistory = _reusableOwnerHistory as IfcOwnerHistory; }); txn.Commit(); } catch (Exception ex) { Debug.WriteLine("Erreur lors de la création de l'IfcTask : " + ex.Message); txn.RollBack(); } } }
关键注意事项
- 所有新实体都要手动设置OwnerHistory:包括任务、任务时间、关联关系等,只要是IFC实体,不设置的话xbim都会自动生成新的OwnerHistory实例。
- 类型转换安全:使用
as运算符进行类型转换,避免强制转换引发的异常。 - MemoryModel初始化:创建空的MemoryModel时,必须调用
InitializeReusableOwnerHistory方法生成可重用的OwnerHistory,否则无法重用。
内容的提问来源于stack exchange,提问作者Rachid Benbrik
相关产品推荐
相关产品推荐

