예제 #1
0
        public T ParseJObject <T>(JObject obj) where T : IBaseClassIfc
        {
            if (obj == null)
            {
                return(default(T));
            }

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

            if (token != null)
            {
                mDictionary.TryGetValue(token.Value <string>(), out result);
                if (result != null && obj.Count == 1)
                {
                    return((T)(IBaseClassIfc)result);
                }
            }
            if (result == null)
            {
                Type type = null;
                token = obj.GetValue("type", StringComparison.InvariantCultureIgnoreCase);
                if (token != null)
                {
                    string keyword = token.Value <string>();
                    type = Type.GetType("GeometryGym.Ifc." + keyword, false, true);
                }
                if (token == null)
                {
                    type = typeof(T);
                }
                if (type != null)
                {
                    if (type.IsAbstract)
                    {
                        JProperty jtoken    = (JProperty)obj.First;
                        Type      valueType = Type.GetType("GeometryGym.Ifc." + jtoken.Name, false, true);
                        if (valueType != null && valueType.IsSubclassOf(typeof(IfcValue)))
                        {
                            return((T)(IBaseClassIfc)ParserIfc.extractValue(jtoken.Name, jtoken.Value.ToString()));
                        }
                    }
                    else
                    {
                        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;

                                IfcCartesianPoint point = result as IfcCartesianPoint;
                                if (point != null)
                                {
                                    point.parseJObject(obj);
                                    if (point.isOrigin)
                                    {
                                        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)
                                            {
                                                result = Factory.XYPlanePlacement;
                                                common = true;
                                            }
                                        }
                                    }
                                }
                                token = obj.GetValue("id", StringComparison.InvariantCultureIgnoreCase);
                                if (token != null)
                                {
                                    string id = token.Value <string>();
                                    if (!(result is IfcRoot))
                                    {
                                        result.mGlobalId = id;
                                    }
                                    mDictionary.TryAdd(id, 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);
        }
        public T ParseJObject <T>(JObject obj) where T : IBaseClassIfc
        {
            if (obj == null)
            {
                return(default(T));
            }

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

            if (token != null)
            {
                if (token.Type == JTokenType.Integer)
                {
                    int index = token.Value <int>();
                    result = this[index];
                }
                else if (token.Type == JTokenType.String)
                {
                    mDictionary.TryGetValue(token.Value <string>(), out result);
                }
                if (result != null && obj.Count == 1)
                {
                    return((T)(IBaseClassIfc)result);
                }
            }
            if (result == null)
            {
                Type type = null;
                token = obj.GetValue("type", StringComparison.InvariantCultureIgnoreCase);
                if (token != null)
                {
                    string keyword = token.Value <string>();
                    type = Type.GetType("GeometryGym.Ifc." + keyword, false, true);
                }
                if (token == null)
                {
                    type = typeof(T);
                }
                if (type != null && !type.IsAbstract)
                {
                    ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                      null, Type.EmptyTypes, null);
                    if (constructor != null)
                    {
                        result = constructor.Invoke(new object[] { }) as BaseClassIfc;
                        if (result != null)
                        {
                            result.mDatabase = this;
                            token            = obj.GetValue("id", StringComparison.InvariantCultureIgnoreCase);
                            int index = 0;                            // (int) (this.mIfcObjects.Count * 1.2);
                            if (token != null)
                            {
                                if (token.Type == JTokenType.Integer)
                                {
                                    try
                                    {
                                        int i = token.Value <int>();
                                        if (this[i] == null)
                                        {
                                            index = i;
                                        }
                                    }
                                    catch (Exception) { }
                                    // TODO merge if existing equivalent
                                }
                                else if (token.Type == JTokenType.String)
                                {
                                    result.mGlobalId = token.Value <string>();
                                    mDictionary.TryAdd(result.mGlobalId, result);
                                }
                            }
                            IfcCartesianPoint   point     = result as IfcCartesianPoint;
                            IfcDirection        direction = result as IfcDirection;
                            IfcAxis2Placement3D placement = result as IfcAxis2Placement3D;
                            if (index == 0)
                            {
                                if (point != null)
                                {
                                    point.parseJObject(obj);
                                    if (point.isOrigin)
                                    {
                                        if (point.is2D)
                                        {
                                            return((T)(IBaseClassIfc)Factory.Origin2d);
                                        }
                                        return((T)(IBaseClassIfc)Factory.Origin);
                                    }
                                }
                                else
                                {
                                    if (direction != null)
                                    {
                                        direction.parseJObject(obj);
                                        if (!direction.is2D)
                                        {
                                            if (direction.isXAxis)
                                            {
                                                return((T)(IBaseClassIfc)Factory.XAxis);
                                            }
                                            if (direction.isYAxis)
                                            {
                                                return((T)(IBaseClassIfc)Factory.YAxis);
                                            }
                                            if (direction.isZAxis)
                                            {
                                                return((T)(IBaseClassIfc)Factory.ZAxis);
                                            }
                                            if (direction.isXAxisNegative)
                                            {
                                                return((T)(IBaseClassIfc)Factory.XAxisNegative);
                                            }
                                            if (direction.isYAxisNegative)
                                            {
                                                return((T)(IBaseClassIfc)Factory.YAxisNegative);
                                            }
                                            if (direction.isZAxisNegative)
                                            {
                                                return((T)(IBaseClassIfc)Factory.ZAxisNegative);
                                            }
                                        }
                                    }
                                    if (placement != null)
                                    {
                                        placement.parseJObject(obj);
                                        if (placement.IsXYPlane)
                                        {
                                            return((T)(IBaseClassIfc)Factory.XYPlanePlacement);
                                        }
                                    }
                                }

                                index       = NextBlank();
                                this[index] = result;

                                if (point != null || direction != null || placement != null)
                                {
                                    return((T)(IBaseClassIfc)result);
                                }
                            }
                            else
                            {
                                this[index] = result;
                            }
                        }
                    }
                }
            }
            if (result == null)
            {
                return(default(T));
            }
            result.parseJObject(obj);
            parseBespoke(result, obj);
            return((T)(IBaseClassIfc)result);
        }
예제 #3
0
        internal T parseJObject <T>(JObject obj) where T : IBaseClassIfc
        {
            if (obj == null)
            {
                return(default(T));
            }
            JToken token = obj.GetValue("href", StringComparison.InvariantCultureIgnoreCase);

            if (token != null)
            {
                int index = token.Value <int>();
                try
                {
                    return((T)(IBaseClassIfc)this[index]);
                }
                catch (Exception) { }
                return(default(T));
            }
            Type type = null;

            token = obj.GetValue("type", StringComparison.InvariantCultureIgnoreCase);
            if (token != null)
            {
                string keyword = token.Value <string>();
                type = Type.GetType("GeometryGym.Ifc." + keyword, false, true);
            }
            if (token == null)
            {
                type = typeof(T);
            }
            if (type != null)
            {
                ConstructorInfo constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                  null, Type.EmptyTypes, null);
                if (constructor != null)
                {
                    BaseClassIfc entity = constructor.Invoke(new object[] { }) as BaseClassIfc;
                    if (entity != null)
                    {
                        token = obj.GetValue("id", StringComparison.InvariantCultureIgnoreCase);
                        int index = 0;                        // (int) (this.mIfcObjects.Count * 1.2);
                        if (token != null)
                        {
                            int i = token.Value <int>();
                            if (this[i] == null)
                            {
                                index = i;
                            }
                            // TODO merge if existing equivalent
                        }
                        if (index == 0)
                        {
                            if (mNextUnassigned == 0)
                            {
                                mNextUnassigned = Math.Max(mIfcObjects.Count * 2, 1000);
                            }
                            index = mNextUnassigned++;
                        }
                        this[index] = entity;
                        entity.parseJObject(obj);
                        parseBespoke(entity, obj);
                        return((T)(IBaseClassIfc)entity);
                    }
                }
            }
            return(default(T));
        }
		public T ParseJObject<T>(JObject obj) where T : IBaseClassIfc
		{
			if (obj == null)
				return default(T);

			Type type = typeof(T);
			
			BaseClassIfc result = null;
			JToken token = obj.GetValue("href", StringComparison.InvariantCultureIgnoreCase);
			if (token != null)
			{
				string hrefId = token.Value<string>();
				if(mDictionary.TryGetValue(hrefId, out result) && obj.Count == 1)
					return (T)(IBaseClassIfc)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);
					}
				}
				else
				{
					token = obj.GetValue("type", StringComparison.InvariantCultureIgnoreCase);
					if(token != null)
					{
						Type nominatedType = BaseClassIfc.GetType(token.Value<string>());
						if (nominatedType != null)
							type = nominatedType;
					}
					string hrefId = "";
					token = obj.GetValue("id", StringComparison.InvariantCultureIgnoreCase);
					if (token != null)
						hrefId = token.Value<string>();
					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;

								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;
		}