public static int Run(Options options) { Console.WriteLine($"{Path.GetFileName(options.Input)} -> {Path.GetFileName(options.Output)}"); var reader = new CircuitDiagramDocumentReader(); CircuitDiagramDocument circuit; using (var fs = File.Open(options.Input, FileMode.Open, FileAccess.Read)) { circuit = reader.ReadCircuit(fs); } var loggerFactory = new LoggerFactory(); if (options.Verbose) { loggerFactory.AddConsole(LogLevel.Information); } var descriptionLookup = new DirectoryComponentDescriptionLookup(loggerFactory, options.ComponentsDirectory ?? Path.GetDirectoryName(options.Input), true); var renderer = new CircuitRenderer(descriptionLookup); var drawingContext = new SkiaDrawingContext((int)circuit.Size.Width, (int)circuit.Size.Height, SKColors.White); renderer.RenderCircuit(circuit, drawingContext); using (var outputFs = File.OpenWrite(options.Output)) { drawingContext.WriteAsPng(outputFs); } return(0); }
public static int Run(Options options) { Console.WriteLine($"{Path.GetFileName(options.Input)} -> {Path.GetFileName(options.Output)}"); var reader = new CircuitDiagramDocumentReader(); CircuitDiagramDocument circuit; using (var fs = File.Open(options.Input, FileMode.Open, FileAccess.Read)) { circuit = reader.ReadCircuit(fs); } var loggerFactory = new LoggerFactory(); if (options.Verbose) { loggerFactory.AddConsole(LogLevel.Information); } var descriptionLookup = new DirectoryComponentDescriptionLookup(loggerFactory, options.ComponentsDirectory ?? Path.GetDirectoryName(options.Input), true); var renderer = new CircuitRenderer(descriptionLookup); var drawingContext = new SkiaDrawingContext((int)circuit.Size.Width, (int)circuit.Size.Height, SKColors.White); try { renderer.RenderCircuit(circuit, drawingContext); } catch (MissingComponentDescriptionException ex) { Console.Error.WriteLine($"Unable to find component {ex.MissingType}."); var allDescriptions = descriptionLookup.GetAllDescriptions().OrderBy(x => x.ComponentName).ToList(); if (!allDescriptions.Any()) { Console.Error.Write("No components were loaded. Is the --components option set correctly?"); } else { Console.Error.WriteLine("Ensure this component is available in the --components directory. The following components were loaded:"); foreach (var description in allDescriptions) { Console.Error.WriteLine($" - {description.ComponentName} ({description.Metadata.GUID})"); } } return(1); } using (var outputFs = File.OpenWrite(options.Output)) { drawingContext.WriteAsPng(outputFs); } return(0); }
public static void Run(string[] args) { string input = null; IReadOnlyList <string> componentDirectories = Array.Empty <string>(); string output = null; var cliOptions = ArgumentSyntax.Parse(args, options => { options.ApplicationName = "cdcli circuit"; options.DefineOptionList("components", ref componentDirectories, "Path to components directory."); options.DefineOption("o|output", ref output, "Path to output file."); options.DefineParameter("input", ref input, "Path to input circuit."); }); if (input == null) { cliOptions.ReportError("Input file must be specified."); } if (componentDirectories == null) { cliOptions.ReportError("Components directory must be specified."); } if (output == null) { cliOptions.ReportError("Output path must be specified."); } Console.WriteLine($"{Path.GetFileName(input)} -> {Path.GetFileName(output)}"); var reader = new CircuitDiagramDocumentReader(); CircuitDiagramDocument circuit; using (var fs = File.Open(input, FileMode.Open, FileAccess.Read)) circuit = reader.ReadCircuit(fs); var descriptionLookup = new DirectoryComponentDescriptionLookup(componentDirectories.ToArray()); var renderer = new CircuitRenderer(descriptionLookup); var drawingContext = new SkiaDrawingContext((int)circuit.Size.Width, (int)circuit.Size.Height, SKColors.White); renderer.RenderCircuit(circuit, drawingContext); using (var outputFs = File.OpenWrite(output)) drawingContext.WriteAsPng(outputFs); }
private static void RenderGrid(SkiaDrawingContext skiaContext, double width, double height, Vector translationOffset) { skiaContext.Mutate(canvas => { int offsetX = (int)translationOffset.X % 10; int offsetY = (int)translationOffset.Y % 10; canvas.Save(); canvas.Translate(offsetX, offsetY); var gridPaint = new SKPaint { Color = SKColors.LightGray.WithAlpha(75), StrokeWidth = 1.0f, }; for (int x = -10; x < width + 10; x += 10) { canvas.DrawLine( x, -10, x, (int)height + 10, gridPaint); } for (int y = -10; y < height + 10; y += 10) { canvas.DrawLine( -10, y, (int)width + 10, y, gridPaint); } canvas.Restore(); }); }
public int Run(Options options) { _logger.LogInformation($"{Path.GetFileName(options.Input)} -> {Path.GetFileName(options.Output)}"); CircuitDocument circuit; var inputFileExtension = Path.GetExtension(options.Input); switch (inputFileExtension) { case ".cddx": { var reader = new CircuitDiagramDocumentReader(); using (var fs = File.Open(options.Input, FileMode.Open, FileAccess.Read)) { circuit = reader.ReadCircuit(fs); } break; } default: { _logger.LogError($"Unknown file type: '{inputFileExtension}'."); return(1); } } var renderer = new CircuitRenderer(_componentDescriptionLookup); var bufferRenderer = new SkiaBufferedDrawingContext(); renderer.RenderCircuit(circuit, bufferRenderer); IPngDrawingContext context; switch (options.Renderer) { case PngRenderer.Skia: context = new SkiaDrawingContext((int)circuit.Size.Width, (int)circuit.Size.Height, SKColors.White); break; case PngRenderer.ImageSharp: context = new ImageSharpDrawingContext((int)circuit.Size.Width, (int)circuit.Size.Height, SixLabors.ImageSharp.Color.White); break; default: _logger.LogError("Unsupported renderer."); return(1); } try { renderer.RenderCircuit(circuit, context); } catch (MissingComponentDescriptionException ex) { _logger.LogError($"Unable to find component {ex.MissingType}."); var allDescriptions = _componentDescriptionLookup.GetAllDescriptions().OrderBy(x => x.ComponentName).ToList(); if (!allDescriptions.Any()) { _logger.LogInformation("No components were loaded. Is the --components option set correctly?"); } else { _logger.LogInformation("Ensure this component is available in the --components directory. The following components were loaded:"); foreach (var description in allDescriptions) { _logger.LogInformation($" - {description.ComponentName} ({description.Metadata.GUID})"); } } return(1); } using (var outputFs = File.OpenWrite(options.Output)) { context.WriteAsPng(outputFs); } return(0); }
private static void RenderDebugLayout( SkiaDrawingContext skiaContext, PositionalComponent component, ComponentDescription desc, Vector translationOffset) { var layoutOptions = new LayoutOptions { AlignMiddle = (desc.DetermineFlags(component) & FlagOptions.MiddleMustAlign) == FlagOptions.MiddleMustAlign, GridSize = 10.0 }; skiaContext.Mutate(canvas => { canvas.Save(); canvas.Translate((float)translationOffset.X, (float)translationOffset.Y); var connectionDebugPaint = new SKPaint { Color = SKColors.CornflowerBlue.WithAlpha(150), StrokeWidth = 1.0f, FilterQuality = SKFilterQuality.High, }; var connectionPositioner = new ConnectionPositioner(); var connectionPoints = connectionPositioner.PositionConnections(component, desc, layoutOptions); foreach (var connection in connectionPoints) { canvas.DrawLine( (float)connection.Location.X - 4, (float)connection.Location.Y - 4, (float)connection.Location.X + 4, (float)connection.Location.Y + 4, connectionDebugPaint); canvas.DrawLine( (float)connection.Location.X + 4, (float)connection.Location.Y - 4, (float)connection.Location.X - 4, (float)connection.Location.Y + 4, connectionDebugPaint); if (connection.Orientation == Orientation.Horizontal) { if (connection.IsEdge) { canvas.DrawLine( (float)connection.Location.X, (float)connection.Location.Y - 20, (float)connection.Location.X, (float)connection.Location.Y + 20, connectionDebugPaint); } else if (connection.Location.X % 20.0 == 0.0) { canvas.DrawLine( (float)connection.Location.X, (float)connection.Location.Y - 10, (float)connection.Location.X, (float)connection.Location.Y, connectionDebugPaint); } else { canvas.DrawLine( (float)connection.Location.X, (float)connection.Location.Y, (float)connection.Location.X, (float)connection.Location.Y + 10, connectionDebugPaint); } } else { if (connection.IsEdge) { canvas.DrawLine( (float)connection.Location.X - 20, (float)connection.Location.Y, (float)connection.Location.X + 20, (float)connection.Location.Y, connectionDebugPaint); } else if (connection.Location.Y % 20.0 == 0.0) { canvas.DrawLine( (float)connection.Location.X - 10, (float)connection.Location.Y, (float)connection.Location.X, (float)connection.Location.Y, connectionDebugPaint); } else { canvas.DrawLine( (float)connection.Location.X, (float)connection.Location.Y, (float)connection.Location.X + 10, (float)connection.Location.Y, connectionDebugPaint); } } } // Start and end points var paint = new SKPaint { Color = SKColors.CornflowerBlue, IsAntialias = true, Style = SKPaintStyle.StrokeAndFill, StrokeWidth = 2f, StrokeCap = SKStrokeCap.Square, }; canvas.DrawOval(component.Layout.Location.ToSkPoint(), new SKSize(2f, 2f), paint); var endOffset = new Vector( component.Layout.Orientation == Orientation.Horizontal ? component.Layout.Size : 0.0, component.Layout.Orientation == Orientation.Vertical ? component.Layout.Size : 0.0); canvas.DrawOval(component.Layout.Location.Add(endOffset).ToSkPoint(), new SKSize(2f, 2f), paint); canvas.Restore(); }); }