diff --git a/README.md b/README.md index ac9c3ad..52a9e92 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,8 @@ The default and the fallback for missing or unsupported drawing units are both m dwgio scene drawing.dwg drawing.scene.json --unit mm ``` -The plain export command writes OBJ, GLB and HTML by output extension and accepts the same `--unit` values: +The plain export command writes OBJ, GLB and HTML by output extension and accepts the same `--unit` values. +OBJ and GLB contain model space only; use HTML to view paper-space sheets and their viewports: ```sh dwgio drawing.dwg drawing.obj --unit mm # Wavefront OBJ + drawing.mtl with the ACI colours diff --git a/src/DwgIO.Cli/Program.cs b/src/DwgIO.Cli/Program.cs index 039f78d..c6e208b 100644 --- a/src/DwgIO.Cli/Program.cs +++ b/src/DwgIO.Cli/Program.cs @@ -384,11 +384,13 @@ internal static int ExportDocument( return DwgExitCodes.Usage; } + sceneOptions.IncludePaperSpace = false; var objResult = DwgSceneExport.SaveObj(doc, target, sceneOptions); output.WriteLine($"{doc.Version} elements={objResult.ElementCount} ({objResult.ElementsWithGeometry} with geometry) -> {target}"); } else if (Path.GetExtension(target).Equals(".glb", StringComparison.OrdinalIgnoreCase)) { + sceneOptions.IncludePaperSpace = false; var glbResult = DwgSceneExport.SaveGlb(doc, target, sceneOptions); output.WriteLine($"{doc.Version} elements={glbResult.ElementCount} ({glbResult.ElementsWithGeometry} with geometry) -> {target}"); } diff --git a/tests/DwgIO.Tests/BlocksAndSheetsTests.cs b/tests/DwgIO.Tests/BlocksAndSheetsTests.cs index db0d05b..9cb9f40 100644 --- a/tests/DwgIO.Tests/BlocksAndSheetsTests.cs +++ b/tests/DwgIO.Tests/BlocksAndSheetsTests.cs @@ -1,8 +1,10 @@ using System.Text; +using DwgIO.Cli; using DwgIO.Export; using DwgIO.Model; using DwgIO.Scene; using IOScene; +using IOScene.Formats; using Xunit; namespace DwgIO.Tests; @@ -87,8 +89,7 @@ public void Insert_ExpandsBlockGeometry_WithTransformBasePointAndByBlockColor() Assert.Equal(new RgbaColor(255, 0, 0), inst.Color); } - [Fact] - public void PaperSpaceLayoutAndViewport_ProjectAndClipModelSpaceGeometryIntoSheet() + private static DwgDocument DrawingWithViewport() { const ulong modelHandle = 0x1F; const ulong paperHandle = 0x20; @@ -186,7 +187,13 @@ public void PaperSpaceLayoutAndViewport_ProjectAndClipModelSpaceGeometryIntoShee }, }; - var doc = new DwgDocument("sheets.dwg", TestHeader, entities, blocks: blocks, layouts: layouts); + return new DwgDocument("sheets.dwg", TestHeader, entities, blocks: blocks, layouts: layouts); + } + + [Fact] + public void PaperSpaceLayoutAndViewport_ProjectAndClipModelSpaceGeometryIntoSheet() + { + var doc = DrawingWithViewport(); var result = DwgSceneReader.Read(doc, new DwgSceneOptions { LengthUnit = "m", @@ -230,6 +237,42 @@ public void PaperSpaceLayoutAndViewport_ProjectAndClipModelSpaceGeometryIntoShee Assert.Contains("\"initial2DMode\":true", autoHtml, StringComparison.Ordinal); } + [Theory] + [InlineData(".obj")] + [InlineData(".glb")] + public void DirectExchangeExport_WritesModelLineWithoutProjectedViewportCopy(string extension) + { + using var directory = new TemporaryDirectory(); + string path = Path.Combine(directory.Path, "model" + extension); + + Assert.Equal(DwgExitCodes.Ok, Program.ExportDocument(DrawingWithViewport(), path, TextWriter.Null, TextWriter.Null)); + + if (extension == ".obj") + { + string[] lines = File.ReadAllLines(path); + Assert.Single(lines, line => line.StartsWith("l ", StringComparison.Ordinal)); + Assert.Collection(lines.Where(line => line.StartsWith("v ", StringComparison.Ordinal)).Order(StringComparer.Ordinal), + vertex => Assert.Equal("v -20 0 0", vertex), + vertex => Assert.Equal("v 20 0 0", vertex)); + } + else + { + var scene = GlbSceneReader.Load(path); + var instance = Assert.Single(scene.Instances); + var geometry = Assert.Single(scene.Geometries, g => g.Id == instance.GeometryId); + Assert.Equal(SceneTopology.Lines, geometry.Topology); + var vertices = geometry.Vertices.Select((instance.Transform ?? Transform3d.Identity).Apply).OrderBy(v => v.X).ToArray(); + Assert.Equal(new[] { new Vector3d(-20, 0, 0), new Vector3d(20, 0, 0) }, vertices); + } + } + + private sealed class TemporaryDirectory : IDisposable + { + public string Path { get; } = Directory.CreateTempSubdirectory("dwgio-sheet-").FullName; + + public void Dispose() => Directory.Delete(Path, recursive: true); + } + [Theory] [InlineData(false)] [InlineData(true)]