/// <summary> /// Populates an entites properties from the binary stream /// </summary> /// <param name="entity"></param> /// <param name="cache"></param> /// <param name="br"></param> /// <param name="unCached">If true instances inside the properties are not added to the cache</param> /// <param name="fromCache"> If true the instance is read from the cache if not present it is created, used during parsing public static void ReadEntityProperties(this IPersistIfcEntity entity, IfcPersistedInstanceCache cache, BinaryReader br, bool unCached = false,bool fromCache = false) { P21ParseAction action = (P21ParseAction)br.ReadByte(); XbimParserState parserState = new XbimParserState(entity); while (action != P21ParseAction.EndEntity) { switch (action) { case P21ParseAction.BeginList: parserState.BeginList(); break; case P21ParseAction.EndList: parserState.EndList(); break; case P21ParseAction.BeginComplex: break; case P21ParseAction.EndComplex: break; case P21ParseAction.SetIntegerValue: parserState.SetIntegerValue(br.ReadInt64()); break; case P21ParseAction.SetHexValue: parserState.SetHexValue(br.ReadInt64()); break; case P21ParseAction.SetFloatValue: parserState.SetFloatValue(br.ReadDouble()); break; case P21ParseAction.SetStringValue: parserState.SetStringValue(br.ReadString()); break; case P21ParseAction.SetEnumValue: parserState.SetEnumValue(br.ReadString()); break; case P21ParseAction.SetBooleanValue: parserState.SetBooleanValue(br.ReadBoolean()); break; case P21ParseAction.SetNonDefinedValue: parserState.SetNonDefinedValue(); break; case P21ParseAction.SetOverrideValue: parserState.SetOverrideValue(); break; case P21ParseAction.SetObjectValueUInt16: if (fromCache) { int label = br.ReadUInt16(); IPersistIfcEntity refEntity; if (!parserState.InList && cache.Read.TryGetValue(label, out refEntity)) //if we are in a list then make a forward reference anyway to make sure we maintain list order parserState.SetObjectValue(refEntity); else { cache.AddForwardReference(new IfcForwardReference(label, parserState.CurrentPropertyId, entity)); parserState.SkipProperty(); } } else parserState.SetObjectValue(cache.GetInstance(br.ReadUInt16(), false, unCached)); break; case P21ParseAction.SetObjectValueInt32: if (fromCache) { int label = br.ReadInt32(); IPersistIfcEntity refEntity; if (!parserState.InList && cache.Read.TryGetValue(label, out refEntity)) //if we are in a list then make a forward reference anyway to make sure we maintain list order parserState.SetObjectValue(refEntity); else { cache.AddForwardReference(new IfcForwardReference(label, parserState.CurrentPropertyId, entity)); parserState.SkipProperty(); } } else parserState.SetObjectValue(cache.GetInstance(br.ReadInt32(), false, unCached)); break; case P21ParseAction.SetObjectValueInt64: throw new XbimException("Entity Label is int64, this is not currently supported"); //parserState.SetObjectValue(cache.GetInstance(br.ReadInt64(), false, unCached)); //break; case P21ParseAction.BeginNestedType: parserState.BeginNestedType(br.ReadString()); break; case P21ParseAction.EndNestedType: parserState.EndNestedType(); break; case P21ParseAction.EndEntity: parserState.EndEntity(); break; case P21ParseAction.NewEntity: parserState = new XbimParserState(entity); break; default: throw new XbimException("Invalid Property Record #" + entity.EntityLabel + " EntityType: " + entity.GetType().Name); } action = (P21ParseAction)br.ReadByte(); } }
/// <summary> /// Populates an entites properties from the binary stream /// </summary> /// <param name="entity"></param> /// <param name="cache"></param> /// <param name="br"></param> /// <param name="unCached">If true instances inside the properties are not added to the cache</param> /// <param name="fromCache"> If true the instance is read from the cache if not present it is created, used during parsing</param> internal static void ReadEntityProperties(this IPersistEntity entity, PersistedEntityInstanceCache cache, BinaryReader br, bool unCached = false, bool fromCache = false) { var action = (P21ParseAction)br.ReadByte(); var parserState = new XbimParserState(entity); while (action != P21ParseAction.EndEntity) { switch (action) { case P21ParseAction.BeginList: parserState.BeginList(); break; case P21ParseAction.EndList: parserState.EndList(); break; case P21ParseAction.BeginComplex: break; case P21ParseAction.EndComplex: break; case P21ParseAction.SetIntegerValue: parserState.SetIntegerValue(br.ReadInt64()); break; case P21ParseAction.SetHexValue: parserState.SetHexValue(br.ReadInt64()); break; case P21ParseAction.SetFloatValue: parserState.SetFloatValue(br.ReadDouble()); break; case P21ParseAction.SetStringValue: parserState.SetStringValue(br.ReadString()); break; case P21ParseAction.SetEnumValue: parserState.SetEnumValue(br.ReadString()); break; case P21ParseAction.SetBooleanValue: parserState.SetBooleanValue(br.ReadBoolean()); break; case P21ParseAction.SetNonDefinedValue: parserState.SetNonDefinedValue(); break; case P21ParseAction.SetOverrideValue: parserState.SetOverrideValue(); break; case P21ParseAction.SetObjectValueUInt16: if (fromCache) { int label = br.ReadUInt16(); IPersistEntity refEntity; if (!parserState.InList && cache.Read.TryGetValue(label, out refEntity)) //if we are in a list then make a forward reference anyway to make sure we maintain list order { parserState.SetObjectValue(refEntity); } else { cache.AddForwardReference(new StepForwardReference(label, parserState.CurrentPropertyId, entity, parserState.NestedIndex)); parserState.SkipProperty(); } } else { parserState.SetObjectValue(cache.GetInstance(br.ReadUInt16(), false, unCached)); } break; case P21ParseAction.SetObjectValueInt32: if (fromCache) { var label = br.ReadInt32(); IPersistEntity refEntity; if (!parserState.InList && cache.Read.TryGetValue(label, out refEntity)) //if we are in a list then make a forward reference anyway to make sure we maintain list order { parserState.SetObjectValue(refEntity); } else { cache.AddForwardReference(new StepForwardReference(label, parserState.CurrentPropertyId, entity, parserState.NestedIndex)); parserState.SkipProperty(); } } else { parserState.SetObjectValue(cache.GetInstance(br.ReadInt32(), false, unCached)); } break; case P21ParseAction.SetObjectValueInt64: throw new XbimException("Entity Label is int64, this is not currently supported"); //parserState.SetObjectValue(cache.GetInstance(br.ReadInt64(), false, unCached)); //break; case P21ParseAction.BeginNestedType: parserState.BeginNestedType(br.ReadString()); break; case P21ParseAction.EndNestedType: parserState.EndNestedType(); break; case P21ParseAction.EndEntity: parserState.EndEntity(); break; case P21ParseAction.NewEntity: parserState = new XbimParserState(entity); break; default: throw new XbimException("Invalid Property Record #" + entity.EntityLabel + " EntityType: " + entity.GetType().Name); } action = (P21ParseAction)br.ReadByte(); } }
internal static void WriteEntity(this IPersistIfcEntity entity, TextWriter tw, byte[] propertyData) { tw.Write(string.Format("#{0}={1}", Math.Abs(entity.EntityLabel), entity.GetType().Name.ToUpper())); BinaryReader br = new BinaryReader(new MemoryStream(propertyData)); P21ParseAction action = (P21ParseAction)br.ReadByte(); bool comma = false; //the first property XbimParserState parserState = new XbimParserState(entity); while (action != P21ParseAction.EndEntity) { switch (action) { case P21ParseAction.BeginList: tw.Write("("); break; case P21ParseAction.EndList: tw.Write(")"); break; case P21ParseAction.BeginComplex: tw.Write("&SCOPE"); break; case P21ParseAction.EndComplex: tw.Write("ENDSCOPE"); break; case P21ParseAction.SetIntegerValue: if (comma) tw.Write(","); comma = true; tw.Write(br.ReadInt64().ToString()); break; case P21ParseAction.SetHexValue: if (comma) tw.Write(","); comma = true; tw.Write(Convert.ToString(br.ReadInt64(),16)); break; case P21ParseAction.SetFloatValue: if (comma) tw.Write(","); comma = true; tw.Write(IfcReal.AsPart21(br.ReadDouble())); break; case P21ParseAction.SetStringValue: if (comma) tw.Write(","); comma = true; tw.Write(br.ReadString()); break; case P21ParseAction.SetEnumValue: if (comma) tw.Write(","); comma = true; tw.Write("." + br.ReadString() + "."); break; case P21ParseAction.SetBooleanValue: if (comma) tw.Write(","); comma = true; tw.Write(br.ReadBoolean() ? ".T." : ".F."); break; case P21ParseAction.SetNonDefinedValue: if (comma) tw.Write(","); comma = true; tw.Write("$"); break; case P21ParseAction.SetOverrideValue: if (comma) tw.Write(","); comma = true; tw.Write("*"); break; case P21ParseAction.SetObjectValueUInt16: if (comma) tw.Write(","); comma = true; tw.Write("#"+ br.ReadUInt16().ToString()); break; case P21ParseAction.SetObjectValueInt32: if (comma) tw.Write(","); comma = true; tw.Write("#" + br.ReadInt32().ToString()); break; case P21ParseAction.SetObjectValueInt64: if (comma) tw.Write(","); comma = true; tw.Write("#" + br.ReadInt64().ToString()); break; case P21ParseAction.BeginNestedType: if (comma) tw.Write(","); comma = false; tw.Write(br.ReadString()+"("); break; case P21ParseAction.EndNestedType: comma = true; tw.Write(")"); break; case P21ParseAction.EndEntity: tw.Write(");"); break; case P21ParseAction.NewEntity: comma = false; tw.Write("("); break; default: throw new Exception("Invalid Property Record #" + entity.EntityLabel + " EntityType: " + entity.GetType().Name); } action = (P21ParseAction)br.ReadByte(); } tw.WriteLine(); }
internal static void WriteEntity(this IPersistIfcEntity entity, TextWriter tw, byte[] propertyData) { tw.Write(string.Format("#{0}={1}", entity.EntityLabel, entity.GetType().Name.ToUpper())); BinaryReader br = new BinaryReader(new MemoryStream(propertyData)); P21ParseAction action = (P21ParseAction)br.ReadByte(); bool comma = false; //the first property XbimParserState parserState = new XbimParserState(entity); while (action != P21ParseAction.EndEntity) { switch (action) { case P21ParseAction.BeginList: tw.Write("("); break; case P21ParseAction.EndList: tw.Write(")"); break; case P21ParseAction.BeginComplex: tw.Write("&SCOPE"); break; case P21ParseAction.EndComplex: tw.Write("ENDSCOPE"); break; case P21ParseAction.SetIntegerValue: if (comma) { tw.Write(","); } comma = true; tw.Write(br.ReadInt64().ToString()); break; case P21ParseAction.SetHexValue: if (comma) { tw.Write(","); } comma = true; tw.Write(Convert.ToString(br.ReadInt64(), 16)); break; case P21ParseAction.SetFloatValue: if (comma) { tw.Write(","); } comma = true; tw.Write(IfcReal.AsPart21(br.ReadDouble())); break; case P21ParseAction.SetStringValue: if (comma) { tw.Write(","); } comma = true; tw.Write(br.ReadString()); break; case P21ParseAction.SetEnumValue: if (comma) { tw.Write(","); } comma = true; tw.Write("." + br.ReadString() + "."); break; case P21ParseAction.SetBooleanValue: if (comma) { tw.Write(","); } comma = true; tw.Write(br.ReadBoolean() ? ".T." : ".F."); break; case P21ParseAction.SetNonDefinedValue: if (comma) { tw.Write(","); } comma = true; tw.Write("$"); break; case P21ParseAction.SetOverrideValue: if (comma) { tw.Write(","); } comma = true; tw.Write("*"); break; case P21ParseAction.SetObjectValueUInt16: if (comma) { tw.Write(","); } comma = true; tw.Write("#" + br.ReadUInt16().ToString()); break; case P21ParseAction.SetObjectValueInt32: if (comma) { tw.Write(","); } comma = true; tw.Write("#" + br.ReadInt32().ToString()); break; case P21ParseAction.SetObjectValueInt64: if (comma) { tw.Write(","); } comma = true; tw.Write("#" + br.ReadInt64().ToString()); break; case P21ParseAction.BeginNestedType: if (comma) { tw.Write(","); } comma = false; tw.Write(br.ReadString() + "("); break; case P21ParseAction.EndNestedType: comma = true; tw.Write(")"); break; case P21ParseAction.EndEntity: tw.Write(");"); break; case P21ParseAction.NewEntity: comma = false; tw.Write("("); break; default: throw new Exception("Invalid Property Record #" + entity.EntityLabel + " EntityType: " + entity.GetType().Name); } action = (P21ParseAction)br.ReadByte(); } tw.WriteLine(); }