public static IStepFileHeader GetStepFileHeader(Stream stream) { var parser = new XbimP21Parser(stream, null, -1); var stepHeader = new StepFileHeader(StepFileHeader.HeaderCreationMode.LeaveEmpty); parser.EntityCreate += (string name, long?label, bool header, out int[] ints) => { //allow all attributes to be parsed ints = null; if (header) { switch (name) { case "FILE_DESCRIPTION": return(stepHeader.FileDescription); case "FILE_NAME": return(stepHeader.FileName); case "FILE_SCHEMA": return(stepHeader.FileSchema); default: return(null); } } parser.Cancel = true; //done enough return(null); }; parser.Parse(); stream.Close(); return(stepHeader); }
/// <summary> /// Opens the model from STEP21 file. /// </summary> /// <param name="stream">Path to the file</param> /// <param name="streamSize"></param> /// <param name="progDelegate"></param> /// <returns>Number of errors in parsing. Always check this to be null or the model might be incomplete.</returns> public virtual int LoadStep21(Stream stream, long streamSize, ReportProgressDelegate progDelegate = null) { var parser = new XbimP21Parser(stream, Metadata, streamSize); if (progDelegate != null) { parser.ProgressStatus += progDelegate; } var first = true; Header = new StepFileHeader(StepFileHeader.HeaderCreationMode.LeaveEmpty); parser.EntityCreate += (string name, long?label, bool header, out int[] ints) => { //allow all attributes to be parsed ints = null; if (header) { switch (name) { case "FILE_DESCRIPTION": return(Header.FileDescription); case "FILE_NAME": return(Header.FileName); case "FILE_SCHEMA": return(Header.FileSchema); default: return(null); } } if (label == null) { return(_instances.Factory.New(name)); } //if this is a first non-header entity header is read completely by now. //So we should check if the schema declared in the file is the one declared in EntityFactory if (first) { first = false; if (!Header.FileSchema.Schemas.All(s => _instances.Factory.SchemasIds.Contains(s))) { throw new Exception("Mismatch between schema defined in the file and schemas available in the data model."); } } var typeId = Metadata.ExpressTypeId(name); var ent = _instances.Factory.New(this, typeId, (int)label, true); // if entity is null do not add so that the file load operation can survive an illegal entity // e.g. an abstract class instantiation. if (ent != null) { _instances.InternalAdd(ent); } else { var msg = $"Error in file at label {label} for type {name}."; if (Metadata.ExpressType(typeId).Type.IsAbstract) { msg = $"Illegal element in file; cannot instantiate the abstract type {name} at label {label}."; } Log.Error(msg); } //make sure that new added entities will have higher labels to avoid any clashes if (label >= _instances.CurrentLabel) { _instances.CurrentLabel = (int)label; } return(ent); }; parser.Parse(); if (progDelegate != null) { parser.ProgressStatus -= progDelegate; } return(parser.ErrorCount); }