From c4d4045858f724219f7d81081210f2bf3b342cc2 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Sun, 4 Dec 2022 16:12:41 -0700 Subject: [PATCH] fix block writing and add example --- .github/workflows/ci.yml | 2 -- src/Examples/TestExamples.cs | 25 +++++++++++++++++++ src/IxMilia.Dwg.Test/EntityTests.cs | 8 +++--- src/IxMilia.Dwg/Objects/DwgBlockHeader.cs | 30 +++++++++++++++++------ src/IxMilia.Dwg/Objects/DwgInsert.cs | 8 ++++-- 5 files changed, 58 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3a8514..eff0992 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,5 @@ jobs: steps: - uses: actions/checkout@v1 - uses: actions/setup-dotnet@v3 - with: - dotnet-version: ${{ env.DOTNET_SDK_VERSION }} - run: .\build-and-test.ps1 -configuration Release -notest - run: dotnet nuget push .\artifacts\packages\Release\*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }} --no-symbols diff --git a/src/Examples/TestExamples.cs b/src/Examples/TestExamples.cs index ab92171..7830c58 100644 --- a/src/Examples/TestExamples.cs +++ b/src/Examples/TestExamples.cs @@ -70,5 +70,30 @@ public void WriteCustomLineType() drawing.SaveExample(); } + + [Fact] + public void WriteBlock() + { + var drawing = new DwgDrawing(); + drawing.CurrentEntityLineType = drawing.ByLayerLineType; + drawing.FileHeader.Version = DwgVersionId.R14; + + // create a block with a line from (0,0) to (1,1) + var line = new DwgLine(new DwgPoint(0.0, 0.0, 0.0), new DwgPoint(1.0, 1.0, 0.0)); + line.Layer = drawing.CurrentLayer; + + var blockHeader = DwgBlockHeader.CreateBlockRecordWithName("my-block", drawing.CurrentLayer); + blockHeader.Entities.Add(line); + drawing.BlockHeaders.Add(blockHeader); + + // insert a copy of the block at location (3,3); the result is a line from (3,3) to (4,4) + var insert = new DwgInsert(blockHeader); + insert.Location = new DwgPoint(3.0, 3.0, 0.0); + insert.Layer = drawing.CurrentLayer; + + drawing.ModelSpaceBlockRecord.Entities.Add(insert); + + drawing.SaveExample(); + } } } diff --git a/src/IxMilia.Dwg.Test/EntityTests.cs b/src/IxMilia.Dwg.Test/EntityTests.cs index cc6aa94..a873637 100644 --- a/src/IxMilia.Dwg.Test/EntityTests.cs +++ b/src/IxMilia.Dwg.Test/EntityTests.cs @@ -58,7 +58,7 @@ public void RoundTripInsertWithoutAttributes() { var drawing = new DwgDrawing(); - var blockHeader = new DwgBlockHeader("some-block", new DwgBlock("some-block") { Layer = drawing.CurrentLayer }, new DwgEndBlock() { Layer = drawing.CurrentLayer }); + var blockHeader = DwgBlockHeader.CreateBlockRecordWithName("some-block", drawing.CurrentLayer); drawing.BlockHeaders.Add(blockHeader); var ins = new DwgInsert(); @@ -80,7 +80,7 @@ public void RoundTripInsertWithAttributes() { var drawing = new DwgDrawing(); - var blockHeader = new DwgBlockHeader("some-block", new DwgBlock("some-block") { Layer = drawing.CurrentLayer }, new DwgEndBlock() { Layer = drawing.CurrentLayer }); + var blockHeader = DwgBlockHeader.CreateBlockRecordWithName("some-block", drawing.CurrentLayer); drawing.BlockHeaders.Add(blockHeader); var ins = new DwgInsert(); @@ -104,7 +104,7 @@ public void RoundTripMInsertWithoutAttributes() { var drawing = new DwgDrawing(); - var blockHeader = new DwgBlockHeader("some-block", new DwgBlock("some-block") { Layer = drawing.CurrentLayer }, new DwgEndBlock() { Layer = drawing.CurrentLayer }); + var blockHeader = DwgBlockHeader.CreateBlockRecordWithName("some-block", drawing.CurrentLayer); drawing.BlockHeaders.Add(blockHeader); var ins = new DwgMInsert(); @@ -126,7 +126,7 @@ public void RoundTripMInsertWithAttributes() { var drawing = new DwgDrawing(); - var blockHeader = new DwgBlockHeader("some-block", new DwgBlock("some-block") { Layer = drawing.CurrentLayer }, new DwgEndBlock() { Layer = drawing.CurrentLayer }); + var blockHeader = DwgBlockHeader.CreateBlockRecordWithName("some-block", drawing.CurrentLayer); drawing.BlockHeaders.Add(blockHeader); var ins = new DwgMInsert(); diff --git a/src/IxMilia.Dwg/Objects/DwgBlockHeader.cs b/src/IxMilia.Dwg/Objects/DwgBlockHeader.cs index 7c14db1..7f1ba4d 100644 --- a/src/IxMilia.Dwg/Objects/DwgBlockHeader.cs +++ b/src/IxMilia.Dwg/Objects/DwgBlockHeader.cs @@ -16,7 +16,7 @@ public partial class DwgBlockHeader : DwgObject public bool IsModelSpaceBlock => string.Compare(Name, ModelSpaceBlockName, StringComparison.OrdinalIgnoreCase) == 0; public bool IsPaperSpaceBlock => string.Compare(Name, PaperSpaceBlockName, StringComparison.OrdinalIgnoreCase) == 0; - public DwgBlockHeader(string name, DwgBlock block, DwgEndBlock endBlock) + public DwgBlockHeader(string name) : this() { if (string.IsNullOrEmpty(name)) @@ -25,8 +25,14 @@ public DwgBlockHeader(string name, DwgBlock block, DwgEndBlock endBlock) } Name = name; - Block = block; - EndBlock = endBlock; + Block = new DwgBlock(name); + EndBlock = new DwgEndBlock(); + } + + public void SetLayer(DwgLayer layer) + { + Block.Layer = layer; + EndBlock.Layer = layer; } internal override DwgHandleReferenceCode ExpectedNullHandleCode => DwgHandleReferenceCode.SoftOwner; @@ -65,6 +71,16 @@ internal override void OnBeforeObjectWrite(DwgVersionId version) _firstEntityHandleReference = flatList.First().MakeHandleReference(DwgHandleReferenceCode.HardPointer); _lastEntityHandleReference = flatList.Last().MakeHandleReference(DwgHandleReferenceCode.HardPointer); } + + // assign subentity references + AssignEntityMode(Block); + AssignEntityMode(EndBlock); + Block.AssignSubentityReference(Handle); + EndBlock.AssignSubentityReference(Handle); + foreach (var entity in Entities) + { + entity.AssignSubentityReference(Handle); + } } private void AssignEntityMode(DwgEntity entity) @@ -149,11 +165,11 @@ private void LoadEntities(BitReader reader, DwgObjectCache objectCache) } } - internal static DwgBlockHeader CreateBlockRecordWithName(string name, DwgLayer layer) + public static DwgBlockHeader CreateBlockRecordWithName(string name, DwgLayer layer) { - var block = new DwgBlock(name) { Layer = layer }; - var endBlock = new DwgEndBlock() { Layer = layer }; - return new DwgBlockHeader(name, block, endBlock); + var blockHeader = new DwgBlockHeader(name); + blockHeader.SetLayer(layer); + return blockHeader; } } } diff --git a/src/IxMilia.Dwg/Objects/DwgInsert.cs b/src/IxMilia.Dwg/Objects/DwgInsert.cs index be8030a..d2ce596 100644 --- a/src/IxMilia.Dwg/Objects/DwgInsert.cs +++ b/src/IxMilia.Dwg/Objects/DwgInsert.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; - +using System.Linq; + namespace IxMilia.Dwg.Objects { public partial class DwgInsert @@ -23,7 +24,10 @@ internal override IEnumerable ChildItems yield return attrib; } - yield return SeqEnd; + if (Attributes.Any()) + { + yield return SeqEnd; + } } }