mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-17 14:27:39 -05:00
fix(import): read source drawings that are open in another program
AutoCAD keeps an open DWG/DXF locked for writing, and ACadSharp's path constructors (and File.OpenRead / XmlReader.Create) deny write sharing, so the CAD Converter could not import a part while it was open in AutoCAD. Open source files read-only with read/write sharing instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e58e8b85c4
commit
5668952e6b
5 files changed
+126
-5
No files matched your search
@@ -0,0 +1,91 @@
|
||||
using ACadSharp;
|
||||
using ACadSharp.IO;
|
||||
using CSMath;
|
||||
using CadLine = ACadSharp.Entities.Line;
|
||||
|
||||
namespace OpenNest.IO.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// AutoCAD keeps an open drawing locked for writing; importing it must still work.
|
||||
/// </summary>
|
||||
public class LockedSourceImportTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(".dxf")]
|
||||
[InlineData(".dwg")]
|
||||
public void ImportsCadFileHeldOpenForWritingByAnotherProgram(string extension)
|
||||
{
|
||||
WithFile(
|
||||
extension,
|
||||
path =>
|
||||
{
|
||||
var doc = Square();
|
||||
if (extension == ".dwg")
|
||||
DwgWriter.Write(path, doc);
|
||||
else
|
||||
DxfWriter.Write(path, doc, false);
|
||||
},
|
||||
path =>
|
||||
{
|
||||
var drawing = CadImporter.ImportDrawing(
|
||||
path,
|
||||
new CadImportOptions { DetectBends = false }
|
||||
);
|
||||
Assert.Equal(100, drawing.Area, 6);
|
||||
// The layer-filtered overload reads DXF only.
|
||||
if (extension == ".dxf")
|
||||
Assert.Equal(4, Dxf.GetGeometry(path, _ => false).Count);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ImportsSvgFileHeldOpenForWritingByAnotherProgram()
|
||||
{
|
||||
WithFile(
|
||||
".svg",
|
||||
path =>
|
||||
File.WriteAllText(
|
||||
path,
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"10in\" height=\"10in\" viewBox=\"0 0 10 10\">"
|
||||
+ "<rect width=\"10\" height=\"10\"/></svg>"
|
||||
),
|
||||
path =>
|
||||
{
|
||||
var result = CadImporter.Import(path);
|
||||
Assert.NotEmpty(result.Entities);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static CadDocument Square()
|
||||
{
|
||||
var doc = new CadDocument();
|
||||
var points = new[]
|
||||
{
|
||||
new XYZ(0, 0, 0),
|
||||
new XYZ(10, 0, 0),
|
||||
new XYZ(10, 10, 0),
|
||||
new XYZ(0, 10, 0),
|
||||
};
|
||||
for (var i = 0; i < points.Length; i++)
|
||||
doc.Entities.Add(new CadLine(points[i], points[(i + 1) % points.Length]));
|
||||
return doc;
|
||||
}
|
||||
|
||||
private static void WithFile(string extension, Action<string> write, Action<string> import)
|
||||
{
|
||||
var path = Path.Combine(Path.GetTempPath(), $"opennest-locked-{Guid.NewGuid()}{extension}");
|
||||
try
|
||||
{
|
||||
write(path);
|
||||
// The lock AutoCAD holds on an open drawing: read/write access, readers allowed.
|
||||
using (new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
|
||||
import(path);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ namespace OpenNest.IO
|
||||
{
|
||||
Span<byte> input = stackalloc byte[32 + 4];
|
||||
Span<byte> digest = stackalloc byte[32];
|
||||
using (var stream = File.OpenRead(path))
|
||||
using (var stream = SharedFile.OpenRead(path))
|
||||
SHA256.HashData(stream, input[..32]);
|
||||
|
||||
for (var i = 0; i < entities.Count; i++)
|
||||
|
||||
+12
-3
@@ -54,7 +54,14 @@ namespace OpenNest.IO
|
||||
GetGeometry(stream, null);
|
||||
|
||||
public static List<Entity> GetGeometry(string path, Func<string, bool> layerFilter) =>
|
||||
ReadGeometry(() => ReadDxf(new DxfReader(path)), layerFilter);
|
||||
ReadGeometry(
|
||||
() =>
|
||||
{
|
||||
using var stream = SharedFile.OpenRead(path);
|
||||
return ReadDxf(new DxfReader(stream));
|
||||
},
|
||||
layerFilter
|
||||
);
|
||||
|
||||
public static List<Entity> GetGeometry(Stream stream, Func<string, bool> layerFilter) =>
|
||||
ReadGeometry(() => ReadDxf(new DxfReader(stream)), layerFilter);
|
||||
@@ -128,16 +135,18 @@ namespace OpenNest.IO
|
||||
return reader.Read();
|
||||
}
|
||||
|
||||
// Read through a shared stream so a drawing still open in AutoCAD can be imported.
|
||||
private static CadDocument ReadDocument(string path)
|
||||
{
|
||||
using var stream = SharedFile.OpenRead(path);
|
||||
if (IsDwg(path))
|
||||
{
|
||||
using var reader = new DwgReader(path);
|
||||
using var reader = new DwgReader(stream);
|
||||
return reader.Read();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var reader = new DxfReader(path);
|
||||
using var reader = new DxfReader(stream);
|
||||
return reader.Read();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.IO;
|
||||
|
||||
namespace OpenNest.IO
|
||||
{
|
||||
/// <summary>
|
||||
/// Opens source drawings read-only without blocking, or being blocked by, the program
|
||||
/// that has them open: AutoCAD keeps an open DWG/DXF locked for writing, and a reader
|
||||
/// that denies write sharing (the <see cref="File.OpenRead"/> default) fails on it.
|
||||
/// </summary>
|
||||
internal static class SharedFile
|
||||
{
|
||||
public static FileStream OpenRead(string path) =>
|
||||
new FileStream(
|
||||
path,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.ReadWrite | FileShare.Delete
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -116,7 +116,8 @@ namespace OpenNest.IO
|
||||
|
||||
try
|
||||
{
|
||||
using var reader = XmlReader.Create(path, settings);
|
||||
using var stream = SharedFile.OpenRead(path);
|
||||
using var reader = XmlReader.Create(stream, settings);
|
||||
var root = XDocument.Load(reader).Root;
|
||||
if (root?.Name.LocalName != "svg")
|
||||
throw new InvalidDataException($"\"{Path.GetFileName(path)}\" is not an SVG file.");
|
||||
|
||||
Reference in new issue
Block a user