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
477 stars 171 forks source link

SaveAs Truncating file #534

Closed fpz016 closed 8 months ago

fpz016 commented 8 months ago

When opening and SaveAs on an existing IFC4 file all data except the header is removed.

The following lines are all that is found in the save as file

ISO-10303-21; HEADER; FILE_DESCRIPTION (('ViewDefinition [CoordinationView]'), '2;1'); FILE_NAME ('D:\_Advanced Steel\OneDrive_2023-10-09\Jason Holden\02 Sample Model\ADVS Export\P0042_01-IFC4 - Copy.ifc', '2023-11-07T12:20:35', (''), (''), 'IfcOpenShell v0.7.0-9cc1f5f0e', 'IfcOpenShell v0.7.0-9cc1f5f0e', ''); FILE_SCHEMA (('IFC4')); ENDSEC; DATA; ENDSEC; END-ISO-10303-21;

Assemblies and versions affected:

image

Steps (or code) to reproduce the issue:

public void GetIFCObj() { //var columns = model.Instances.OfType(); //vertical beams //var braces = model.Instances.OfType(); //diagonal braces

        const string fileName = @"D:\\_Advanced Steel\\OneDrive_2023-10-09\\Jason Holden\\02 Sample Model\\ADVS Export\\P0042_01-IFC4 - Copy.ifc";

        XbimEditorCredentials ADN = new XbimEditorCredentials();
        ADN.ApplicationDevelopersName = "JH";

        using (model = IfcStore.Open(fileName, ADN, null, null, XbimDBAccess.ReadWrite))
        {
        }

        model.SaveAs(fileName);
        model.Dispose();
    }

Minimal file to reproduce the issue:

IFC files need to be zipped to be uploaded. Then just drag & drop here

P0042_01-IFC4 - Copy.zip

Expected behavior:

Expect a full copy of the file to be saved

Actual behavior or exception details:

All data is removed except what is shown in file attached below P0042_01-IFC4 - Copy_postProcess.zip

martin1cerny commented 8 months ago

Thank you for the code example, but I'd need to see what you actually do when you open the model. Do you commit the transaction after you make the changes? What are you trying to do? The best would be a minimalistic but complete code example, preferably in a form of a simple unit test.

andyward commented 8 months ago

model.SaveAs(filename) needs to be within the using statement. You're trying to save the IFC file after it has been disposed.

Also be aware you seem to be over-writing the original file with the new one. If you're making changes it may be better to save a new one. And if you're not making changes do you even need to save?

Try this

   public void GetIFCObj()
   {

        const string fileName = @"D:\\_Advanced Steel\\OneDrive_2023-10-09\\Jason Holden\\02 Sample Model\\ADVS Export\\P0042_01-IFC4 - Copy.ifc";

        XbimEditorCredentials ADN = new XbimEditorCredentials();
        ADN.ApplicationDevelopersName = "JH";

        using (model = IfcStore.Open(fileName, ADN, null, null, XbimDBAccess.ReadWrite))
        {
              // do any edits in an xbim transaction
              // commit transaction
              model.SaveAs(fileName);   // Resave the file
        }

        // model.Dispose(); // Not needed as disposed in using above
    }
fpz016 commented 8 months ago

Wow. Awesome. Perfect. I'm a bit rusty at the moment. Thank you.