Ejemplo n.º 1
0
        public static Model FromSqliteFile(string fullPath)
        {
            SQLiteDatabase database     = new SQLiteDatabase();
            Model          CurrentModel = new ifc.Model(fullPath.Replace(".sqlite", ""));
            DataSet        dataSet      = database.GetContentAsDataSet(fullPath);

#if _DEBUG
            Console.WriteLine(string.Format("Reading SQLite-File: {0}", NetSystem.IO.Path.GetFileName(fullPath)));
            Console.WriteLine("======================================================");
#endif
            foreach (DataTable dt in dataSet.Tables)
            {
#if _DEBUG
                Console.WriteLine("______________________________________________________");
                Console.WriteLine(dt.TableName);
                foreach (DataColumn c in dt.Columns)
                {
                    Console.Write(string.Format("{0} ", c.ColumnName));
                }
                Console.Write("\r\n");
#endif
                foreach (DataRow row in dt.Rows)
                {
                    Type   entityType = Type.GetType("ifc." + dt.TableName);
                    ENTITY entityInstance;
                    if (entityType == typeof(EntityComment))
                    {
                        EntityComment ec = new EntityComment((string)row["Comment"], (int)row["PreviousEntity"]);
                        entityInstance = ec;
                    }
                    else
                    {
                        object[] ctorArgs = GetEntityConstructorArgs(entityType, row);
                        entityInstance = Activator.CreateInstance(entityType, ctorArgs) as ENTITY;
                    }

                    if (row["Id"] != null)
                    {
                        entityInstance.LocalId = (int)row["Id"];
                    }
                    CurrentModel.EntityList.Add(entityInstance);
#if _DEBUG
                    foreach (DataColumn c in dt.Columns)
                    {
                        Console.Write(string.Format("{0} ", row[c] is DBNull ? "NULL" : row[c].ToString()));
                    }
                    Console.Write("\r\n");
#endif
                }
            }
            Console.WriteLine("======================================================");

            // before we assign the entities, we need to order the list according to the Ids
            CurrentModel.EntityList = CurrentModel.EntityList.OrderBy(e => e.LocalId).ToList();

            // then we change the position of all EntityComment´s to match the actual order,
            // since they are being read sequentially
            foreach (EntityComment ec in CurrentModel.EntityList.FindAll(e => e.GetType() == typeof(EntityComment)))
            {
                int oldIndex = CurrentModel.EntityList.FindIndex(e => e.LocalId == ec.LocalId);
                int newIndex = CurrentModel.EntityList.FindIndex(e => e.LocalId == ec.PreviousEntityId) + 1;
                var item     = CurrentModel.EntityList[oldIndex];
                CurrentModel.EntityList.RemoveAt(oldIndex);

                if (newIndex > oldIndex)
                {
                    newIndex--;                      // the actual index could have shifted due to the removal
                }
                CurrentModel.EntityList.Insert(newIndex, item);
            }

            CurrentModel.AssignEntities();
            return(CurrentModel);
        }
Ejemplo n.º 2
0
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        public static void ParseIfcLine(Model CurrentModel, string line)
        {
//Console.WriteLine("parse="+line);
            CurrentLine = line;
            int CommentOpenPos = line.IndexOf("/*"); if (CommentOpenPos >= 0)

            {
                CurrentEntityComment = line.Substring(CommentOpenPos + 2).Replace("*/", ""); line = line.Substring(0, CommentOpenPos);

//Console.WriteLine("CommentOpenPos="+CommentOpenPos+" "+CurrentEntityComment);
            }
            // if (EndOfLineComment!=null) s+="/* "+EndOfLineComment+" */";
            if (CommentOpenPos != 0)//====================================================================================================

            {
                int    posA        = line.IndexOf('=');
                int    posLpar     = line.IndexOf('(');
                int    posRpar     = line.LastIndexOf(')');
                string ElementName = line.Substring(posA + 1, posLpar - posA - 1).TrimStart(' ').Substring(3);
                string body        = line.Substring(posLpar + 1, posRpar - posLpar - 1); // Argumentkörper extrahieren
                bool   TxtOpen     = false;
                int    ParOpen     = 0;
                for (int i = 0; i < body.Length; i++)
                {
                    if ((!TxtOpen) && (body[i] == '\''))
                    {
                        TxtOpen = true;
                    }                                                                    //body=ReplaceCharAt(body,i,'[');}
                    else if ((!TxtOpen) && (body[i] == '('))
                    {
                        ParOpen++;
                    }                                                                 //body=ReplaceCharAt(body,i,'<');}
                    else if ((!TxtOpen) && (body[i] == ')'))
                    {
                        ParOpen--;
                    }                                                                //body=ReplaceCharAt(body,i,'>');}
                    else if ((TxtOpen) && (body[i] == '\''))
                    {
                        TxtOpen = false;
                    }                                                                    //body=ReplaceCharAt(body,i,']');}
                    if ((TxtOpen) || (ParOpen > 0))
                    {
                        if (body[i] == ',')
                        {
                            body = ReplaceCharAt(body, i, (char)9);
                        }
                    }
                }
                string[] args = body.Split(',');
                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = args[i].Replace((char)9, ',');
                }
                try{ Type   t             = Type.GetType("ifc." + ElementName, true, true);// 2. true: ignoreCase
                     object CurrentEntity = Activator.CreateInstance(t); if (CommentOpenPos > 0)
                     {
                         ((ENTITY)CurrentEntity).EndOfLineComment = CurrentEntityComment;
                     }
                     ((ENTITY)CurrentEntity).LocalId = int.Parse(line.Substring(1, posA - 1));
                     Dictionary <int, FieldInfo> VarDict = new Dictionary <int, FieldInfo>();
                     int VarCount = 0;

//foreach (FieldInfo field in CurrentEntity.GetType().GetFields(BindingFlags.Public|BindingFlags.Instance|BindingFlags.FlattenHierarchy)) Console.WriteLine(field.Name);

                     foreach (FieldInfo field in CurrentEntity.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
                     {
                         foreach (Attribute attr in field.GetCustomAttributes(true))
                         {
                             if (attr is ifcAttribute)
                             {
                                 VarDict.Add(((ifcAttribute)attr).OrdinalPosition, field); VarCount++;
                             }                                                                                                                                  //  if (attr is ifcAttribute) //Console.WriteLine(VarCount+": "+attr.OrdinalPosition+": "+field.Name);
                         }
                     }
                     for (int i = 1; i <= VarCount; i++)
                     {
                         FieldInfo field = VarDict[i]; // Console.Write(field.Name+", ");
                         string    value = "$";
                         if (i <= args.GetLength(0))
                         {
                             value = args[i - 1].Trim(' ').Trim('\'');
                         }
                         if (field.FieldType == typeof(String))
                         {
                             if (value == "$")
                             {
                                 field.SetValue(CurrentEntity, "" /*null*/);
                             }
                             else
                             {
                                 field.SetValue(CurrentEntity, ifc.IfcString.Decode(value));
                             }
                         }
                         else if (field.FieldType == typeof(int))
                         {
                             if (value == "$")
                             {
                                 field.SetValue(CurrentEntity, 0);
                             }
                             else
                             {
                                 field.SetValue(CurrentEntity, int.Parse(value));
                             }
                         }
                         else if (field.FieldType.IsSubclassOf(typeof(TypeBase)))
                         {
                             try{ field.SetValue(CurrentEntity, Parse2TYPE(value, field.FieldType)); }catch (Exception e) { throw new Exception("Parse.Type: Field " + i + ": " + field.FieldType.ToString() + ": " + e.Message); }
                         }                                      //tb.GetBaseType()
                         else if (field.FieldType.IsSubclassOf(typeof(SELECT)))
                         {
                             //Console.WriteLine("begin SELECT: "+line);
                             try{ object o = Activator.CreateInstance(field.FieldType);
                                  if ((value.Length > 0) && (value[0] == '$'))
                                  {
                                  }
                                  else if ((value.Length > 0) && (value[0] == '*'))
                                  {
                                  }
                                  else if ((value.Length > 0) && (value[0] == '#'))
                                  {
                                      ((SELECT)o).Id = int.Parse(value.Substring(1));
                                  }
                                  else
                                  {
                                      o = ParseSelect(value, o);
                                  }
                                  field.SetValue(CurrentEntity, o); }catch (Exception e) { throw new Exception("xx Parse.SELECT: Field " + i + ": " + field.FieldType.ToString() + ": " + e.Message); }
                         }
                         else if (field.FieldType.IsSubclassOf(typeof(ENTITY)))
                         {
                             try{ object o = null;                               //falls $
                                  if (value.Length > 0)
                                  {
                                      if (value[0] == '*')
                                      {
                                          o = Activator.CreateInstance(field.FieldType);
                                      }
                                  }
                                  if (value.Length > 0)
                                  {
                                      if (value[0] == '#')
                                      {
                                          o = Activator.CreateInstance(field.FieldType); ((ENTITY)o).LocalId = int.Parse(value.Substring(1));
                                      }
                                  }
                                  field.SetValue(CurrentEntity, o); }catch (Exception e) { throw new Exception("Parse.ENTITY: Field " + i + ": " + field.FieldType.ToString() + ": " + e.Message); }
                         }
                         else if (field.FieldType.IsSubclassOf(typeof(Enum)))
                         {
                             try{ object FieldInstance = Activator.CreateInstance(field.FieldType);
                                  if ((value.Length > 0) && (value[0] == '$'))
                                  {
                                      FieldInstance = 0;
                                  }
                                  else
                                  {
                                      try{ FieldInstance = Enum.Parse(field.FieldType, value.Substring(1, value.Length - 2)); }catch { Console.WriteLine("enum " + field.FieldType + "." + value + " not recognized"); }
                                  }
                                  field.SetValue(CurrentEntity, FieldInstance); }catch (Exception e) { throw new Exception("Parse.Enum: Field " + i + ": " + field.FieldType.ToString() + ": " + e.Message); }
                         }
                         else if ((Nullable.GetUnderlyingType(field.FieldType) != null) && (Nullable.GetUnderlyingType(field.FieldType).IsSubclassOf(typeof(Enum))))
                         {
                             try{ object FieldInstance = null;
                                  if ((value.Length > 0) && (value[0] != '$'))
                                  {
                                      FieldInstance = Activator.CreateInstance(field.FieldType);
                                      try{ FieldInstance = Enum.Parse(Nullable.GetUnderlyingType(field.FieldType), value.Substring(1, value.Length - 2)); }catch { Console.WriteLine("enum " + field.FieldType + "." + value + " not recognized"); }
                                  }
                                  field.SetValue(CurrentEntity, FieldInstance); }catch (Exception e) { throw new Exception("Parse.Enum: Field " + i + ": " + field.FieldType.ToString() + ": " + e.Message); }
                         }
                         //else if( typeof(IEnumerable).IsAssignableFrom(field.FieldType)){
                         else if (typeof(ifcListInterface).IsAssignableFrom(field.FieldType))
                         {
                             try{ if (value == "$")
                                  {
                                      field.SetValue(CurrentEntity, null);
                                  }
                                  else
                                  {
                                      field.SetValue(CurrentEntity, Parse2LIST(value, field.FieldType));
                                  } }catch (Exception e) { Console.WriteLine("Parse.LIST:" + line);
                                                           throw new Exception("Parse.LIST: Field " + i + ": " + field.FieldType.ToString() + ": " + e.Message); }
                         }
                         else
                         {
                             Console.WriteLine(i + ": is " + field.FieldType.Name + "???????x? " + field.FieldType.Name + "  " + line); //ifc.WallStandardCase
                         }
                     }

                     CurrentModel.EntityList.Add((ENTITY)CurrentEntity); //Console.WriteLine();Console.WriteLine("AAA "+((ENTITY)CurrentEntity).ToString());
                }catch (Exception e) { Console.WriteLine("ERROR on ParseIfcLine:" + e.Message); Console.WriteLine(line); }//Console.ReadLine();}
            }//====================================================================================================
            else
            {
                EntityComment ec = new EntityComment(); ec.CommentLine = CurrentEntityComment; ec.LocalId = NextGlobalCommentId--; CurrentModel.EntityList.Add(ec);
            }
        }