XbimEssentials icon indicating copy to clipboard operation
XbimEssentials copied to clipboard

XBim have “IfcStore.AddModelReference” API,but where is the "Remove ModelReference" API ?

Open lhx24 opened this issue 2 years ago • 3 comments

XBim have “IfcStore.AddModelReference” API,but where is the "Remove ModelReference" API ?

lhx24 avatar Aug 08 '23 02:08 lhx24

Moved from Windows UI project.

Looks like it didn't get thought about....

You could implement as a simple extension method with a couple of lines of code:

    public static class IfcStoreFederationExtensions
    {
        public static bool RemoveModelReference(this IfcStore store, IReferencedModel model)
        {
            var referenced = store.ReferencedModels as ReferencedModelCollection;

            return referenced!.Remove(model);
        }
    }

    // then call like this
    var toRemove = model.ReferencedModels.First(m => m.Name == "yourModelToRemove");
    myStore.RemoveModelReference(toRemove),

Happy to take a PR if you want to add it formally

andyward avatar Aug 08 '23 14:08 andyward

Thank you very much for your response. I have tried the code you provided and also studied the source code. The ReferencedModelCollection contains two parts: one is stored in the IFC file, and the other is in the ReferencedModelCollection itself. So, removing the model reference using RemoveModelReference still keeps the graphical representation in the UI, and the link document is still present when opening the IFC file again. Therefore, it is necessary to delete the link information within the IFC file as well. Below is my code, please feel free to provide any suggestions for improvement.

Xbim.Ifc4.ExternalReferenceResource.IfcDocumentInformation document = refence.DocumentInformation as Xbim.Ifc4.ExternalReferenceResource.IfcDocumentInformation;
if (document != null)
{
    Xbim.Ifc4.ActorResource.IfcOrganization owner = document.DocumentOwner as Xbim.Ifc4.ActorResource.IfcOrganization;
    if (owner != null)
    {
        if (owner.Roles != null && owner.Roles.Count == 1)
        {
            modelIFC.Delete(owner.Roles[0]);
        }
        modelIFC.Delete(owner);
    }
    modelIFC.Delete(document);
}

lhx24 avatar Aug 09 '23 03:08 lhx24

OK, that makes sense. I won't profess to being that familiar with this code

That will only work for IFC4 schema (not Ifc2x3, 4.3 etc). if you cast to IIfcDocumentationInfo and IIfcOrganization it should work across schemas

andyward avatar Aug 09 '23 09:08 andyward