Exemplo n.º 1
0
        /// <summary>
        /// Reflection method to read entities.
        /// </summary>
        /// <remarks>
        /// Is functional but not very reliable so it should disapear in the future and have a method for each entity.
        /// </remarks>
        /// <param name="template"></param>
        /// <returns></returns>
        private Entity readEntity(DxfEntityTemplate template)
        {
            Entity entity = null;

            //Get the current entity
            switch (template.EntityName)
            {
            //TODO: Check the SubclassMarker
            case DxfFileToken.EntityArc:
                entity = new Arc(template);
                break;

            case DxfFileToken.EntityCircle:
                entity = new Circle(template);
                break;

            case DxfFileToken.EntityPolyline:
                entity = new PolyLine(template);
                break;

            case DxfFileToken.EntityText:
                entity = new Text(template);
                break;

            case DxfFileToken.EntityVertex:
                entity = new Vertex(template);
                break;

            case DxfFileToken.EntityAttributeDefinition:
                Debug.Fail("Check the property VerticalJustification and see the assigned code, should be 74");
                entity = new AttributeDefinition(template);
                break;

            default:
                //Debug.Fail($"Unhandeled entity {template.EntityName}.");
                break;
            }

            //Jump the SubclassMarker
            m_reader.ReadNext();

            Dictionary <DxfCode, object> map = entity?.GetCadObjectMap() ?? new Dictionary <DxfCode, object>();

            while (m_reader.LastValueAsString != DxfFileToken.EndSection)
            {
                if (m_reader.LastDxfCode == DxfCode.Start)
                {
                    //Check if the entity has children in it
                    Dictionary <string, PropertyInfo> subEntity = entity?.GetSubEntitiesMap() ?? new Dictionary <string, PropertyInfo>();

                    if (!subEntity.ContainsKey(m_reader.LastValueAsString))
                    {
                        //Is a separated entity
                        break;
                    }

                    //Read the sequence
                    while (m_reader.LastValueAsString != DxfFileToken.EndSequence)
                    {
                        Entity child = this.readEntity();

                        PropertyInfo prop = subEntity[child.ObjectName];

                        if (prop.GetValue(entity, null) is ICollection <Entity> pvalue)
                        {
                            pvalue.Add(child);
                        }
                        else
                        {
                            //Will be used for something??
                            Debug.Fail("");
                            prop.SetValue(entity, child);
                        }
                    }

                    //Read the end of sequence
                    m_reader.ReadNext();
                    while (m_reader.LastDxfCode != DxfCode.Start)
                    {
                        m_reader.ReadNext();
                    }

                    //The end of the sequence is the end of the entity
                    break;
                }
                else if (map.ContainsKey(m_reader.LastDxfCode))
                {
                    //Set the value
                    map[m_reader.LastDxfCode] = m_reader.LastValue;
                }

                //Get the next line
                m_reader.ReadNext();
            }

            //Build the entity based on the map
            entity?.Build(map);

            return(entity);
        }