xBimTeam / XbimEssentials

A .NET library to work with data in the IFC format. This is the core component of the Xbim Toolkit
https://xbimteam.github.io/
Other
485 stars 172 forks source link

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

Open lhx24 opened 1 year ago

lhx24 commented 1 year ago

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

andyward commented 1 year ago

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

lhx24 commented 1 year ago

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);
}
andyward commented 1 year ago

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