Skip to content

Commit 53f02d8

Browse files
authored
Merge pull request DomCR#753 from DomCR/CadDictionary-variables
AcDbVariableDictionary
2 parents 0b88701 + 9765d95 commit 53f02d8

34 files changed

+423
-292
lines changed

src/ACadSharp.Tests/CadDocumentTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ACadSharp.Blocks;
88
using System.Linq;
99
using System.Diagnostics;
10+
using ACadSharp.Objects;
1011

1112
namespace ACadSharp.Tests
1213
{
@@ -244,6 +245,36 @@ public void Get0HandleObject()
244245
Assert.Null(cadObject);
245246
}
246247

248+
[Fact]
249+
public void GetCurrentTest()
250+
{
251+
CadDocument doc = new CadDocument();
252+
253+
Layer layer = doc.GetCurrent<Layer>();
254+
Assert.NotNull(layer);
255+
Assert.Equal(Layer.DefaultName, layer.Name);
256+
257+
LineType lineType = doc.GetCurrent<LineType>();
258+
Assert.NotNull(lineType);
259+
Assert.Equal(LineType.ByLayerName, lineType.Name);
260+
261+
TextStyle textStyle = doc.GetCurrent<TextStyle>();
262+
Assert.NotNull(textStyle);
263+
Assert.Equal(TextStyle.DefaultName, textStyle.Name);
264+
265+
DimensionStyle dimStyle = doc.GetCurrent<DimensionStyle>();
266+
Assert.NotNull(dimStyle);
267+
Assert.Equal(DimensionStyle.DefaultName, dimStyle.Name);
268+
269+
MLineStyle mlineStyle = doc.GetCurrent<MLineStyle>();
270+
Assert.NotNull(mlineStyle);
271+
Assert.Equal(MLineStyle.DefaultName, mlineStyle.Name);
272+
273+
MultiLeaderStyle multiLeaderStyle = doc.GetCurrent<MultiLeaderStyle>();
274+
Assert.NotNull(multiLeaderStyle);
275+
Assert.Equal(MultiLeaderStyle.DefaultName, multiLeaderStyle.Name);
276+
}
277+
247278
[Fact]
248279
public void NotAllowDuplicate()
249280
{
@@ -337,5 +368,40 @@ public void RestoreHandlesTest()
337368
Assert.Equal(line.Handle, l.Handle);
338369
Assert.True(line.Handle < bigHandle);
339370
}
371+
372+
[Fact]
373+
public void SetCurrentTest()
374+
{
375+
CadDocument doc = new CadDocument();
376+
377+
string layerName = "my_layer";
378+
doc.SetCurrent(new Layer(layerName));
379+
Assert.True(doc.Layers.Contains(layerName));
380+
Assert.Equal(layerName, doc.Header.CurrentLayerName);
381+
382+
string lineTypeName = "my_linetype";
383+
doc.SetCurrent(new LineType(lineTypeName));
384+
Assert.True(doc.LineTypes.Contains(lineTypeName));
385+
Assert.Equal(lineTypeName, doc.Header.CurrentLineTypeName);
386+
387+
string textStyleName = "my_textstyle";
388+
doc.SetCurrent(new TextStyle(textStyleName));
389+
Assert.True(doc.TextStyles.Contains(textStyleName));
390+
Assert.Equal(textStyleName, doc.Header.CurrentTextStyleName);
391+
392+
string dimStyleName = "my_dimstyle";
393+
doc.SetCurrent(new DimensionStyle(dimStyleName));
394+
Assert.True(doc.DimensionStyles.Contains(dimStyleName));
395+
Assert.Equal(dimStyleName, doc.Header.CurrentDimensionStyleName);
396+
397+
string mlineStyleName = "my_mlinestyle";
398+
doc.SetCurrent(new MLineStyle(mlineStyleName));
399+
Assert.True(doc.MLineStyles.ContainsKey(mlineStyleName));
400+
Assert.Equal(mlineStyleName, doc.Header.CurrentMLineStyleName);
401+
402+
string multiLeaderStyleName = "my_multileaderstyle";
403+
doc.SetCurrent(new MultiLeaderStyle(multiLeaderStyleName));
404+
Assert.True(doc.MLeaderStyles.ContainsKey(multiLeaderStyleName));
405+
}
340406
}
341407
}

src/ACadSharp.Tests/Header/CadHeaderTests.cs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,10 @@ public void CadHeaderCurrentCurrentTextStyleTest()
3939
CadHeader header = new CadHeader(document);
4040

4141
Assert.NotNull(header.CurrentTextStyle);
42-
Assert.True(header.CurrentTextStyle.Name == header.TextStyleName, "Name does not match");
42+
Assert.True(header.CurrentTextStyle.Name == header.CurrentTextStyleName, "Name does not match");
4343
Assert.True(header.CurrentTextStyle.Name == TextStyle.DefaultName, "Name does not match");
44-
Assert.True(header.CurrentTextStyle.Handle == document.TextStyles[header.TextStyleName].Handle, "Handle does not match");
44+
Assert.True(header.CurrentTextStyle.Handle == document.TextStyles[header.CurrentTextStyleName].Handle, "Handle does not match");
4545
Assert.Equal(document.TextStyles, header.CurrentTextStyle.Owner);
4646
}
47-
48-
[Fact]
49-
public void CadHeaderCurrentCurrentMultiLeaderStyleTest()
50-
{
51-
CadDocument document = new CadDocument();
52-
CadHeader header = new CadHeader(document);
53-
54-
Assert.NotNull(header.CurrentMultiLeaderStyle);
55-
Assert.True(header.CurrentMultiLeaderStyle.Name == header.CurrentMultiLeaderStyleName, "Name does not match");
56-
Assert.True(header.CurrentMultiLeaderStyle.Name == MultiLeaderStyle.DefaultName, "Name does not match");
57-
Assert.True(header.CurrentMultiLeaderStyle.Handle == document.MLeaderStyles[header.CurrentMultiLeaderStyleName].Handle, "Handle does not match");
58-
Assert.Equal(document.MLeaderStyles, header.CurrentMultiLeaderStyle.Owner);
59-
60-
Assert.True(((CadDictionary)document.RootDictionary[CadDictionary.VariableDictionary]).TryGetEntry(CadDictionary.CurrentMultiLeaderStyle, out DictionaryVariable currentMultiLeaderStyleNameVariable), "Dictionary variable for CMLEADERSTYLE does not exist.");
61-
Assert.True(currentMultiLeaderStyleNameVariable.Value == MultiLeaderStyle.DefaultName, "Dictionary variable CMLEADERSTYLE='standard' does not match.");
62-
63-
string multiLeaderStyleName = "Test";
64-
document.MLeaderStyles.Add(new MultiLeaderStyle(multiLeaderStyleName));
65-
header.CurrentMultiLeaderStyleName = multiLeaderStyleName;
66-
Assert.True(document.MLeaderStyles.TryGetValue(multiLeaderStyleName, out MultiLeaderStyle multiLeaderStyle), "MultiLeaderStyle for new name 'Test' not created.");
67-
Assert.True(header.CurrentMultiLeaderStyle.Name == multiLeaderStyleName, "MultiLeaderStyle for new name 'Test' not set.");
68-
69-
Assert.True(currentMultiLeaderStyleNameVariable.Value == multiLeaderStyleName, "Dictionary variable CMLEADERSTYLE='Test' does not match.");
70-
71-
currentMultiLeaderStyleNameVariable.Value = MultiLeaderStyle.DefaultName;
72-
Assert.True(header.CurrentMultiLeaderStyleName == MultiLeaderStyle.DefaultName, "CurrentMultiLeaderStyleName for new name 'Standard' doe not match.");
73-
Assert.True(header.CurrentMultiLeaderStyle.Name == MultiLeaderStyle.DefaultName, "CurrentMultiLeaderStyle for new name 'Standard' not set.");
74-
}
7547
}
7648
}

src/ACadSharp.Tests/IO/CadReaderTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public virtual void AssertDocumentHeader(FileModel test)
9999
Assert.Equal(LineType.ByLayerName, header.CurrentLineTypeName, ignoreCase: true);
100100

101101
Assert.Equal(doc.TextStyles[TextStyle.DefaultName], header.CurrentTextStyle);
102-
Assert.Equal(TextStyle.DefaultName, header.TextStyleName, ignoreCase: true);
102+
Assert.Equal(TextStyle.DefaultName, header.CurrentTextStyleName, ignoreCase: true);
103103
}
104104

105105
public void Dispose()

src/ACadSharp/CadDocument.cs

Lines changed: 123 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public class CadDocument : IHandledCadObject
3939
/// </remarks>
4040
public ColorCollection Colors { get; private set; }
4141

42+
/// <summary>
43+
/// The collection of the system variables in the drawing.
44+
/// </summary>
45+
/// <remarks>
46+
/// The collection is null if the <see cref="CadDictionary.VariableDictionary"/> doesn't exist in the root dictionary.
47+
/// </remarks>
48+
public DictionaryVariableCollection DictionaryVariables { get; private set; }
49+
4250
/// <summary>
4351
/// The collection of all dimension styles in the drawing.
4452
/// </summary>
@@ -75,14 +83,6 @@ public class CadDocument : IHandledCadObject
7583
/// </remarks>
7684
public ImageDefinitionCollection ImageDefinitions { get; private set; }
7785

78-
/// <summary>
79-
/// The collection of all images in the drawing.
80-
/// </summary>
81-
/// <remarks>
82-
/// The collection is null if the <see cref="CadDictionary.AcadImageDict"/> doesn't exist in the root dictionary.
83-
/// </remarks>
84-
public PdfDefinitionCollection PdfDefinitions { get; private set; }
85-
8686
/// <summary>
8787
/// The collection of all layers in the drawing.
8888
/// </summary>
@@ -127,6 +127,14 @@ public class CadDocument : IHandledCadObject
127127
/// </summary>
128128
public BlockRecord PaperSpace { get { return this.BlockRecords[BlockRecord.PaperSpaceName]; } }
129129

130+
/// <summary>
131+
/// The collection of all images in the drawing.
132+
/// </summary>
133+
/// <remarks>
134+
/// The collection is null if the <see cref="CadDictionary.AcadImageDict"/> doesn't exist in the root dictionary.
135+
/// </remarks>
136+
public PdfDefinitionCollection PdfDefinitions { get; private set; }
137+
130138
/// <summary>
131139
/// Root dictionary of the document.
132140
/// </summary>
@@ -206,28 +214,6 @@ internal CadDocument(bool createDefaults)
206214
}
207215
}
208216

209-
/// <summary>
210-
/// Updates the <see cref="DxfClass"/> in the document and their instance count.
211-
/// </summary>
212-
/// <param name="reset">Resets the list and clears any unnecessary classes.</param>
213-
public void UpdateDxfClasses(bool reset)
214-
{
215-
if (reset)
216-
{
217-
this.Classes.Clear();
218-
}
219-
220-
DxfClassCollection.UpdateDxfClasses(this);
221-
222-
foreach (var item in this.Classes)
223-
{
224-
item.InstanceCount = this._cadObjects.Values
225-
.OfType<CadObject>()
226-
.Where(c => c.ObjectName == item.DxfName)
227-
.Count();
228-
}
229-
}
230-
231217
/// <summary>
232218
/// Create the default entries and objects for the <see cref="CadDocument"/>.
233219
/// </summary>
@@ -317,6 +303,42 @@ public T GetCadObject<T>(ulong handle)
317303
return null;
318304
}
319305

306+
/// <summary>
307+
/// Retrieves the current object of the specified type from the document's configuration.
308+
/// </summary>
309+
/// <typeparam name="T">The type of the object to retrieve. Must be a type that implements <see cref="CadObject"/> and <see
310+
/// cref="INamedCadObject"/>.</typeparam>
311+
/// <returns>The current object of the specified type, or throws an exception if the type is not supported.</returns>
312+
/// <exception cref="NotSupportedException">Thrown if the specified type <typeparamref name="T"/> is not a configurable type in the document.</exception>
313+
public T GetCurrent<T>()
314+
where T : CadObject, INamedCadObject
315+
{
316+
switch (typeof(T))
317+
{
318+
case Type t when t.Equals(typeof(Layer)):
319+
return this.Header.CurrentLayer as T;
320+
case Type t when t.Equals(typeof(LineType)):
321+
return this.Header.CurrentLineType as T;
322+
case Type t when t.Equals(typeof(TextStyle)):
323+
return this.Header.CurrentTextStyle as T;
324+
case Type t when t.Equals(typeof(DimensionStyle)):
325+
return this.Header.CurrentDimensionStyle as T;
326+
case Type t when t.Equals(typeof(MLineStyle)):
327+
return this.Header.CurrentMLineStyle as T;
328+
case Type t when t.Equals(typeof(MultiLeaderStyle)):
329+
if (this.DictionaryVariables.TryGetValue(DictionaryVariable.CurrentMultiLeaderStyle, out DictionaryVariable variable))
330+
{
331+
if (this.MLeaderStyles.TryGetValue(variable.Value, out MultiLeaderStyle style))
332+
{
333+
return style as T;
334+
}
335+
}
336+
return null;
337+
default:
338+
throw new NotSupportedException($"The type {typeof(T)} is not a configurable type in the document.");
339+
}
340+
}
341+
320342
/// <summary>
321343
/// Reassign all the handles in the document to avoid the variable <see cref="CadHeader.HandleSeed"/> to grow past its limit.
322344
/// </summary>
@@ -341,6 +363,49 @@ public void RestoreHandles()
341363
this.Header.HandleSeed = nextHandle;
342364
}
343365

366+
/// <summary>
367+
/// This method sets the current configurable object of the specified type in the document's configuration.
368+
/// </summary>
369+
/// <typeparam name="T"></typeparam>
370+
/// <param name="obj"></param>
371+
/// <exception cref="NotSupportedException"></exception>
372+
public void SetCurrent<T>(T obj)
373+
where T : CadObject, INamedCadObject
374+
{
375+
switch (obj)
376+
{
377+
case Layer layer:
378+
this.Header.CurrentLayerName = this.Layers.TryAdd(layer).Name;
379+
break;
380+
case LineType lineType:
381+
this.Header.CurrentLineTypeName = this.LineTypes.TryAdd(lineType).Name; ;
382+
break;
383+
case TextStyle textStyle:
384+
this.Header.CurrentTextStyleName = this.TextStyles.TryAdd(textStyle).Name;
385+
break;
386+
case DimensionStyle dimensionStyle:
387+
this.Header.CurrentDimensionStyleName = this.DimensionStyles.TryAdd(dimensionStyle).Name;
388+
break;
389+
case MLineStyle mlineStyle:
390+
this.Header.CurrentMLineStyleName = this.MLineStyles.TryAdd(mlineStyle).Name;
391+
break;
392+
case MultiLeaderStyle multiLeaderStyle:
393+
if (this.DictionaryVariables.TryGetValue(DictionaryVariable.CurrentMultiLeaderStyle, out DictionaryVariable variable))
394+
{
395+
variable.Value = multiLeaderStyle.Name;
396+
}
397+
else
398+
{
399+
variable = new DictionaryVariable(DictionaryVariable.CurrentMultiLeaderStyle, multiLeaderStyle.Name);
400+
this.DictionaryVariables.Add(variable);
401+
}
402+
this.MLeaderStyles.TryAdd(multiLeaderStyle);
403+
break;
404+
default:
405+
throw new NotSupportedException($"The type {typeof(T)} is not a configurable type in the document.");
406+
}
407+
}
408+
344409
/// <summary>
345410
/// Gets an object in the document by it's handle
346411
/// </summary>
@@ -366,7 +431,7 @@ public bool TryGetCadObject<T>(ulong handle, out T cadObject)
366431
}
367432

368433
/// <summary>
369-
/// Updates the collections in the document and link them to it's dictionary
434+
/// Updates the collections in the document and link them to it's dictionary.
370435
/// </summary>
371436
/// <param name="createDictionaries"></param>
372437
public void UpdateCollections(bool createDictionaries)
@@ -419,6 +484,33 @@ public void UpdateCollections(bool createDictionaries)
419484
{
420485
this.Colors = new ColorCollection(colors);
421486
}
487+
488+
if (this.updateCollection(CadDictionary.VariableDictionary, createDictionaries, out CadDictionary variables))
489+
{
490+
this.DictionaryVariables = new DictionaryVariableCollection(variables);
491+
}
492+
}
493+
494+
/// <summary>
495+
/// Updates the <see cref="DxfClass"/> in the document and their instance count.
496+
/// </summary>
497+
/// <param name="reset">Resets the list and clears any unnecessary classes.</param>
498+
public void UpdateDxfClasses(bool reset)
499+
{
500+
if (reset)
501+
{
502+
this.Classes.Clear();
503+
}
504+
505+
DxfClassCollection.UpdateDxfClasses(this);
506+
507+
foreach (var item in this.Classes)
508+
{
509+
item.InstanceCount = this._cadObjects.Values
510+
.OfType<CadObject>()
511+
.Where(c => c.ObjectName == item.DxfName)
512+
.Count();
513+
}
422514
}
423515

424516
internal void RegisterCollection<T>(IObservableCadCollection<T> collection)

0 commit comments

Comments
 (0)