diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5be8449..595d0e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,3 +31,14 @@ jobs: name: packages-${{ matrix.os }} path: artifacts/*.nupkg if-no-files-found: error + + native-core: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-dotnet@v6 + with: + dotnet-version: | + 8.0.x + 10.0.x + - run: dotnet build src/DwgIO.Scene/DwgIO.Scene.csproj -c Release -p:BuildNativeCoreOnly=true --artifacts-path native-core-check diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c34da7..c640f9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ API between minor releases. ## [Unreleased] ### Added +- `DwgIO.Scene.Adapter` provides scene conversion with only DwgIO and IOScene as dependencies. + The `DwgIO.Scene` package retains its export API and forwards the existing conversion types. +- JSON and SVG export options expose a `ReportDiagnostic` callback for export warnings. + Document diagnostics cover DWG reads. +- CI builds the native engine and scene adapter with `BuildNativeCoreOnly` enabled. - CI runs the tests on Linux and macOS as well as Windows (both target frameworks), replacing the Linux build-only job (#34). - `test-files/public`: the folder for committed redistributable test drawings, with the origin and licence diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1de5a63..e2bd1ea 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,6 +55,18 @@ dotnet test The library is in `src/DwgIO`, the CLI in `src/DwgIO.Cli`, and tests in `tests/DwgIO.Tests`. +The engine owns DWG reading, its model and the JSON representation of the decoded drawing. +`DwgIO.Scene.Adapter` uses the public API of `DwgIO` and the IOScene contract. OBJ, GLB, SVG and HTML output +live in the existing `DwgIO.Scene` package alongside forwards for the adapter types. +Output file replacement and export diagnostics belong to the writers. + +CI also builds the engine and adapter without the compatibility output code or its dependencies. +Run the compile check with a separate artifact directory. + +```sh +dotnet build src/DwgIO.Scene/DwgIO.Scene.csproj -c Release -p:BuildNativeCoreOnly=true --artifacts-path ./artifacts/native-core-check +``` + Tests that need real drawings are skipped with a message when no drawing is available. Set `DWGIO_TEST_FILES` to the folder containing your private drawings, or place them in a `test_files` folder, to run them. The repository diff --git a/DwgIO.slnx b/DwgIO.slnx index 5cd14c6..a4ceab2 100644 --- a/DwgIO.slnx +++ b/DwgIO.slnx @@ -1,5 +1,6 @@ + diff --git a/README.md b/README.md index 14f224d..a206203 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ DwgIO is a .NET library and command line tool that reads Autodesk AutoCAD drawin - Parses drawing metadata: summary information with custom properties and dates, application information, file dependencies and the embedded thumbnail preview. - Lists the objects of a drawing through its handle map and decodes the geometry of LINE, CIRCLE, ARC and POINT entities. - Writes a JSON export of the metadata, the section catalog and the decoded entities. -- Converts a drawing into a neutral [IOScene](https://www.nuget.org/packages/IOScene) scene document (`DwgIO.Scene` package): geometry in metres, ACI entity colours, one element per entity with its handle. +- Converts a drawing into a neutral [IOScene](https://www.nuget.org/packages/IOScene) scene document (`DwgIO.Scene.Adapter` package): geometry in metres, ACI entity colours, one element per entity with its handle. - Exports the scene to Wavefront OBJ (with an MTL colour library) and binary glTF (GLB). - Draws model space to SVG (`dwgio svg`): LINE, CIRCLE, ARC and POINT in the top view, in their ACI colours. - Writes a standalone interactive 3D HTML viewer (`.html`): the WebGL viewer of [IOViewer](https://github.com/IOExtend/IOViewer) embedded with the scene, no server or external scripts. @@ -29,9 +29,12 @@ and add the libraries to a .NET project: ```sh dotnet add package DwgIO -dotnet add package DwgIO.Scene +dotnet add package DwgIO.Scene.Adapter ``` +Install `DwgIO.Scene` for the existing scene API with OBJ, GLB, SVG and HTML exporters. +It includes the adapter. + The libraries target .NET 8 and .NET 10. They use managed code without native dependencies or Windows-only APIs and support Windows, Linux and macOS. Tests run on Windows, Linux and macOS. ## Command line @@ -143,7 +146,8 @@ foreach (var entity in doc.Entities) The JSON export is `DwgJsonExporter` in the `DwgIO.Json` namespace. See [Getting started](docs/getting-started.md) and [Public API](docs/public-api.md) for the supported types. -The scene export is `DwgSceneReader` in the `DwgIO.Scene` package. It converts a drawing into an `IOScene.SceneDocument` — the neutral scene model of the IOExtend tools, shared with RvtIO and NwcIO: +`DwgSceneReader` converts a drawing into an `IOScene.SceneDocument`, the neutral model shared with RvtIO and NwcIO. +The converter is available in `DwgIO.Scene.Adapter` and through the existing `DwgIO.Scene` API. ```csharp using DwgIO.Scene; @@ -171,7 +175,7 @@ The documentation site with the API reference is at + + + + <_Pinned Include="@(_ProjectReferencesWithVersions)">[%(_ProjectReferencesWithVersions.ProjectVersion)] + <_ProjectReferencesWithVersions Remove="@(_ProjectReferencesWithVersions)" /> + <_ProjectReferencesWithVersions Include="@(_Pinned)" /> + + + diff --git a/src/DwgIO.Cli/DwgIO.Cli.csproj b/src/DwgIO.Cli/DwgIO.Cli.csproj index 386ee4d..a608187 100644 --- a/src/DwgIO.Cli/DwgIO.Cli.csproj +++ b/src/DwgIO.Cli/DwgIO.Cli.csproj @@ -15,6 +15,7 @@ + diff --git a/src/DwgIO.Cli/Program.cs b/src/DwgIO.Cli/Program.cs index 6512a89..e4e1811 100644 --- a/src/DwgIO.Cli/Program.cs +++ b/src/DwgIO.Cli/Program.cs @@ -3,7 +3,7 @@ using System.Text; using System.Text.Json; using DwgIO; -using DwgIO.Internal; +using DwgIO.Output; using DwgIO.Json; using DwgIO.Model; using DwgIO.Scene; diff --git a/src/DwgIO.Scene.Adapter/DwgIO.Scene.Adapter.csproj b/src/DwgIO.Scene.Adapter/DwgIO.Scene.Adapter.csproj new file mode 100644 index 0000000..e1c56ab --- /dev/null +++ b/src/DwgIO.Scene.Adapter/DwgIO.Scene.Adapter.csproj @@ -0,0 +1,27 @@ + + + + Converts a DWG drawing read by DwgIO to an IOScene document. + dwg;autocad;cad;ioscene + + $(NoWarn);NU5104 + true + + + + false + + + + + + + + + + + + + + + diff --git a/src/DwgIO.Scene/DwgSceneOptions.cs b/src/DwgIO.Scene.Adapter/DwgSceneOptions.cs similarity index 100% rename from src/DwgIO.Scene/DwgSceneOptions.cs rename to src/DwgIO.Scene.Adapter/DwgSceneOptions.cs diff --git a/src/DwgIO.Scene/DwgSceneReader.cs b/src/DwgIO.Scene.Adapter/DwgSceneReader.cs similarity index 100% rename from src/DwgIO.Scene/DwgSceneReader.cs rename to src/DwgIO.Scene.Adapter/DwgSceneReader.cs diff --git a/src/DwgIO.Scene/DwgSceneResult.cs b/src/DwgIO.Scene.Adapter/DwgSceneResult.cs similarity index 100% rename from src/DwgIO.Scene/DwgSceneResult.cs rename to src/DwgIO.Scene.Adapter/DwgSceneResult.cs diff --git a/src/DwgIO.Scene/DwgSceneExport.cs b/src/DwgIO.Scene/Compatibility/DwgSceneExport.cs similarity index 99% rename from src/DwgIO.Scene/DwgSceneExport.cs rename to src/DwgIO.Scene/Compatibility/DwgSceneExport.cs index a2b3db4..bcbbba9 100644 --- a/src/DwgIO.Scene/DwgSceneExport.cs +++ b/src/DwgIO.Scene/Compatibility/DwgSceneExport.cs @@ -1,6 +1,6 @@ using System.Security.Cryptography; using System.Text; -using DwgIO.Internal; +using DwgIO.Output; using IOScene; using IOScene.Formats; diff --git a/src/DwgIO.Scene/DwgSvgExporter.cs b/src/DwgIO.Scene/Compatibility/DwgSvgExporter.cs similarity index 94% rename from src/DwgIO.Scene/DwgSvgExporter.cs rename to src/DwgIO.Scene/Compatibility/DwgSvgExporter.cs index a2807de..f968dcf 100644 --- a/src/DwgIO.Scene/DwgSvgExporter.cs +++ b/src/DwgIO.Scene/Compatibility/DwgSvgExporter.cs @@ -1,6 +1,6 @@ using System.Globalization; using System.Xml; -using DwgIO.Internal; +using DwgIO.Output; using DwgIO.Model; namespace DwgIO.Scene; @@ -50,7 +50,7 @@ public static void Write(DwgDocument document, XmlWriter writer, DwgSvgOptions? throw new ArgumentException("DwgSvgOptions.StrokeWidth must be finite and positive.", nameof(options)); } - var shapes = Collect(document, options.Scale); + var shapes = Collect(document, options); (double minX, double minY, double width, double height) = ViewBox(shapes); // Margins and markers are pixel values: they stay fixed while the scale zooms the drawing. double margin = Math.Max(0.02 * Math.Max(width, height), 8.0); @@ -144,8 +144,9 @@ private sealed record SvgArc(double Cx, double Cy, double Radius, double Start, private sealed record SvgPoint(double X, double Y, int ColorIndex) : SvgShape(ColorIndex); - private static List Collect(DwgDocument document, double scale) + private static List Collect(DwgDocument document, DwgSvgOptions options) { + double scale = options.Scale; var shapes = new List(); foreach (var entity in document.Entities) { @@ -163,7 +164,7 @@ private static List Collect(DwgDocument document, double scale) } else { - Skip(document, entity, "non-finite coordinates"); + Skip(options.ReportDiagnostic, entity, "non-finite coordinates"); } break; @@ -174,7 +175,7 @@ private static List Collect(DwgDocument document, double scale) } else { - Skip(document, entity, "non-finite or non-positive radius"); + Skip(options.ReportDiagnostic, entity, "non-finite or non-positive radius"); } break; @@ -188,7 +189,7 @@ private static List Collect(DwgDocument document, double scale) } else { - Skip(document, entity, "non-finite or non-positive radius"); + Skip(options.ReportDiagnostic, entity, "non-finite or non-positive radius"); } break; @@ -199,7 +200,7 @@ private static List Collect(DwgDocument document, double scale) } else { - Skip(document, entity, "non-finite coordinates"); + Skip(options.ReportDiagnostic, entity, "non-finite coordinates"); } break; @@ -209,8 +210,8 @@ private static List Collect(DwgDocument document, double scale) return shapes; } - private static void Skip(DwgDocument document, DwgEntity entity, string reason) => - document.ReportDiagnostic($"svg: entity {entity.Handle:X} ({entity.TypeName}) skipped: {reason}"); + private static void Skip(Action? reportDiagnostic, DwgEntity entity, string reason) => + reportDiagnostic?.Invoke($"svg: entity {entity.Handle:X} ({entity.TypeName}) skipped: {reason}"); private static bool IsUsable(params double[] values) => values.All(double.IsFinite); diff --git a/src/DwgIO.Scene/DwgSvgOptions.cs b/src/DwgIO.Scene/Compatibility/DwgSvgOptions.cs similarity index 86% rename from src/DwgIO.Scene/DwgSvgOptions.cs rename to src/DwgIO.Scene/Compatibility/DwgSvgOptions.cs index ce403ce..15ddff7 100644 --- a/src/DwgIO.Scene/DwgSvgOptions.cs +++ b/src/DwgIO.Scene/Compatibility/DwgSvgOptions.cs @@ -16,4 +16,7 @@ public sealed class DwgSvgOptions /// Background colour, as CSS. AutoCAD renders on a dark background, but SVG files are usually /// viewed on light ones, so the default is white and colour 7 (white/black) renders black. public string Background { get; set; } = "white"; + + /// Optional receiver for skipped-entity warnings. + public Action? ReportDiagnostic { get; set; } } diff --git a/src/DwgIO.Scene/DwgViewerExport.cs b/src/DwgIO.Scene/Compatibility/DwgViewerExport.cs similarity index 99% rename from src/DwgIO.Scene/DwgViewerExport.cs rename to src/DwgIO.Scene/Compatibility/DwgViewerExport.cs index 861a597..dc16fc1 100644 --- a/src/DwgIO.Scene/DwgViewerExport.cs +++ b/src/DwgIO.Scene/Compatibility/DwgViewerExport.cs @@ -1,5 +1,5 @@ using IOViewer.Exporters; -using DwgIO.Internal; +using DwgIO.Output; using IOScene; namespace DwgIO.Scene; diff --git a/src/DwgIO.Scene/ViewerMeshGeometry.cs b/src/DwgIO.Scene/Compatibility/ViewerMeshGeometry.cs similarity index 100% rename from src/DwgIO.Scene/ViewerMeshGeometry.cs rename to src/DwgIO.Scene/Compatibility/ViewerMeshGeometry.cs diff --git a/src/DwgIO.Scene/DwgIO.Scene.csproj b/src/DwgIO.Scene/DwgIO.Scene.csproj index c333a16..a5d8f87 100644 --- a/src/DwgIO.Scene/DwgIO.Scene.csproj +++ b/src/DwgIO.Scene/DwgIO.Scene.csproj @@ -1,22 +1,30 @@ - Turns an AutoCAD drawing read by DwgIO into an IOScene scene document: decoded model-space geometry as lines and points, drawing-unit scale, ACI entity colours and producer metadata. - dwg;autocad;autodesk;cad;ioscene;scene;bim - true - + Preserves the DwgIO scene API and writes DWG drawings as OBJ, GLB, SVG and standalone HTML. + dwg;autocad;cad;ioscene;export $(NoWarn);NU5104 + true + + + + false - - - + - - + + + + + + + + @@ -25,13 +33,4 @@ - - - - <_Pinned Include="@(_ProjectReferencesWithVersions)">[%(_ProjectReferencesWithVersions.ProjectVersion)] - <_ProjectReferencesWithVersions Remove="@(_ProjectReferencesWithVersions)" /> - <_ProjectReferencesWithVersions Include="@(_Pinned)" /> - - - diff --git a/src/DwgIO.Scene/TypeForwarders.cs b/src/DwgIO.Scene/TypeForwarders.cs new file mode 100644 index 0000000..ae3c119 --- /dev/null +++ b/src/DwgIO.Scene/TypeForwarders.cs @@ -0,0 +1,6 @@ +using System.Runtime.CompilerServices; +using DwgIO.Scene; + +[assembly: TypeForwardedTo(typeof(DwgSceneReader))] +[assembly: TypeForwardedTo(typeof(DwgSceneOptions))] +[assembly: TypeForwardedTo(typeof(DwgSceneResult))] diff --git a/src/DwgIO/Compatibility/DwgJsonExporter.File.cs b/src/DwgIO/Compatibility/DwgJsonExporter.File.cs new file mode 100644 index 0000000..ef3fc55 --- /dev/null +++ b/src/DwgIO/Compatibility/DwgJsonExporter.File.cs @@ -0,0 +1,12 @@ +using DwgIO.Output; + +namespace DwgIO.Json; + +public static partial class DwgJsonExporter +{ + /// Writes as JSON to a new file at . + public static void WriteFile(DwgDocument document, string path, DwgJsonExportOptions options) + { + AtomicOutput.Write(path, stream => Write(document, stream, options)); + } +} diff --git a/src/DwgIO/DwgDocument.cs b/src/DwgIO/DwgDocument.cs index 49e5612..3e0326c 100644 --- a/src/DwgIO/DwgDocument.cs +++ b/src/DwgIO/DwgDocument.cs @@ -96,7 +96,8 @@ private DwgDocument(string path, byte[] fileBytes, DwgFileHeader fileHeader, R20 /// Embedded thumbnail from AcDb:Preview, when present. public DwgPreview? Preview { get; } - internal string? PreviewReadError { get; private set; } + /// Reason the thumbnail section could not be read, when reading it failed. + public string? PreviewReadError { get; private set; } /// Handle→offset index from AcDb:Handles (R2004-family files, ReadObjects on). public DwgHandleMap? HandleMap { get; } @@ -107,12 +108,12 @@ private DwgDocument(string path, byte[] fileBytes, DwgFileHeader fileHeader, R20 /// Decoded entities with geometry (LINE/CIRCLE/ARC/POINT) — requires ReadObjects. public IReadOnlyList Entities { get; } = []; - /// Best-effort parse failures collected while opening (section name → message). + /// Read diagnostics from opening the document and from explicit calls to . public IReadOnlyList Diagnostics { get; } = []; /// Creates an in-memory document over a file header and a fixed entity list (tests). internal DwgDocument(string path, DwgFileHeader fileHeader, IReadOnlyList entities, - DwgPreview? preview = null, IReadOnlyList? objects = null) + DwgPreview? preview = null, IReadOnlyList? objects = null, IEnumerable? diagnostics = null) { Path = path; _fileBytes = []; @@ -123,6 +124,7 @@ internal DwgDocument(string path, DwgFileHeader fileHeader, IReadOnlyListCreates a document over a container built in the tests; reads follow . @@ -131,9 +133,6 @@ internal DwgDocument(string path, R2004Container container, DwgFileHeader fileHe { } - /// Records a non-fatal problem found after the document was opened (e.g. by an exporter). - internal void ReportDiagnostic(string message) => _diagnostics.Add(message); - /// Opens and parses a DWG file. /// The file does not exist. /// The file's container is not implemented yet @@ -222,7 +221,9 @@ private static DwgDocument Parse(string path, byte[] fileBytes, DwgReaderOptions /// /// Reads and decompresses a named section (e.g. "AcDb:Header", "AcDb:SummaryInfo"). /// - public byte[] ReadSection(string name) + public byte[] ReadSection(string name) => ReadSection(name, _diagnostics.Add); + + internal byte[] ReadSection(string name, Action? reportDiagnostic) { var container = _container ?? throw new DwgException("The document was opened without its section map (ReadSectionMap = false)."); @@ -230,7 +231,7 @@ public byte[] ReadSection(string name) ?? throw new DwgException($"Section '{name}' not found. Available: {string.Join(", ", container.SectionMap.Sections.Select(s => s.Name))}"); try { - return container.ReadSection(_fileBytes, entry, _verifyChecksums, _diagnostics.Add); + return container.ReadSection(_fileBytes, entry, _verifyChecksums, reportDiagnostic); } catch (Exception ex) when (ex is not DwgException && IsParseFailure(ex)) { diff --git a/src/DwgIO/DwgIO.csproj b/src/DwgIO/DwgIO.csproj index 7d6db93..81cde37 100644 --- a/src/DwgIO/DwgIO.csproj +++ b/src/DwgIO/DwgIO.csproj @@ -6,10 +6,20 @@ Reads Autodesk DWG drawings (.dwg) without AutoCAD and exposes sections, metadata and entities. + + false + + + + + + + + + + - - diff --git a/src/DwgIO/Json/DwgJsonExportOptions.cs b/src/DwgIO/Json/DwgJsonExportOptions.cs index f5a06e4..f70b74b 100644 --- a/src/DwgIO/Json/DwgJsonExportOptions.cs +++ b/src/DwgIO/Json/DwgJsonExportOptions.cs @@ -12,6 +12,9 @@ public sealed class DwgJsonExportOptions /// Embed decompressed section contents in the JSON (base64; large!). Default: false. public bool IncludeSectionContents { get; set; } + /// Optional receiver for warnings found during export. + public Action? ReportDiagnostic { get; set; } + /// Explicit section names to embed when is true; /// empty means every readable section. public HashSet SectionsToInclude { get; } = new(StringComparer.OrdinalIgnoreCase); diff --git a/src/DwgIO/Json/DwgJsonExporter.cs b/src/DwgIO/Json/DwgJsonExporter.cs index 48c7aab..926c129 100644 --- a/src/DwgIO/Json/DwgJsonExporter.cs +++ b/src/DwgIO/Json/DwgJsonExporter.cs @@ -1,10 +1,9 @@ -using DwgIO.Internal; using System.Text.Json; namespace DwgIO.Json; /// Writes a parsed to JSON. -public static class DwgJsonExporter +public static partial class DwgJsonExporter { /// Writes as JSON to . public static void Write(DwgDocument document, Stream output, DwgJsonExportOptions options) @@ -211,7 +210,7 @@ public static void Write(DwgDocument document, Stream output, DwgJsonExportOptio writer.WriteEndArray(); if (nonFiniteValues > 0) { - document.ReportDiagnostic( + options.ReportDiagnostic?.Invoke( $"entity {entity.Handle:X}: {nonFiniteValues} non-finite geometry value(s) written as null"); } } @@ -234,16 +233,17 @@ public static void Write(DwgDocument document, Stream output, DwgJsonExportOptio writer.WriteNumber("compression", section.Compression); writer.WriteNumber("encryption", section.Encryption); writer.WriteNumber("sectionId", section.SectionId); - if (options.IncludeSectionContents && IsIncluded(document, section.Name, options)) + if (options.IncludeSectionContents && IsIncluded(section.Name, options)) { try { - var bytes = document.ReadSection(section.Name); + var bytes = document.ReadSection(section.Name, options.ReportDiagnostic); writer.WriteString("contentBase64", Convert.ToBase64String(bytes)); } catch (DwgException ex) { writer.WriteString("readError", ex.Message); + options.ReportDiagnostic?.Invoke($"{section.Name}: {ex.Message}"); } } @@ -255,12 +255,6 @@ public static void Write(DwgDocument document, Stream output, DwgJsonExportOptio writer.Flush(); } - /// Writes as JSON to a new file at . - public static void WriteFile(DwgDocument document, string path, DwgJsonExportOptions options) - { - AtomicOutput.Write(path, stream => Write(document, stream, options)); - } - - private static bool IsIncluded(DwgDocument document, string name, DwgJsonExportOptions options) => + private static bool IsIncluded(string name, DwgJsonExportOptions options) => options.SectionsToInclude.Count == 0 || options.SectionsToInclude.Contains(name); } diff --git a/src/DwgIO/Model/DwgEntity.cs b/src/DwgIO/Model/DwgEntity.cs index b055ec8..4bc4fbd 100644 --- a/src/DwgIO/Model/DwgEntity.cs +++ b/src/DwgIO/Model/DwgEntity.cs @@ -42,7 +42,8 @@ public sealed class DwgEntity /// Point = [x,y,z]. Null for non-decoded kinds. public double[]? Geometry { get; init; } - internal bool IsModelSpace { get; init; } = true; + /// Whether the entity belongs directly to model space. + public bool IsModelSpace { get; init; } = true; /// DXF 62 colour index from the common preamble: the low 9 bits of the stored colour value, /// so 0 is BYBLOCK, 1 to 255 are indexed colours and 256 is BYLAYER. diff --git a/src/DwgIO.Scene/ArcAngles.cs b/src/Shared/ArcAngles.cs similarity index 100% rename from src/DwgIO.Scene/ArcAngles.cs rename to src/Shared/ArcAngles.cs diff --git a/src/DwgIO/Internal/AtomicOutput.cs b/src/Shared/AtomicOutput.cs similarity index 99% rename from src/DwgIO/Internal/AtomicOutput.cs rename to src/Shared/AtomicOutput.cs index 61c1b98..857280d 100644 --- a/src/DwgIO/Internal/AtomicOutput.cs +++ b/src/Shared/AtomicOutput.cs @@ -1,6 +1,6 @@ using System.Security.AccessControl; -namespace DwgIO.Internal; +namespace DwgIO.Output; internal static class AtomicOutput { diff --git a/src/DwgIO.Scene/DwgAciPalette.cs b/src/Shared/DwgAciPalette.cs similarity index 100% rename from src/DwgIO.Scene/DwgAciPalette.cs rename to src/Shared/DwgAciPalette.cs diff --git a/tests/DwgIO.Tests/CliOutputTests.cs b/tests/DwgIO.Tests/CliOutputTests.cs index 13b6fd4..e2d91fd 100644 --- a/tests/DwgIO.Tests/CliOutputTests.cs +++ b/tests/DwgIO.Tests/CliOutputTests.cs @@ -50,8 +50,8 @@ public void Inspect_PrintsDiagnosticsWithoutDecodedEntities() PreviewOffset = 0, Codepage = 30, }, - []); - document.ReportDiagnostic("AcDb:Handles: unexpected end of stream"); + [], + diagnostics: ["AcDb:Handles: unexpected end of stream"]); using var output = new StringWriter(); int exit = Program.InspectDocument(document, output); @@ -81,11 +81,8 @@ public void Inspect_PrintsTheDiagnosticTotal_Invariantly() PreviewOffset = 0, Codepage = 30, }, - []); - for (int i = 0; i < 1234; i++) - { - document.ReportDiagnostic($"problem {i}"); - } + [], + diagnostics: Enumerable.Range(0, 1234).Select(i => $"problem {i}")); using var output = new StringWriter(); Program.InspectDocument(document, output); diff --git a/tests/DwgIO.Tests/DwgSceneReaderTests.cs b/tests/DwgIO.Tests/DwgSceneReaderTests.cs index 3657f4c..758b464 100644 --- a/tests/DwgIO.Tests/DwgSceneReaderTests.cs +++ b/tests/DwgIO.Tests/DwgSceneReaderTests.cs @@ -8,7 +8,9 @@ namespace DwgIO.Tests; public class DwgSceneReaderTests { - private static DwgDocument CreateDocument(params DwgEntity[] entities) => + private static DwgDocument CreateDocument(params DwgEntity[] entities) => CreateDocument(entities, []); + + private static DwgDocument CreateDocument(DwgEntity[] entities, string[] diagnostics) => new( "synthetic.dwg", new DwgFileHeader @@ -24,7 +26,8 @@ private static DwgDocument CreateDocument(params DwgEntity[] entities) => PreviewOffset = 0, Codepage = 30, }, - entities); + entities, + diagnostics: diagnostics); private static DwgEntity Entity(ulong handle, DwgEntityKind kind, string typeName, double[] geometry, int colorIndex = 256) => new() @@ -37,6 +40,17 @@ private static DwgEntity Entity(ulong handle, DwgEntityKind kind, string typeNam ColorIndex = colorIndex, }; + [Fact] + public void LegacyAssemblyForwardsSceneApiToTheAdapter() + { + var adapter = typeof(DwgSceneReader).Assembly; + var legacy = System.Reflection.Assembly.Load("DwgIO.Scene"); + Assert.Contains(typeof(DwgSceneReader), legacy.GetForwardedTypes()); + Assert.Contains(typeof(DwgSceneOptions), legacy.GetForwardedTypes()); + Assert.Contains(typeof(DwgSceneResult), legacy.GetForwardedTypes()); + Assert.DoesNotContain(adapter.GetReferencedAssemblies(), name => name.Name is "IOScene.Formats" or "IOViewer"); + } + [Fact] public void Read_ConvertsLineAndPointToSceneGeometries() { @@ -147,8 +161,7 @@ public void Read_MemoryDocumentWithoutModelKey_Throws() [Fact] public void Read_ForwardsDocumentDiagnostics() { - var document = CreateDocument(); - document.ReportDiagnostic("AcDb:Handles: unexpected end"); + var document = CreateDocument([], ["AcDb:Handles: unexpected end"]); var result = DwgSceneReader.Read(document, new DwgSceneOptions { ModelKey = "synthetic" }); diff --git a/tests/DwgIO.Tests/DwgSvgExporterTests.cs b/tests/DwgIO.Tests/DwgSvgExporterTests.cs index 1867bb2..a603bc4 100644 --- a/tests/DwgIO.Tests/DwgSvgExporterTests.cs +++ b/tests/DwgIO.Tests/DwgSvgExporterTests.cs @@ -151,10 +151,12 @@ public void Write_NonFiniteGeometry_SkipsAndReports() Entity(0x2A, DwgEntityKind.Line, "LINE", [0.0, 0.0, 0.0, double.NaN, 0.0, 0.0]), Entity(0x2B, DwgEntityKind.Line, "LINE", [0.0, 0.0, 0.0, 5.0, 0.0, 0.0])); - string svg = WriteToString(document); + var diagnostics = new List(); + string svg = WriteToString(document, new DwgSvgOptions { ReportDiagnostic = diagnostics.Add }); Assert.Single(System.Xml.Linq.XDocument.Parse(svg).Descendants(XName("line"))); - var diagnostic = Assert.Single(document.Diagnostics); + Assert.Empty(document.Diagnostics); + var diagnostic = Assert.Single(diagnostics); Assert.Contains("entity 2A", diagnostic, StringComparison.Ordinal); } diff --git a/tests/DwgIO.Tests/EncryptedSectionTests.cs b/tests/DwgIO.Tests/EncryptedSectionTests.cs index 8dba824..fe6d920 100644 --- a/tests/DwgIO.Tests/EncryptedSectionTests.cs +++ b/tests/DwgIO.Tests/EncryptedSectionTests.cs @@ -85,6 +85,23 @@ public void PreviewCommand_WhenSectionCannotBeRead_ReturnsParseError(uint encryp Assert.Empty(output.ToString()); } + [Fact] + public void JsonExport_ReportsUnreadableSectionWithoutChangingReadDiagnostics() + { + var document = CreateDocument(Section("AcDb:SummaryInfo", encryption: 1)); + var diagnostics = new List(); + using var output = new MemoryStream(); + var options = new Json.DwgJsonExportOptions { IncludeSectionContents = true, ReportDiagnostic = diagnostics.Add }; + + Json.DwgJsonExporter.Write(document, output, options); + + Assert.Empty(document.Diagnostics); + Assert.Contains("encrypted", Assert.Single(diagnostics), StringComparison.Ordinal); + using var json = System.Text.Json.JsonDocument.Parse(output.ToArray()); + Assert.Contains("encrypted", json.RootElement.GetProperty("sections")[0].GetProperty("readError").GetString(), + StringComparison.Ordinal); + } + [Fact] public void TryGetReadableSection_EncryptedEntry_IsSkippedWithADiagnostic() { diff --git a/tests/DwgIO.Tests/JsonExporterTests.cs b/tests/DwgIO.Tests/JsonExporterTests.cs index 11afcaa..8e16729 100644 --- a/tests/DwgIO.Tests/JsonExporterTests.cs +++ b/tests/DwgIO.Tests/JsonExporterTests.cs @@ -64,8 +64,9 @@ public void Write_NonFiniteGeometry_WritesNullAndRecordsDiagnostic() var document = CreateDocument(CreateEntity( [double.NaN, double.PositiveInfinity, 1.5, double.NegativeInfinity, -2.25, 0.0])); + var diagnostics = new List(); using var stream = new MemoryStream(); - DwgJsonExporter.Write(document, stream, DwgJsonExportOptions.Default); + DwgJsonExporter.Write(document, stream, new DwgJsonExportOptions { ReportDiagnostic = diagnostics.Add }); using var json = JsonDocument.Parse(stream.ToArray()); var geometry = json.RootElement.GetProperty("entities")[0].GetProperty("geometry"); @@ -75,7 +76,9 @@ public void Write_NonFiniteGeometry_WritesNullAndRecordsDiagnostic() Assert.Equal(JsonValueKind.Null, geometry[3].ValueKind); Assert.Equal(-2.25, geometry[4].GetDouble()); Assert.Equal(0.0, geometry[5].GetDouble()); - Assert.Contains(document.Diagnostics, d => d.StartsWith("entity 2A:", StringComparison.Ordinal)); + var diagnostic = Assert.Single(diagnostics); + Assert.StartsWith("entity 2A:", diagnostic, StringComparison.Ordinal); + Assert.Empty(document.Diagnostics); } [Fact]