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

Dxf writer MLine #170

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion ACadSharp.Tests/IO/CadReaderTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using ACadSharp.Header;
using ACadSharp.Entities;
using ACadSharp.Header;
using ACadSharp.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down
29 changes: 29 additions & 0 deletions ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@ public void SingleLine()
this.Document.Entities.Add(line);
}

public void SingleMLine()
{
//It creates a valid dxf but the MLine is wrongly drawn

MLine line = new MLine();

line.StartPoint = XYZ.Zero;

var v1 = new MLine.Vertex();
v1.Position = XYZ.Zero;
v1.Direction = XYZ.AxisY;

v1.Segments.Add(new MLine.Vertex.Segment { Parameters = new List<double> { 0.75, 0 } });
v1.Segments.Add(new MLine.Vertex.Segment { Parameters = new List<double> { -0.75, 0 } });

var v2 = new MLine.Vertex();
v2.Position = new XYZ(100, 100, 0);
v2.Direction = XYZ.AxisY;

v2.Segments.Add(new MLine.Vertex.Segment { Parameters = new List<double> { 0.75, 0 } });
v2.Segments.Add(new MLine.Vertex.Segment { Parameters = new List<double> { -0.75, 0 } });

line.Vertices.Add(v1);
line.Vertices.Add(v2);

this.Document.Entities.Add(line);
}

public void SingleMText()
{
MText mtext = new MText();
Expand Down Expand Up @@ -180,6 +208,7 @@ static WriterSingleObjectTests()

Data.Add(new(nameof(SingleCaseGenerator.Empty)));
Data.Add(new(nameof(SingleCaseGenerator.SingleLine)));
Data.Add(new(nameof(SingleCaseGenerator.SingleMLine)));
Data.Add(new(nameof(SingleCaseGenerator.EntityColorByLayer)));
Data.Add(new(nameof(SingleCaseGenerator.EntityColorTrueColor)));
Data.Add(new(nameof(SingleCaseGenerator.CurrentEntityColorTrueColor)));
Expand Down
20 changes: 20 additions & 0 deletions ACadSharp/CadObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ACadSharp.Attributes;
using ACadSharp.Objects;
using ACadSharp.Objects.Collections;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -149,5 +150,24 @@ protected T updateTable<T>(T entry, Table<T> table)
return entry;
}
}

protected T updateCollection<T>(T entry, ObjectDictionaryCollection<T> collection)
where T : CadObject, IDictionaryEntry
{
if (collection == null)
{
return entry;
}

if (collection.TryGetValue(entry.Name, out T existing))
{
return existing;
}
else
{
collection.Add(entry);
return entry;
}
}
}
}
29 changes: 28 additions & 1 deletion ACadSharp/Entities/MLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ACadSharp.Objects;
using CSMath;
using System.Collections.Generic;
using System.Linq;

namespace ACadSharp.Entities
{
Expand Down Expand Up @@ -51,7 +52,7 @@ public MLineStyle Style
/// Scale factor
/// </summary>
[DxfCodeValue(40)]
public double ScaleFactor { get; set; }
public double ScaleFactor { get; set; } = 1;

/// <summary>
/// Justification
Expand Down Expand Up @@ -101,5 +102,31 @@ public override CadObject Clone()

return clone;
}

internal override void AssignDocument(CadDocument doc)
{
base.AssignDocument(doc);

this._style = this.updateCollection(this.Style, doc.MLineStyles);

this.Document.MLineStyles.OnRemove += this.mLineStylesOnRemove;
}

internal override void UnassignDocument()
{
this.Document.MLineStyles.OnRemove -= this.mLineStylesOnRemove;

base.UnassignDocument();

this._style = (MLineStyle)this.Style.Clone();
}

private void mLineStylesOnRemove(object sender, CollectionChangedEventArgs e)
{
if (e.Item.Equals(this.Style))
{
this.Style = this.Document.MLineStyles[MLineStyle.DefaultName];
}
}
}
}
18 changes: 15 additions & 3 deletions ACadSharp/IO/CadDocumentBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ACadSharp.Entities;
using ACadSharp.IO.DWG;
using ACadSharp.IO.Templates;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
Expand Down Expand Up @@ -44,19 +43,26 @@ internal abstract class CadDocumentBuilder

protected Dictionary<ulong, ICadTableTemplate> tableTemplates = new Dictionary<ulong, ICadTableTemplate>();

protected Dictionary<ulong, ICadDictionaryTemplate> dictionaryTemplates = new();

public CadDocumentBuilder(CadDocument document)
{
this.DocumentToBuild = document;
}

public virtual void BuildDocument()
{
foreach (CadTemplate template in this.templates.Values)
foreach (ICadDictionaryTemplate dictionaryTemplate in dictionaryTemplates.Values)
{
template.Build(this);
dictionaryTemplate.Build(this);
}

this.DocumentToBuild.UpdateCollections(false);

foreach (CadTemplate template in this.templates.Values)
{
template.Build(this);
}
}

public void AddTableTemplate(ICadTableTemplate tableTemplate)
Expand Down Expand Up @@ -190,5 +196,11 @@ public void BuildTable<T>(Table<T> table)
this.Notify($"Table {table.ObjectName} not found in the document", NotificationType.Warning);
}
}

public void AddDictionaryTemplate(ICadDictionaryTemplate dictionaryTemplate)
{
this.dictionaryTemplates[dictionaryTemplate.CadObject.Handle] = dictionaryTemplate;
this.cadObjects[dictionaryTemplate.CadObject.Handle] = dictionaryTemplate.CadObject;
}
}
}
11 changes: 10 additions & 1 deletion ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public void Read()
{
this._builder.AddTableTemplate(tableTemplate);
}
else if (template is ICadDictionaryTemplate dictionaryTemplate)
{
this._builder.AddDictionaryTemplate(dictionaryTemplate);
}
else
{
this._builder.AddTemplate(template);
Expand Down Expand Up @@ -2539,6 +2543,11 @@ private CadTemplate readDictionary()

this.readCommonDictionary(template);

if (cadDictionary.Handle == this._builder.HeaderHandles.DICTIONARY_NAMED_OBJECTS)
{

}

return template;
}

Expand Down Expand Up @@ -5339,7 +5348,7 @@ private CadTemplate readScale()
this.readCommonNonEntityData(template);

//BS 70 Unknown(ODA writes 0).
scale.Unknown = this._mergedReaders.ReadBitShort();
this._mergedReaders.ReadBitShort();
//TV 300 Name
scale.Name = this._mergedReaders.ReadVariableText();
//BD 140 Paper units(numerator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private void writePlotSettings(PlotSettings plot)
private void writeScale(Scale scale)
{
//BS 70 Unknown(ODA writes 0).
this._writer.WriteBitShort(scale.Unknown);
this._writer.WriteBitShort(0);
//TV 300 Name
this._writer.WriteVariableText(scale.Name);
//BD 140 Paper units(numerator)
Expand Down
9 changes: 8 additions & 1 deletion ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ public override void Read()
continue;

//Add the object and the template to the builder
this._builder.AddTemplate(template);
if (template is ICadDictionaryTemplate dictionaryTemplate)
{
this._builder.AddDictionaryTemplate(dictionaryTemplate);
}
else
{
this._builder.AddTemplate(template);
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions ACadSharp/IO/DXF/DxfStreamWriter/DxfObjectsSectionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ protected void writeObject<T>(T co)
case Material:
case MultiLeaderStyle:
case SortEntitiesTable:
case Scale:
case VisualStyle:
//case XRecrod: //TODO: XRecord Understand how it works for the reader
this.notify($"Object not implemented : {co.GetType().FullName}");
return;
}
Expand All @@ -52,7 +50,7 @@ protected void writeObject<T>(T co)
this.writeDictionaryVariable(dictvar);
break;
case Group group:
this.writeGroup(group);
this.writeGroup(group);
break;
case Layout layout:
this.writeLayout(layout);
Expand All @@ -63,6 +61,9 @@ protected void writeObject<T>(T co)
case PlotSettings plotSettings:
this.writePlotSettings(plotSettings);
break;
case Scale scale:
this.writeScale(scale);
break;
case SortEntitiesTable sortensTable:
//this.writeSortentsTable(sortensTable);
break;
Expand Down Expand Up @@ -152,6 +153,17 @@ protected void writePlotSettings(PlotSettings plot)
this._writer.Write(149, plot.PaperImageOrigin.Y, map);
}

protected void writeScale(Scale scale)
{
this._writer.Write(100, DxfSubclassMarker.Scale);

this._writer.Write(70, 0);
this._writer.Write(300, scale.Name);
this._writer.Write(140, scale.PaperUnits);
this._writer.Write(141, scale.DrawingUnits);
this._writer.Write(290, scale.IsUnitScale ? (short)1 : (short)0);
}

protected void writeGroup(Group group)
{
this._writer.Write(100, DxfSubclassMarker.Group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ protected void writeEntity<T>(T entity)
//TODO: Implement complex entities in a separated branch
switch (entity)
{
case MLine:
case Solid3D:
case MultiLeader:
case Wipeout:
Expand Down Expand Up @@ -616,7 +615,7 @@ private void writeMLine(MLine mLine)

this._writer.Write(DxfCode.Subclass, DxfSubclassMarker.MLine);

//Style has to references
//Style has two references
this._writer.WriteName(2, mLine.Style, map);
this._writer.WriteHandle(340, mLine.Style, map);

Expand Down
2 changes: 1 addition & 1 deletion ACadSharp/IO/Templates/CadDictionaryTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ACadSharp.IO.Templates
{
internal class CadDictionaryTemplate : CadTemplate<CadDictionary>
internal class CadDictionaryTemplate : CadTemplate<CadDictionary>, ICadDictionaryTemplate
{
public Dictionary<string, ulong?> Entries { get; set; } = new Dictionary<string, ulong?>();

Expand Down
7 changes: 7 additions & 0 deletions ACadSharp/IO/Templates/ICadDictionaryTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ACadSharp.IO.Templates
{
internal interface ICadDictionaryTemplate : ICadObjectTemplate
{
public CadObject CadObject { get; set; }
}
}
18 changes: 17 additions & 1 deletion ACadSharp/Objects/Collections/ObjectDictionaryCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -11,6 +12,9 @@ namespace ACadSharp.Objects.Collections
public abstract class ObjectDictionaryCollection<T> : IHandledCadObject, IEnumerable<T>
where T : CadObject, IDictionaryEntry
{
public event EventHandler<CollectionChangedEventArgs> OnAdd { add { this._dictionary.OnAdd += value; } remove { this._dictionary.OnAdd -= value; } }
public event EventHandler<CollectionChangedEventArgs> OnRemove { add { this._dictionary.OnRemove += value; } remove { this._dictionary.OnRemove -= value; } }

/// <inheritdoc/>
public ulong Handle { get { return this._dictionary.Handle; } }

Expand All @@ -32,6 +36,18 @@ public void Add(T entry)
this._dictionary.Add(entry.Name, entry);
}

/// <summary>
/// Gets the value associated with the specific key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="entry"></param>
/// <returns>true if the value is found or false if not found</returns>
public bool TryGetValue(string name, out T entry)
{
return this._dictionary.TryGetEntry(name, out entry);
}

/// <summary>
/// Remove an entry from the collection
/// </summary>
Expand Down
6 changes: 0 additions & 6 deletions ACadSharp/Objects/Scale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public class Scale : CadObject, IDictionaryEntry
/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.Scale;

/// <summary>
/// Unknown
/// </summary>
[DxfCodeValue(70)]
public short Unknown { get; internal set; }

/// <summary>
/// Name
/// </summary>
Expand Down
Loading