Пример #1
0
        /// <summary>
        /// Attempts to read a CDDX document from the specified stream.
        /// </summary>
        /// <param name="stream">The stream containing a CDDX document to load.</param>
        /// <returns>True if the load was successful, false otherwise.</returns>
        public bool Load(Stream stream)
        {
            try
            {
                // Set up result
                LoadResult        = new DocumentLoadResult();
                LoadResult.Format = "Circuit Diagram Document";
                bool success = true;

                m_package = ZipPackage.Open(stream);

                // Load format properties, if available
                ReadFormatProperties();

                // Load main document
                success = LoadMainDocument(m_package);

                // Load metadata
                ReadMetadata();

                // Load thumbnail
                ReadThumbnail();

                return(success);
            }
            catch
            {
                m_package = null;
                return(false);
            }
        }
Пример #2
0
        public bool Load(System.IO.Stream stream)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(stream);

                double formatVersion = 1.0d;
                if ((doc.SelectSingleNode("/circuit") as XmlElement).HasAttribute("version"))
                {
                    string versionString = doc.SelectSingleNode("/circuit").Attributes["version"].InnerText;
                    double.TryParse(versionString, out formatVersion);
                }

                if (formatVersion == 1.1d)
                {
                    return(LoadVersion1_1(doc));
                }
                else if (formatVersion == 1.0d)
                {
                    return(LoadVersion1_0(doc));
                }

                DocumentLoadResult loadResult = new DocumentLoadResult();
                loadResult.Type = DocumentLoadResultType.FailNewerVersion;
                return(false);
            }
            catch (Exception)
            {
                LoadResult      = new DocumentLoadResult();
                LoadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return(false);
            }
        }
Пример #3
0
        public static bool Read(Stream stream, out IODocument circuitDocument, out DocumentLoadResult loadResult)
        {
            try
            {
                BinaryReader        reader        = new BinaryReader(stream);
                int                 magicNumber   = reader.ReadInt32();
                int                 formatVersion = reader.ReadInt32();
                string              appVersion    = reader.ReadString();
                CDDXContentEncoding contentFlags  = (CDDXContentEncoding)reader.ReadInt32();
                uint                contentOffset = 0;
                if (formatVersion >= 2)
                {
                    contentOffset = reader.ReadUInt32(); // offset to content
                }
                if (formatVersion >= 2)
                {
                    stream.Seek(contentOffset, SeekOrigin.Begin);
                }
                CircuitDocument newDocument = new CircuitDocument();
                if ((contentFlags & CDDXContentEncoding.Deflate) == CDDXContentEncoding.Deflate)
                {
                    DeflateStream deflateStream = new DeflateStream(stream, CompressionMode.Decompress);

                    Xml.XmlReader xmlReader = new Xml.XmlReader();
                    bool          result    = xmlReader.Load(deflateStream);
                    circuitDocument   = xmlReader.Document;
                    loadResult        = xmlReader.LoadResult;
                    loadResult.Format = "CDDX (Legacy 1.0)";
                    return(result);
                }
                else
                {
                    Xml.XmlReader xmlReader = new Xml.XmlReader();
                    bool          result    = xmlReader.Load(stream);
                    circuitDocument   = xmlReader.Document;
                    loadResult        = xmlReader.LoadResult;
                    loadResult.Format = "CDDX (Legacy 1.0)";
                    return(result);
                }
            }
            catch (Exception)
            {
                circuitDocument = null;
                loadResult      = new DocumentLoadResult();
                loadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return(false);
            }
        }
Пример #4
0
        public static bool Read(Stream stream, out IODocument circuitDocument, out DocumentLoadResult loadResult)
        {
            try
            {
                BinaryReader reader = new BinaryReader(stream);
                int magicNumber = reader.ReadInt32();
                int formatVersion = reader.ReadInt32();
                string appVersion = reader.ReadString();
                CDDXContentEncoding contentFlags = (CDDXContentEncoding)reader.ReadInt32();
                uint contentOffset = 0;
                if (formatVersion >= 2)
                    contentOffset = reader.ReadUInt32(); // offset to content

                if (formatVersion >= 2)
                    stream.Seek(contentOffset, SeekOrigin.Begin);
                CircuitDocument newDocument = new CircuitDocument();
                if ((contentFlags & CDDXContentEncoding.Deflate) == CDDXContentEncoding.Deflate)
                {
                    DeflateStream deflateStream = new DeflateStream(stream, CompressionMode.Decompress);

                    Xml.XmlReader xmlReader = new Xml.XmlReader();
                    bool result = xmlReader.Load(deflateStream);
                    circuitDocument = xmlReader.Document;
                    loadResult = xmlReader.LoadResult;
                    loadResult.Format = "CDDX (Legacy 1.0)";
                    return result;
                }
                else
                {
                    Xml.XmlReader xmlReader = new Xml.XmlReader();
                    bool result = xmlReader.Load(stream);
                    circuitDocument = xmlReader.Document;
                    loadResult = xmlReader.LoadResult;
                    loadResult.Format = "CDDX (Legacy 1.0)";
                    return result;
                }
            }
            catch (Exception)
            {
                circuitDocument = null;
                loadResult = new DocumentLoadResult();
                loadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return false;
            }
        }
Пример #5
0
        /// <summary>
        /// Attempts to read a CDDX document from the specified stream.
        /// </summary>
        /// <param name="stream">The stream containing a CDDX document to load.</param>
        /// <returns>True if the load was successful, false otherwise.</returns>
        public bool Load(Stream stream)
        {
            try
            {
                // Set up result
                LoadResult        = new DocumentLoadResult();
                LoadResult.Format = "Circuit Diagram Document";
                bool success = true;

                m_package = ZipPackage.Open(stream);

                // Load format properties, if available
                ReadFormatProperties();

                // Load main document
                success = LoadMainDocument(m_package);

                // Load metadata
                ReadMetadata();

                // Load thumbnail
                ReadThumbnail();

                return(success);
            }
            catch (FileFormatException)
            {
                // Try opening as legacy format
                IODocument         circuitDocument;
                DocumentLoadResult loadResult;
                stream.Seek(0, SeekOrigin.Begin);
                bool succeeded = LegacyCDDXReader.Read(stream, out circuitDocument, out loadResult);
                Document   = circuitDocument;
                LoadResult = loadResult;
                return(succeeded);
            }
            catch
            {
                m_package = null;
                return(false);
            }
        }
Пример #6
0
        public bool Load(System.IO.Stream stream)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(stream);

                double formatVersion = 1.0d;
                if ((doc.SelectSingleNode("/circuit") as XmlElement).HasAttribute("version"))
                {
                    string versionString = doc.SelectSingleNode("/circuit").Attributes["version"].InnerText;
                    double.TryParse(versionString, out formatVersion);
                }

                if (formatVersion == 1.1d)
                    return LoadVersion1_1(doc);
                else if (formatVersion == 1.0d)
                    return LoadVersion1_0(doc);

                DocumentLoadResult loadResult = new DocumentLoadResult();
                loadResult.Type = DocumentLoadResultType.FailNewerVersion;
                return false;
            }
            catch (Exception)
            {
                LoadResult = new DocumentLoadResult();
                LoadResult.Type = DocumentLoadResultType.FailIncorrectFormat;
                return false;
            }
        }
Пример #7
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;
            }
        }
Пример #8
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;
            }
        }
Пример #9
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);
            }
        }
Пример #10
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);
            }
        }