Skip to content

Commit

Permalink
Fixed xBimTeam#333
Browse files Browse the repository at this point in the history
  • Loading branch information
martin1cerny committed Apr 17, 2020
1 parent b5d4440 commit b5879fa
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
49 changes: 48 additions & 1 deletion Tests/OwnerHistoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xbim.Common;
using Xbim.Common.Step21;
using Xbim.Ifc;
using Xbim.Ifc4.Interfaces;
Expand All @@ -9,17 +10,63 @@ namespace Xbim.Essentials.Tests
[TestClass]
public class OwnerHistoryTests
{
[TestMethod]
public void NonEmptyEditorCredentials()
{
using (var model = IfcStore.Create(XbimSchemaVersion.Ifc4, XbimStoreType.InMemoryModel))
{
using (var txn = model.BeginTransaction())
{
var create = new Create(model);
var wall = create.Wall(w => w.Name = "New wall");
Assert.IsNotNull(wall.OwnerHistory);
Assert.IsTrue(model.Instances.Count > 1);
}
}
}

[TestMethod]
public void EmptyEditorCredentials()
{
using (var model = IfcStore.Create(XbimSchemaVersion.Ifc4, XbimStoreType.InMemoryModel))
{
model.ManageOwnerHistory = false;
using (var txn = model.BeginTransaction())
{
var create = new Create(model);
var wall = create.Wall(w => w.Name = "New wall");
Assert.IsNull(wall.OwnerHistory);
Assert.AreEqual(1, model.Instances.Count);
}
}
}

[TestMethod]
public void NoOwnerHistoryForInsertCopy()
{
using (var source = IfcStore.Create(XbimSchemaVersion.Ifc4, XbimStoreType.InMemoryModel))
{
using (var txn = source.BeginTransaction())
{
var create = new Create(source);
source.ManageOwnerHistory = false;
create.Wall(w => w.Name = "New wall #1");
create.Wall(w => w.Name = "New wall #2");
create.Wall(w => w.Name = "New wall #3");
source.ManageOwnerHistory = true;
txn.Commit();
}

using (var target = IfcStore.Create(source.SchemaVersion, XbimStoreType.InMemoryModel))
{
using (var txn = target.BeginTransaction())
{
var map = new XbimInstanceHandleMap(source, target);
target.InsertCopy(source.Instances.OfType<IIfcProduct>(), true, true, map);
txn.Commit();
}

var usr = model.DefaultOwningUser;
Assert.AreEqual(source.Instances.Count, target.Instances.Count);
}
}
}
Expand Down
29 changes: 24 additions & 5 deletions Xbim.Ifc/IfcStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Xbim.Common;
using Xbim.Common.Exceptions;
using Xbim.Common.Federation;
Expand All @@ -15,6 +16,7 @@
using Xbim.IO.Memory;
using Xbim.IO.Step21;

[assembly: InternalsVisibleTo("Xbim.Essentials.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010029a3c6da60efcb3ebe48c3ce14a169b5fa08ffbf5f276392ffb2006a9a2d596f5929cf0e68568d14ac7cbe334440ca0b182be7fa6896d2a73036f24bca081b2427a8dec5689a97f3d62547acd5d471ee9f379540f338bbb0ae6a165b44b1ae34405624baa4388404bce6d3e30de128cec379147af363ce9c5845f4f92d405ed0")]
namespace Xbim.Ifc
{
/// <summary>
Expand Down Expand Up @@ -350,7 +352,18 @@ public IModelFactors ModelFactors
public T InsertCopy<T>(T toCopy, XbimInstanceHandleMap mappings, PropertyTranformDelegate propTransform, bool includeInverses,
bool keepLabels) where T : IPersistEntity
{
return Model.InsertCopy(toCopy, mappings, propTransform, includeInverses, keepLabels);
try
{
// don't handle new and modified instances for inserted objects as these should have their own
// data or should be handles explicitely
ManageOwnerHistory = false;
return Model.InsertCopy(toCopy, mappings, propTransform, includeInverses, keepLabels);
}
finally
{
// switch back
ManageOwnerHistory = true;
}
}

public void ForEach<TSource>(IEnumerable<TSource> source, Action<TSource> body) where TSource : IPersistEntity
Expand Down Expand Up @@ -404,10 +417,14 @@ private void Model_EntityModified(IPersistEntity entity, int property)
if (EntityModified != null) EntityModified.Invoke(entity, property);
}

internal bool ManageOwnerHistory = true;

private void IfcRootModified(IPersistEntity entity, int property)
{
var root = entity as IIfcRoot;
if (root == null || root.OwnerHistory == _ownerHistoryAddObject)
if (!ManageOwnerHistory)
return;

if (!(entity is IIfcRoot root) || root.OwnerHistory == _ownerHistoryAddObject)
return;

if (root.OwnerHistory != _ownerHistoryModifyObject)
Expand All @@ -419,8 +436,10 @@ private void IfcRootModified(IPersistEntity entity, int property)

private void IfcRootInit(IPersistEntity entity)
{
var root = entity as IIfcRoot;
if (root != null)
if (!ManageOwnerHistory)
return;

if (entity is IIfcRoot root)
{
root.OwnerHistory = OwnerHistoryAddObject;
root.GlobalId = Guid.NewGuid().ToPart21();
Expand Down

0 comments on commit b5879fa

Please sign in to comment.