コード例 #1
0
        /// <summary>
        /// Shows the save file dialog and then saves the document.
        /// </summary>
        /// <param name="saved">Whether the document was saved.</param>
        private void SaveDocumentAs(out bool saved)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Save As";
            sfd.Filter = "Circuit Diagram Document (*.cddx)|*.cddx|XML Files (*.xml)|*.xml";
            if (sfd.ShowDialog() == true)
            {
                string extension = Path.GetExtension(sfd.FileName);

                IDictionary<IOComponentType, EmbedComponentData> embedComponents;
                IODocument ioDocument = circuitDisplay.Document.ToIODocument(out embedComponents);

                if (extension == ".cddx")
                {
                    bool doSave = false;

                    // Load default save options
                    SettingsManager.SettingsSerializer serializer = new SettingsManager.SettingsSerializer();
                    serializer.Category = "CDDX";
                    CircuitDiagram.IO.CDDX.CDDXSaveOptions saveOptions = new CircuitDiagram.IO.CDDX.CDDXSaveOptions();
                    saveOptions.Deserialize(serializer);

                    if (!SettingsManager.Settings.ReadBool("CDDX.AlwaysUseSettings"))
                    {
                        winSaveOptions saveOptionsWindow = new winSaveOptions(new CDDXSaveOptionsEditor(), saveOptions);
                        saveOptionsWindow.Owner = this;
                        if (saveOptionsWindow.ShowDialog() == true)
                        {
                            saveOptions = saveOptionsWindow.Options as CircuitDiagram.IO.CDDX.CDDXSaveOptions;

                            if (saveOptionsWindow.AlwaysUseSettings)
                            {
                                saveOptions.Serialize(serializer);
                                SettingsManager.Settings.Write("CDDX.AlwaysUseSettings", true);
                            }

                            doSave = true;
                        }
                    }
                    else
                        doSave = true;

                    if (doSave)
                    {
                        IO.CDDX.CDDXWriter writer = new IO.CDDX.CDDXWriter();
                        writer.Document = ioDocument;
                        writer.Options = saveOptions;
                        writer.EmbedComponents = embedComponents;

                        writer.Begin();
                        if (writer.RenderContext != null)
                        {
                            writer.RenderContext.Begin();
                            circuitDisplay.Document.Render(writer.RenderContext);
                            writer.RenderContext.End();
                        }

                        using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write, FileShare.Read))
                        {
                            writer.Write(fs);
                        }

                        writer.End();

                        m_docPath = sfd.FileName;
                        m_documentTitle = System.IO.Path.GetFileNameWithoutExtension(sfd.FileName);
                        this.Title = m_documentTitle + " - Circuit Diagram";
                        m_undoManager.SetSaveIndex();
                        AddRecentFile(m_docPath);
                        m_lastSaveOptions = saveOptions;

                        saved = true;
                    }
                    else
                        saved = false;
                }
                else
                {
                    IO.Xml.XmlWriter writer = new IO.Xml.XmlWriter();
                    writer.Document = ioDocument;

                    writer.Begin();
                    using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        writer.Write(fs);
                    }
                    writer.End();

                    m_docPath = sfd.FileName;
                    m_documentTitle = System.IO.Path.GetFileNameWithoutExtension(sfd.FileName);
                    this.Title = m_documentTitle + " - Circuit Diagram";
                    m_undoManager.SetSaveIndex();
                    AddRecentFile(m_docPath);

                    saved = true;
                }
            }
            else
                saved = false;
        }
コード例 #2
0
        /// <summary>
        /// Attempts to save the document, prompting for a file name if it has not previously been saved.
        /// </summary>
        /// <param name="saved">Whether the document was saved.</param>
        private void SaveDocument(out bool saved)
        {
            if (!String.IsNullOrEmpty(m_docPath))
            {
                IDictionary<IOComponentType, EmbedComponentData> embedComponents;
                IODocument ioDocument = circuitDisplay.Document.ToIODocument(out embedComponents);

                string extension = Path.GetExtension(m_docPath);
                if (extension == ".cddx")
                {
                    // Load default save options if no previous options (e.g. if file opened)
                    SettingsManager.SettingsSerializer serializer = new SettingsManager.SettingsSerializer();
                    serializer.Category = "CDDX";
                    m_lastSaveOptions = new IO.CDDX.CDDXSaveOptions();
                    m_lastSaveOptions.Deserialize(serializer);

                    IO.CDDX.CDDXWriter writer = new IO.CDDX.CDDXWriter();
                    writer.Document = ioDocument;
                    writer.Options = m_lastSaveOptions;
                    writer.EmbedComponents = embedComponents;

                    writer.Begin();
                    if (writer.RenderContext != null)
                    {
                        writer.RenderContext.Begin();
                        circuitDisplay.Document.Render(writer.RenderContext);
                        writer.RenderContext.End();
                    }

                    using (FileStream fs = new FileStream(m_docPath, FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        writer.Write(fs);
                    }

                    writer.End();
                }
                else
                {
                    IO.Xml.XmlWriter writer = new IO.Xml.XmlWriter();
                    writer.Document = ioDocument;

                    writer.Begin();
                    using (FileStream fs = new FileStream(m_docPath, FileMode.Create, FileAccess.Write, FileShare.Read))
                    {
                        writer.Write(fs);
                    }
                    writer.End();
                }
                this.Title = System.IO.Path.GetFileNameWithoutExtension(m_docPath) + " - Circuit Diagram";
                UndoManager.SetSaveIndex();

                saved = true;
            }
            else
                SaveDocumentAs(out saved);
        }