/// <summary>
        /// Opens a model from the provided stream
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="dataType"></param>
        /// <param name="schema"></param>
        /// <param name="modelType"></param>
        /// <param name="accessMode"></param>
        /// <param name="progDelegate"></param>
        /// <param name="codePageOverride"></param>
        /// <returns></returns>
        public override IModel Open(Stream stream, StorageType dataType, XbimSchemaVersion schema, XbimModelType modelType,
                                    XbimDBAccess accessMode = XbimDBAccess.Read, ReportProgressDelegate progDelegate = null, int codePageOverride = -1)
        {
            //any Esent model needs to run from the file so we need to create a temporal one
            var xbimFilePath = Path.GetTempFileName();

            xbimFilePath = Path.ChangeExtension(xbimFilePath, ".xbim");

            switch (dataType)
            {
            case StorageType.Xbim:
                //xBIM file has to be opened from the file so we need to create temporary file if it is not a local file stream
                var localFile  = false;
                var fileStream = stream as FileStream;
                if (fileStream != null)
                {
                    var name = fileStream.Name;
                    //if it is an existing local file, just use it
                    if (File.Exists(name))
                    {
                        xbimFilePath = name;
                        //close the stream from argument to have an exclusive access to the file
                        stream.Close();
                        localFile = true;
                    }
                }
                if (!localFile)
                {
                    using (var tempFile = File.Create(xbimFilePath))
                    {
                        stream.CopyTo(tempFile);
                        tempFile.Close();
                    }
                }
                // Scope to avoid name clashes
                {
                    var model = CreateEsentModel(schema, codePageOverride);
                    model.Open(xbimFilePath, accessMode, progDelegate);
                    return(model);
                }

            case StorageType.IfcXml:
                if (modelType == XbimModelType.EsentModel)
                {
                    var model = CreateEsentModel(schema, codePageOverride);
                    if (model.CreateFrom(stream, stream.Length, dataType, xbimFilePath, progDelegate, keepOpen: true, cacheEntities: true))
                    {
                        return(model);
                    }
                    else
                    {
                        throw new XbimException("Failed to create Esent model");
                    }
                }
                if (modelType == XbimModelType.MemoryModel)
                {
                    var model = CreateMemoryModel(schema);
                    model.LoadXml(stream, stream.Length, progDelegate);
                    return(model);
                }
                throw new ArgumentOutOfRangeException("HeuristicModelProvider only supports EsentModel and MemoryModel");

            case StorageType.Stp:
            case StorageType.Ifc:
                if (modelType == XbimModelType.EsentModel)
                {
                    var model = CreateEsentModel(schema, codePageOverride);
                    if (model.CreateFrom(stream, stream.Length, dataType, xbimFilePath, progDelegate, keepOpen: true, cacheEntities: true))
                    {
                        return(model);
                    }
                    else
                    {
                        throw new XbimException("Failed to create Esent model");
                    }
                }
                if (modelType == XbimModelType.MemoryModel)
                {
                    var model = CreateMemoryModel(schema);
                    model.LoadStep21(stream, stream.Length, progDelegate);
                    return(model);
                }
                throw new ArgumentOutOfRangeException("HeuristicModelProvider only supports EsentModel and MemoryModel");

            case StorageType.IfcZip:
            case StorageType.StpZip:
            case StorageType.Zip:
                if (modelType == XbimModelType.EsentModel)
                {
                    var model = CreateEsentModel(schema, codePageOverride);
                    if (model.CreateFrom(stream, stream.Length, dataType, xbimFilePath, progDelegate, true, true))
                    {
                        return(model);
                    }
                    else
                    {
                        throw new XbimException("Failed to create Esent model");
                    }
                }
                if (modelType == XbimModelType.MemoryModel)
                {
                    var model = CreateMemoryModel(schema);
                    model.LoadZip(stream, progDelegate);
                    return(model);
                }
                throw new ArgumentOutOfRangeException("HeuristicModelProvider only supports EsentModel and MemoryModel");

            default:
                throw new ArgumentOutOfRangeException("dataType");
            }
        }
Esempio n. 2
0
        public override IModel Open(Stream stream, StorageType dataType, XbimSchemaVersion schema, XbimModelType modelType,
                                    XbimDBAccess accessMode = XbimDBAccess.Read, ReportProgressDelegate progDelegate = null, int codePageOverride = -1)
        {
            if (modelType != XbimModelType.MemoryModel)
            {
                throw new ArgumentOutOfRangeException(nameof(modelType), "MemoryModelProvider only supports MemoryModel");
            }
            switch (dataType)
            {
            case StorageType.Xbim:
                throw new NotSupportedException("MemoryModelProvider cannot support opening XBIM Streams");

            case StorageType.IfcXml:
            {
                var model = CreateMemoryModel(schema);
                model.LoadXml(stream, stream.Length, progDelegate);
                return(model);
            }

            case StorageType.Stp:
            case StorageType.Ifc:
            {
                var model = CreateMemoryModel(schema);
                model.LoadStep21(stream, stream.Length, progDelegate);
                return(model);
            }

            case StorageType.IfcZip:
            case StorageType.StpZip:
            case StorageType.Zip:
            {
                var model = CreateMemoryModel(schema);
                model.LoadZip(stream, progDelegate);
                return(model);
            }

            default:
                throw new ArgumentOutOfRangeException("dataType");
            }
        }
Esempio n. 3
0
        /// <summary>
        /// You can use this function to open IFC model from a <see cref="Stream"/>.
        /// You need to know file type (IFC, IFCZIP, IFCXML) and schema type (IFC2x3 or IFC4) to be able to use this function.
        /// If you don't know, you should the overloaded <see cref="Open(string, XbimEditorCredentials, double?, ReportProgressDelegate, XbimDBAccess, int)"/>
        /// method which takes file paths as an argument, and can automatically detect schema and file type.
        /// If are opening an *.xbim file you should also use the path-based overload because Esent database needs to operate
        /// on the file and this function will have to create temporal file if it is not a file stream.
        /// If the input is a FileStream, be aware this method may call <see cref="Stream.Close"/> on it to keep exclusive access.
        /// </summary>
        /// <param name="stream">Stream of data</param>
        /// <param name="dataType">Type of data (*.ifc, *.ifcxml, *.ifczip)</param>
        /// <param name="schema">IFC schema (IFC2x3, IFC4). Other schemas are not supported by this class.</param>
        /// <param name="modelType">Type of model to be used. You can choose between EsentModel and MemoryModel</param>
        /// <param name="editorDetails">Optional details. You should always pass these if you are going to change the data.</param>
        /// <param name="accessMode">Access mode to the stream. This is only important if you choose EsentModel. MemoryModel is completely in memory so this is not relevant</param>
        /// <param name="progDelegate">Progress reporting delegate</param>
        /// <param name="codePageOverride">
        /// A CodePage that will be used to read implicitly encoded one-byte-char strings. If -1 is specified the default ISO8859-1
        /// encoding will be used accoring to the Ifc specification. </param>
        /// <returns></returns>
        public static IfcStore Open(Stream stream, StorageType dataType, XbimSchemaVersion schema, XbimModelType modelType, XbimEditorCredentials editorDetails = null,
                                    XbimDBAccess accessMode = XbimDBAccess.Read, ReportProgressDelegate progDelegate = null, int codePageOverride = -1)
        {
            var newStore = new IfcStore();
            var model    = newStore.ModelProvider.Open(stream, dataType, schema, modelType, accessMode, progDelegate, codePageOverride);

            newStore.AssignModel(model, editorDetails, schema);
            return(newStore);
        }
 public abstract IModel Open(Stream data, StorageType dataType, XbimSchemaVersion schema, XbimModelType modelType, XbimDBAccess accessMode = XbimDBAccess.Read, ReportProgressDelegate progDelegate = null, int codePageOverride = -1);