From bed50b62dcfde322371d66b86f2063b458463548 Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 10 Jul 2023 18:39:52 +0200 Subject: [PATCH] blocks --- .../DxfStreamReader/DxfBlockSectionReader.cs | 140 ++++++++---------- .../DxfStreamReader/DxfSectionReaderBase.cs | 21 ++- ACadSharp/IO/Templates/CadPolyLineTemplate.cs | 7 - 3 files changed, 72 insertions(+), 96 deletions(-) diff --git a/ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs b/ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs index c68464b5..847df8d2 100644 --- a/ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs +++ b/ACadSharp/IO/DXF/DxfStreamReader/DxfBlockSectionReader.cs @@ -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 { @@ -56,45 +52,56 @@ private void readBlock() //Read the table name this._reader.ReadNext(); - Block blckEntity = null; - CadEntityTemplate template = null; + DxfMap map = DxfMap.Create(); - this.readCommonObjectData(out string name, out ulong handle, out ulong? ownerHandle, out ulong? xdictHandle, out List 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(blckEntity, template); - - Debug.Assert(this._reader.ValueAsString == DxfSubclassMarker.BlockBegin); + string name = null; + BlockRecord record = null; - //this.readMapped(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) @@ -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(); + 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(block, template); - - this.readMapped(block, template); this._builder.AddTemplate(template); } diff --git a/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs b/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs index 77e237fe..be7bb610 100644 --- a/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs +++ b/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs @@ -48,8 +48,6 @@ protected void readCommonObjectData(out string name, out ulong handle, out ulong xdictHandle = null; reactors = new List(); - bool handleNotFound = true; - if (this._reader.DxfCode == DxfCode.Start || this._reader.DxfCode == DxfCode.Subclass) this._reader.ReadNext(); @@ -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: @@ -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) @@ -191,7 +186,16 @@ protected CadEntityTemplate readEntity() case DxfFileToken.EntityPoint: return this.readEntityCodes(new CadEntityTemplate(), readEntitySubclassMap); case DxfFileToken.EntityPolyline: - return this.readEntityCodes(new CadPolyLineTemplate(), readPolyline); + var template = this.readEntityCodes(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(new CadEntityTemplate(), readEntitySubclassMap); case DxfFileToken.EndSequence: @@ -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; diff --git a/ACadSharp/IO/Templates/CadPolyLineTemplate.cs b/ACadSharp/IO/Templates/CadPolyLineTemplate.cs index 5b1675e5..b5da5152 100644 --- a/ACadSharp/IO/Templates/CadPolyLineTemplate.cs +++ b/ACadSharp/IO/Templates/CadPolyLineTemplate.cs @@ -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;