Beispiel #1
0
        private bool LoadVersion1_1(XmlDocument doc)
        {
            try
            {
                Document = new IODocument();

                XmlElement circuitNode = doc.SelectSingleNode("/circuit") as XmlElement;
                if (circuitNode.HasAttribute("width") && circuitNode.HasAttribute("height"))
                    Document.Size = new System.Windows.Size(double.Parse(circuitNode.Attributes["width"].InnerText), double.Parse(circuitNode.Attributes["height"].InnerText));
                string application = null;
                string appVersion = null;
                if (circuitNode.HasAttribute("application"))
                {
                    application = circuitNode.Attributes["application"].InnerText;
                    if (circuitNode.HasAttribute("appversion"))
                        appVersion = circuitNode.Attributes["appversion"].InnerText;
                }
                else if (circuitNode.HasAttribute("cd-version"))
                {
                    application = "Circuit Diagram ";
                    appVersion = circuitNode.Attributes["cd-version"].InnerText;
                }

                XmlNodeList componentNodes = doc.SelectNodes("/circuit/component");
                foreach (XmlNode node in componentNodes)
                {
                    string type = node.Attributes["type"].InnerText;
                    double x = double.Parse(node.Attributes["x"].InnerText);
                    double y = double.Parse(node.Attributes["y"].InnerText);
                    double size = 10d;
                    if ((node as XmlElement).HasAttribute("size"))
                        size = double.Parse(node.Attributes["size"].InnerText);
                    bool horizontal = true;
                    if ((node as XmlElement).HasAttribute("orientation") && node.Attributes["orientation"].InnerText.ToLowerInvariant() == "vertical")
                        horizontal = false;
                    Guid guid = Guid.Empty;
                    if ((node as XmlElement).HasAttribute("guid"))
                        guid = new Guid(node.Attributes["guid"].InnerText);

                    // Check component type
                    string lType = type.ToLowerInvariant();

                    if (lType == "wire")
                    {
                        // Element is wire

                        IOWire wire = new IOWire();
                        wire.Location = new System.Windows.Point(x, y);
                        wire.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);
                        wire.Size = size;

                        // Add to document
                        Document.Wires.Add(wire);
                    }
                    else
                    {
                        // Element is component

                        IOComponent component = new IOComponent();
                        component.Location = new System.Windows.Point(x, y);
                        component.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);
                        component.Size = size;

                        // Other properties
                        Dictionary<string, object> properties = new Dictionary<string, object>(node.Attributes.Count);
                        foreach (XmlAttribute attribute in node.Attributes)
                            properties.Add(attribute.Name, attribute.InnerText);

                        // Read type parameter
                        string t = null;
                        if ((node as XmlElement).HasAttribute("t"))
                            t = node.Attributes["t"].InnerText;
                        if (t == null && lType == "logicgate" && (node as XmlElement).HasAttribute("logictype"))
                            t = node.Attributes["logictype"].InnerText;

                        if (properties.ContainsKey("flipped"))
                        {
                            component.IsFlipped = bool.Parse(properties["flipped"].ToString());
                            properties.Remove("flipped");
                        }

                        // Convert component name
                        string componentCollection;
                        string standardComponentName;
                        ConvertComponentName(lType, t, out componentCollection, out standardComponentName);

                        // Set properties
                        ConvertProperties(componentCollection, standardComponentName, properties);
                        foreach (var property in properties)
                            component.Properties.Add(new IOComponentProperty(property.Key, property.Value, true));

                        // Set type
                        component.Type = new IOComponentType(componentCollection, standardComponentName);
                        component.Type.Name = type;
                        component.Type.GUID = guid;

                        // Add to document
                        Document.Components.Add(component);
                    }
                }

                // Set metadata
                Document.Metadata.Application = application;
                Document.Metadata.AppVersion = appVersion;

                LoadResult = new DocumentLoadResult();
                LoadResult.Format = "XML (1.1)";
                LoadResult.Type = DocumentLoadResultType.Success;

                return true;
            }
            catch (Exception)
            {
                // Wrong format
                LoadResult = new DocumentLoadResult();
                LoadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return false;
            }
        }
Beispiel #2
0
        private bool LoadVersion1_0(XmlDocument doc)
        {
            try
            {
                DocumentLoadResult loadResult = new DocumentLoadResult();

                Document = new IODocument();

                XmlElement circuitNode = doc.SelectSingleNode("/circuit") as XmlElement;
                if (circuitNode.HasAttribute("width") && circuitNode.HasAttribute("height"))
                    Document.Size = new System.Windows.Size(double.Parse(circuitNode.Attributes["width"].InnerText), double.Parse(circuitNode.Attributes["height"].InnerText));
                string application = null;
                string appVersion = null;
                if (circuitNode.HasAttribute("application"))
                    application = circuitNode.Attributes["application"].InnerText;
                else if (circuitNode.HasAttribute("cd-version"))
                {
                    application = "Circuit Diagram ";
                    appVersion = circuitNode.Attributes["cd-version"].InnerText;
                }

                foreach (XmlNode node in circuitNode.ChildNodes)
                {
                    if (node.NodeType != XmlNodeType.Element)
                        continue;

                    string type = node.Name;
                    string location = node.Attributes["location"].InnerText;
                    string[] locationSplit = location.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    // Snap points to grid
                    double x = Math.Round(double.Parse(locationSplit[0]) / 10d) * 10d;
                    double y = Math.Round(double.Parse(locationSplit[1]) / 10d) * 10d;
                    double endX = Math.Round(double.Parse(locationSplit[2]) / 10d) * 10d;
                    double endY = Math.Round(double.Parse(locationSplit[3]) / 10d) * 10d;
                    double size = endX - x;
                    bool horizontal = true;
                    if (endX == x)
                    {
                        size = endY - y;
                        horizontal = false;
                    }

                    // Other properties
                    Dictionary<string, object> properties = new Dictionary<string, object>(node.Attributes.Count);
                    foreach (XmlAttribute attribute in node.Attributes)
                        properties.Add(attribute.Name, attribute.InnerText);

                    string lType = type.ToLowerInvariant();

                    if (lType == "wire")
                    {
                        // Wire

                        IOWire wire = new IOWire();
                        wire.Location = new System.Windows.Point(x, y);
                        wire.Size = size;
                        wire.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);

                        // Add to document
                        Document.Wires.Add(wire);
                    }
                    else
                    {
                        // Full component

                        IOComponent component = new IOComponent();
                        component.Location = new System.Windows.Point(x, y);
                        component.Size = size;
                        component.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);

                        // Read type parameter
                        string t = null;
                        if ((node as XmlElement).HasAttribute("t"))
                            t = node.Attributes["t"].InnerText;
                        else if ((node as XmlElement).HasAttribute("type"))
                            t = node.Attributes["type"].InnerText;

                        if (properties.ContainsKey("t"))
                            properties.Remove("t");

                        // Convert component name
                        string componentCollection;
                        string standardComponentName;
                        ConvertComponentName(lType, t, out componentCollection, out standardComponentName);

                        // Set properties
                        ConvertProperties(componentCollection, standardComponentName, properties);
                        foreach (var property in properties)
                            component.Properties.Add(new IOComponentProperty(property.Key, property.Value, true));

                        // Set type
                        component.Type = new IOComponentType(componentCollection, standardComponentName);
                        component.Type.Name = type;

                        // Add to document
                        Document.Components.Add(component);
                    }
                }

                // Reset metadata
                Document.Metadata = new CircuitDocumentMetadata();
                LoadResult.Format = "XML (1.0)";
                Document.Metadata.Application = application;
                Document.Metadata.AppVersion = appVersion;

                LoadResult.Type = DocumentLoadResultType.Success;
                return true;
            }
            catch (Exception)
            {
                // Wrong format
                LoadResult = new DocumentLoadResult();
                LoadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return false;
            }
        }
Beispiel #3
0
        private bool LoadVersion1_0(XmlDocument doc)
        {
            try
            {
                DocumentLoadResult loadResult = new DocumentLoadResult();

                Document = new IODocument();

                XmlElement circuitNode = doc.SelectSingleNode("/circuit") as XmlElement;
                if (circuitNode.HasAttribute("width") && circuitNode.HasAttribute("height"))
                {
                    Document.Size = new Size(double.Parse(circuitNode.Attributes["width"].InnerText), double.Parse(circuitNode.Attributes["height"].InnerText));
                }
                string application = null;
                string appVersion  = null;
                if (circuitNode.HasAttribute("application"))
                {
                    application = circuitNode.Attributes["application"].InnerText;
                }
                else if (circuitNode.HasAttribute("cd-version"))
                {
                    application = "Circuit Diagram ";
                    appVersion  = circuitNode.Attributes["cd-version"].InnerText;
                }

                foreach (XmlNode node in circuitNode.ChildNodes)
                {
                    if (node.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    string   type          = node.Name;
                    string   location      = node.Attributes["location"].InnerText;
                    string[] locationSplit = location.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    // Snap points to grid
                    double x          = Math.Round(double.Parse(locationSplit[0]) / 10d) * 10d;
                    double y          = Math.Round(double.Parse(locationSplit[1]) / 10d) * 10d;
                    double endX       = Math.Round(double.Parse(locationSplit[2]) / 10d) * 10d;
                    double endY       = Math.Round(double.Parse(locationSplit[3]) / 10d) * 10d;
                    double size       = endX - x;
                    bool   horizontal = true;
                    if (endX == x)
                    {
                        size       = endY - y;
                        horizontal = false;
                    }

                    // Other properties
                    Dictionary <string, object> properties = new Dictionary <string, object>(node.Attributes.Count);
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        properties.Add(attribute.Name, attribute.InnerText);
                    }

                    string lType = type.ToLowerInvariant();

                    if (lType == "wire")
                    {
                        // Wire

                        IOWire wire = new IOWire();
                        wire.Location    = new Point(x, y);
                        wire.Size        = size;
                        wire.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);

                        // Add to document
                        Document.Wires.Add(wire);
                    }
                    else
                    {
                        // Full component

                        var component = new IOComponent();
                        component.Location    = new Point(x, y);
                        component.Size        = size;
                        component.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);

                        // Read type parameter
                        string t = null;
                        if ((node as XmlElement).HasAttribute("t"))
                        {
                            t = node.Attributes["t"].InnerText;
                        }
                        else if ((node as XmlElement).HasAttribute("type"))
                        {
                            t = node.Attributes["type"].InnerText;
                        }

                        if (properties.ContainsKey("t"))
                        {
                            properties.Remove("t");
                        }

                        // Convert component name
                        string componentCollection;
                        string standardComponentName;
                        ConvertComponentName(lType, t, out componentCollection, out standardComponentName);

                        // Set properties
                        ConvertProperties(componentCollection, standardComponentName, properties);
                        foreach (var property in properties)
                        {
                            component.Properties.Add(new IOComponentProperty(property.Key, property.Value, true));
                        }

                        // Set type
                        component.Type      = new IOComponentType(componentCollection, standardComponentName);
                        component.Type.Name = type;

                        // Add to document
                        Document.Components.Add(component);
                    }
                }

                // Reset metadata
                Document.Metadata             = new CircuitDocumentMetadata();
                LoadResult.Format             = "XML (1.0)";
                Document.Metadata.Application = application;
                Document.Metadata.AppVersion  = appVersion;

                LoadResult.Type = DocumentLoadResultType.Success;
                return(true);
            }
            catch (Exception)
            {
                // Wrong format
                LoadResult      = new DocumentLoadResult();
                LoadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return(false);
            }
        }
Beispiel #4
0
        private bool LoadVersion1_1(XmlDocument doc)
        {
            try
            {
                Document = new IODocument();

                XmlElement circuitNode = doc.SelectSingleNode("/circuit") as XmlElement;
                if (circuitNode.HasAttribute("width") && circuitNode.HasAttribute("height"))
                {
                    Document.Size = new Size(double.Parse(circuitNode.Attributes["width"].InnerText), double.Parse(circuitNode.Attributes["height"].InnerText));
                }
                string application = null;
                string appVersion  = null;
                if (circuitNode.HasAttribute("application"))
                {
                    application = circuitNode.Attributes["application"].InnerText;
                    if (circuitNode.HasAttribute("appversion"))
                    {
                        appVersion = circuitNode.Attributes["appversion"].InnerText;
                    }
                }
                else if (circuitNode.HasAttribute("cd-version"))
                {
                    application = "Circuit Diagram ";
                    appVersion  = circuitNode.Attributes["cd-version"].InnerText;
                }

                XmlNodeList componentNodes = doc.SelectNodes("/circuit/component");
                foreach (XmlNode node in componentNodes)
                {
                    string type = node.Attributes["type"].InnerText;
                    double x    = double.Parse(node.Attributes["x"].InnerText);
                    double y    = double.Parse(node.Attributes["y"].InnerText);
                    double size = 10d;
                    if ((node as XmlElement).HasAttribute("size"))
                    {
                        size = double.Parse(node.Attributes["size"].InnerText);
                    }
                    bool horizontal = true;
                    if ((node as XmlElement).HasAttribute("orientation") && node.Attributes["orientation"].InnerText.ToLowerInvariant() == "vertical")
                    {
                        horizontal = false;
                    }
                    Guid guid = Guid.Empty;
                    if ((node as XmlElement).HasAttribute("guid"))
                    {
                        guid = new Guid(node.Attributes["guid"].InnerText);
                    }

                    // Check component type
                    string lType = type.ToLowerInvariant();

                    if (lType == "wire")
                    {
                        // Element is wire

                        IOWire wire = new IOWire();
                        wire.Location    = new Point(x, y);
                        wire.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);
                        wire.Size        = size;

                        // Add to document
                        Document.Wires.Add(wire);
                    }
                    else
                    {
                        // Element is component

                        IOComponent component = new IOComponent();
                        component.Location    = new Point(x, y);
                        component.Orientation = (horizontal ? Orientation.Horizontal : Orientation.Vertical);
                        component.Size        = size;

                        // Other properties
                        Dictionary <string, object> properties = new Dictionary <string, object>(node.Attributes.Count);
                        foreach (XmlAttribute attribute in node.Attributes)
                        {
                            properties.Add(attribute.Name, attribute.InnerText);
                        }

                        // Read type parameter
                        string t = null;
                        if ((node as XmlElement).HasAttribute("t"))
                        {
                            t = node.Attributes["t"].InnerText;
                        }
                        if (t == null && lType == "logicgate" && (node as XmlElement).HasAttribute("logictype"))
                        {
                            t = node.Attributes["logictype"].InnerText;
                        }

                        if (properties.ContainsKey("flipped"))
                        {
                            component.IsFlipped = bool.Parse(properties["flipped"].ToString());
                            properties.Remove("flipped");
                        }

                        // Convert component name
                        string componentCollection;
                        string standardComponentName;
                        ConvertComponentName(lType, t, out componentCollection, out standardComponentName);

                        // Set properties
                        ConvertProperties(componentCollection, standardComponentName, properties);
                        foreach (var property in properties)
                        {
                            component.Properties.Add(new IOComponentProperty(property.Key, property.Value, true));
                        }

                        // Set type
                        component.Type      = new IOComponentType(componentCollection, standardComponentName);
                        component.Type.Name = type;
                        component.Type.GUID = guid;

                        // Add to document
                        Document.Components.Add(component);
                    }
                }

                // Set metadata
                Document.Metadata.Application = application;
                Document.Metadata.AppVersion  = appVersion;

                LoadResult        = new DocumentLoadResult();
                LoadResult.Format = "XML (1.1)";
                LoadResult.Type   = DocumentLoadResultType.Success;

                return(true);
            }
            catch (Exception)
            {
                // Wrong format
                LoadResult      = new DocumentLoadResult();
                LoadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return(false);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Converts a <see cref="CircuitDocument"/> to an <see cref="IODocument"/>.
        /// </summary>
        /// <param name="document">The CircuitDocument to convert.</param>
        /// <returns>An IODocument constructed from the CircuitDocument.</returns>
        public static IODocument ToIODocument(this CircuitDocument document, out IDictionary <IOComponentType, EmbedComponentData> embedComponents)
        {
            IODocument ioDocument = new IODocument();

            ioDocument.Size = document.Size;

            // Set metadata
            ioDocument.Metadata = document.Metadata;

            // Get connections
            Dictionary <Component, Dictionary <string, string> > connections = ConnectionHelper.RemoveWires(document);

            // Generate types
            Dictionary <ComponentIdentifier, IOComponentType> componentTypes = new Dictionary <ComponentIdentifier, IOComponentType>();

            foreach (Component component in document.Components)
            {
                if (ComponentHelper.IsWire(component))
                {
                    continue; // Skip wires
                }
                ComponentIdentifier identifier = new ComponentIdentifier(component.Description, component.Configuration());
                if (!componentTypes.ContainsKey(identifier))
                {
                    IOComponentType ioType = new IOComponentType(component.Description.Metadata.ImplementSet,
                                                                 (identifier.Configuration != null && !String.IsNullOrEmpty(identifier.Configuration.ImplementationName) ? identifier.Configuration.ImplementationName : component.Description.Metadata.ImplementItem));
                    ioType.Name = component.Description.ComponentName;
                    ioType.GUID = component.Description.Metadata.GUID;
                    componentTypes.Add(identifier, ioType);
                }
            }

            // Add visible components
            int idCounter = 0; // generate component IDs

            foreach (IComponentElement component in document.Elements.Where(component => component is IComponentElement && !ComponentHelper.IsWire(component)))
            {
                IOComponent ioComponent = new IOComponent();
                ioComponent.ID          = idCounter.ToString();
                ioComponent.Size        = component.Size;
                ioComponent.Location    = new Point(component.Location.X, component.Location.Y);
                ioComponent.IsFlipped   = component.IsFlipped;
                ioComponent.Orientation = component.Orientation;
                IOComponentType ioType = new IOComponentType(component.ImplementationCollection, component.ImplementationItem);

                if (component is Component)
                {
                    Component cComponent = component as Component;
                    ioType.Name      = cComponent.Description.ComponentName;
                    ioType.GUID      = cComponent.Description.Metadata.GUID;
                    ioComponent.Type = ioType;

                    // Set connections
                    if (connections.ContainsKey(cComponent))
                    {
                        foreach (var connection in connections[cComponent])
                        {
                            ioComponent.Connections.Add(connection.Key, connection.Value);
                        }
                    }
                }

                // Set properties
                foreach (var property in component.Properties)
                {
                    ioComponent.Properties.Add(new IOComponentProperty(property.Key, property.Value, true)); // TODO: implement IsStandard
                }
                ioDocument.Components.Add(ioComponent);

                idCounter++;
            }

            // Add unavailable components
            foreach (DisabledComponent component in document.DisabledComponents)
            {
                Point?location = null;
                if (component.Location.HasValue)
                {
                    location = new Point(component.Location.Value.X, component.Location.Value.Y);
                }

                IOComponent ioComponent = new IOComponent();
                ioComponent.ID          = idCounter.ToString();
                ioComponent.Location    = location;
                ioComponent.Size        = component.Size;
                ioComponent.IsFlipped   = component.IsFlipped;
                ioComponent.Orientation = component.Orientation;

                IOComponentType ioType = new IOComponentType(component.ImplementationCollection, component.ImplementationItem);

                // Set name
                if (!String.IsNullOrEmpty(component.Name))
                {
                    ioType.Name = component.Name;
                }

                // Set GUID
                if (component.GUID.HasValue)
                {
                    ioType.GUID = component.GUID.Value;
                }

                ioComponent.Type = ioType;

                // Set properties
                foreach (var property in component.Properties)
                {
                    ioComponent.Properties.Add(new IOComponentProperty(property.Key, property.Value, true));
                }

                ioDocument.Components.Add(ioComponent);

                idCounter++;
            }

            // Add wires
            foreach (IComponentElement wire in document.Components.Where(component => ComponentHelper.IsWire(component)))
            {
                IOWire ioWire = new IOWire(new Point(wire.Location.X, wire.Location.Y), wire.Size, wire.Orientation);
                ioDocument.Wires.Add(ioWire);
            }

            // Embed components
            embedComponents = new Dictionary <IOComponentType, EmbedComponentData>();
            foreach (EmbedDescription embedItem in document.Metadata.EmbedComponents.Where(item => item.IsEmbedded == true))
            {
                if (!String.IsNullOrEmpty(embedItem.Description.Source.Path) && System.IO.File.Exists(embedItem.Description.Source.Path))
                {
                    EmbedComponentData embedData = new EmbedComponentData();
                    embedData.Stream        = System.IO.File.OpenRead(embedItem.Description.Source.Path);
                    embedData.FileExtension = System.IO.Path.GetExtension(embedItem.Description.Source.Path);

                    switch (embedData.FileExtension)
                    {
                    case ".xml":
                        embedData.ContentType = "application/xml";
                        break;

                    case ".cdcom":
                        embedData.ContentType = IO.CDDX.ContentTypeNames.BinaryComponent;
                        break;
                    }

                    List <IOComponentType> associatedTypes = new List <IOComponentType>();
                    foreach (var item in componentTypes)
                    {
                        if (item.Key.Description == embedItem.Description && !embedComponents.ContainsKey(item.Value))
                        {
                            embedComponents.Add(item.Value, embedData);
                        }
                    }
                }
            }

            return(ioDocument);
        }