diff --git a/src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs b/src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs index c95b7915..721e5e65 100644 --- a/src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs +++ b/src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs @@ -25,6 +25,7 @@ public SingleCaseGenerator() { } public SingleCaseGenerator(string name) { this.Name = name; + this.Document.Header.ShowModelSpace = true; } public override string ToString() diff --git a/src/ACadSharp/Entities/Viewport.cs b/src/ACadSharp/Entities/Viewport.cs index 2c27ad3a..e97a0ff3 100644 --- a/src/ACadSharp/Entities/Viewport.cs +++ b/src/ACadSharp/Entities/Viewport.cs @@ -335,19 +335,38 @@ public Scale Scale { get { - return this._scale; + if (this.Document != null) + { + if (this.XDictionary != null && this.XDictionary.TryGetEntry(ASDK_XREC_ANNOTATION_SCALE_INFO, out XRecord record)) + { + foreach (XRecord.Entry item in record.Entries) + { + if (item.Code == 340) + { + return item.Value as Scale; + } + } + } + + return null; + } + else + { + return this._scale; + } } set { if (this.Document != null) { this._scale = this.updateCollection(value, this.Document.Scales); - this.updateScaleXRecord(); } else { this._scale = value; } + + this.updateScaleXRecord(); } } @@ -433,7 +452,7 @@ internal override void AssignDocument(CadDocument doc) { base.AssignDocument(doc); - this._scale = this.updateCollection(this.Scale, doc.Scales); + this._scale = this.updateCollection(this._scale, doc.Scales); this.Document.Scales.OnRemove += this.scalesOnRemove; } @@ -468,7 +487,7 @@ private void updateScaleXRecord() { if (item.Code == 340) { - item.Value = this._scale.Handle; + item.Value = this._scale; } } } @@ -477,7 +496,7 @@ private void updateScaleXRecord() record = new XRecord(ASDK_XREC_ANNOTATION_SCALE_INFO); this.XDictionary.Add(record); - record.CreateEntry(340, _scale.Handle); + record.CreateEntry(340, _scale); } } } diff --git a/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs b/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs index d0cf5365..25563c0e 100644 --- a/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs +++ b/src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs @@ -14,6 +14,7 @@ using static ACadSharp.Objects.MultiLeaderAnnotContext; using CSUtilities.Converters; using CSUtilities.Extensions; +using System.Globalization; namespace ACadSharp.IO.DWG { @@ -1116,8 +1117,6 @@ private CadTemplate readBlockVisibilityParameter() this.readCommonNonEntityData(template); - //analyse02(200); - var l1 = _objectReader.ReadBitLong(); var s2 = _objectReader.ReadBitShort(); // can also be L var s3 = _objectReader.ReadBitShort(); // can also be L @@ -1142,16 +1141,12 @@ private CadTemplate readBlockVisibilityParameter() // 300 Parameter Type blockVisibilityParameter.ParameterType = _textReader.ReadVariableText(); - //resetPosition(214275, 2); // 1010, 1020, 1030 Menu position blockVisibilityParameter.BasePosition = _objectReader.Read3BitDouble(); // 2x0 <- var s170 = _objectReader.ReadBitShort(); var s171 = _objectReader.ReadBitShort(); var l93 = _objectReader.ReadBitLong(); - //DwgAnalyseTools.ShowCurrentPosAndShift(); - - //var s281 = _objectReader.ReadBitShort(); // 301 blockVisibilityParameter.Name = _textReader.ReadVariableText(); @@ -3839,7 +3834,8 @@ private CadTemplate readLayer() //Common: //Color CMC 62 - layer.Color = this._mergedReaders.ReadCmColor(); + var color = this._mergedReaders.ReadCmColor(); + layer.Color = color.IsByBlock || color.IsByLayer ? new(30) : color; //TODO: This is not the Layer control handle template.LayerControlHandle = this.handleReference(); @@ -5462,7 +5458,15 @@ private CadTemplate readXRecord() xRecord.CreateEntry(code, this._objectReader.ReadRawULong()); break; case GroupCodeValueType.Handle: - xRecord.CreateEntry(code, this._objectReader.ReadTextUnicode()); + string hex = this._objectReader.ReadTextUnicode(); + if (ulong.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong result)) + { + template.AddHandleReference(code, result); + } + else + { + this.notify($"Failed to parse {hex} to handle", NotificationType.Warning); + } break; case GroupCodeValueType.Bool: xRecord.CreateEntry(code, this._objectReader.ReadByte() > 0); @@ -5473,7 +5477,7 @@ private CadTemplate readXRecord() break; case GroupCodeValueType.ObjectId: case GroupCodeValueType.ExtendedDataHandle: - xRecord.CreateEntry(code, this._objectReader.ReadRawULong()); + template.AddHandleReference(code, this._objectReader.ReadRawULong()); break; default: this.notify($"Unidentified GroupCodeValueType {code} for XRecord [{xRecord.Handle}]", NotificationType.Warning); diff --git a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs index 8c84a75b..be0830ba 100644 --- a/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs +++ b/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs @@ -199,20 +199,28 @@ private bool readXRecord(CadTemplate template, DxfMap map) switch (this._reader.Code) { case 100 when this._reader.ValueAsString == DxfSubclassMarker.XRecord: - this.readXRecordEntries(tmp.CadObject); + this.readXRecordEntries(tmp); return true; default: return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.XRecord]); } } - private void readXRecordEntries(XRecord recrod) + private void readXRecordEntries(CadXRecordTemplate template) { this._reader.ReadNext(); while (this._reader.DxfCode != DxfCode.Start) { - recrod.CreateEntry(this._reader.Code, this._reader.Value); + switch (this._reader.GroupCodeValue) + { + case GroupCodeValueType.Handle: + template.AddHandleReference(this._reader.Code, this._reader.ValueAsHandle); + break; + default: + template.CadObject.CreateEntry(this._reader.Code, this._reader.Value); + break; + } this._reader.ReadNext(); } diff --git a/src/ACadSharp/IO/Templates/CadXRecordTemplate.cs b/src/ACadSharp/IO/Templates/CadXRecordTemplate.cs index d913b2b8..f3d2b7ef 100644 --- a/src/ACadSharp/IO/Templates/CadXRecordTemplate.cs +++ b/src/ACadSharp/IO/Templates/CadXRecordTemplate.cs @@ -1,11 +1,36 @@ using ACadSharp.Objects; +using System; namespace ACadSharp.IO.Templates { internal class CadXRecordTemplate : CadTemplate { + private readonly System.Collections.Generic.List> _entries = new(); + public CadXRecordTemplate() : base(new XRecord()) { } public CadXRecordTemplate(XRecord cadObject) : base(cadObject) { } + + public void AddHandleReference(int code, ulong handle) + { + _entries.Add(new Tuple(code, handle)); + } + + public override void Build(CadDocumentBuilder builder) + { + base.Build(builder); + + foreach (var entry in _entries) + { + if (builder.TryGetCadObject(entry.Item2, out CadObject obj)) + { + this.CadObject.CreateEntry(entry.Item1, obj); + } + else + { + builder.Notify($"XRecord reference not found {entry.Item1}|{entry.Item2}", NotificationType.Warning); + } + } + } } -} +} \ No newline at end of file