Exemplo n.º 1
0
        internal BaseClassIfc Duplicate(BaseClassIfc entity, bool downStream)
        {
            if (mDuplicateMapping.ContainsKey(entity.mIndex))
            {
                return(mDatabase[mDuplicateMapping[entity.mIndex]]);
            }
            Type type = Type.GetType("GeometryGym.Ifc." + entity.GetType().Name, false, true);

            if (type != null)
            {
                Type[]          types       = new Type[] { typeof(DatabaseIfc), type, typeof(bool) };
                ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, types, null);
                if (constructor != null)
                {
                    return(constructor.Invoke(new object[] { this.mDatabase, entity, downStream }) as BaseClassIfc);
                }
                types       = new Type[] { typeof(DatabaseIfc), type };
                constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, types, null);
                if (constructor != null)
                {
                    return(constructor.Invoke(new object[] { this.mDatabase, entity }) as BaseClassIfc);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        internal static IfcValue extractValue(string keyword, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }
            Type type = BaseClassIfc.GetType(keyword);

            if (type != null)
            {
                if (type.IsSubclassOf(typeof(IfcSimpleValue)))
                {
                    return(extractSimpleValue(type, value));
                }
                if (type.IsSubclassOf(typeof(IfcMeasureValue)))
                {
                    return(extractMeasureValue(type, value));
                }
                if (type.IsSubclassOf(typeof(IfcDerivedMeasureValue)))
                {
                    return(extractDerivedMeasureValue(type, value));
                }
                if (type == typeof(IfcSpecularExponent))
                {
                    return(new IfcSpecularExponent(double.Parse(value, ParserSTEP.NumberFormat)));
                }
                if (type == typeof(IfcSpecularRoughness))
                {
                    return(new IfcSpecularRoughness(double.Parse(value, ParserSTEP.NumberFormat)));
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        public T ParseJObject <T>(JObject obj) where T : IBaseClassIfc
        {
            if (obj == null)
            {
                return(default(T));
            }

            Type type = typeof(T);

            string       hrefId = "";
            BaseClassIfc result = null;
            JToken       token  = obj.GetValue("href", StringComparison.InvariantCultureIgnoreCase);

            if (token != null)
            {
                hrefId = token.Value <string>();
                if (mDictionary.TryGetValue(hrefId, out result) && obj.Count == 1)
                {
                    return((T)(IBaseClassIfc)result);
                }
            }
            if (string.IsNullOrEmpty(hrefId))
            {
                token = obj.GetValue("id", StringComparison.InvariantCultureIgnoreCase);
                if (token != null)
                {
                    hrefId = token.Value <string>();
                    mDictionary.TryGetValue(hrefId, out result);
                }
            }

            if (result == null)
            {
                if (type.IsAbstract)
                {
                    JProperty jtoken    = (JProperty)obj.First;
                    Type      valueType = BaseClassIfc.GetType(jtoken.Name);
                    if (valueType != null && valueType.IsSubclassOf(typeof(IfcValue)))
                    {
                        IBaseClassIfc val = ParserIfc.extractValue(jtoken.Name, jtoken.Value.ToString()) as IBaseClassIfc;
                        if (val != null)
                        {
                            return((T)val);
                        }
                        return(default(T));
                    }
                }


                token = obj.GetValue("type", StringComparison.InvariantCultureIgnoreCase);
                if (token != null)
                {
                    Type nominatedType = BaseClassIfc.GetType(token.Value <string>());
                    if (nominatedType != null)
                    {
                        type = nominatedType;
                    }
                }
                if (string.IsNullOrEmpty(hrefId) || !mDictionary.TryGetValue(hrefId, out result))
                {
                    ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                      null, Type.EmptyTypes, null);
                    if (constructor != null)
                    {
                        bool common = false;
                        result = constructor.Invoke(new object[] { }) as BaseClassIfc;
                        if (result != null)
                        {
                            result.mDatabase = this;
                            if (!string.IsNullOrEmpty(hrefId))
                            {
                                mDictionary[hrefId] = result;
                            }

                            IfcCartesianPoint point = result as IfcCartesianPoint;
                            if (point != null)
                            {
                                point.parseJObject(obj);
                                if (point.isOrigin(Tolerance))
                                {
                                    if (point.is2D)
                                    {
                                        result = Factory.Origin2d;
                                    }
                                    else
                                    {
                                        result = Factory.Origin;
                                    }
                                    common = true;
                                }
                            }
                            else
                            {
                                IfcDirection direction = result as IfcDirection;
                                if (direction != null)
                                {
                                    direction.parseJObject(obj);
                                    if (!direction.is2D)
                                    {
                                        common = true;
                                        if (direction.isXAxis)
                                        {
                                            result = Factory.XAxis;
                                        }
                                        else if (direction.isYAxis)
                                        {
                                            result = Factory.YAxis;
                                        }
                                        else if (direction.isZAxis)
                                        {
                                            result = Factory.ZAxis;
                                        }
                                        else if (direction.isXAxisNegative)
                                        {
                                            result = Factory.XAxisNegative;
                                        }
                                        else if (direction.isYAxisNegative)
                                        {
                                            result = Factory.YAxisNegative;
                                        }
                                        else if (direction.isZAxisNegative)
                                        {
                                            result = Factory.ZAxisNegative;
                                        }
                                        else
                                        {
                                            common = false;
                                        }
                                    }
                                }
                                else
                                {
                                    IfcAxis2Placement3D placement = result as IfcAxis2Placement3D;
                                    if (placement != null)
                                    {
                                        placement.parseJObject(obj);
                                        if (placement.IsXYPlane(Tolerance))
                                        {
                                            result = Factory.XYPlanePlacement;
                                            common = true;
                                        }
                                    }
                                }
                            }
                            token = obj.GetValue("id", StringComparison.InvariantCultureIgnoreCase);
                            if (!string.IsNullOrEmpty(hrefId))
                            {
                                if (!(result is IfcRoot))
                                {
                                    result.setGlobalId(hrefId);
                                }
                                mDictionary.TryAdd(hrefId, result);
                            }

                            if (common)
                            {
                                return((T)(IBaseClassIfc)result);
                            }

                            int index = NextBlank();
                            this[index] = result;
                        }
                    }
                }
            }
            if (result == null)
            {
                return(default(T));
            }
            result.parseJObject(obj);
            parseBespoke(result, obj);
            IfcRoot root = result as IfcRoot;

            if (root != null)
            {
                mDictionary[root.GlobalId] = root;
            }
            return((T)(IBaseClassIfc)result);
        }
Exemplo n.º 4
0
        internal T ParseXml <T>(XmlElement xml) where T : IBaseClassIfc
        {
            if (xml == null)
            {
                return(default(T));
            }
            if (xml.HasAttribute("ref"))
            {
                string str = xml.Attributes["ref"].Value;
                if (mParsed.ContainsKey(str))
                {
                    return((T)(IBaseClassIfc)mParsed[str]);
                }
                if (xml.HasAttribute("xsi:nil"))
                {
                    string query = string.Format("//*[@id='{0}']", str, CultureInfo.InvariantCulture);
                    xml = (XmlElement)xml.OwnerDocument.SelectSingleNode(query);
                    if (xml == null)
                    {
                        return(default(T));
                    }
                }
            }
            else if (xml.HasAttribute("href"))
            {
                string str = xml.Attributes["href"].Value;
                if (mParsed.ContainsKey(str))
                {
                    return((T)(IBaseClassIfc)mParsed[str]);
                }
                if (xml.HasAttribute("xsi:nil"))
                {
                    string query = string.Format("//*[@id='{0}']", str, CultureInfo.InvariantCulture);                     // or "//book[@id='{0}']"
                    xml = (XmlElement)xml.OwnerDocument.SelectSingleNode(query);
                    if (xml == null)
                    {
                        return(default(T));
                    }
                }
            }
            string id = (xml.HasAttribute("id") ? xml.Attributes["id"].Value : "");

            if (!string.IsNullOrEmpty(id) && mParsed.ContainsKey(id))
            {
                return((T)(IBaseClassIfc)mParsed[id]);
            }

            string keyword = xml.HasAttribute("xsi:type") ? xml.Attributes["xsi:type"].Value : xml.Name;
            Type   type    = BaseClassIfc.GetType(keyword);

            if (type == null)
            {
                type = typeof(T);
                if (xml.Attributes.Count == 0 && xml.ChildNodes.Count == 1)
                {
                    XmlNode node      = xml.ChildNodes[0];
                    Type    childType = BaseClassIfc.GetType(node.Name);
                    if (string.Compare(node.Name, type.Name, true) == 0 || (childType != null && childType.IsSubclassOf(type)))
                    {
                        return(ParseXml <T>(node as XmlElement));
                    }
                }
            }
            if (type.IsAbstract)
            {
                IEnumerable <Type> types = type.Assembly.GetTypes().Where(x => x != type && type.IsAssignableFrom(x));
                if (types != null && types.Count() == 1)
                {
                    type = types.First();
                }
            }
            ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                              null, Type.EmptyTypes, null);

            if (type.IsAbstract && xml.HasChildNodes && xml.ChildNodes.Count == 1)
            {
                T result = ParseXml <T>(xml.ChildNodes[0] as XmlElement);
                if (result != null)
                {
                    return(result);
                }
            }
            if (!type.IsAbstract && constructor != null)
            {
                BaseClassIfc entity = constructor.Invoke(new object[] { }) as BaseClassIfc;
                if (entity != null)
                {
                    int index = NextBlank();
                    if (!string.IsNullOrEmpty(id))
                    {
                        int i = 0;
                        if (id[0] == 'i')
                        {
                            if (int.TryParse(id.Substring(1), out i))
                            {
                                if (this[i] == null)
                                {
                                    index = i;
                                }
                            }
                        }
                    }
                    this[index] = entity;
                    entity.ParseXml(xml);
                    if (xml.HasAttribute("id"))
                    {
                        string str = xml.Attributes["id"].Value;
                        if (!mParsed.ContainsKey(str))
                        {
                            mParsed.Add(str, entity);
                        }
                    }
                    return((T)(IBaseClassIfc)entity);
                }
            }
            if (xml.HasChildNodes && xml.ChildNodes.Count == 1)
            {
                T result = ParseXml <T>(xml.ChildNodes[0] as XmlElement);
                if (result != null)
                {
                    return(result);
                }
            }
            foreach (XmlNode node in xml.ChildNodes)
            {
                ParseXml <IBaseClassIfc>(node as XmlElement);
            }

            return(default(T));
        }