Skip to content

Commit e0bc2de

Browse files
authored
Merge pull request #576 from DomCR/issue-575_write-group
Write Group
2 parents 3cc639c + 8df396f commit e0bc2de

File tree

14 files changed

+286
-44
lines changed

14 files changed

+286
-44
lines changed

src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,8 @@ public void CreateGroup()
672672

673673
Group group = new Group();
674674
group.Name = "MyGroup";
675-
group.Entities.Add(circle.Handle, circle);
676-
circle.AddReactor(group);
677-
group.Entities.Add(line.Handle, line);
678-
line.AddReactor(group);
679-
group.Selectable = true;
675+
group.Add( circle);
676+
group.Add(line);
680677

681678
this.Document.Groups.Add(group);
682679

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using ACadSharp.Entities;
2+
using ACadSharp.Objects;
3+
using System;
4+
using Xunit;
5+
6+
namespace ACadSharp.Tests.Objects
7+
{
8+
public class GroupTests : NonGraphicalObjectTests<Group>
9+
{
10+
[Fact]
11+
public void AddTest()
12+
{
13+
CadDocument doc = new CadDocument();
14+
Line l = new Line();
15+
var group = new Group();
16+
17+
group.Add(l);
18+
19+
Assert.Contains(group, l.Reactors);
20+
Assert.Throws<InvalidOperationException>(() => doc.Groups.Add(group));
21+
22+
group.Clear();
23+
24+
Assert.DoesNotContain(group, l.Reactors);
25+
26+
doc.Groups.Add(group);
27+
Assert.Contains(group, doc.Groups);
28+
29+
Assert.Throws<InvalidOperationException>(() => group.Add(l));
30+
31+
doc.Entities.Add(l);
32+
group.Add(l);
33+
34+
Assert.Contains(l, group.Entities);
35+
}
36+
37+
[Fact]
38+
public void IsUnnamedTest()
39+
{
40+
var group = new Group();
41+
42+
Assert.True(group.IsUnnamed);
43+
group.Name = null;
44+
Assert.True(group.IsUnnamed);
45+
group.Name = " ";
46+
Assert.True(group.IsUnnamed);
47+
48+
group.Name = "my_group";
49+
Assert.False(group.IsUnnamed);
50+
}
51+
}
52+
}

src/ACadSharp/CadDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public bool TryGetCadObject<T>(ulong handle, out T cadObject)
250250
}
251251

252252
/// <summary>
253-
///
253+
/// Create the default entries and objects for the <see cref="CadDocument"/>.
254254
/// </summary>
255255
public void CreateDefaults()
256256
{

src/ACadSharp/CadObject.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ public IEnumerable<CadObject> Reactors
5959
{
6060
get
6161
{
62-
if (this.Document == null)
63-
{
64-
return Enumerable.Empty<CadObject>();
65-
}
66-
else
67-
{
68-
return this._reactors;
69-
}
62+
return this._reactors;
7063
}
7164
}
7265

@@ -174,10 +167,20 @@ public void CleanReactors()
174167
/// <remarks>
175168
/// The <see cref="CadObject"/> and its reactors must be in the same <see cref="CadDocument"/> to be valid.
176169
/// </remarks>
177-
/// <param name="cadObject"></param>
178-
public void AddReactor(CadObject cadObject)
170+
/// <param name="reactor"></param>
171+
public void AddReactor(CadObject reactor)
172+
{
173+
this._reactors.Add(reactor);
174+
}
175+
176+
/// <summary>
177+
/// Remove a reactor linked to this object.
178+
/// </summary>
179+
/// <param name="reactor"></param>
180+
/// <returns></returns>
181+
public bool RemoveReactor(CadObject reactor)
179182
{
180-
this._reactors.Add(cadObject);
183+
return this._reactors.Remove(reactor);
181184
}
182185

183186
internal virtual void AssignDocument(CadDocument doc)

src/ACadSharp/DxfFileToken.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public static class DxfFileToken
131131
public const string ObjectBlockVisibilityParameter = "BLOCKVISIBILITYPARAMETER";
132132
public const string ObjectTableContent = "TABLECONTENT";
133133
public const string ObjectGeoData = "GEODATA";
134+
public const string ObjectGroup = "GROUP";
134135

135136
#endregion
136137
}

src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5015,15 +5015,15 @@ private CadTemplate readGeoData()
50155015
private CadTemplate readGroup()
50165016
{
50175017
Group group = new Group();
5018-
DwgGroupTemplate template = new DwgGroupTemplate(group);
5018+
CadGroupTemplate template = new CadGroupTemplate(group);
50195019

50205020
this.readCommonNonEntityData(template);
50215021

50225022
//Str TV name of group
50235023
group.Description = this._textReader.ReadVariableText();
50245024

50255025
//Unnamed BS 1 if group has no name
5026-
group.IsUnnamed = this._objectReader.ReadBitShort() > 0;
5026+
bool isUnnamed = this._objectReader.ReadBitShort() > 0;
50275027
//Selectable BS 1 if group selectable
50285028
group.Selectable = this._objectReader.ReadBitShort() > 0;
50295029

src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Objects.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ private void writeGroup(Group group)
356356
this._writer.WriteBitShort((short)(group.Selectable ? 1 : 0));
357357

358358
//Numhandles BL # objhandles in this group
359-
this._writer.WriteBitLong(group.Entities.Count);
360-
foreach (ulong h in group.Entities.Keys)
359+
this._writer.WriteBitLong(group.Entities.Count());
360+
foreach (var e in group.Entities)
361361
{
362362
//the entries in the group(hard pointer)
363-
this._writer.HandleReference(DwgReferenceType.HardPointer, h);
363+
this._writer.HandleReference(DwgReferenceType.HardPointer, e);
364364
}
365365
}
366366

src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ private CadTemplate readObject()
7070
return this.readObjectCodes<PdfUnderlayDefinition>(new CadNonGraphicalObjectTemplate(new PdfUnderlayDefinition()), this.readObjectSubclassMap);
7171
case DxfFileToken.ObjectSortEntsTable:
7272
return this.readSortentsTable();
73+
case DxfFileToken.ObjectGroup:
74+
return this.readObjectCodes<Group>(new CadGroupTemplate(), this.readGroup);
7375
case DxfFileToken.ObjectGeoData:
7476
return this.readObjectCodes<GeoData>(new CadGeoDataTemplate(), this.readGeoData);
7577
case DxfFileToken.ObjectScale:
@@ -236,6 +238,22 @@ private bool readLayout(CadTemplate template, DxfMap map)
236238
}
237239
}
238240

241+
private bool readGroup(CadTemplate template, DxfMap map)
242+
{
243+
CadGroupTemplate tmp = template as CadGroupTemplate;
244+
245+
switch (this._reader.Code)
246+
{
247+
case 70:
248+
return true;
249+
case 340:
250+
tmp.Handles.Add(this._reader.ValueAsHandle);
251+
return true;
252+
default:
253+
return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[template.CadObject.SubclassMarker]);
254+
}
255+
}
256+
239257
private bool readGeoData(CadTemplate template, DxfMap map)
240258
{
241259
CadGeoDataTemplate tmp = template as CadGeoDataTemplate;

src/ACadSharp/IO/DXF/DxfStreamWriter/DxfObjectsSectionWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ protected void writeGroup(Group group)
362362
this._writer.Write(70, group.IsUnnamed ? (short)1 : (short)0);
363363
this._writer.Write(71, group.Selectable ? (short)1 : (short)0);
364364

365-
foreach (Entity entity in group.Entities.Values)
365+
foreach (Entity entity in group.Entities)
366366
{
367367
this._writer.WriteHandle(340, entity);
368368
}

src/ACadSharp/IO/Templates/DwgGroupTemplate.cs renamed to src/ACadSharp/IO/Templates/CadGroupTemplate.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace ACadSharp.IO.Templates
66
{
7-
internal class DwgGroupTemplate : CadTemplate<Group>
7+
internal class CadGroupTemplate : CadTemplate<Group>
88
{
99
public List<ulong> Handles { get; set; } = new List<ulong>();
1010

11-
public DwgGroupTemplate(Group group) : base(group) { }
11+
public CadGroupTemplate() : base(new Group()) { }
12+
13+
public CadGroupTemplate(Group group) : base(group) { }
1214

1315
public override void Build(CadDocumentBuilder builder)
1416
{
@@ -18,7 +20,7 @@ public override void Build(CadDocumentBuilder builder)
1820
{
1921
if (builder.TryGetCadObject<Entity>(handle, out Entity e))
2022
{
21-
this.CadObject.Entities.Add(handle, e);
23+
this.CadObject.Add(e);
2224
}
2325
else
2426
{

0 commit comments

Comments
 (0)