xBimTeam / XbimExchange

XbimExchange contains several COBie schemas and serialisation functions as well as the Model Validation library adopted by theNBS digital toolkit.
https://xbimteam.github.io/
Other
46 stars 41 forks source link

Conversion IFC file in a Json file #8

Closed soniagagliardi closed 7 years ago

soniagagliardi commented 8 years ago

Hi,

I have converted my IFC Model to .wexbim and .xbim but how can I create the expected .json file?

I used to generate the json file using Xbim.Exchange in this way:

    var context = new Xbim3DModelContext(model1);
            context.CreateContext(geomStorageType: XbimGeometryType.PolyhedronBinary);
            var wexBimFilename = Path.ChangeExtension(IfcModelFile, "wexBIM");
            using (var wexBiMfile = new FileStream(wexBimFilename, FileMode.Create,                 FileAccess.Write))
            {
                using (var wexBimBinaryWriter = new BinaryWriter(wexBiMfile))
                {
                    Console.WriteLine("Creating " + wexBimFilename);
                    context.Write(wexBimBinaryWriter);
                    wexBimBinaryWriter.Close();
                }
                wexBiMfile.Close();
            }
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(IfcModelFile);
            var fileDirectoryName = Path.GetDirectoryName(IfcModelFile);
            List<Facility> facilities = new List<Facility>();

                var ifcToCoBieLiteUkExchanger = new IfcToCOBieLiteUkExchanger(model1, facilities);
                if (ifcToCoBieLiteUkExchanger != null)
                {

                    facilities = ifcToCoBieLiteUkExchanger.Convert();

                    var facilityNumber = 0;

                    foreach (var facility in facilities)
                    {.......

but I have this error

erroremapping

What is the problem?

Thanks

chirdeeptomar commented 8 years ago

Please make sure you open the model with XbimDBAccess.Exclusive

var model = GetXbimModel(filePath, xbimDatabase, true);

model.Open(xbimDatabase, XbimDBAccess.Exclusive);

chirdeeptomar commented 8 years ago
private void SaveAsJson(string filePath, XbimModel model)
        {
            FileStream stream = null;
            try
            {
                var cobieFileName = Path.ChangeExtension(filePath, "json");
                stream = new FileStream(cobieFileName, FileMode.Create);
                var helper = new CoBieLiteHelper(model);
                var facility = helper.GetFacilities().FirstOrDefault();
                if (facility != null)
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        CoBieLiteHelper.WriteJson(writer, facility);
                        writer.Close();
                    }
                }

            }
            catch (Exception exception)
            {
                _logger.Error(exception);
            }
            finally
            {
                stream?.Dispose();
            }
        }
soniagagliardi commented 8 years ago

Hi,

Thank you for your reply. I followed your suggestion but still does not work completely. The method GetXbimModel(filePath, xbimDatabase, true) as it is defined? Maybe the problem is right there. I create the model as:

var model = new XbimModel(); model.CreateFrom(file.ifc, null, null, true); model.Open(file.xbim, XbimDBAccess.Exclusive); SaveAsJson(file.ifc, model);

where is my mistake?

chirdeeptomar commented 8 years ago

CreateFrom call is wrong...

You have to do something like this:

private static XbimModel GetXbimModel(string inFileName, string xbimDb, bool caching)
        {
            var model = new XbimModel();

           model.CreateFrom(
                    inFileName,
                    xbimDb,
                    null, false, caching
                    );
                return model;        

        }

        private static string GetXbimDatabase(string file, string extension)
        {
            return Path.ChangeExtension(file, extension);
        }

inFileName is the IFC file name and xbimDb is a XbimDb that you have to create by calling the method below

var xbimDatabase = GetXbimDatabase(filePath, ".xbim");

            var model = GetXbimModel(filePath, xbimDatabase, true);

            model.Open(xbimDatabase, XbimDBAccess.Exclusive);

And then use the method SaveAsJson from previous comment.

soniagagliardi commented 8 years ago

The method SaveAsJson does not work at all. Does not display all the characteristics of the model.

The method that I wrote in the first post:

private void JsonFile(XbimModel model, string ifcmodelfile)
        {          
            if (model != null)
            {
                var context = new Xbim3DModelContext(model);
                context.CreateContext(geomStorageType: XbimGeometryType.PolyhedronBinary);
                var wexBimFilename = Path.ChangeExtension(ifcModelFile, "wexBIM");
                using (var wexBiMfile = new FileStream(wexBimFilename, FileMode.Create, FileAccess.Write))
                {
                    using (var wexBimBinaryWriter = new BinaryWriter(wexBiMfile))
                    {
                        Console.WriteLine("Creating " + wexBimFilename);
                        context.Write(wexBimBinaryWriter);
                        wexBimBinaryWriter.Close();
                    }
                    wexBiMfile.Close();
                }
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ifcModelFile);
                var fileDirectoryName = Path.GetDirectoryName(ifcModelFile);
                List<Facility> facilities = new List<Facility>();

                    var ifcToCoBieLiteUkExchanger = new IfcToCOBieLiteUkExchanger(model, facilities);
                    if (ifcToCoBieLiteUkExchanger != null)
                    {

                        facilities = ifcToCoBieLiteUkExchanger.Convert();

                        var facilityNumber = 0;

                        foreach (var facility in facilities)
                        {
                            var dpow = "DPoW";
                            if (facilities.Count > 1)
                                dpow += ++facilityNumber;
                            var dPoWFile = Path.Combine(fileDirectoryName, fileNameWithoutExtension + "_" + dpow);
                            dPoWFile = Path.ChangeExtension(dPoWFile, "json");
                            facility.WriteJson(dPoWFile);             

                        }
                    }
                }
        }

but It does not work with all IFC file and returns an exception in the class XbimExchanger.IfcToCOBieLiteUK.MappingIfcBuildingToFacility like this:

exceptionnull

facility.Attributes is null but facility.Site is not null.

I hope I was understandable enough

chirdeeptomar commented 8 years ago

I had the same issue with one of the IFC files where it comes to be null but I have no clue why that would be.

var facility = helper.GetFacilities().FirstOrDefault();

Facility comes out to be null and I think you having the same issue.

soniagagliardi commented 8 years ago

while this method you have ever used?

chirdeeptomar commented 8 years ago

Not sure what you mean

soniagagliardi commented 8 years ago

Have you ever used the method JsonFile the comment Previous?

SteveLockley commented 7 years ago

Please see the Pas1192 example in the Exchange project on the Develop Branch for correct usage