Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Aug 14, 2024
2 parents 654d3dc + 44749f7 commit 80230f1
Show file tree
Hide file tree
Showing 23 changed files with 217 additions and 129 deletions.
7 changes: 7 additions & 0 deletions src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ public void EntityTransparency()
this.Document.Entities.Add(line);
}

public void ChangedEncoding()
{
this.Document.Header.CodePage = "gb2312";
this.Document.Layers.Add(new Layer("我的自定义层"));
}

public void Deserialize(IXunitSerializationInfo info)
{
this.Name = info.GetValue<string>(nameof(this.Name));
Expand Down Expand Up @@ -303,6 +309,7 @@ static WriterSingleObjectTests()
Data.Add(new(nameof(SingleCaseGenerator.CreateLayout)));
Data.Add(new(nameof(SingleCaseGenerator.EntityTransparency)));
Data.Add(new(nameof(SingleCaseGenerator.LineTypeWithSegments)));
Data.Add(new(nameof(SingleCaseGenerator.ChangedEncoding)));
}

protected string getPath(string name, string ext, ACadVersion version)
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp.Tests/Internal/DwgHandleWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void WriteTest(ACadVersion version)
writer.Write();

IDwgStreamReader sreader = DwgStreamReaderBase.GetStreamHandler(version, stream, resetPositon: true);
DwgHandleReader reader = new DwgHandleReader(sreader, version);
DwgHandleReader reader = new DwgHandleReader(version, sreader);
reader.OnNotification += onNotification;

var outmap = reader.Read();
Expand Down
3 changes: 2 additions & 1 deletion src/ACadSharp.Tests/Internal/DwgHeaderWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ACadSharp.Header;
using ACadSharp.IO.DWG;
using System.IO;
using System.Text;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -20,7 +21,7 @@ public void WriteTest(ACadVersion version)
CadDocument document = new CadDocument();
document.Header.Version = version;

DwgHeaderWriter writer = new DwgHeaderWriter(stream, document);
DwgHeaderWriter writer = new DwgHeaderWriter(stream, document, Encoding.Default);
writer.Write();

IDwgStreamReader sreader = DwgStreamReaderBase.GetStreamHandler(version, stream, resetPositon: true);
Expand Down
3 changes: 2 additions & 1 deletion src/ACadSharp.Tests/Internal/DwgObjectWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -121,7 +122,7 @@ private DwgDocumentBuilder writeInfo(CadDocument docToWrite)
{
Stream stream = new MemoryStream();

DwgObjectWriter writer = new DwgObjectWriter(stream, docToWrite);
DwgObjectWriter writer = new DwgObjectWriter(stream, docToWrite, Encoding.Default);
writer.OnNotification += onNotification;
writer.Write();

Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp.Tests/Objects/CadDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ACadSharp.Tests.Objects
{
public class CadDictionaryTests
public class CadDictionaryTests : NonGraphicalObjectTests<CadDictionary>
{
[Fact]
public void AvoidDuplicatedEntries()
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp.Tests/Objects/LayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ACadSharp.Tests.Objects
{
public class LayoutTests
public class LayoutTests: NonGraphicalObjectTests<Layout>
{
[Fact]
public void AddLayout()
Expand Down
20 changes: 20 additions & 0 deletions src/ACadSharp.Tests/Objects/NonGraphicalObjectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ACadSharp.Objects;
using System;
using Xunit;

namespace ACadSharp.Tests.Objects
{
public abstract class NonGraphicalObjectTests<T>
where T : NonGraphicalObject
{
[Fact]
public void InitName()
{
string name = "custom_name";
T obj = (T)Activator.CreateInstance(typeof(T), name);

Assert.NotNull(obj.Name);
Assert.Equal(name, obj.Name);
}
}
}
3 changes: 3 additions & 0 deletions src/ACadSharp.Tests/TestVariables.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using CSUtilities;
using System;
using System.IO;

namespace ACadSharp.Tests
{
public static class TestVariables
{
public static string DesktopFolder { get { return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } }

public static string SamplesFolder { get { return EnvironmentVars.Get<string>("SAMPLES_FOLDER"); } }

public static string OutputSamplesFolder { get { return EnvironmentVars.Get<string>("OUTPUT_SAMPLES_FOLDER"); } }
Expand Down
10 changes: 10 additions & 0 deletions src/ACadSharp/IO/CadWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
using System;
using System.IO;
using ACadSharp.Classes;
using ACadSharp.IO;
using ACadSharp.Exceptions;
using ACadSharp.IO.DWG;
using ACadSharp.IO.DWG.DwgStreamWriters;
using CSUtilities.IO;
using System.Collections.Generic;

namespace ACadSharp.IO
{
Expand All @@ -29,6 +35,8 @@ public abstract class CadWriterBase<T> : ICadWriter

protected CadDocument _document;

protected Encoding _encoding;

protected CadWriterBase(Stream stream, CadDocument document)
{
this._stream = stream;
Expand All @@ -39,6 +47,8 @@ protected CadWriterBase(Stream stream, CadDocument document)
public virtual void Write()
{
DxfClassCollection.UpdateDxfClasses(_document);

this._encoding = this.getListedEncoding(this._document.Header.CodePage);
}

/// <inheritdoc/>
Expand Down
6 changes: 3 additions & 3 deletions src/ACadSharp/IO/DWG/DwgReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ private DxfClassCollection readClasses()

IDwgStreamReader sreader = this.getSectionStream(DwgSectionDefinition.Classes);

var reader = new DwgClassesReader(this._fileHeader.AcadVersion, this._fileHeader);
var reader = new DwgClassesReader(this._fileHeader.AcadVersion, sreader, this._fileHeader);
reader.OnNotification += onNotificationEvent;

return reader.Read(sreader);
return reader.Read();
}

private void readAppInfo()
Expand Down Expand Up @@ -340,7 +340,7 @@ private Dictionary<ulong, long> readHandles()

IDwgStreamReader sreader = this.getSectionStream(DwgSectionDefinition.Handles);

var handleReader = new DwgHandleReader(sreader, this._fileHeader.AcadVersion);
var handleReader = new DwgHandleReader(this._fileHeader.AcadVersion, sreader);
handleReader.OnNotification += onNotificationEvent;

return handleReader.Read();
Expand Down
83 changes: 43 additions & 40 deletions src/ACadSharp/IO/DWG/DwgStreamReaders/DwgClassesReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,92 @@ internal class DwgClassesReader : DwgSectionIO

private DwgFileHeader _fileHeader;

public DwgClassesReader(ACadVersion version, DwgFileHeader fileHeader) : base(version)
private IDwgStreamReader _sreader;

public DwgClassesReader(ACadVersion version, IDwgStreamReader sreader, DwgFileHeader fileHeader) : base(version)
{
this._sreader = sreader;
this._fileHeader = fileHeader;
}

public DxfClassCollection Read(IDwgStreamReader sreader)
public DxfClassCollection Read()
{
DxfClassCollection classes = new DxfClassCollection();

//SN : 0x8D 0xA1 0xC4 0xB8 0xC4 0xA9 0xF8 0xC5 0xC0 0xDC 0xF4 0x5F 0xE7 0xCF 0xB6 0x8A
this.checkSentinel(sreader, DwgSectionDefinition.StartSentinels[SectionName]);
this.checkSentinel(this._sreader, DwgSectionDefinition.StartSentinels[this.SectionName]);

//RL : size of class data area
long size = sreader.ReadRawLong();
long endSection = sreader.Position + size;
long size = this._sreader.ReadRawLong();
long endSection = this._sreader.Position + size;

//R2010+ (only present if the maintenance version is greater than 3!)
if (this._fileHeader.AcadVersion >= ACadVersion.AC1024
&& this._fileHeader.AcadMaintenanceVersion > 3
|| this._fileHeader.AcadVersion > ACadVersion.AC1027)
{
//RL : unknown, possibly the high 32 bits of a 64-bit size?
long unknown = sreader.ReadRawLong();
long unknown = this._sreader.ReadRawLong();
}

long flagPos = 0;
//+R2007 Only:
if (R2007Plus)
if (this.R2007Plus)
{
//Setup readers
flagPos = sreader.PositionInBits() + sreader.ReadRawLong() - 1L;
long savedOffset = sreader.PositionInBits();
endSection = sreader.SetPositionByFlag(flagPos);
flagPos = this._sreader.PositionInBits() + this._sreader.ReadRawLong() - 1L;
long savedOffset = this._sreader.PositionInBits();
endSection = this._sreader.SetPositionByFlag(flagPos);

sreader.SetPositionInBits(savedOffset);
this._sreader.SetPositionInBits(savedOffset);

//Setup the text reader for versions 2007 and above
IDwgStreamReader textReader = DwgStreamReaderBase.GetStreamHandler(_version,
IDwgStreamReader textReader = DwgStreamReaderBase.GetStreamHandler(this._version,
//Create a copy of the stream
new StreamIO(sreader.Stream, true).Stream);
new StreamIO(this._sreader.Stream, true).Stream);
//Set the position and use the flag
textReader.SetPositionInBits(endSection);

sreader = new DwgMergedReader(sreader, textReader, null);
this._sreader = new DwgMergedReader(this._sreader, textReader, null);

//BL: 0x00
sreader.ReadBitLong();
this._sreader.ReadBitLong();
//B : flag - to find the data string at the end of the section
sreader.ReadBit();
this._sreader.ReadBit();
}

if (this._fileHeader.AcadVersion == ACadVersion.AC1018)
{
//BS : Maxiumum class number
sreader.ReadBitShort();
this._sreader.ReadBitShort();
//RC: 0x00
sreader.ReadRawChar();
this._sreader.ReadRawChar();
//RC: 0x00
sreader.ReadRawChar();
this._sreader.ReadRawChar();
//B : true
sreader.ReadBit();
this._sreader.ReadBit();
}

//We read sets of these until we exhaust the data.
while (getCurrPos(sreader) < endSection)
while (this.getCurrPos(this._sreader) < endSection)
{
DxfClass dxfClass = new DxfClass();
//BS : classnum
dxfClass.ClassNumber = sreader.ReadBitShort();
dxfClass.ClassNumber = this._sreader.ReadBitShort();
//BS : version – in R14, becomes a flag indicating whether objects can be moved, edited, etc.
dxfClass.ProxyFlags = (ProxyFlags)sreader.ReadBitShort();
dxfClass.ProxyFlags = (ProxyFlags)this._sreader.ReadBitShort();

//TV : appname
dxfClass.ApplicationName = sreader.ReadVariableText();
dxfClass.ApplicationName = this._sreader.ReadVariableText();
//TV: cplusplusclassname
dxfClass.CppClassName = sreader.ReadVariableText();
dxfClass.CppClassName = this._sreader.ReadVariableText();
//TV : classdxfname
dxfClass.DxfName = sreader.ReadVariableText();
dxfClass.DxfName = this._sreader.ReadVariableText();

//B : wasazombie
dxfClass.WasZombie = sreader.ReadBit();
dxfClass.WasZombie = this._sreader.ReadBit();
//BS : itemclassid -- 0x1F2 for classes which produce entities, 0x1F3 for classes which produce objects.
dxfClass.ItemClassId = sreader.ReadBitShort();
dxfClass.ItemClassId = this._sreader.ReadBitShort();
if (dxfClass.ItemClassId == 0x1F2)
{
dxfClass.IsAnEntity = true;
Expand All @@ -108,49 +111,49 @@ public DxfClassCollection Read(IDwgStreamReader sreader)
if (this._fileHeader.AcadVersion >= ACadVersion.AC1018)
{
//BL : Number of objects created of this type in the current DB(DXF 91).
dxfClass.InstanceCount = sreader.ReadBitLong();
dxfClass.InstanceCount = this._sreader.ReadBitLong();

if (this._fileHeader.AcadVersion == ACadVersion.AC1018)
{
//BS : Dwg Version
dxfClass.DwgVersion = (ACadVersion)sreader.ReadBitShort();
dxfClass.DwgVersion = (ACadVersion)this._sreader.ReadBitShort();
//BS : Maintenance release version.
dxfClass.MaintenanceVersion = sreader.ReadBitShort();
dxfClass.MaintenanceVersion = this._sreader.ReadBitShort();
}
else if (this._fileHeader.AcadVersion > ACadVersion.AC1018)
{
//BS : Dwg Version
dxfClass.DwgVersion = (ACadVersion)sreader.ReadBitLong();
dxfClass.DwgVersion = (ACadVersion)this._sreader.ReadBitLong();
//BS : Maintenance release version.
dxfClass.MaintenanceVersion = (short)sreader.ReadBitLong();
dxfClass.MaintenanceVersion = (short)this._sreader.ReadBitLong();
}

//BL : Unknown(normally 0L)
sreader.ReadBitLong();
this._sreader.ReadBitLong();
//BL : Unknown(normally 0L)
sreader.ReadBitLong();
this._sreader.ReadBitLong();
}

classes.AddOrUpdate(dxfClass);
}

if (R2007Plus)
if (this.R2007Plus)
{
sreader.SetPositionInBits(flagPos + 1);
this._sreader.SetPositionInBits(flagPos + 1);
}

//RS: CRC
sreader.ResetShift();
this._sreader.ResetShift();

//0x72,0x5E,0x3B,0x47,0x3B,0x56,0x07,0x3A,0x3F,0x23,0x0B,0xA0,0x18,0x30,0x49,0x75
this.checkSentinel(sreader, DwgSectionDefinition.EndSentinels[SectionName]);
this.checkSentinel(this._sreader, DwgSectionDefinition.EndSentinels[this.SectionName]);

return classes;
}

private long getCurrPos(IDwgStreamReader sreader)
{
if (R2007Plus)
if (this.R2007Plus)
{
return sreader.PositionInBits();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/IO/DWG/DwgStreamReaders/DwgHandleReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class DwgHandleReader : DwgSectionIO

private IDwgStreamReader _sreader;

public DwgHandleReader(IDwgStreamReader sreader, ACadVersion version) : base(version)
public DwgHandleReader(ACadVersion version, IDwgStreamReader sreader) : base(version)
{
this._sreader = sreader;
}
Expand Down
Loading

0 comments on commit 80230f1

Please sign in to comment.