예제 #1
0
        private void WriteProperty(XbimModel model, string propName, Type propType, object propVal, object entity, XmlWriter output,
                                   int pos, IfcAttribute attr)
        {
            if (propVal == null)
            //null or a value type that maybe null, need to write out sets and lists if they are mandatroy but empty
            {
                if (typeof(ExpressEnumerable).IsAssignableFrom(propType) && attr.State == IfcAttributeState.Mandatory
                    && !typeof(IfcCartesianPoint).IsAssignableFrom(propType)
                    && !typeof(IfcDirection).IsAssignableFrom(propType))
                //special case as these two classes are optimised
                {
                    output.WriteStartElement(propName);
                    output.WriteAttributeString("ex", "cType", null, attr.ListType);
                    output.WriteEndElement();
                }
                return;
            }
            else if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
                     (typeof(ExpressType).IsAssignableFrom(propVal.GetType()))) //deal with undefined types (nullables)
            {
                if (string.IsNullOrEmpty((propVal).ToString()))
                {
                    output.WriteElementString(propName, (propVal).ToString());
                }
                else
                {
                    output.WriteStartElement(propName);
                    if (pos > -1)
                        output.WriteAttributeString("pos", pos.ToString());
                    if (typeof(ExpressComplexType).IsAssignableFrom(propVal.GetType()))
                    {
                        IEnumerable<object> complexProps = ((ExpressComplexType)propVal).Properties;
                        foreach (object complexProp in complexProps)
                        {
                            int wrapPos = 0;
                            WriteProperty(model, propName, complexProp.GetType(), complexProp, entity, output, wrapPos, attr);
                            wrapPos++;
                        }
                    }
                    else
                        output.WriteValue((propVal).ToString());

                    output.WriteEndElement();
                }

            }
            else if (typeof(ExpressType).IsAssignableFrom(propType))
            {
                Type realType = propVal.GetType();

                if (realType != propType)
                //we have a type but it is a select type use the actual value but write out explicitly
                {
                    output.WriteStartElement(propName);
                    //WriteProperty(model, realType.Name + "-wrapper", realType, propVal, entity, output, pos, attr);
                    WriteProperty(model, realType.Name, realType, propVal, entity, output, pos, attr);
                    output.WriteEndElement();
                }
                else
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement(propName);
                        output.WriteAttributeString("pos", pos.ToString());
                        output.WriteValue((propVal).ToString());
                        output.WriteEndElement();
                    }
                    // if its nullable 
                    //if (((ExpressType)propVal).ToPart21 != "$")
                    else if (propVal.ToString() != null)
                        output.WriteElementString(propName, (propVal).ToString());
                }
            }
            else if (typeof(ExpressEnumerable).IsAssignableFrom(propType)
                     && !typeof(IfcCartesianPoint).IsAssignableFrom(propType)
                     && !typeof(IfcDirection).IsAssignableFrom(propType))
            //special case as these two classes are optimised
            {
                //try
                //{
                output.WriteStartElement(propName);
                output.WriteAttributeString("ex", "cType", null, ((ExpressEnumerable)propVal).ListType);
                int i = 0;
                foreach (object item in ((ExpressEnumerable)propVal))
                {
                    WriteProperty(model, item.GetType().Name, item.GetType(), item, entity, output, i, attr);
                    i++;
                }
                output.WriteEndElement();

                //}
                //catch (Exception e)
                //{

                //    throw;
                //}
            }
            else if (typeof(IPersistIfcEntity).IsAssignableFrom(propType))
            //all writable entities must support this interface and ExpressType have been handled so only entities left
            {
                IPersistIfcEntity persistVal = (IPersistIfcEntity)propVal;
                if (pos == -1) output.WriteStartElement(propName);
                if (_written.Contains(persistVal.EntityLabel)) //we have already written it so use an xlink
                {
                    output.WriteStartElement(propVal.GetType().Name);
                    output.WriteAttributeString("ref", string.Format("i{0}", persistVal.EntityLabel));
                    output.WriteAttributeString("xsi", "nil", null, "true");
                    if (pos > -1) //we are writing out a list element
                        output.WriteAttributeString("pos", pos.ToString());
                    output.WriteEndElement();
                }
                else
                {
                    Write(model, persistVal.EntityLabel, output, pos);
                }
                if (pos == -1) output.WriteEndElement();
            }
            else if (typeof(ExpressComplexType).IsAssignableFrom(propType)) //it is a complex value tpye
            {
                IEnumerable<object> properties = ((ExpressComplexType)propVal).Properties;
            }
            else if (propType.IsValueType) //it might be an in-built value type double, string etc
            {
                Type pInfoType = propVal.GetType();

                if (pInfoType.IsEnum) //convert enum
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement(propName);
                        output.WriteAttributeString("pos", pos.ToString());
                    }
                    else
                        output.WriteStartElement(propName);
                    output.WriteValue(propVal.ToString().ToLower());
                }
                else if (pInfoType.UnderlyingSystemType == typeof(Boolean))
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement("ex", "boolean-wrapper", null);
                        output.WriteAttributeString("pos", pos.ToString());
                    }
                    else
                        output.WriteStartElement(propName);
                    output.WriteValue((bool)propVal ? "true" : "false");
                }
                else if (pInfoType.UnderlyingSystemType == typeof(Double))
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement("ex", "double-wrapper", null);
                        output.WriteAttributeString("pos", pos.ToString());
                    }
                    else
                        output.WriteStartElement(propName);
                    output.WriteValue(string.Format(new Part21Formatter(), "{0:R}", propVal));
                }
                else if (pInfoType.UnderlyingSystemType == typeof(Int16))
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement("ex", "integer-wrapper", null);
                        output.WriteAttributeString("pos", pos.ToString());
                    }
                    else
                        output.WriteStartElement(propName);
                    output.WriteValue(propVal.ToString());
                }
                else if (pInfoType.UnderlyingSystemType == typeof(Int32) ||
                         pInfoType.UnderlyingSystemType == typeof(Int64))
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement("ex", "long-wrapper", null);
                        output.WriteAttributeString("pos", pos.ToString());
                    }
                    else
                        output.WriteStartElement(propName);
                    output.WriteValue(propVal.ToString());
                }
                else if (pInfoType.UnderlyingSystemType == typeof(String)) //convert  string
                {
                    if (pos > -1)
                    {
                        output.WriteStartElement("ex", "string-wrapper", null);
                        output.WriteAttributeString("pos", pos.ToString());
                    }
                    else
                        output.WriteStartElement(propName);
                    output.WriteValue(string.Format(new Part21Formatter(), "{0}", propVal));
                }

                else
                    throw new ArgumentException(string.Format("Invalid Value Type {0}", pInfoType.Name),
                                                "pInfoType");

                output.WriteEndElement();
            }
            else if (typeof(ExpressSelectType).IsAssignableFrom(propType))
            // a select type get the type of the actual value
            {
                Type realType = propVal.GetType();
                output.WriteStartElement(propName);
                if (typeof(ExpressType).IsAssignableFrom(realType))
                {
                    //WriteProperty(model, realType.Name + "-wrapper", realType, propVal, entity, output, pos, attr);
                    WriteProperty(model, realType.Name, realType, propVal, entity, output, pos, attr);
                }
                else
                {
                    WriteProperty(model, realType.Name, realType, propVal, entity, output, -2, attr);
                }
                output.WriteEndElement();
            }
            //else
            //    throw new Exception(string.Format("Entity of type {0} has illegal property {1} of type {2}", entity.GetType().ToString(), propType.Name, propType.Name));
        }
예제 #2
0
 public IfcEntityAttribute(string propertyName, string typeName, IfcAttribute attributeProperties)
 {
     PropertyName = propertyName;
     TypeName = typeName;
     AttributeProperties = attributeProperties;
 }