oi-metrology/Server/Services/SpreadingResistanceProfileService.cs
Mike Phares 127634f5ab Delete self contained Thunder Tests
Back to .net8.0
api/v4/InfinityQS
ApiExplorerSettings
Wafer Counter
Color Sorting
2024-03-13 13:15:56 -07:00

300 lines
19 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Adaptation.FileHandlers.json;
using OI.Metrology.Shared.Models.Stateless;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Text;
using System.Text.Json;
namespace OI.Metrology.Server.Services;
public class SpreadingResistanceProfileService : ISpreadingResistanceProfileService
{
#pragma warning disable CA1416
private static RectangleF GetRectangleF(double left, double top, double width, double height) =>
new((float)left, (float)top, (float)width, (float)height);
private static void DrawLine(Graphics graphics, Pen pen, double x1, double y1, double x2, double y2) =>
graphics.DrawLine(pen, (float)x1, (float)y1, (float)x2, (float)y2);
private static void DrawString(Graphics graphics, string s, Font font, Brush brush, double x, double y) =>
graphics.DrawString(s, font, brush, (float)x, (float)y);
private static void FillEllipse(Graphics graphics, Brush brush, double x, double y, double width, double height) =>
graphics.FillEllipse(brush, (float)x, (float)y, (float)width, (float)height);
private static void DrawString(Graphics graphics, string s, Font font, Brush brush, double x, double y, StringFormat stringFormat) =>
graphics.DrawString(s, font, brush, (float)x, (float)y, stringFormat);
private static void GetMinMax(List<ProfilePoint> profilePoints, out double decades, out double maxDepth, out double concentrationMin, out double resistanceEditedMin, out double resistivityMin)
{
double ceilingCD;
double ceilingEdited;
maxDepth = int.MinValue;
double ceilingResistivity;
resistivityMin = double.MaxValue;
concentrationMin = double.MaxValue;
resistanceEditedMin = double.MaxValue;
double resistivityMax = double.MinValue;
double concentrationMax = double.MinValue;
double resistanceEditedMax = double.MinValue;
foreach (ProfilePoint profilePoint in profilePoints)
{
if (profilePoint.Log10?.ResistanceEdited is null || profilePoint.Log10?.Resistivity is null || profilePoint.Log10?.Concentration is null)
continue;
if (profilePoint.Depth <= 0 || profilePoint.ResistanceRaw == 0)
continue;
maxDepth = profilePoint.Depth;
if (profilePoint.Log10.Resistivity.Value < resistivityMin)
resistivityMin = profilePoint.Log10.Resistivity.Value;
ceilingResistivity = Math.Ceiling(profilePoint.Log10.Resistivity.Value);
if (ceilingResistivity > resistivityMax)
resistivityMax = ceilingResistivity;
if (profilePoint.Log10.ResistanceEdited.Value < resistanceEditedMin)
resistanceEditedMin = profilePoint.Log10.ResistanceEdited.Value;
ceilingEdited = Math.Ceiling(profilePoint.Log10.ResistanceEdited.Value);
if (ceilingEdited > resistanceEditedMax)
resistanceEditedMax = ceilingEdited;
if (profilePoint.Log10.Concentration.Value < concentrationMin)
concentrationMin = profilePoint.Log10.Concentration.Value;
ceilingCD = Math.Ceiling(profilePoint.Log10.Concentration.Value);
if (ceilingCD > concentrationMax)
concentrationMax = ceilingCD;
}
decades = resistivityMax - resistivityMin;
if (resistanceEditedMax - resistanceEditedMin > decades)
decades = resistanceEditedMax - resistanceEditedMin;
if (concentrationMax - concentrationMin > decades)
decades = concentrationMax - concentrationMin;
}
private static RectangleF[] GetRectangles(double leftChartArea, double widthChartArea, double topChartArea, double sizeOfBlock, double widthOfBlacks)
{
List<RectangleF> rectangles = new()
{
GetRectangleF(leftChartArea, 10, widthChartArea, 65),
// GetRectangleF(leftChartArea + widthChartArea, topChartArea, widthOfBlacks, sizeOfBlock * 5);
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 0, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 1, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 2, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 3, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 4, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 5, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 6, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 7, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 8, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 9, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 10, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 11, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 12, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 13, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 14, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 15, widthOfBlacks, sizeOfBlock),
GetRectangleF(leftChartArea + widthChartArea, topChartArea + sizeOfBlock * 16, widthOfBlacks, sizeOfBlock * 2)
};
return rectangles.ToArray();
}
private static ReadOnlyCollection<string> GetRectanglesDescriptions(Info info, Setup setup, List<Layer> layers)
{
List<string> results = new() {
string.Empty, // 0
string.Empty, // 1
string.Empty, // 2
string.Empty, // 3
string.Empty, // 4
string.Empty, // 5
string.Empty, // 6
string.Empty, // 7
string.Empty, // 8
string.Concat(info.SystemId, Environment.NewLine, info.SoftwareVersion), // 9
string.Concat("SURFACE FINISH", Environment.NewLine, setup.Finish), // 10
string.Concat("ORIENTATION", Environment.NewLine, setup.Orientation), // 11
string.Concat("BEVEL ANGLE", Environment.NewLine, setup.SineBevelAngle), // 12
string.Concat("X-STEP (um)", Environment.NewLine, setup.Steps.First().X), // 13
string.Concat("PROBE LOAD (gm)", Environment.NewLine, setup.ProbeLoad), // 14
string.Concat("SPACING (um)", Environment.NewLine, setup.ProbeSpacing), // 15
string.Concat("OPERATOR", Environment.NewLine, info.Operator), // 16
string.Concat("DATE", Environment.NewLine, info.DateTime.ToString("dd MMM yy"), Environment.NewLine, "TIME", Environment.NewLine, info.DateTime.ToString("HH:mm:ss tt")), // 17
};
StringBuilder stringBuilder = new();
foreach (Layer layer in layers)
_ = stringBuilder.AppendLine(string.Concat("First Pt. ", layer.FirstPoint, " Last Pt. ", layer.LastPoint, " Type ", layer.Type, " Smoothing ", layer.Smoothing, " Correction ", layer.Correction));
_ = stringBuilder.AppendLine(string.Join(" ", info.Comments));
results[0] = stringBuilder.ToString();
return new(results);
}
internal static byte[] GetImageBytes(CSV csv)
{
if (csv.Info is null)
throw new NullReferenceException(nameof(csv.Info));
if (csv.Setup is null)
throw new NullReferenceException(nameof(csv.Setup));
if (csv.LayerHeader is null)
throw new NullReferenceException(nameof(csv.LayerHeader));
if (csv.ProfileHeader is null)
throw new NullReferenceException(nameof(csv.ProfileHeader));
double decades, maxDepth, concentrationMin, resistanceEditedMin, resistivityMin;
GetMinMax(csv.ProfileHeader.ProfilePoints, out decades, out maxDepth, out concentrationMin, out resistanceEditedMin, out resistivityMin);
byte[] bytes;
int penSize = 1;
int width = 694;
int height = 714;
int ellipseSize = 3;
Pen pen = Pens.Black;
int blocksOfDepth = 6;
RectangleF[] rectangles;
double topChartArea = 90;
double leftChartArea = 60;
double widthOfBlacks = 120;
Brush brush = Brushes.Black;
double widthChartArea = 500;
double heightChartArea = 600;
Font consolas = new("Consolas", 9);
Color backgroundColor = Color.White;
Brush resistivityBrush = Brushes.Green;
Font consolasBold = new("Consolas", 9);
Brush concentrationBrush = Brushes.Blue;
Brush resistanceRawBrush = Brushes.Black;
Brush resistanceEditedBrush = Brushes.Red;
double sizeOfBlock = heightChartArea / 18;
Brush backgroundBrush = Brushes.WhiteSmoke;
Pen resistivityPen = new(Color.Green, penSize);
Pen concentrationPen = new(Color.Blue, penSize);
Pen resistanceRawPen = new(Color.Black, penSize);
Pen resistanceEditedPen = new(Color.Red, penSize);
double widthOfDepthBlock = Math.Ceiling(maxDepth / 3) * 3 / 6;
StringFormat stringFormat = new() { Alignment = StringAlignment.Far };
Bitmap bitmap = new(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(backgroundColor);
rectangles = new RectangleF[1];
rectangles[0] = GetRectangleF(leftChartArea, topChartArea, widthChartArea, heightChartArea);
graphics.FillRectangles(backgroundBrush, rectangles);
rectangles = GetRectangles(leftChartArea, widthChartArea, topChartArea, sizeOfBlock, widthOfBlacks);
graphics.FillRectangles(Brushes.White, rectangles);
graphics.DrawRectangles(pen, rectangles);
ReadOnlyCollection<string> descriptions = GetRectanglesDescriptions(csv.Info, csv.Setup, csv.LayerHeader.Layers);
for (int i = 0; i < descriptions.Count; i++)
graphics.DrawString(descriptions[i], consolas, brush, rectangles[i]);
DrawLine(graphics, concentrationPen, 13, 6, 13, 40);
graphics.DrawString("C", consolasBold, concentrationBrush, 8, 41);
graphics.DrawString("D", consolasBold, concentrationBrush, 8, 53);
DrawLine(graphics, resistivityPen, 28, 6, 28, 40);
graphics.DrawString("ρ", consolasBold, resistivityBrush, 21, 41);
graphics.DrawString("c", consolasBold, resistivityBrush, 21, 53);
graphics.DrawString("m", consolasBold, resistivityBrush, 21, 62);
DrawLine(graphics, resistanceRawPen, 39, 7, 39, 41);
graphics.DrawString("Ω", consolasBold, resistanceRawBrush, 34, 41);
DrawLine(graphics, resistanceEditedPen, 51, 7, 51, 41);
graphics.DrawString("Ω", consolasBold, resistanceEditedBrush, 46, 41);
graphics.DrawString("E", consolasBold, resistanceEditedBrush, 46, 53);
for (double i = decades; i >= 0; i += -1)
{
for (int j = 1; j <= 10; j++)
DrawLine(graphics, Pens.LightGray, leftChartArea, topChartArea + heightChartArea - (i * heightChartArea / decades) + heightChartArea / decades * Math.Log10(j), leftChartArea + widthChartArea, topChartArea + heightChartArea - (i * heightChartArea / decades) + heightChartArea / decades * Math.Log10(j));
}
DrawString(graphics, "0", consolas, brush, leftChartArea - 6, topChartArea + heightChartArea + 5);
double x, x1, x2;
for (int i = 0; i <= blocksOfDepth - 1; i++)
{
for (int j = 1; j <= 10; j++)
{
x = leftChartArea + 13 + (i + 1) * widthChartArea / blocksOfDepth;
x1 = leftChartArea + (i * widthChartArea / blocksOfDepth) + j * widthChartArea / blocksOfDepth / 10;
x2 = leftChartArea + (i * widthChartArea / blocksOfDepth) + (j * widthChartArea / blocksOfDepth / 10);
DrawLine(graphics, Pens.LightGray, x1, topChartArea, x2, topChartArea + heightChartArea);
DrawString(graphics, ((i + 1) * widthOfDepthBlock).ToString("0.0"), consolas, brush, x, topChartArea + heightChartArea + 5, stringFormat);
}
}
DrawString(graphics, "(um)", consolas, brush, leftChartArea + widthChartArea + 12, topChartArea + heightChartArea + 5);
for (int i = 0; i <= decades; i++)
{
DrawLine(graphics, pen, leftChartArea, topChartArea + (i * heightChartArea / decades), leftChartArea + widthChartArea, topChartArea + (i * heightChartArea / decades));
DrawString(graphics, (decades - i + resistivityMin).ToString("0"), consolasBold, resistivityBrush, 33, topChartArea - 10 + (i * heightChartArea / decades), stringFormat);
DrawString(graphics, (decades - i + concentrationMin).ToString("0"), consolasBold, concentrationBrush, 20, topChartArea - 10 + (i * heightChartArea / decades), stringFormat);
DrawString(graphics, (decades - i + resistanceEditedMin).ToString("0"), consolasBold, resistanceRawBrush, 45, topChartArea - 10 + (i * heightChartArea / decades), stringFormat);
DrawString(graphics, (decades - i + resistanceEditedMin).ToString("0"), consolasBold, resistanceEditedBrush, 58, topChartArea - 10 + (i * heightChartArea / decades), stringFormat);
}
for (int i = 0; i <= blocksOfDepth; i++)
DrawLine(graphics, pen, leftChartArea + (i * widthChartArea / blocksOfDepth), topChartArea, leftChartArea + (i * widthChartArea / blocksOfDepth), topChartArea + heightChartArea);
double y, y2;
foreach (ProfilePoint profilePoint in csv.ProfileHeader.ProfilePoints)
{
if (profilePoint.Log10?.ResistanceEdited is null || profilePoint.Log10?.Resistivity is null || profilePoint.Log10?.Concentration is null)
continue;
if (profilePoint.Depth <= 0 || profilePoint.ResistanceRaw == 0)
continue;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
x = leftChartArea + profilePoint.Depth / maxDepth * widthChartArea - ellipseSize;
FillEllipse(graphics, concentrationBrush, x, topChartArea + heightChartArea - ((profilePoint.Log10.Concentration.Value - concentrationMin) / decades * heightChartArea) - ellipseSize - ellipseSize, ellipseSize, ellipseSize);
FillEllipse(graphics, resistanceRawBrush, x, topChartArea + heightChartArea - ((profilePoint.Log10.ResistanceRaw - resistanceEditedMin) / decades * heightChartArea) - ellipseSize - ellipseSize, ellipseSize, ellipseSize);
FillEllipse(graphics, resistivityBrush, x, topChartArea + heightChartArea - ((profilePoint.Log10.Resistivity.Value - resistivityMin) / decades * heightChartArea) - ellipseSize - ellipseSize, ellipseSize, ellipseSize);
FillEllipse(graphics, resistanceEditedBrush, x, topChartArea + heightChartArea - ((profilePoint.Log10.ResistanceEdited.Value - resistanceEditedMin) / decades * heightChartArea) - ellipseSize - ellipseSize, ellipseSize, ellipseSize);
if (profilePoint.LastProfilePoint?.ResistanceEdited is not null && profilePoint.LastProfilePoint?.Resistivity is not null && profilePoint.LastProfilePoint?.Concentration is not null && profilePoint.LastProfilePoint?.Log10?.ResistanceEdited is not null && profilePoint.LastProfilePoint?.Log10?.Resistivity is not null && profilePoint.LastProfilePoint?.Log10?.Concentration is not null && profilePoint.DeltaPercent is not null && profilePoint.DeltaPercent is >= .2 or <= -.2)
{
x = leftChartArea + profilePoint.Depth / maxDepth * widthChartArea - penSize;
x2 = leftChartArea + profilePoint.LastProfilePoint.Depth / maxDepth * widthChartArea - penSize;
y = topChartArea + heightChartArea - ((profilePoint.Log10.Concentration.Value - concentrationMin) / decades * heightChartArea) - penSize;
y2 = topChartArea + heightChartArea - ((profilePoint.LastProfilePoint.Log10.Concentration.Value - concentrationMin) / decades * heightChartArea) - penSize;
DrawLine(graphics, concentrationPen, x, y, x2, y2);
y = topChartArea + heightChartArea - ((profilePoint.Log10.ResistanceRaw - resistanceEditedMin) / decades * heightChartArea) - penSize;
y2 = topChartArea + heightChartArea - ((profilePoint.LastProfilePoint.Log10.ResistanceRaw - resistanceEditedMin) / decades * heightChartArea) - penSize;
DrawLine(graphics, resistanceRawPen, x, y, x2, y2);
y = topChartArea + heightChartArea - ((profilePoint.Log10.Resistivity.Value - resistivityMin) / decades * heightChartArea) - penSize;
y2 = topChartArea + heightChartArea - ((profilePoint.LastProfilePoint.Log10.Resistivity.Value - resistivityMin) / decades * heightChartArea) - penSize;
DrawLine(graphics, resistivityPen, x, y, x2, y2);
y = topChartArea + heightChartArea - ((profilePoint.Log10.ResistanceEdited.Value - resistanceEditedMin) / decades * heightChartArea) - penSize;
y2 = topChartArea + heightChartArea - ((profilePoint.LastProfilePoint.Log10.ResistanceEdited.Value - resistanceEditedMin) / decades * heightChartArea) - penSize;
DrawLine(graphics, resistanceEditedPen, x, y, x2, y2);
}
}
using MemoryStream msMemoryStream = new();
bitmap.Save(msMemoryStream, System.Drawing.Imaging.ImageFormat.Png);
bytes = new byte[Convert.ToInt32(msMemoryStream.Length) + 1];
_ = msMemoryStream.Read(bytes, 0, bytes.Length);
bytes = msMemoryStream.ToArray();
return bytes;
}
private static byte[] GetImageBytes(string json)
{
byte[] results;
Adaptation.FileHandlers.csv.CSV? csv = JsonSerializer.Deserialize<Adaptation.FileHandlers.csv.CSV>(json);
if (csv is null)
throw new NullReferenceException(nameof(csv));
CSV result = new(csv);
results = GetImageBytes(result);
return results;
}
byte[] ISpreadingResistanceProfileService.GetImageBytes(string json)
{
byte[] results = GetImageBytes(json);
return results;
}
byte[] ISpreadingResistanceProfileService.GetImageBytes(Stream stream)
{
byte[] results;
using StreamReader streamReader = new(stream, Encoding.UTF8);
string json = streamReader.ReadToEnd();
results = GetImageBytes(json);
return results;
}
}