From 534280b7656d79f2816c1bd14643a11d51631d4d Mon Sep 17 00:00:00 2001 From: gryaznulka Date: Fri, 4 Dec 2026 13:45:35 +0100 Subject: [PATCH 1/4] docs: describe model-space OBJ and GLB exports --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From f1b68df9b824a07fe06dc9bd279603e20c6d676c Mon Sep 17 00:00:00 2001 From: gryaznulka Date: Fri, 4 Dec 2026 13:45:55 +0100 Subject: [PATCH 2/4] fix: keep paper space out of direct OBJ and GLB exports --- src/DwgIO.Cli/Program.cs | 2 ++ 1 file changed, 2 insertions(+) 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}"); } From 28335c6b1b20e05a5e14f4611d3b9fda63f23d5b Mon Sep 17 00:00:00 2001 From: gryaznulka Date: Fri, 4 Dec 2026 13:46:28 +0100 Subject: [PATCH 3/4] test: verify direct exchange omits viewport copies --- tests/DwgIO.Tests/BlocksAndSheetsTests.cs | 49 +++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) 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)] From b47ed6b9d7504a2b432677400b24b0cb78a21367 Mon Sep 17 00:00:00 2001 From: mahach666 Date: Fri, 4 Dec 2026 15:54:55 +0300 Subject: [PATCH 4/4] Honor model-space filters when DWG space tables are missing --- docs/public-api.md | 1 + src/DwgIO.Scene.Adapter/DwgSceneReader.cs | 24 ++++++++++---- tests/DwgIO.Tests/BlocksAndSheetsTests.cs | 31 ++++++++++++++++++- .../DwgIO.Tests/HatchDimensionAttribTests.cs | 9 ++++++ 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/docs/public-api.md b/docs/public-api.md index 426f2ee..511341d 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -104,6 +104,7 @@ The CLI uses these exporters and adds a 2D HTML viewer: - `dwgio drawing.dwg drawing.html --mode 2d` exports an AutoCAD-style top view. Use `--bg black|slate|light` to set its background. - `dwgio drawing.dwg drawing.html --mode auto` opens paper sheets or flat linework in 2D. Paper sheets remain selectable when the viewer starts in 3D model space. - `dwgio drawing.dwg out.obj --unit mm` and `dwgio drawing.dwg out.glb --unit mm` pass `--unit` to the scene converter. + These direct CLI exports include only model space, including when the drawing has no block or layout tables. - The scene, OBJ, GLB and HTML commands use metres by default. Pass `--unit auto` to use a supported `$INSUNITS`. - External DWG references are skipped by default. Library callers can set `DwgSceneOptions.ResolveXrefs = true` to read trusted sibling DWG files by name. diff --git a/src/DwgIO.Scene.Adapter/DwgSceneReader.cs b/src/DwgIO.Scene.Adapter/DwgSceneReader.cs index 2b65814..c2ce98c 100644 --- a/src/DwgIO.Scene.Adapter/DwgSceneReader.cs +++ b/src/DwgIO.Scene.Adapter/DwgSceneReader.cs @@ -176,8 +176,14 @@ public void Build() if (_rootDoc.Blocks.Count == 0 && _rootDoc.Layouts.Count == 0) { var ids = new List(); + var entitiesByHandle = _options.IncludePaperSpace ? null : IndexEntities(_rootDoc); foreach (var entity in _rootDoc.Entities) { + if (entitiesByHandle is not null && !(FindOwningInsert(entity, entitiesByHandle) ?? entity).IsModelSpace) + { + continue; + } + EmitEntity(_rootDoc, entity, Affine3d.Identity, _rootScale, 256, null, sheetKey: null, recordForViewports: false, ids); } } @@ -855,12 +861,7 @@ private static DocIndex IndexDocument(DwgDocument doc) } } - var entityByHandle = new Dictionary(doc.Entities.Count); - foreach (var entity in doc.Entities) - { - entityByHandle.TryAdd(entity.Handle, entity); - } - + var entityByHandle = IndexEntities(doc); var byBlock = new Dictionary>(); void AddToBlock(ulong blkHandle, DwgEntity e) { @@ -924,6 +925,17 @@ void AddToBlock(ulong blkHandle, DwgEntity e) return new DocIndex(doc, modelHandle, blocks, entityByHandle, byBlock, paperLayouts); } + private static Dictionary IndexEntities(DwgDocument document) + { + var entitiesByHandle = new Dictionary(document.Entities.Count); + foreach (var entity in document.Entities) + { + entitiesByHandle.TryAdd(entity.Handle, entity); + } + + return entitiesByHandle; + } + private static DwgEntity? FindOwningInsert(DwgEntity entity, Dictionary entities) => entity.Kind == DwgEntityKind.Attrib && entity.OwnerHandle is ulong handle && entities.TryGetValue(handle, out var owner) && owner.Kind == DwgEntityKind.Insert diff --git a/tests/DwgIO.Tests/BlocksAndSheetsTests.cs b/tests/DwgIO.Tests/BlocksAndSheetsTests.cs index 9cb9f40..f43f534 100644 --- a/tests/DwgIO.Tests/BlocksAndSheetsTests.cs +++ b/tests/DwgIO.Tests/BlocksAndSheetsTests.cs @@ -241,11 +241,40 @@ public void PaperSpaceLayoutAndViewport_ProjectAndClipModelSpaceGeometryIntoShee [InlineData(".obj")] [InlineData(".glb")] public void DirectExchangeExport_WritesModelLineWithoutProjectedViewportCopy(string extension) + { + AssertDirectExchangeWritesModelLine(DrawingWithViewport(), extension); + } + + [Theory] + [InlineData(".obj")] + [InlineData(".glb")] + public void DirectExchangeExport_ExcludesPaperLineWithoutBlockAndLayoutTables(string extension) + { + var document = new DwgDocument("spaces.dwg", TestHeader, + [ + new DwgEntity + { + Handle = 0x101, TypeCode = 0x13, TypeName = "LINE", Kind = DwgEntityKind.Line, + IsModelSpace = true, Geometry = [-20.0, 0.0, 0.0, 20.0, 0.0, 0.0], + }, + new DwgEntity + { + Handle = 0x102, TypeCode = 0x13, TypeName = "LINE", Kind = DwgEntityKind.Line, + IsModelSpace = false, Geometry = [100.0, 0.0, 0.0, 110.0, 0.0, 0.0], + }, + ]); + + var fullScene = DwgSceneReader.Read(document, new DwgSceneOptions { ModelKey = "spaces.dwg" }); + Assert.Equal(2, fullScene.Document.Instances.Count); + AssertDirectExchangeWritesModelLine(document, extension); + } + + private static void AssertDirectExchangeWritesModelLine(DwgDocument document, 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)); + Assert.Equal(DwgExitCodes.Ok, Program.ExportDocument(document, path, TextWriter.Null, TextWriter.Null)); if (extension == ".obj") { diff --git a/tests/DwgIO.Tests/HatchDimensionAttribTests.cs b/tests/DwgIO.Tests/HatchDimensionAttribTests.cs index 2b3e6c8..5b789cc 100644 --- a/tests/DwgIO.Tests/HatchDimensionAttribTests.cs +++ b/tests/DwgIO.Tests/HatchDimensionAttribTests.cs @@ -375,6 +375,15 @@ public void AttribFollowsOwningInsert_AndInvisibleAttribIsOmitted_InSceneAndSvg( Assert.Equal("end", (string?)svgText.Attribute("text-anchor")); Assert.Equal("#FF0000", (string?)svgText.Attribute("fill"), ignoreCase: true); Assert.Single(svgDoc.Descendants(XName.Get("path", "http://www.w3.org/2000/svg"))); + + var documentWithoutTables = new DwgDocument("attribs.dwg", Header2018, entities); + var modelOnly = DwgSceneReader.Read(documentWithoutTables, new DwgSceneOptions + { + ModelKey = "attribs.dwg", + IncludePaperSpace = false, + }); + Assert.Equal("VISIBLE_VAL", Assert.Single(modelOnly.Annotations).Text); + Assert.Single(modelOnly.Document.Geometries, geometry => geometry.Topology == SceneTopology.Lines); } [Fact]