/// <summary> /// Loads a component description from the stream and adds it to the component descriptions store. /// </summary> /// <param name="stream">The stream to load from.</param> /// <param name="type">The type to find within the description.</param> /// <returns>A configuration with the loaded component description if it was available, null if it could not be loaded.</returns> private static ComponentIdentifier LoadDescription(EmbedComponentData data, IOComponentType type) { if (data.ContentType == IO.CDDX.ContentTypeNames.BinaryComponent) { // Binary component var reader = new CircuitDiagram.IO.Descriptions.BinaryDescriptionReader(); if (reader.Read(data.Stream)) { var descriptions = reader.ComponentDescriptions; if (descriptions.Count > 0) { return(FindIdentifier(type, descriptions[0])); } else { return(null); } } else { // Load failed return(null); } } else if (data.ContentType == "application/xml") { // XML component XmlLoader loader = new XmlLoader(); loader.Load(data.Stream); if (loader.LoadErrors.Count() == 0) { var descriptions = loader.GetDescriptions(); if (descriptions.Length > 0) { return(FindIdentifier(type, descriptions[0])); } else { return(null); } } else { // Load failed return(null); } } else { // Unknown type return(null); } }
/// <summary> /// Retrieves the specified component type from the document. /// </summary> /// <param name="type">The type to check.</param> /// <returns>The data stream if the type is available, null otherwise.</returns> public EmbedComponentData GetEmbeddedDescription(IOComponentType type) { PackagePart typePart; if (m_typeParts.TryGetValue(type, out typePart)) { EmbedComponentData data = new EmbedComponentData(); data.ContentType = typePart.ContentType; data.FileExtension = Path.GetExtension(typePart.Uri.ToString()); data.Stream = typePart.GetStream(FileMode.Open); data.IsEmbedded = true; return(data); } else { return(null); } }
/// <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); }