/// <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; }
private void CommandNew_Executed(object sender, ExecutedRoutedEventArgs e) { winNewDocument newDocumentWindow = new winNewDocument(); newDocumentWindow.Owner = this; if (newDocumentWindow.ShowDialog() == true) { if (!UndoManager.IsSavedState()) { TaskDialogOptions tdOptions = new TaskDialogOptions(); tdOptions.Title = "Circuit Diagram"; tdOptions.MainInstruction = "Do you want to save changes to " + m_documentTitle + "?"; tdOptions.CustomButtons = new string[] { "&Save", "Do&n't Save", "Cancel" }; tdOptions.Owner = this; TaskDialogResult result = TaskDialog.Show(tdOptions); bool saved = false; if (result.CustomButtonResult == 0) SaveDocument(out saved); if (result.CustomButtonResult == 2 || result.Result == TaskDialogSimpleResult.Cancel || (result.CustomButtonResult == 0 && !saved)) return; // Don't create new document } CircuitDocument newDocument = new CircuitDocument(); newDocument.Size = new Size(newDocumentWindow.DocumentWidth, newDocumentWindow.DocumentHeight); InitializeMetadata(newDocument); circuitDisplay.Document = newDocument; circuitDisplay.DrawConnections(); this.Title = "Untitled - Circuit Diagram"; m_documentTitle = "Untitled"; m_docPath = ""; m_undoManager = new UndoManager(); circuitDisplay.UndoManager = m_undoManager; m_undoManager.ActionDelegate = new CircuitDiagram.UndoManager.UndoActionDelegate(UndoActionProcessor); m_undoManager.ActionOccurred += new EventHandler(m_undoManager_ActionOccurred); } }
private static void InitializeMetadata(CircuitDocument newDocument) { newDocument.Metadata.Creator = SettingsManager.Settings.Read("ComputerUserName") as string; if (!SettingsManager.Settings.ReadBool("CreatorUseComputerUserName") && SettingsManager.Settings.HasSetting("CreatorName")) newDocument.Metadata.Creator = SettingsManager.Settings.Read("CreatorName") as string; newDocument.Metadata.Created = DateTime.Now; newDocument.Metadata.Application = ApplicationInfo.Name; newDocument.Metadata.AppVersion = ApplicationInfo.Version; }
/// <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); }
/// <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); }
public void SetDocument(CircuitDocument document) { m_document = document; this.DataContext = document; tbxDimensions.Text = String.Format("{0}x{1}", document.Size.Width, document.Size.Height); // Embedded components document.UpdateEmbedComponents(); lbxEmbedComponents.ItemsSource = document.Metadata.EmbedComponents; // Components used in document - convert to tree structure Dictionary <string, List <ComponentUsageItem> > processed = new Dictionary <string, List <ComponentUsageItem> >(); // Sort components into collections foreach (IComponentElement element in document.Elements.Where(item => item is IComponentElement)) { if (element is Component && (element as Component).Description == ComponentHelper.WireDescription) { continue; // Ignore wires } string key = element.ImplementationCollection; if (element.ImplementationCollection == CircuitDiagram.IO.ComponentCollections.Common) { key = "Common"; } else if (element.ImplementationCollection == CircuitDiagram.IO.ComponentCollections.Misc) { key = "Misc"; } else if (String.IsNullOrEmpty(element.ImplementationCollection)) { key = "(unknown)"; } string name = element.ImplementationItem; bool nonstandard = false; if (String.IsNullOrEmpty(element.ImplementationItem)) { if (element is Component) { name = (element as Component).Description.ComponentName; } else { name = "Unnamed component"; } nonstandard = true; } if (processed.ContainsKey(key) && processed[key].Find(item => item.Name == name) != null) { continue; // Avoid duplicates } if (!processed.ContainsKey(key)) { processed.Add(key, new List <ComponentUsageItem>()); } processed[key].Add(new ComponentUsageItem(name, true, nonstandard)); } // Add disabled components foreach (DisabledComponent component in document.DisabledComponents) { string key = component.ImplementationCollection; if (component.ImplementationCollection == CircuitDiagram.IO.ComponentCollections.Common) { key = "Common"; } else if (component.ImplementationCollection == CircuitDiagram.IO.ComponentCollections.Misc) { key = "Misc"; } else if (String.IsNullOrEmpty(component.ImplementationCollection)) { key = "(unknown)"; } string name = component.ImplementationItem; bool nonstandard = false; if (String.IsNullOrEmpty(component.ImplementationItem)) { if (!String.IsNullOrEmpty(component.Name)) { name = component.Name; } else { name = String.Format("Unnamed component"); } nonstandard = true; } if (processed.ContainsKey(key) && processed[key].Find(item => item.Name == name) != null) { continue; // Avoid duplicates } if (!processed.ContainsKey(key)) { processed.Add(key, new List <ComponentUsageItem>()); } processed[key].Add(new ComponentUsageItem(name, false, nonstandard)); } // Add to treeview foreach (KeyValuePair <string, List <ComponentUsageItem> > item in processed) { TreeViewItem collectionItem = new TreeViewItem(); TextBlock header = new TextBlock(); header.Text = item.Key; if (item.Key == "Common") { header.ToolTip = CircuitDiagram.IO.ComponentCollections.Common; } else if (item.Key == "Misc") { header.ToolTip = CircuitDiagram.IO.ComponentCollections.Misc; } collectionItem.Header = header; item.Value.Sort(new Comparison <ComponentUsageItem>((element1, element2) => element1.Name.CompareTo(element2.Name))); item.Value.ForEach(leaf => { TreeViewItem trvItem = new TreeViewItem(); trvItem.Header = leaf.Name; if (!leaf.IsAvailable) { trvItem.Foreground = Brushes.Gray; trvItem.ToolTip = "not available"; } collectionItem.Items.Add(trvItem); }); collectionItem.IsExpanded = true; trvComponents.Items.Add(collectionItem); } }