Skip to content

Commit

Permalink
Merge pull request #502 from DomCR/xrecord-reference
Browse files Browse the repository at this point in the history
Xrecord linked
  • Loading branch information
DomCR authored Dec 6, 2024
2 parents 8fa9f0a + 2dade95 commit 2e227e1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public SingleCaseGenerator() { }
public SingleCaseGenerator(string name)
{
this.Name = name;
this.Document.Header.ShowModelSpace = true;
}

public override string ToString()
Expand Down
29 changes: 24 additions & 5 deletions src/ACadSharp/Entities/Viewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -468,7 +487,7 @@ private void updateScaleXRecord()
{
if (item.Code == 340)
{
item.Value = this._scale.Handle;
item.Value = this._scale;
}
}
}
Expand All @@ -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);
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using static ACadSharp.Objects.MultiLeaderAnnotContext;
using CSUtilities.Converters;
using CSUtilities.Extensions;
using System.Globalization;

namespace ACadSharp.IO.DWG
{
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
27 changes: 26 additions & 1 deletion src/ACadSharp/IO/Templates/CadXRecordTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
using ACadSharp.Objects;
using System;

namespace ACadSharp.IO.Templates
{
internal class CadXRecordTemplate : CadTemplate<XRecord>
{
private readonly System.Collections.Generic.List<Tuple<int, ulong>> _entries = new();

public CadXRecordTemplate() : base(new XRecord()) { }

public CadXRecordTemplate(XRecord cadObject) : base(cadObject) { }

public void AddHandleReference(int code, ulong handle)
{
_entries.Add(new Tuple<int, ulong>(code, handle));
}

public override void Build(CadDocumentBuilder builder)
{
base.Build(builder);

foreach (var entry in _entries)
{
if (builder.TryGetCadObject<CadObject>(entry.Item2, out CadObject obj))
{
this.CadObject.CreateEntry(entry.Item1, obj);
}
else
{
builder.Notify($"XRecord reference not found {entry.Item1}|{entry.Item2}", NotificationType.Warning);
}
}
}
}
}
}

0 comments on commit 2e227e1

Please sign in to comment.