Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewport scale #476

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/ACadSharp.Tests/IO/LocalSampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ public void ReadUserDwg(FileModel test)
return;

CadDocument doc = DwgReader.Read(test.Path, this._dwgConfiguration, this.onNotification);

CadDocument newDoc = new CadDocument();

foreach (var entity in doc.Entities)
{
doc.Entities.Remove(entity);
newDoc.Entities.Add(entity);
}
}

[Theory]
Expand Down
1 change: 0 additions & 1 deletion src/ACadSharp.Tests/IO/MultiLeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ACadSharp.IO;
using ACadSharp.Tests.TestModels;
using System.Collections.Generic;
using System.IO;
using Xunit;
using Xunit.Abstractions;

Expand Down
38 changes: 38 additions & 0 deletions src/ACadSharp.Tests/IO/ViewportTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using ACadSharp.Entities;
using ACadSharp.IO;
using ACadSharp.Tests.TestModels;
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace ACadSharp.Tests.IO
{
public class ViewportTests : IOTestsBase
{
public ViewportTests(ITestOutputHelper output) : base(output)
{
}

[Theory]
[MemberData(nameof(DwgFilePaths))]
[MemberData(nameof(DxfAsciiFiles))]
public void ScaleInViewport(FileModel test)
{
CadDocument doc;
if (Path.GetExtension(test.FileName).Equals(".dxf"))
{
doc = DxfReader.Read(test.Path);
}
else
{
doc = DwgReader.Read(test.Path);
}

ACadSharp.Tables.BlockRecord paper = doc.PaperSpace;
foreach (Viewport v in paper.Viewports)
{
Assert.NotNull(v.Scale);
}
}
}
}
6 changes: 6 additions & 0 deletions src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ public void ChangedEncoding()
this.Document.Layers.Add(new Layer("我的自定义层"));
}

public void AddCustomScale()
{
this.Document.Scales.Add(new Scale("Hello"));
}

public void AddBlockWithAttributes()
{
BlockRecord record = new("my_block");
Expand Down Expand Up @@ -491,6 +496,7 @@ static WriterSingleObjectTests()
Data.Add(new(nameof(SingleCaseGenerator.CreateCircleHatch)));
Data.Add(new(nameof(SingleCaseGenerator.ChangedEncoding)));
Data.Add(new(nameof(SingleCaseGenerator.AddBlockWithAttributes)));
Data.Add(new(nameof(SingleCaseGenerator.AddCustomScale)));
}

protected string getPath(string name, string ext, ACadVersion version)
Expand Down
80 changes: 80 additions & 0 deletions src/ACadSharp/Entities/Viewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CSMath;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ACadSharp.Entities
{
Expand All @@ -23,6 +24,8 @@ public class Viewport : Entity
/// </summary>
public const int PaperViewId = 1;

public const string ASDK_XREC_ANNOTATION_SCALE_INFO = "ASDK_XREC_ANNOTATION_SCALE_INFO";

/// <inheritdoc/>
public override ObjectType ObjectType => ObjectType.VIEWPORT;

Expand Down Expand Up @@ -322,6 +325,31 @@ public double ViewWidth

//Soft pointer reference to viewport object (for layer VP property override)

/// <summary>
///
/// </summary>
public Scale Scale
{
get
{
return this._scale;
}
set
{
if (this.Document != null)
{
this._scale = this.updateCollection(value, this.Document.Scales);
this.updateScaleXRecord();
}
else
{
this._scale = value;
}
}
}

private Scale _scale;

/// <inheritdoc/>
public override CadObject Clone()
{
Expand Down Expand Up @@ -372,5 +400,57 @@ public List<Entity> SelectEntities(bool includePartial = true)

return entities;
}

internal override void AssignDocument(CadDocument doc)
{
base.AssignDocument(doc);

this._scale = this.updateCollection(this.Scale, doc.Scales);

this.Document.Scales.OnRemove += this.scalesOnRemove;
}

internal override void UnassignDocument()
{
this.Document.Scales.OnRemove -= this.scalesOnRemove;

base.UnassignDocument();

this._scale = (Scale)this.Scale.Clone();
}

private void scalesOnRemove(object sender, CollectionChangedEventArgs e)
{
if (e.Item.Equals(this.Scale))
{
this.Scale = this.Document.Scales.FirstOrDefault();
}
}

private void updateScaleXRecord()
{
if (this.Document == null)
{
return;
}

if (this.XDictionary.TryGetEntry(ASDK_XREC_ANNOTATION_SCALE_INFO, out XRecord record))
{
foreach (XRecord.Entry item in record.Entries)
{
if (item.Code == 340)
{
item.Value = this._scale.Handle;
}
}
}
else
{
record = new XRecord(ASDK_XREC_ANNOTATION_SCALE_INFO);
this.XDictionary.Add(record);

record.CreateEntry(340, _scale.Handle);
}
}
}
}
32 changes: 16 additions & 16 deletions src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ private CadTemplate readDimOrdinate()
//14 - pt 3BD 14 See DXF documentation.
dimension.LeaderEndpoint = this._objectReader.Read3BitDouble();

byte flags = (this._objectReader.ReadByte());
byte flags = this._objectReader.ReadByte();
dimension.IsOrdinateTypeX = (flags & 0b01) != 0;

this.readCommonDimensionHandles(template);
Expand Down Expand Up @@ -1936,7 +1936,7 @@ private void readCommonDimensionData(CadDimensionTemplate template)
//The actual 70 - group value comes from 3 things:
//6 for being an ordinate DIMENSION, plus whatever bits "Flags 1" and "Flags 2" specify.

byte flags = (this._objectReader.ReadByte());
byte flags = this._objectReader.ReadByte();
dimension.IsTextUserDefinedLocation = (flags & 0b01) == 0;

//User text TV 1
Expand Down Expand Up @@ -5297,50 +5297,50 @@ private CadTemplate readXRecord()
{
case GroupCodeValueType.String:
case GroupCodeValueType.ExtendedDataString:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadTextUnicode()));
xRecord.CreateEntry(code, this._objectReader.ReadTextUnicode());
break;
case GroupCodeValueType.Point3D:
xRecord.Entries.Add(new XRecord.Entry(code,
xRecord.CreateEntry(code,
new XYZ(
this._objectReader.ReadDouble(),
this._objectReader.ReadDouble(),
this._objectReader.ReadDouble()
)));
));
break;
case GroupCodeValueType.Double:
case GroupCodeValueType.ExtendedDataDouble:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadDouble()));
xRecord.CreateEntry(code, this._objectReader.ReadDouble());
break;
case GroupCodeValueType.Byte:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadByte()));
xRecord.CreateEntry(code, this._objectReader.ReadByte());
break;
case GroupCodeValueType.Int16:
case GroupCodeValueType.ExtendedDataInt16:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadShort()));
xRecord.CreateEntry(code, this._objectReader.ReadShort());
break;
case GroupCodeValueType.Int32:
case GroupCodeValueType.ExtendedDataInt32:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadRawLong()));
xRecord.CreateEntry(code, this._objectReader.ReadRawLong());
break;
case GroupCodeValueType.Int64:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadRawULong()));
xRecord.CreateEntry(code, this._objectReader.ReadRawULong());
break;
case GroupCodeValueType.Handle:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadTextUnicode()));
xRecord.CreateEntry(code, this._objectReader.ReadTextUnicode());
break;
case GroupCodeValueType.Bool:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadByte() > 0));
xRecord.CreateEntry(code, this._objectReader.ReadByte() > 0);
break;
case GroupCodeValueType.Chunk:
case GroupCodeValueType.ExtendedDataChunk:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadBytes(this._objectReader.ReadByte())));
xRecord.CreateEntry(code, this._objectReader.ReadBytes(this._objectReader.ReadByte()));
break;
case GroupCodeValueType.ObjectId:
case GroupCodeValueType.ExtendedDataHandle:
xRecord.Entries.Add(new XRecord.Entry(code, this._objectReader.ReadRawULong()));
xRecord.CreateEntry(code, this._objectReader.ReadRawULong());
break;
default:
this.notify($"Unedintified GroupCodeValueType {code} for XRecord [{xRecord.Handle}]", NotificationType.Warning);
this.notify($"Unidentified GroupCodeValueType {code} for XRecord [{xRecord.Handle}]", NotificationType.Warning);
break;
}
}
Expand All @@ -5349,7 +5349,7 @@ private CadTemplate readXRecord()
if (this.R2000Plus)
{
//Cloning flag BS 280
xRecord.ClonningFlags = (DictionaryCloningFlags)this._objectReader.ReadBitShort();
xRecord.CloningFlags = (DictionaryCloningFlags)this._objectReader.ReadBitShort();
}

long size = this._objectInitialPos + (long)(this._size * 8U) - 7L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ private void writeXRecord(XRecord xrecord)
if (this.R2000Plus)
{
//Cloning flag BS 280
this._writer.WriteBitShort((short)xrecord.ClonningFlags);
this._writer.WriteBitShort((short)xrecord.CloningFlags);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ private bool readXRecord(CadTemplate template, DxfMap map)
{
CadXRecordTemplate tmp = template as CadXRecordTemplate;

//TODO: Finsih cadXrecordtemplate

switch (this._reader.Code)
{
case 100 when this._reader.ValueAsString == DxfSubclassMarker.XRecord:
Expand All @@ -212,7 +210,7 @@ private void readXRecordEntries(XRecord recrod)

while (this._reader.DxfCode != DxfCode.Start)
{
recrod.Entries.Add(new XRecord.Entry(this._reader.Code, this._reader.Value));
recrod.CreateEntry(this._reader.Code, this._reader.Value);

this._reader.ReadNext();
}
Expand Down
6 changes: 5 additions & 1 deletion src/ACadSharp/IO/Templates/CadDictionaryTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ public override void Build(CadDocumentBuilder builder)
{
if (builder.TryGetCadObject(item.Value, out NonGraphicalObject entry))
{
//This section basically sets the key and name to the same value to make sure that the
//different collections and dictionaries work properly.
//For some collections like Scales there are some cases where the key doesn't match the name
//but regarding changing the key doesn't seem to take an effect on it.
if (string.IsNullOrEmpty(entry.Name))
{
entry.Name = item.Key;
}

try
{
this.CadObject.Add(item.Key, entry);
this.CadObject.Add(entry.Name, entry);
}
catch (System.Exception ex)
{
Expand Down
20 changes: 18 additions & 2 deletions src/ACadSharp/IO/Templates/CadViewportTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using ACadSharp.Entities;
using ACadSharp.Objects;
using ACadSharp.Tables;
using System.Collections.Generic;

namespace ACadSharp.IO.Templates
{
internal class CadViewportTemplate : CadEntityTemplate
internal class CadViewportTemplate : CadEntityTemplate<Viewport>
{
public ulong? ViewportHeaderHandle { get; set; }

Expand Down Expand Up @@ -39,7 +40,7 @@ public override void Build(CadDocumentBuilder builder)
{
viewport.Boundary = entity;
}
else if(this.BoundaryHandle.HasValue && this.BoundaryHandle > 0)
else if (this.BoundaryHandle.HasValue && this.BoundaryHandle > 0)
{
builder.Notify($"Boundary {this.BoundaryHandle} not found for viewport {this.CadObject.Handle}", NotificationType.Warning);
}
Expand All @@ -54,6 +55,21 @@ public override void Build(CadDocumentBuilder builder)
builder.Notify($"Base ucs not implemented for Viewport, handle {this.BaseUcsHandle}");
}

if (this.CadObject.XDictionary != null &&
this.CadObject.XDictionary.TryGetEntry(Viewport.ASDK_XREC_ANNOTATION_SCALE_INFO, out XRecord record))
{
foreach (XRecord.Entry item in record.Entries)
{
if (item.Code == 340)
{
if (builder.TryGetCadObject((ulong?)item.Value, out Scale scale))
{
this.CadObject.Scale = scale;
}
}
}
}

foreach (var handle in this.FrozenLayerHandles)
{
if (builder.TryGetCadObject(handle, out Layer layer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected ObjectDictionaryCollection(CadDictionary dictionary)
/// Add an entry to the collection
/// </summary>
/// <param name="entry"></param>
public void Add(T entry)
public virtual void Add(T entry)
{
this._dictionary.Add(entry);
}
Expand Down
Loading
Loading