Skip to content

Commit

Permalink
blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Jul 10, 2023
1 parent 81d7a40 commit bed50b6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 96 deletions.
140 changes: 58 additions & 82 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using ACadSharp.Blocks;
using ACadSharp.Entities;
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 @@ -56,45 +52,56 @@ private void readBlock()
//Read the table name
this._reader.ReadNext();

Block blckEntity = null;
CadEntityTemplate template = null;
DxfMap map = DxfMap.Create<Block>();

this.readCommonObjectData(out string name, out ulong handle, out ulong? ownerHandle, out ulong? xdictHandle, out List<ulong> reactors);
Block blckEntity = new Block();
CadEntityTemplate template = new CadEntityTemplate(blckEntity);

if (this._builder.TryGetCadObject(ownerHandle, out BlockRecord record))
{
blckEntity = record.BlockEntity;
}
else
{
blckEntity = new Block();
this._builder.Notify($"Block Record {ownerHandle} not found for Block {handle} | {name}", NotificationType.Warning);
}

//Assign the handle to the entity
blckEntity.Handle = handle;
template = new CadEntityTemplate(blckEntity);
template.OwnerHandle = ownerHandle;
template.XDictHandle = xdictHandle;
template.ReactorsHandles = reactors;

Debug.Assert(this._reader.ValueAsString == DxfSubclassMarker.Entity);

this.readMapped<Entity>(blckEntity, template);

Debug.Assert(this._reader.ValueAsString == DxfSubclassMarker.BlockBegin);
string name = null;
BlockRecord record = null;

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

if (record == null && this._builder.DocumentToBuild.BlockRecords.TryGetValue(blckEntity.Name, out record))
while (this._reader.DxfCode != DxfCode.Start)
{
record.BlockEntity = blckEntity;
this._builder.Notify($"Block record find by name {blckEntity.Name}", NotificationType.None);
switch (this._reader.Code)
{
case 2:
case 3:
name = this._reader.ValueAsString;
if (record == null && this._builder.TryGetTableEntry(name, out record))
{
record.BlockEntity = blckEntity;
}
else if (record == null)
{
this._builder.Notify($"Block record [{name}] not found at line {this._reader.Position}", NotificationType.Warning);
}
break;
case 330:
if (record == null && this._builder.TryGetCadObject(this._reader.ValueAsHandle, out record))
{
record.BlockEntity = blckEntity;
}
else if (record == null)
{
this._builder.Notify($"Block record with handle [{this._reader.ValueAsString}] not found at line {this._reader.Position}", NotificationType.Warning);
}
break;
default:
if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.BlockBegin]))
{
this.readCommonEntityCodes(template, out bool isExtendedData, map);
if (isExtendedData)
continue;
}
break;
}

this._reader.ReadNext();
}
else if (record == null)

if (record == null)
{
throw new DxfException($"Could not find the block record for {blckEntity.Name} and handle {blckEntity.Handle}");
throw new DxfException($"Could not find the block record for {name} and handle {blckEntity.Handle}");
}

while (this._reader.ValueAsString != DxfFileToken.EndBlock)
Expand Down Expand Up @@ -128,63 +135,32 @@ private void readBlock()
this._builder.AddTemplate(template);
}

private void readBlockBegin(Block blckEntity)
private void readBlockEnd(BlockEnd block)
{
this._reader.ReadNext();
DxfMap map = DxfMap.Create<BlockEnd>();
CadEntityTemplate template = new CadEntityTemplate(block);

if (this._reader.DxfCode == DxfCode.Start)
{
this._reader.ReadNext();
}

while (this._reader.DxfCode != DxfCode.Start
&& this._reader.DxfCode != DxfCode.Subclass)
while (this._reader.DxfCode != DxfCode.Start)
{
switch (this._reader.Code)
{
case 1:
blckEntity.XrefPath = this._reader.ValueAsString;
break;
case 4:
blckEntity.Comments = this._reader.ValueAsString;
break;
case 2:
case 3:
if (blckEntity.Owner == null && this._builder.TryGetTableEntry(this._reader.ValueAsString, out BlockRecord record))
{
record.BlockEntity = blckEntity;
this._builder.Notify($"Block record find by name {blckEntity.Name}", NotificationType.None);
}
else if (blckEntity.Owner == null)
default:
if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.BlockEnd]))
{
throw new DxfException($"Could not find the block record for {blckEntity.Name} and handle {blckEntity.Handle}");
this.readCommonEntityCodes(template, out bool isExtendedData, map);
if (isExtendedData)
continue;
}
break;
case 70:
blckEntity.Flags = (BlockTypeFlags)this._reader.ValueAsUShort;
break;
case 10:
blckEntity.BasePoint = new XYZ(this._reader.ValueAsDouble, blckEntity.BasePoint.Y, blckEntity.BasePoint.Z);
break;
case 20:
blckEntity.BasePoint = new XYZ(blckEntity.BasePoint.X, this._reader.ValueAsDouble, blckEntity.BasePoint.Z);
break;
case 30:
blckEntity.BasePoint = new XYZ(blckEntity.BasePoint.X, blckEntity.BasePoint.Y, this._reader.ValueAsDouble);
break;
default:
this._builder.Notify($"Unhandeled dxf code : {this._reader.Code} with value : {this._reader.Value} for subclass {DxfSubclassMarker.BlockBegin}");
break;
}

this._reader.ReadNext();
}
}

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

this.readCommonObjectData(template);

this.readMapped<Entity>(block, template);

this.readMapped<BlockEnd>(block, template);

this._builder.AddTemplate(template);
}
Expand Down
21 changes: 14 additions & 7 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ protected void readCommonObjectData(out string name, out ulong handle, out ulong
xdictHandle = null;
reactors = new List<ulong>();

bool handleNotFound = true;

if (this._reader.DxfCode == DxfCode.Start
|| this._reader.DxfCode == DxfCode.Subclass)
this._reader.ReadNext();
Expand All @@ -68,7 +66,6 @@ protected void readCommonObjectData(out string name, out ulong handle, out ulong
case 5:
case 105:
handle = this._reader.ValueAsHandle;
handleNotFound = false;
break;
//Start of application - defined group
case 102:
Expand All @@ -89,11 +86,9 @@ protected void readCommonObjectData(out string name, out ulong handle, out ulong

this._reader.ReadNext();
}

//if (handleNotFound) //TODO: Set exception for no handle
// throw new Exception();
}

[Obsolete]
protected void readCommonObjectData(CadTemplate template)
{
while (this._reader.DxfCode != DxfCode.Subclass)
Expand Down Expand Up @@ -191,7 +186,16 @@ protected CadEntityTemplate readEntity()
case DxfFileToken.EntityPoint:
return this.readEntityCodes<Point>(new CadEntityTemplate<Point>(), readEntitySubclassMap);
case DxfFileToken.EntityPolyline:
return this.readEntityCodes<Entity>(new CadPolyLineTemplate(), readPolyline);
var template = this.readEntityCodes<Entity>(new CadPolyLineTemplate(), readPolyline);
if (template.CadObject is CadPolyLineTemplate.PolyLinePlaceholder)
{
this._builder.Notify($"[{DxfFileToken.EntityPolyline}] Subclass not found, entity discarded", NotificationType.Warning);
return null;
}
else
{
return template;
}
case DxfFileToken.EntityRay:
return this.readEntityCodes<Ray>(new CadEntityTemplate<Ray>(), readEntitySubclassMap);
case DxfFileToken.EndSequence:
Expand Down Expand Up @@ -253,6 +257,9 @@ protected void readCommonEntityCodes(CadEntityTemplate template, out bool isExte
case 8:
template.LayerName = this._reader.ValueAsString;
break;
//Absent or zero indicates entity is in model space. 1 indicates entity is in paper space (optional).
case 67:
break;
case 347:
template.MaterialHandle = this._reader.ValueAsHandle;
break;
Expand Down
7 changes: 0 additions & 7 deletions ACadSharp/IO/Templates/CadPolyLineTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ public override void Build(CadDocumentBuilder builder)
public void SetPolyLineObject(Polyline polyLine)
{
polyLine.Handle = this.CadObject.Handle;
polyLine.Owner = this.CadObject.Owner;

polyLine.XDictionary = this.CadObject.XDictionary;

//polyLine.Reactors = this.CadObject.Reactors;
//polyLine.ExtendedData = this.CadObject.ExtendedData;

polyLine.Color = this.CadObject.Color;
polyLine.LineWeight = this.CadObject.LineWeight;
polyLine.LinetypeScale = this.CadObject.LinetypeScale;
Expand Down

0 comments on commit bed50b6

Please sign in to comment.