diff --git a/OpenNest.IO.Tests/LockedSourceImportTests.cs b/OpenNest.IO.Tests/LockedSourceImportTests.cs
new file mode 100644
index 00000000..d43a3ca9
--- /dev/null
+++ b/OpenNest.IO.Tests/LockedSourceImportTests.cs
@@ -0,0 +1,91 @@
+using ACadSharp;
+using ACadSharp.IO;
+using CSMath;
+using CadLine = ACadSharp.Entities.Line;
+
+namespace OpenNest.IO.Tests;
+
+///
+/// AutoCAD keeps an open drawing locked for writing; importing it must still work.
+///
+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,
+ ""
+ ),
+ 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 write, Action 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);
+ }
+ }
+}
diff --git a/OpenNest.IO/CadImporter.cs b/OpenNest.IO/CadImporter.cs
index 74f1733d..57c5fcc0 100644
--- a/OpenNest.IO/CadImporter.cs
+++ b/OpenNest.IO/CadImporter.cs
@@ -114,7 +114,7 @@ namespace OpenNest.IO
{
Span input = stackalloc byte[32 + 4];
Span 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++)
diff --git a/OpenNest.IO/Dxf.cs b/OpenNest.IO/Dxf.cs
index 5658d1d7..844c8dc5 100644
--- a/OpenNest.IO/Dxf.cs
+++ b/OpenNest.IO/Dxf.cs
@@ -54,7 +54,14 @@ namespace OpenNest.IO
GetGeometry(stream, null);
public static List GetGeometry(string path, Func layerFilter) =>
- ReadGeometry(() => ReadDxf(new DxfReader(path)), layerFilter);
+ ReadGeometry(
+ () =>
+ {
+ using var stream = SharedFile.OpenRead(path);
+ return ReadDxf(new DxfReader(stream));
+ },
+ layerFilter
+ );
public static List GetGeometry(Stream stream, Func 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();
}
}
diff --git a/OpenNest.IO/SharedFile.cs b/OpenNest.IO/SharedFile.cs
new file mode 100644
index 00000000..39d94e1e
--- /dev/null
+++ b/OpenNest.IO/SharedFile.cs
@@ -0,0 +1,20 @@
+using System.IO;
+
+namespace OpenNest.IO
+{
+ ///
+ /// 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 default) fails on it.
+ ///
+ internal static class SharedFile
+ {
+ public static FileStream OpenRead(string path) =>
+ new FileStream(
+ path,
+ FileMode.Open,
+ FileAccess.Read,
+ FileShare.ReadWrite | FileShare.Delete
+ );
+ }
+}
diff --git a/OpenNest.IO/SvgReader.cs b/OpenNest.IO/SvgReader.cs
index a4b5ea77..2f2c6512 100644
--- a/OpenNest.IO/SvgReader.cs
+++ b/OpenNest.IO/SvgReader.cs
@@ -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.");