Skip to content

Commit

Permalink
Merge pull request #183 from DomCR/Issue-182_DwgReader-assign-rootDic…
Browse files Browse the repository at this point in the history
…tionary

Issue 182 dwg reader assign root dictionary
  • Loading branch information
DomCR authored Oct 16, 2023
2 parents d7c4cb5 + 686e344 commit 052746d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal set
/// </summary>
public BlockRecord PaperSpace { get { return this.BlockRecords[BlockRecord.PaperSpaceName]; } }

private CadDictionary _rootDictionary = new CadDictionary();
private CadDictionary _rootDictionary = null;

//Contains all the objects in the document
private readonly Dictionary<ulong, IHandledCadObject> _cadObjects = new Dictionary<ulong, IHandledCadObject>();
Expand Down
4 changes: 3 additions & 1 deletion ACadSharp/IO/DWG/DwgHeaderHandlesCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ internal class DwgHeaderHandlesCollection

public ulong? GetHandle(string name)
{
return _handles[name];
this._handles.TryGetValue(name, out var handle);
return handle;
}

public void SetHandle(string name, ulong? value)
{
_handles[name] = value;
Expand Down
38 changes: 35 additions & 3 deletions ACadSharp/IO/Templates/CadDictionaryTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ACadSharp.Objects;
using ACadSharp.IO.DWG;
using ACadSharp.Objects;
using System.Collections.Generic;
using System.Linq;

namespace ACadSharp.IO.Templates
{
Expand All @@ -15,9 +17,39 @@ public override void Build(CadDocumentBuilder builder)
{
base.Build(builder);

if (this.OwnerHandle.HasValue && this.OwnerHandle == 0)
if (this.OwnerHandle.HasValue
&& this.OwnerHandle == 0
&& builder.DocumentToBuild.RootDictionary == null)
{
builder.DocumentToBuild.RootDictionary = this.CadObject;
if (builder is DwgDocumentBuilder dwgBuilder)
{
//There is no way to find the root dictionary, dwg does not provide an explicit handle for it
//the only way to check for the root dictionary is to try to find the handles that belong to it
List<ulong?> rootHandles = new() {
dwgBuilder.HeaderHandles.DICTIONARY_ACAD_GROUP,
dwgBuilder.HeaderHandles.DICTIONARY_ACAD_MLINESTYLE,
dwgBuilder.HeaderHandles.DICTIONARY_COLORS,
dwgBuilder.HeaderHandles.DICTIONARY_LAYOUTS,
dwgBuilder.HeaderHandles.DICTIONARY_MATERIALS,
dwgBuilder.HeaderHandles.DICTIONARY_NAMED_OBJECTS,
dwgBuilder.HeaderHandles.DICTIONARY_PLOTSETTINGS,
dwgBuilder.HeaderHandles.DICTIONARY_PLOTSTYLES,
dwgBuilder.HeaderHandles.DICTIONARY_VISUALSTYLE,
};

foreach (ulong handle in rootHandles.Where(h => h.HasValue).Select(v => (ulong)v))
{
if (this.Entries.ContainsValue(handle))
{
builder.DocumentToBuild.RootDictionary = this.CadObject;
break;
}
}
}
else
{
builder.DocumentToBuild.RootDictionary = this.CadObject;
}
}

foreach (var item in this.Entries)
Expand Down
15 changes: 14 additions & 1 deletion ACadSharp/IO/Templates/CadTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,20 @@ public virtual void Build(CadDocumentBuilder builder)
foreach (ulong handle in this.ReactorsHandles)
{
if (builder.TryGetCadObject(handle, out CadObject reactor))
this.CadObject.Reactors.Add(handle, reactor);
{
if (this.CadObject.Reactors.ContainsKey(handle))
{
builder.Notify($"Reactor with handle {handle} already exist in the object {this.CadObject.Handle}", NotificationType.Warning);
}
else
{
this.CadObject.Reactors.Add(handle, reactor);
}
}
else
{
builder.Notify($"Reactor with handle {handle} not found", NotificationType.Warning);
}
}

foreach (KeyValuePair<ulong, ExtendedData> item in this.EDataTemplate)
Expand Down

0 comments on commit 052746d

Please sign in to comment.