refactor(PepLib.Core): reorganize files into logical folder structure
Move 38 files from root directory into organized subfolders: - Enums/ (7 files): StatusType, ApplicationType, DrawingType, etc. - Geometry/ (5 files): Vector, Box, Size, Spacing, Node - Models/ (15 files): Nest, Plate, Part, Program, Report, etc. - Utilities/ (7 files): MathHelper, Tolerance, ZipHelper, etc. - Extensions/ (2 files): PartListExtensions, PlateListExtensions - Interfaces/ (1 file): IMovable Update namespaces to follow folder hierarchy (e.g., PepLib.Models). Add GlobalUsings.cs for internal backward compatibility. Update Codes/ and IO/ files with new using statements. Update PepApi.Core consumers to reference new namespaces. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
15
PepLib.Core/Utilities/AngleConverter.cs
Normal file
15
PepLib.Core/Utilities/AngleConverter.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public static class AngleConverter
|
||||
{
|
||||
public static double ToDegrees(double radians)
|
||||
{
|
||||
return 180.0 / Math.PI * radians;
|
||||
}
|
||||
|
||||
public static double ToRadians(double degrees)
|
||||
{
|
||||
return Math.PI / 180.0 * degrees;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
PepLib.Core/Utilities/Generic.cs
Normal file
12
PepLib.Core/Utilities/Generic.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public static class Generic
|
||||
{
|
||||
public static void Swap<T>(ref T a, ref T b)
|
||||
{
|
||||
T c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
100
PepLib.Core/Utilities/IniConfig.cs
Normal file
100
PepLib.Core/Utilities/IniConfig.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using PepLib.Geometry;
|
||||
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public class IniConfig
|
||||
{
|
||||
public List<Node> Nodes;
|
||||
|
||||
public IniConfig()
|
||||
{
|
||||
Nodes = new List<Node>();
|
||||
}
|
||||
|
||||
private static int LeadingWhitespaceCount(string s)
|
||||
{
|
||||
for (int i = 0; i < s.Length; ++i)
|
||||
if (s[i] != ' ') return i;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Node FindNode(string path)
|
||||
{
|
||||
return FindNode(path, Nodes);
|
||||
}
|
||||
|
||||
private Node FindNode(string path, List<Node> nodes)
|
||||
{
|
||||
var a = path.Split('/');
|
||||
|
||||
var b = nodes.FirstOrDefault(node =>
|
||||
{
|
||||
if (node is KeyNode)
|
||||
{
|
||||
var c = node as KeyNode;
|
||||
return c.Name.ToUpper() == a[0].ToUpper();
|
||||
}
|
||||
else
|
||||
{
|
||||
return node.Value == a[0].Trim();
|
||||
}
|
||||
});
|
||||
|
||||
string path2 = string.Empty;
|
||||
|
||||
for (int i = 1; i < a.Length; ++i)
|
||||
path2 += a[i] + '/';
|
||||
|
||||
if (b == null || a.Length == 1)
|
||||
return b;
|
||||
else
|
||||
return FindNode(path2.TrimEnd('/'), b.Children);
|
||||
}
|
||||
|
||||
public static IniConfig Load(string file)
|
||||
{
|
||||
var doc = new IniConfig();
|
||||
var reader = new StreamReader(file);
|
||||
|
||||
Node currentNode = null;
|
||||
string line;
|
||||
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
int spaces = LeadingWhitespaceCount(line) / 2;
|
||||
var node = new Node();
|
||||
node.Value = line.Trim();
|
||||
|
||||
var keyNode = KeyNode.Parse(node);
|
||||
|
||||
if (keyNode != null)
|
||||
node = keyNode;
|
||||
|
||||
int currentdepth = currentNode != null ? currentNode.Level : 0;
|
||||
|
||||
if (spaces == 0)
|
||||
doc.Nodes.Add(node);
|
||||
else if (spaces == currentdepth)
|
||||
currentNode.Parent.AddChild(node);
|
||||
else if (spaces > currentdepth)
|
||||
currentNode.AddChild(node);
|
||||
else if (spaces < currentdepth)
|
||||
{
|
||||
var n = currentNode.Parent;
|
||||
|
||||
while (spaces < n.Level)
|
||||
n = n.Parent;
|
||||
|
||||
n.Parent.AddChild(node);
|
||||
}
|
||||
|
||||
currentNode = node;
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
PepLib.Core/Utilities/MathHelper.cs
Normal file
53
PepLib.Core/Utilities/MathHelper.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public static class MathHelper
|
||||
{
|
||||
public const double HalfPI = Math.PI * 0.5;
|
||||
public const double TwoPI = Math.PI * 2.0;
|
||||
|
||||
public static double NormalizeAngleRad(double angle)
|
||||
{
|
||||
double r = angle % TwoPI;
|
||||
return r < 0 ? TwoPI + r : r;
|
||||
}
|
||||
|
||||
public static double NormalizeAngleDeg(double angle)
|
||||
{
|
||||
double r = angle % 360.0;
|
||||
return r < 0 ? 360.0 + r : r;
|
||||
}
|
||||
|
||||
public static bool IsAngleBetween(double angle, double a1, double a2, bool reversed = false)
|
||||
{
|
||||
if (reversed)
|
||||
Generic.Swap(ref a1, ref a2);
|
||||
|
||||
var diff = NormalizeAngleRad(a2 - a1);
|
||||
|
||||
// full circle
|
||||
if (a2.IsEqualTo(a1))
|
||||
return true;
|
||||
|
||||
a1 = NormalizeAngleRad(angle - a1);
|
||||
a2 = NormalizeAngleRad(a2 - angle);
|
||||
|
||||
return diff >= a1 - Tolerance.Epsilon ||
|
||||
diff >= a2 - Tolerance.Epsilon;
|
||||
}
|
||||
|
||||
public static double RoundDownToNearest(double num, double factor)
|
||||
{
|
||||
return factor == 0 ? num : Math.Floor(num / factor) * factor;
|
||||
}
|
||||
|
||||
public static double RoundUpToNearest(double num, double factor)
|
||||
{
|
||||
return factor == 0 ? num : Math.Ceiling(num / factor) * factor;
|
||||
}
|
||||
|
||||
public static double RoundToNearest(double num, double factor)
|
||||
{
|
||||
return factor == 0 ? num : Math.Round(num / factor) * factor;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
PepLib.Core/Utilities/Tolerance.cs
Normal file
12
PepLib.Core/Utilities/Tolerance.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public static class Tolerance
|
||||
{
|
||||
public const double Epsilon = 0.0001;
|
||||
|
||||
public static bool IsEqualTo(this double a, double b, double tolerance = Epsilon)
|
||||
{
|
||||
return Math.Abs(b - a) <= tolerance;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
PepLib.Core/Utilities/Util.cs
Normal file
30
PepLib.Core/Utilities/Util.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public static class Util
|
||||
{
|
||||
public static string GetNestFileFormat(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
var name = Path.GetFileName(filename);
|
||||
var ext = Path.GetExtension(name);
|
||||
|
||||
if (name.LastIndexOf(ext) > 5 && !name.Contains("-"))
|
||||
name = name.Insert(5, "-");
|
||||
|
||||
if (name.LastIndexOf(ext) > 8 && char.IsLetter(name[8]))
|
||||
name = name.Remove(8, 1);
|
||||
|
||||
return Path.Combine(Path.GetDirectoryName(filename), name);
|
||||
}
|
||||
catch (SystemException ex)
|
||||
{
|
||||
Debug.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
81
PepLib.Core/Utilities/ZipHelper.cs
Normal file
81
PepLib.Core/Utilities/ZipHelper.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System.IO.Compression;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PepLib.Utilities
|
||||
{
|
||||
public static class ZipHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the files that match the specified pattern.
|
||||
/// </summary>
|
||||
/// <param name="file">Input zip file.</param>
|
||||
/// <param name="pattern">Pattern to match.</param>
|
||||
/// <param name="names">Names of the files that match the pattern.</param>
|
||||
/// <param name="streams">Data of the files that match the pattern.</param>
|
||||
/// <returns></returns>
|
||||
public static int ExtractByPattern(string file, string pattern, out string[] names, out Stream[] streams)
|
||||
{
|
||||
var nameList = new List<string>();
|
||||
var streamList = new List<Stream>();
|
||||
|
||||
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
|
||||
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Read))
|
||||
{
|
||||
foreach (var entry in zip.Entries)
|
||||
{
|
||||
if (!Regex.IsMatch(entry.FullName, pattern))
|
||||
continue;
|
||||
|
||||
nameList.Add(entry.FullName);
|
||||
|
||||
var memstream = new MemoryStream();
|
||||
using var entryStream = entry.Open();
|
||||
entryStream.CopyTo(memstream);
|
||||
memstream.Seek(0, SeekOrigin.Begin);
|
||||
streamList.Add(memstream);
|
||||
}
|
||||
}
|
||||
|
||||
names = nameList.ToArray();
|
||||
streams = streamList.ToArray();
|
||||
|
||||
return streams.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the first file found that matches the specified file extension.
|
||||
/// </summary>
|
||||
/// <param name="file">Input zip file.</param>
|
||||
/// <param name="extension">Extension to match.</param>
|
||||
/// <param name="name">The name of the file that matches the file extension.</param>
|
||||
/// <param name="stream">The data of the file that matches the file extension.</param>
|
||||
/// <returns></returns>
|
||||
public static bool ExtractByExtension(string file, string extension, out string name, out Stream stream)
|
||||
{
|
||||
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
|
||||
using (var zip = new ZipArchive(fileStream, ZipArchiveMode.Read))
|
||||
{
|
||||
foreach (var entry in zip.Entries)
|
||||
{
|
||||
if (Path.GetExtension(entry.FullName) != extension)
|
||||
continue;
|
||||
|
||||
var memstream = new MemoryStream();
|
||||
using var entryStream = entry.Open();
|
||||
entryStream.CopyTo(memstream);
|
||||
memstream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
stream = memstream;
|
||||
name = entry.FullName;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
stream = null;
|
||||
name = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user