Skip to content

Commit

Permalink
fix block writing and add example
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Dec 4, 2022
1 parent 8102b6a commit c4d4045
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions src/Examples/TestExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
8 changes: 4 additions & 4 deletions src/IxMilia.Dwg.Test/EntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
30 changes: 23 additions & 7 deletions src/IxMilia.Dwg/Objects/DwgBlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
8 changes: 6 additions & 2 deletions src/IxMilia.Dwg/Objects/DwgInsert.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;

using System.Linq;

namespace IxMilia.Dwg.Objects
{
public partial class DwgInsert
Expand All @@ -23,7 +24,10 @@ internal override IEnumerable<DwgObject> ChildItems
yield return attrib;
}

yield return SeqEnd;
if (Attributes.Any())
{
yield return SeqEnd;
}
}
}

Expand Down

0 comments on commit c4d4045

Please sign in to comment.