Skip to content
New issue

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

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

Already on GitHub? # to your account

Table operations #56

Merged
merged 32 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
77ec0dc
setup
DomCR Jul 30, 2022
9ce8fda
Merge remote-tracking branch 'origin/master' into table-operations
DomCR Sep 30, 2022
38dfe30
Merge remote-tracking branch 'origin/master' into table-operations
DomCR Oct 2, 2022
4587cfd
unignore test
DomCR Oct 2, 2022
1a19de5
Merge remote-tracking branch 'origin/master' into table-operations
DomCR Mar 28, 2023
6127c2a
Merge remote-tracking branch 'origin/master' into table-operations
DomCR Jun 11, 2023
cad3f6c
entity references
DomCR Jun 11, 2023
ba14c92
add and remove
DomCR Jun 12, 2023
3005d4d
autocad check
DomCR Jun 12, 2023
9ca9cb5
clones issue
DomCR Jun 12, 2023
e6c14b7
cleanup
DomCR Jun 12, 2023
7f424c0
reference removed
DomCR Jun 12, 2023
836a80a
factory revert
DomCR Jun 14, 2023
2cff6ad
merge
DomCR Sep 16, 2023
cda8c0f
dimension
DomCR Sep 16, 2023
807e23b
TextStyle
DomCR Sep 17, 2023
3f70f4c
table entry naming
DomCR Sep 17, 2023
a0af94b
layer
DomCR Sep 17, 2023
31f2638
build fix
DomCR Sep 17, 2023
e95e503
table builder
DomCR Sep 17, 2023
e69a5cd
merge with master
DomCR Sep 17, 2023
03bf46a
fix
DomCR Sep 17, 2023
0de3d59
merge
DomCR Sep 21, 2023
c09ebbb
build fix
DomCR Sep 21, 2023
de7c03d
RegisterTables
DomCR Sep 21, 2023
18e97f1
dwg reader dictionary fix
DomCR Sep 21, 2023
acf72d0
rollback add and remove
DomCR Sep 21, 2023
cf27894
naming
DomCR Sep 21, 2023
0d9c081
adding fix
DomCR Sep 21, 2023
ddde48a
internal test fix
DomCR Sep 21, 2023
4e540be
optional vport visualstyle
DomCR Oct 8, 2023
1374088
Merge remote-tracking branch 'origin/master' into table-operations
DomCR Oct 9, 2023
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
6 changes: 3 additions & 3 deletions ACadSharp.Tests/CadDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void Get0HandleObject()
Assert.Null(cadObject);
}

[Fact(Skip = "Implementation in branch : table-operations")]
[Fact]
public void RemoveLayer()
{
string layerName = "custom_layer";
Expand All @@ -199,7 +199,7 @@ public void RemoveLayer()
Assert.Equal(doc.Layers[Layer.DefaultName], line.Layer);
}

[Fact(Skip = "Implementation in branch : table-operations")]
[Fact]
public void RemoveLineType()
{
string ltypeName = "custom_ltype";
Expand All @@ -215,7 +215,7 @@ public void RemoveLineType()
Assert.False(doc.Layers.Contains(ltypeName));
Assert.Null(ltype.Document);
Assert.True(ltype.Handle == 0);
Assert.Equal(doc.LineTypes[LineType.ByBlockName], line.LineType);
Assert.Equal(doc.LineTypes[LineType.ByLayerName], line.LineType);
}

[Fact]
Expand Down
18 changes: 0 additions & 18 deletions ACadSharp.Tests/CadObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,5 @@ public void Clone(Type t)

CadObjectTestUtils.AssertClone(cadObject, clone);
}

[Theory(Skip = "Factory refactor needed")]
[MemberData(nameof(ACadTypes))]
public void CloneUnattachEvent(Type t)
{
CadObject cadObject = Factory.CreateObject(t);
cadObject.OnReferenceChanged += this.cadObject_OnReferenceChanged;

CadObject clone = (CadObject)cadObject.Clone();

CadObjectTestUtils.AssertClone(cadObject, clone);
}

private void cadObject_OnReferenceChanged(object sender, ReferenceChangedEventArgs e)
{
//The clone must not have any attachment
throw new InvalidOperationException();
}
}
}
3 changes: 2 additions & 1 deletion ACadSharp.Tests/Common/DocumentIntegrity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ public void AssertBlockRecords(CadDocument doc)
foreach (BlockRecord br in doc.BlockRecords)
{
Assert.Equal(br.Name, br.BlockEntity.Name);

Assert.NotNull(br.BlockEntity.Document);
this.documentObjectNotNull(doc, br.BlockEntity);

Assert.True(br.Handle == br.BlockEntity.Owner.Handle, "Block entity owner doesn't mach");

Assert.NotNull(br.BlockEnd.Document);
this.documentObjectNotNull(doc, br.BlockEnd);

foreach (Entity e in br.Entities)
Expand Down
18 changes: 0 additions & 18 deletions ACadSharp.Tests/Entities/EntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,5 @@ public void Clone(Type entityType)

CadObjectTestUtils.AssertEntityClone(entity, clone);
}

[Theory]
[MemberData(nameof(EntityTypes))]
public void CloneUnattachEvent(Type t)
{
Entity entity = EntityFactory.Create(t);
entity.OnReferenceChanged += this.entity_OnReferenceChanged;

Entity clone = (Entity)entity.Clone();

CadObjectTestUtils.AssertEntityClone(entity, clone);
}

private void entity_OnReferenceChanged(object sender, ReferenceChangedEventArgs e)
{
//The clone must not have any attachment
throw new InvalidOperationException();
}
}
}
13 changes: 8 additions & 5 deletions ACadSharp.Tests/IO/IOTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public abstract class IOTestsBase

protected const string _samplesOutFolder = "../../../../samples/out";

public static string AcCoreConsolePath { get; }

public static TheoryData<string> DwgFilePaths { get; }

public static TheoryData<string> DxfAsciiFiles { get; }
Expand All @@ -24,8 +26,6 @@ public abstract class IOTestsBase

public static TheoryData<ACadVersion> Versions { get; }

public static string AcCoreConsolePath { get; }

protected readonly DwgReaderConfiguration _dwgConfiguration = new DwgReaderConfiguration
{
Failsafe = false
Expand Down Expand Up @@ -78,7 +78,8 @@ static IOTestsBase()
Directory.CreateDirectory(_samplesOutFolder);
}

if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null
|| !TestVariables.AutocadConsoleCheck)
{
return;
}
Expand Down Expand Up @@ -138,7 +139,8 @@ protected void onNotification(object sender, NotificationEventArgs e)

protected void checkDxfDocumentInAutocad(string path)
{
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null
|| !TestVariables.AutocadConsoleCheck)
return;

System.Diagnostics.Process process = new System.Diagnostics.Process();
Expand Down Expand Up @@ -199,7 +201,8 @@ protected void checkDxfDocumentInAutocad(string path)

protected void checkDwgDocumentInAutocad(string path)
{
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null
|| !TestVariables.AutocadConsoleCheck)
return;

System.Diagnostics.Process process = new System.Diagnostics.Process();
Expand Down
34 changes: 34 additions & 0 deletions ACadSharp.Tests/Tables/BlockRecordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,39 @@ public void CloneTest()
CadObjectTestUtils.AssertEntityClone(recordEntities[i], cloneEntities[i]);
}
}

[Fact()]
public void CloneInDocumentTest()
{
string name = "my_block";
BlockRecord record = new BlockRecord(name);
CadDocument doc = new CadDocument();

doc.BlockRecords.Add(record);

Assert.NotNull(record.Document);
Assert.NotNull(record.BlockEntity.Document);
Assert.NotNull(record.BlockEnd.Document);
}

[Fact()]
public void CloneDetachDocumentTest()
{
string name = "my_block";
BlockRecord record = new BlockRecord(name);
CadDocument doc = new CadDocument();

doc.BlockRecords.Add(record);

BlockRecord clone = (BlockRecord)record.Clone();

Assert.Null(clone.Document);
Assert.Null(clone.BlockEntity.Document);
Assert.Null(clone.BlockEnd.Document);

Assert.NotNull(record.Document);
Assert.NotNull(record.BlockEntity.Document);
Assert.NotNull(record.BlockEnd.Document);
}
}
}
7 changes: 0 additions & 7 deletions ACadSharp.Tests/Tables/TableEntryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void Clone(Type entryType)
public void CloneUnattachEvent(Type t)
{
TableEntry entry = TableEntryFactory.Create(t);
entry.OnReferenceChanged += this.tableEntry_OnReferenceChanged;

TableEntry clone = (TableEntry)entry.Clone();

Expand All @@ -49,11 +48,5 @@ public void SetFlagUsingMapper()

Assert.True(layer.Flags.HasFlag(LayerFlags.Frozen));
}

private void tableEntry_OnReferenceChanged(object sender, ReferenceChangedEventArgs e)
{
//The clone must not have any attachment
throw new InvalidOperationException();
}
}
}
2 changes: 2 additions & 0 deletions ACadSharp.Tests/TestVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public static class TestVariables
public const double Delta = 0.00001d;

public const int DecimalPrecision = 5;

public const bool AutocadConsoleCheck = false;
}
}
103 changes: 14 additions & 89 deletions ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ public T GetCadObject<T>(ulong handle)
return null;
}

/// <summary>
/// Gets an object in the document by it's handle
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="handle"></param>
/// <param name="cadObject"></param>
/// <returns></returns>
public bool TryGetCadObject<T>(ulong handle, out T cadObject)
where T : CadObject
{
Expand All @@ -245,8 +252,6 @@ private void addCadObject(CadObject cadObject)
throw new ArgumentException($"The item with handle {cadObject.Handle} is already assigned to a document");
}

cadObject.Document = this;

if (cadObject.Handle == 0 || this._cadObjects.ContainsKey(cadObject.Handle))
{
var nextHandle = this._cadObjects.Keys.Max() + 1;
Expand All @@ -257,105 +262,25 @@ private void addCadObject(CadObject cadObject)
}

this._cadObjects.Add(cadObject.Handle, cadObject);
cadObject.OnReferenceChanged += this.onReferenceChanged;

if (cadObject.XDictionary != null)
this.RegisterCollection(cadObject.XDictionary);

if (cadObject is Entity e)
if (cadObject is BlockRecord record)
{
if (this.Layers.TryGetValue(e.Layer.Name, out Layer layer))
{
e.Layer = layer;
}
else
{
//Add the layer if it does not exist
this.Layers.Add(e.Layer);
}

if (this.LineTypes.TryGetValue(e.LineType.Name, out LineType lineType))
{
e.LineType = lineType;
}
else
{
//Add the LineType if it does not exist
this.LineTypes.Add(e.LineType);
}
this.addCadObject(record.BlockEntity);
this.addCadObject(record.BlockEnd);
}

switch (cadObject)
{
case BlockRecord record:
this.RegisterCollection(record.Entities);
this.addCadObject(record.BlockEnd);
this.addCadObject(record.BlockEntity);
break;
case Insert insert:
this.RegisterCollection(insert.Attributes);

//Should only be triggered for internal use
if (insert.Block == null)
break;

if (this.BlockRecords.TryGetValue(insert.Block.Name, out BlockRecord blk))
{
insert.Block = blk;
}
else
{
this.BlockRecords.Add(insert.Block);
}
break;
case Polyline pline:
this.RegisterCollection(pline.Vertices);
break;
}
cadObject.AssignDocument(this);
}

private void removeCadObject(CadObject cadObject)
{
if (!this.TryGetCadObject(cadObject.Handle, out CadObject obj) || !this._cadObjects.Remove(cadObject.Handle))
if (!this.TryGetCadObject(cadObject.Handle, out CadObject _)
|| !this._cadObjects.Remove(cadObject.Handle))
{
return;
}

cadObject.Handle = 0;
cadObject.Document = null;
cadObject.OnReferenceChanged -= this.onReferenceChanged;

if (cadObject.XDictionary != null)
this.UnregisterCollection(cadObject.XDictionary);

if (cadObject is Entity e)
{
//TODO: Replace for clones
e.Layer = new Layer(e.Layer.Name);
e.LineType = new LineType(e.LineType.Name);
}

switch (cadObject)
{
case BlockRecord record:
this.UnregisterCollection(record.Entities);
this.removeCadObject(record.BlockEnd);
this.removeCadObject(record.BlockEntity);
break;
case Insert insert:
insert.Block = (BlockRecord)insert.Block.Clone();
this.UnregisterCollection(insert.Attributes);
break;
case Polyline pline:
this.UnregisterCollection(pline.Vertices);
break;
}
}

private void onReferenceChanged(object sender, ReferenceChangedEventArgs e)
{
this.addCadObject(e.Current);
this.removeCadObject(e.Old);
cadObject.UnassignDocument();
}

private void onAdd(object sender, CollectionChangedEventArgs e)
Expand Down
Loading