Skip to content

Commit

Permalink
Merge pull request #133 from DomCR/issue-127_DxfReader-header-set
Browse files Browse the repository at this point in the history
Issue 127 dxf reader header set
  • Loading branch information
jankozik committed May 18, 2023
2 parents a786f0d + 7a7b4f1 commit 2805cb9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ACadSharp/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class Block : Entity, INamedCadObject
[DxfCodeValue(2, 3)]
public string Name
{
get { return BlockOwner.Name; }
set { BlockOwner.Name = value; }
get { return this.BlockOwner.Name; }
set { this.BlockOwner.Name = value; }
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions ACadSharp/IO/CadDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public bool TryGetCadObject<T>(ulong? handle, out T value) where T : CadObject
return false;
}

public bool TryGetTableEntry<T>(string name, out T entry)
where T : TableEntry
{
entry = cadObjects.Values.OfType<T>().FirstOrDefault(e => e.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
return entry != null;
}

public T GetObjectTemplate<T>(ulong handle) where T : CadTemplate
{
if (this.templates.TryGetValue(handle, out CadTemplate builder))
Expand Down
2 changes: 2 additions & 0 deletions ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ public override CadHeader ReadHeader()

if (this._reader.LastValueAsString == null || !headerMap.TryGetValue(currVar, out var data))
{
#if TEST
this.triggerNotification($"Header variable not implemented {currVar}", NotificationType.NotImplemented);
#endif
this._reader.ReadNext();
continue;
}
Expand Down
54 changes: 53 additions & 1 deletion ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using ACadSharp.Exceptions;
using ACadSharp.IO.Templates;
using ACadSharp.Tables;
using CSMath;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;

namespace ACadSharp.IO.DXF
{
Expand Down Expand Up @@ -65,6 +67,7 @@ private void readBlock()
}
else
{
blckEntity = new Block();
this._builder.Notify($"Block Record {ownerHandle} not found for Block {handle} | {name}", NotificationType.Warning);
}

Expand All @@ -81,7 +84,8 @@ private void readBlock()

Debug.Assert(this._reader.LastValueAsString == DxfSubclassMarker.BlockBegin);

this.readMapped<Block>(blckEntity, template);
//this.readMapped<Block>(blckEntity, template);
this.readBlockBegin(blckEntity);

if (record == null && this._builder.DocumentToBuild.BlockRecords.TryGetValue(blckEntity.Name, out record))
{
Expand Down Expand Up @@ -124,6 +128,54 @@ private void readBlock()
this._builder.AddTemplate(template);
}

private void readBlockBegin(Block blckEntity)
{
this._reader.ReadNext();

while (this._reader.LastDxfCode != DxfCode.Start
&& this._reader.LastDxfCode != DxfCode.Subclass)
{
switch (this._reader.LastCode)
{
case 1:
blckEntity.XrefPath = this._reader.LastValueAsString;
break;
case 4:
blckEntity.Comments = this._reader.LastValueAsString;
break;
case 2:
case 3:
if (blckEntity.Owner == null && this._builder.TryGetTableEntry(this._reader.LastValueAsString, out BlockRecord record))
{
record.BlockEntity = blckEntity;
this._builder.Notify($"Block record find by name {blckEntity.Name}", NotificationType.None);
}
else if (blckEntity.Owner == null)
{
throw new DxfException($"Could not find the block record for {blckEntity.Name} and handle {blckEntity.Handle}");
}
break;
case 70:
blckEntity.Flags = (BlockTypeFlags)this._reader.LastValueAsShort;
break;
case 10:
blckEntity.BasePoint = new XYZ(this._reader.LastValueAsDouble, blckEntity.BasePoint.Y, blckEntity.BasePoint.Z);
break;
case 20:
blckEntity.BasePoint = new XYZ(blckEntity.BasePoint.X, this._reader.LastValueAsDouble, blckEntity.BasePoint.Z);
break;
case 30:
blckEntity.BasePoint = new XYZ(blckEntity.BasePoint.X, blckEntity.BasePoint.Y, this._reader.LastValueAsDouble);
break;
default:
this._builder.Notify($"Unhandeled dxf code : {this._reader.LastCode} with value : {this._reader.LastValue} for subclass {DxfSubclassMarker.BlockBegin}");
break;
}

this._reader.ReadNext();
}
}

private void readBlockEnd(BlockEnd block)
{
CadEntityTemplate template = new CadEntityTemplate(block);
Expand Down

0 comments on commit 2805cb9

Please sign in to comment.