public void ReadElements(CircuitDiagramDocument document, XElement elements, ReaderContext context) { var components = from el in elements.Elements() where el.Name == Ns.Document + "c" select el; foreach (var componentElement in components) { var typeAttr = componentElement.Attribute("tp"); if (typeAttr == null) { context.Log(ReaderErrorCodes.MissingRequiredAttribute, componentElement, "tp"); continue; } var componentType = context.GetComponentType(ParseType(typeAttr.Value)); if (componentType == null) { context.Log(ReaderErrorCodes.UnknownComponentType, typeAttr, typeAttr.Value); continue; } Component component; if (componentElement.Attribute("x") != null) { component = new PositionalComponent(componentType, new LayoutInformation()); // Layout ReadLayout((PositionalComponent)component, componentElement, context); } else { component = new Component(componentType); } // Properties var propertiesElement = componentElement.Elements(Ns.Document + "prs").SingleOrDefault(); var properties = propertiesElement != null ? from el in propertiesElement.Elements() where el.Name == Ns.Document + "p" select el : new XElement[0]; foreach (var propertyElement in properties) { var keyAttr = propertyElement.Attribute("k"); if (keyAttr == null) { context.Log(ReaderErrorCodes.MissingRequiredAttribute, propertyElement, "k"); continue; } var valueAttr = propertyElement.Attribute("v"); if (valueAttr == null) { context.Log(ReaderErrorCodes.MissingRequiredAttribute, propertyElement, "v"); continue; } component.Properties[keyAttr.Value] = PropertyValue.Dynamic(valueAttr.Value); } // Connections var connectionsElement = componentElement.Elements(Ns.Document + "cns").SingleOrDefault(); var connections = connectionsElement != null ? from el in connectionsElement.Elements() where el.Name == Ns.Document + "cn" select el : new XElement[0]; foreach (var connectionElement in connections) { var idAttr = connectionElement.Attribute("id"); if (idAttr == null) { context.Log(ReaderErrorCodes.MissingRequiredAttribute, connectionElement, "id"); continue; } var pointAttr = connectionElement.Attribute("pt"); if (pointAttr == null) { context.Log(ReaderErrorCodes.MissingRequiredAttribute, connectionElement, "pt"); continue; } var namedConnection = component.Connections.FirstOrDefault(x => x.Value.Name.Value == pointAttr.Value).Value ?? new NamedConnection(pointAttr.Value, component); context.ApplyConnection(idAttr.Value, namedConnection); } document.Elements.Add(component); } var wires = from el in elements.Elements() where el.Name == Ns.Document + "w" select el; foreach (var wireElement in wires) { var wire = new Wire(new LayoutInformation()); ReadLayout(wire, wireElement, context); document.Elements.Add(wire); } }
public static T RenderPreview <T>(Func <Size, T> drawingContext, ComponentDescription desc, PreviewGenerationOptions options) where T : IDrawingContext { var componentType = new TypeDescriptionComponentType(desc.Metadata.GUID, ComponentType.UnknownCollection, desc.ComponentName); var component = new PositionalComponent(componentType); component.Layout.Location = new Point(options.Width / 2 - (options.Horizontal ? options.Size : 0), options.Height / 2 - (!options.Horizontal ? options.Size : 0)); component.Layout.Orientation = options.Horizontal ? Orientation.Horizontal : Orientation.Vertical; // Minimum size component.Layout.Size = Math.Max(desc.MinSize, options.Size); // Configuration var configurationDesc = desc.Metadata.Configurations.FirstOrDefault(x => x.Name == options.Configuration); if (configurationDesc != null) { foreach (var setter in configurationDesc.Setters) { component.Properties[setter.Key] = setter.Value; } } // Orientation FlagOptions flagOptions = desc.DetermineFlags(component); if ((flagOptions & FlagOptions.HorizontalOnly) == FlagOptions.HorizontalOnly && component.Layout.Orientation == Orientation.Vertical) { component.Layout.Orientation = Orientation.Horizontal; } else if ((flagOptions & FlagOptions.VerticalOnly) == FlagOptions.VerticalOnly && component.Layout.Orientation == Orientation.Horizontal) { component.Layout.Orientation = Orientation.Vertical; } // Flip if ((flagOptions & FlagOptions.FlipPrimary) == FlagOptions.FlipPrimary && (options.Flip & FlipState.Primary) == FlipState.Primary) { component.Layout.Flip |= FlipState.Primary; } if ((flagOptions & FlagOptions.FlipSecondary) == FlagOptions.FlipSecondary && (options.Flip & FlipState.Secondary) == FlipState.Secondary) { component.Layout.Flip |= FlipState.Secondary; } // Properties foreach (var property in options.Properties) { // Look up serialized name var propertyInfo = desc.Properties.FirstOrDefault(x => x.Name == property.Key); if (propertyInfo != null) { component.Properties[propertyInfo.SerializedName] = PropertyValue.Dynamic(property.Value); } } foreach (var property in options.RawProperties) { component.Properties[property.Key] = property.Value; } CircuitDocument document = new CircuitDocument(); document.Elements.Add(component); var lookup = new DictionaryComponentDescriptionLookup(); lookup.AddDescription(componentType, desc); var docRenderer = new CircuitRenderer(lookup); var buffer = new SkiaBufferedDrawingContext(); docRenderer.RenderCircuit(document, buffer); var bb = buffer.BoundingBox ?? new Rect(); T resultContext; IDrawingContext dc; Vector translationOffset = new Vector(0, 0); if (options.Crop) { resultContext = drawingContext(options.Crop ? bb.Size : new Size(options.Width * options.Scale, options.Height * options.Scale)); dc = new TranslationDrawingContext(new Vector(Math.Round(-bb.X), Math.Round(-bb.Y)), resultContext); } else if (options.Center) { resultContext = drawingContext(new Size(options.Width, options.Height)); var x = bb.X - options.Width / 2 + bb.Width / 2; var y = bb.Y - options.Height / 2 + bb.Height / 2; translationOffset = new Vector(Math.Round(-x), Math.Round(-y)); dc = new TranslationDrawingContext(translationOffset, resultContext); } else { resultContext = drawingContext(new Size(options.Width, options.Height)); dc = resultContext; } if (options.Grid && resultContext is SkiaDrawingContext gridSkiaContext) { RenderGrid(gridSkiaContext, options.Width, options.Height, translationOffset); } if (options.DebugLayout && resultContext is SkiaDrawingContext debugSkiaContext) { RenderDebugLayout(debugSkiaContext, component, desc, translationOffset); } docRenderer.RenderCircuit(document, dc); return(resultContext); }