/// <summary>
        /// Converts a <see cref="IODocument"/> to a <see cref="CircuitDocument"/>.
        /// </summary>
        /// <param name="document">The IODocument to convert.</param>
        /// <returns>A CircuitDocument constructed from the IODocument.</returns>
        public static CircuitDocument ToCircuitDocument(this IODocument document, IDocumentReader reader, out List<IOComponentType> unavailableComponents)
        {
            CircuitDocument circuitDocument = new CircuitDocument();
            circuitDocument.Size = document.Size;

            // Set metadata
            circuitDocument.Metadata = new CircuitDocumentMetadata(null, null, document.Metadata);

            // Add components
            unavailableComponents = new List<IOComponentType>();
            foreach (IOComponent component in document.Components)
            {
                ComponentIdentifier identifier = null;

                // Find description
                if (component.Type.GUID != Guid.Empty && ComponentHelper.IsDescriptionAvailable(component.Type.GUID))
                    identifier = new ComponentIdentifier(ComponentHelper.FindDescription(component.Type.GUID));
                if (identifier == null && reader.IsDescriptionEmbedded(component.Type))
                    identifier = LoadDescription(reader.GetEmbeddedDescription(component.Type), component.Type);
                if (identifier == null && component.Type.IsStandard)
                    identifier = ComponentHelper.GetStandardComponent(component.Type.Collection, component.Type.Item);

                if (identifier != null)
                {
                    // Add full component

                    Dictionary<string, object> properties = new Dictionary<string,object>();
                    foreach(var property in component.Properties)
                        properties.Add(property.Key, property.Value);

                    Component addComponent = Component.Create(identifier, properties);
                    addComponent.Layout(component.Location.Value.X, component.Location.Value.Y, (component.Size.HasValue ? component.Size.Value : identifier.Description.MinSize), component.Orientation.Value, component.IsFlipped == true);
                    addComponent.ImplementMinimumSize(addComponent.Description.MinSize);
                    FlagOptions flagOptions = ComponentHelper.ApplyFlags(addComponent);
                    if ((flagOptions & FlagOptions.HorizontalOnly) == FlagOptions.HorizontalOnly && component.Orientation == Orientation.Vertical)
                        addComponent.Orientation = Orientation.Horizontal;
                    else if ((flagOptions & FlagOptions.VerticalOnly) == FlagOptions.VerticalOnly && component.Orientation == Orientation.Horizontal)
                        addComponent.Orientation = Orientation.Vertical;
                    circuitDocument.Elements.Add(addComponent);
                }
                else
                {
                    // Add disabled component

                    if (!unavailableComponents.Contains(component.Type))
                        unavailableComponents.Add(component.Type);

                    DisabledComponent addComponent = new DisabledComponent();

                    Dictionary<string, object> properties = new Dictionary<string,object>();
                    foreach(var property in component.Properties)
                        addComponent.Properties.Add(property.Key, property.Value);

                    addComponent.ImplementationCollection = component.Type.Collection;
                    addComponent.ImplementationItem = component.Type.Item;
                    addComponent.Name = component.Type.Name;
                    addComponent.GUID = component.Type.GUID;
                    if (component.Location.HasValue)
                        addComponent.Location = new Vector(component.Location.Value.X, component.Location.Value.Y);
                    addComponent.Size = component.Size;
                    addComponent.Orientation = component.Orientation;

                    circuitDocument.DisabledComponents.Add(addComponent);
                }
            }

            // Add wires
            IOComponentType wireType = new IOComponentType("wire");
            if (ComponentHelper.WireDescription == null)
            {
                unavailableComponents.Add(wireType);
            }
            else
            {
                foreach (IOWire wire in document.Wires)
                {
                    Dictionary<string, object> properties = new Dictionary<string, object>(4);
                    properties.Add("@x", wire.Location.X);
                    properties.Add("@y", wire.Location.Y);
                    properties.Add("@orientation", wire.Orientation == Orientation.Horizontal);
                    properties.Add("@size", wire.Size);

                    Component wireComponent = Component.Create(ComponentHelper.WireDescription, properties);
                    wireComponent.Layout(wire.Location.X, wire.Location.Y, wire.Size, wire.Orientation, false);
                    wireComponent.ApplyConnections(circuitDocument);
                    circuitDocument.Elements.Add(wireComponent);
                }
            }

            // Connections
            foreach (Component component in circuitDocument.Components)
                component.ApplyConnections(circuitDocument);

            return circuitDocument;
        }
Esempio n. 2
0
        /// <summary>
        /// Converts a <see cref="IODocument"/> to a <see cref="CircuitDocument"/>.
        /// </summary>
        /// <param name="document">The IODocument to convert.</param>
        /// <returns>A CircuitDocument constructed from the IODocument.</returns>
        public static CircuitDocument ToCircuitDocument(this IODocument document, IDocumentReader reader, out List <IOComponentType> unavailableComponents)
        {
            CircuitDocument circuitDocument = new CircuitDocument();

            circuitDocument.Size = document.Size;

            // Set metadata
            circuitDocument.Metadata = new CircuitDocumentMetadata(null, null, document.Metadata);

            // Add components
            unavailableComponents = new List <IOComponentType>();
            foreach (IOComponent component in document.Components)
            {
                ComponentIdentifier identifier = null;

                // Find description
                if (component.Type.GUID != Guid.Empty && ComponentHelper.IsDescriptionAvailable(component.Type.GUID))
                {
                    identifier = new ComponentIdentifier(ComponentHelper.FindDescription(component.Type.GUID));
                }
                if (identifier == null && reader.IsDescriptionEmbedded(component.Type))
                {
                    identifier = LoadDescription(reader.GetEmbeddedDescription(component.Type), component.Type);
                }
                if (identifier == null && component.Type.IsStandard)
                {
                    identifier = ComponentHelper.GetStandardComponent(component.Type.Collection, component.Type.Item);
                }

                if (identifier != null)
                {
                    // Add full component

                    Dictionary <string, object> properties = new Dictionary <string, object>();
                    foreach (var property in component.Properties)
                    {
                        properties.Add(property.Key, property.Value);
                    }

                    Component addComponent = Component.Create(identifier, properties);
                    addComponent.Layout(component.Location.Value.X, component.Location.Value.Y, (component.Size.HasValue ? component.Size.Value : identifier.Description.MinSize), component.Orientation.Value, component.IsFlipped == true);
                    addComponent.ImplementMinimumSize(addComponent.Description.MinSize);
                    FlagOptions flagOptions = ComponentHelper.ApplyFlags(addComponent);
                    if ((flagOptions & FlagOptions.HorizontalOnly) == FlagOptions.HorizontalOnly && component.Orientation == Orientation.Vertical)
                    {
                        addComponent.Orientation = Orientation.Horizontal;
                    }
                    else if ((flagOptions & FlagOptions.VerticalOnly) == FlagOptions.VerticalOnly && component.Orientation == Orientation.Horizontal)
                    {
                        addComponent.Orientation = Orientation.Vertical;
                    }
                    circuitDocument.Elements.Add(addComponent);
                }
                else
                {
                    // Add disabled component

                    if (!unavailableComponents.Contains(component.Type))
                    {
                        unavailableComponents.Add(component.Type);
                    }

                    DisabledComponent addComponent = new DisabledComponent();

                    Dictionary <string, object> properties = new Dictionary <string, object>();
                    foreach (var property in component.Properties)
                    {
                        addComponent.Properties.Add(property.Key, property.Value);
                    }

                    addComponent.ImplementationCollection = component.Type.Collection;
                    addComponent.ImplementationItem       = component.Type.Item;
                    addComponent.Name = component.Type.Name;
                    addComponent.GUID = component.Type.GUID;
                    if (component.Location.HasValue)
                    {
                        addComponent.Location = new Vector(component.Location.Value.X, component.Location.Value.Y);
                    }
                    addComponent.Size        = component.Size;
                    addComponent.Orientation = component.Orientation;

                    circuitDocument.DisabledComponents.Add(addComponent);
                }
            }

            // Add wires
            IOComponentType wireType = new IOComponentType("wire");

            if (ComponentHelper.WireDescription == null)
            {
                unavailableComponents.Add(wireType);
            }
            else
            {
                foreach (IOWire wire in document.Wires)
                {
                    Dictionary <string, object> properties = new Dictionary <string, object>(4);
                    properties.Add("@x", wire.Location.X);
                    properties.Add("@y", wire.Location.Y);
                    properties.Add("@orientation", wire.Orientation == Orientation.Horizontal);
                    properties.Add("@size", wire.Size);

                    Component wireComponent = Component.Create(ComponentHelper.WireDescription, properties);
                    wireComponent.Layout(wire.Location.X, wire.Location.Y, wire.Size, wire.Orientation, false);
                    wireComponent.ApplyConnections(circuitDocument);
                    circuitDocument.Elements.Add(wireComponent);
                }
            }

            // Connections
            foreach (Component component in circuitDocument.Components)
            {
                component.ApplyConnections(circuitDocument);
            }

            return(circuitDocument);
        }