Exemple #1
0
        private string GetEntityInfo(IRevitEntity revitEntity)
        {
            StringBuilder sb = new StringBuilder();

            Type type = revitEntity.GetType();

            sb.AppendLine(type.ToString());


            foreach (var property in type.GetProperties())
            {
                sb.Append("\t");
                sb.Append(property.Name);
                sb.Append(": ");

                var propertyValue = property.GetValue(revitEntity, null);

                IRevitEntity subRevitEntity = propertyValue as IRevitEntity;
                if (subRevitEntity == null)
                {
                    sb.AppendLine(propertyValue == null? "null": propertyValue.ToString());
                }
                else
                {
                    sb.AppendLine(GetEntityInfo(subRevitEntity));
                }
            }

            return(sb.ToString());
        }
        public static void SetEntity(this Element element, IRevitEntity revitEntity)
        {
            ISchemaCreator   schemaCreator   = new SchemaCreator();
            IEntityConverter entityConverter = new EntityConverter(schemaCreator);
            Entity           entity          = entityConverter.Convert(revitEntity);

            element.SetEntity(entity);
        }
        /// <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);
        }