示例#1
0
        /// <summary>
        /// Convert object from IRevitEntity to a ExStorage.Entity object
        /// </summary>
        /// <param name="revitEntity">IRevitEntity object to convert</param>
        /// <returns>Converted ExStorage.Entity</returns>
        public Entity Convert(IRevitEntity revitEntity)
        {
            var entityType = revitEntity.GetType();

            //Create the schema for IRevitEntity object
            var schema = _schemaCreator.CreateSchema(entityType);

            Entity entity = new Entity(schema);

            /* Iterate all of the schema field and
             * get IRevitEntity object property value
             * for each field
             */
            var schemaFields = schema.ListFields();

            foreach (var field in schemaFields)
            {
                /*Get the property of the IRevitEntity with the
                 * same name as FieldName
                 */

                var property = entityType.GetProperty(field.FieldName);

                //Get the property value
                dynamic propertyValue = property.GetValue(revitEntity, null);

                /*We don't need to write null value to
                 * the ExStorage.Entity
                 * So we just skip this property
                 */
                if (propertyValue == null)
                {
                    continue;
                }

                Type propertyValueType =
                    propertyValue.GetType();

                switch (field.ContainerType)
                {
                case ContainerType.Simple:

                    propertyValue = ConvertSimpleProperty(propertyValue, field);

                    if (field.UnitType == UnitType.UT_Undefined)
                    {
                        entity.Set(field, propertyValue);
                    }
                    else
                    {
                        var firstCompatibleDUT = GetFirstCompatibleDUT(field);

                        entity.Set(field, propertyValue, firstCompatibleDUT);
                    }

                    break;

                case ContainerType.Array:

                    /* If we have a deal with null IList or with
                     * empty IList we must skip this field.
                     */

                    if (propertyValue.Count == 0)
                    {
                        continue;
                    }

                    var convertedIListFieldValue =
                        ConvertIListProperty(propertyValue, field);

                    /* convertedArrayFieldValue is an List<T> object.
                     * Entity.Set method throws an exception if I do not pass
                     * an IList interface as value.
                     * Even if the type implements IList<T> interface
                     * With this method which do nothing except takes a
                     * IList parameter instead FieldType, it works propoerly
                     */

                    if (field.UnitType == UnitType.UT_Undefined)
                    {
                        EntityExtension.SetWrapper(entity, field, convertedIListFieldValue);
                    }
                    else
                    {
                        var firstCompatibleDUT = GetFirstCompatibleDUT(field);

                        EntityExtension.SetWrapper(entity,
                                                   field,
                                                   convertedIListFieldValue,
                                                   firstCompatibleDUT);
                    }

                    break;

                case ContainerType.Map:
                    var convertedMapFieldValue =
                        ConvertIDictionaryProperty(propertyValue, field);

                    if (field.UnitType == UnitType.UT_Undefined)
                    {
                        EntityExtension.SetWrapper(entity, field, convertedMapFieldValue);
                    }
                    else
                    {
                        var firstCompatibleDUT = GetFirstCompatibleDUT(field);

                        EntityExtension.SetWrapper(entity,
                                                   field,
                                                   convertedMapFieldValue,
                                                   firstCompatibleDUT);
                    }
                    break;

                default:
                    throw new NotSupportedException("Unknown Field.ContainerType");
                }
            }

            return(entity);
        }