Skip to content

Commit 8ddb6e5

Browse files
committed
MLineStyle
1 parent e95fe5f commit 8ddb6e5

File tree

6 files changed

+187
-51
lines changed

6 files changed

+187
-51
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5596,7 +5596,7 @@ 1024 512
55965596
}
55975597

55985598
template.ElementTemplates.Add(elementTemplate);
5599-
mlineStyle.Elements.Add(element);
5599+
mlineStyle.AddElement(element);
56005600
}
56015601

56025602
return template;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ private void writeMLineStyle(MLineStyle mlineStyle)
534534
this._writer.WriteBitDouble(mlineStyle.EndAngle);
535535

536536
//linesinstyle RC Number of lines in this style
537-
this._writer.WriteByte((byte)mlineStyle.Elements.Count);
537+
this._writer.WriteByte((byte)mlineStyle.Elements.Count());
538538
foreach (MLineStyle.Element element in mlineStyle.Elements)
539539
{
540540
//Offset BD Offset of this segment

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ protected void writeMLineStyle(MLineStyle style)
467467

468468
this._writer.Write(51, style.StartAngle, map);
469469
this._writer.Write(52, style.EndAngle, map);
470-
this._writer.Write(71, (short)style.Elements.Count, map);
470+
this._writer.Write(71, (short)style.Elements.Count(), map);
471471
foreach (MLineStyle.Element element in style.Elements)
472472
{
473473
this._writer.Write(49, element.Offset, map);

src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.Entities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ private void writeMLine(MLine mLine)
775775

776776
if (mLine.Style != null)
777777
{
778-
this._writer.Write(73, (short)mLine.Style.Elements.Count);
778+
this._writer.Write(73, (short)mLine.Style.Elements.Count());
779779
}
780780

781781
this._writer.Write(10, mLine.StartPoint, map);
Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ACadSharp.Attributes;
2+
using ACadSharp.Extensions;
23
using ACadSharp.Tables;
34

45
namespace ACadSharp.Objects
@@ -8,22 +9,58 @@ public partial class MLineStyle
89
public class Element
910
{
1011
/// <summary>
11-
/// Element offset
12+
/// Element color.
13+
/// </summary>
14+
[DxfCodeValue(62)]
15+
public Color Color { get; set; } = Color.ByLayer;
16+
17+
/// <summary>
18+
/// Element linetype.
19+
/// </summary>
20+
[DxfCodeValue(6)]
21+
public LineType LineType
22+
{
23+
get => _lineType;
24+
set
25+
{
26+
this._lineType = CadObject.updateCollection(value, this.Owner?.Document?.LineTypes);
27+
}
28+
}
29+
30+
/// <summary>
31+
/// Element offset.
1232
/// </summary>
1333
[DxfCodeValue(49)]
1434
public double Offset { get; set; }
1535

1636
/// <summary>
17-
/// Element color
37+
/// Line type where this segment belongs.
1838
/// </summary>
19-
[DxfCodeValue(62)]
20-
public Color Color { get; set; } = Color.ByBlock;
39+
public MLineStyle Owner { get; internal set; }
40+
41+
private LineType _lineType = LineType.ByLayer;
2142

2243
/// <summary>
23-
/// Element linetype
44+
/// Clone the current segment.
2445
/// </summary>
25-
[DxfCodeValue(6)]
26-
public LineType LineType { get; set; } = LineType.ByLayer;
46+
/// <returns></returns>
47+
public MLineStyle.Element Clone()
48+
{
49+
Element clone = MemberwiseClone() as Element;
50+
clone.Owner = null;
51+
clone._lineType = (LineType)(this.LineType?.Clone());
52+
return clone;
53+
}
54+
55+
internal void AssignDocument(CadDocument doc)
56+
{
57+
this._lineType = CadObject.updateCollection(this._lineType, doc.LineTypes);
58+
}
59+
60+
internal void UnassignDocument()
61+
{
62+
this._lineType = this._lineType.CloneTyped();
63+
}
2764
}
2865
}
29-
}
66+
}
Lines changed: 138 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using ACadSharp.Attributes;
2+
using ACadSharp.Tables;
3+
using CSMath;
4+
using System;
25
using System.Collections.Generic;
6+
using System.Linq;
37

48
namespace ACadSharp.Objects
59
{
@@ -15,23 +19,64 @@ namespace ACadSharp.Objects
1519
public partial class MLineStyle : NonGraphicalObject
1620
{
1721
/// <summary>
18-
/// Default multiline style name
22+
/// Gets the default multiline style with predefined settings.
1923
/// </summary>
20-
public const string DefaultName = "Standard";
24+
/// <remarks>This property provides a preconfigured instance of <see cref="MLineStyle"/> that can be used as a
25+
/// baseline for creating multiline styles. The default configuration includes two elements with offsets of 0.5 and
26+
/// -0.5, respectively.</remarks>
27+
public static MLineStyle Default
28+
{
29+
get
30+
{
31+
var def = new MLineStyle(DefaultName);
32+
def.StartAngle = MathHelper.HalfPI;
33+
def.EndAngle = MathHelper.HalfPI;
34+
def.AddElement(new Element
35+
{
36+
LineType = LineType.ByLayer,
37+
Offset = 0.5
38+
});
39+
def.AddElement(new Element
40+
{
41+
LineType = LineType.ByLayer,
42+
Offset = -0.5
43+
});
44+
return def;
45+
}
46+
}
2147

2248
/// <summary>
23-
/// Gets the default MLine style
49+
/// Style description
2450
/// </summary>
25-
public static MLineStyle Default { get { return new MLineStyle(DefaultName); } }
51+
/// <value>
52+
/// 255 characters maximum
53+
/// </value>
54+
[DxfCodeValue(3)]
55+
public string Description { get; set; }
2656

27-
/// <inheritdoc/>
28-
public override ObjectType ObjectType => ObjectType.MLINESTYLE;
57+
/// <summary>
58+
/// Elements in the style
59+
/// </summary>
60+
[DxfCodeValue(DxfReferenceType.Count, 71)]
61+
public IEnumerable<Element> Elements => this._elements;
2962

30-
/// <inheritdoc/>
31-
public override string ObjectName => DxfFileToken.ObjectMLineStyle;
63+
/// <summary>
64+
/// End angle
65+
/// </summary>
66+
[DxfCodeValue(DxfReferenceType.IsAngle, 52)]
67+
public double EndAngle { get; set; } = System.Math.PI / 2;
3268

33-
/// <inheritdoc/>
34-
public override string SubclassMarker => DxfSubclassMarker.MLineStyle;
69+
/// <summary>
70+
/// Fill color
71+
/// </summary>
72+
[DxfCodeValue(62)]
73+
public Color FillColor { get; set; } = Color.ByLayer;
74+
75+
/// <summary>
76+
/// Multi line style flags
77+
/// </summary>
78+
[DxfCodeValue(70)]
79+
public MLineStyleFlags Flags { get; set; }
3580

3681
/// <summary>
3782
/// Mline style name
@@ -49,50 +94,104 @@ public override string Name
4994
}
5095
}
5196

52-
/// <summary>
53-
/// Multi line style flags
54-
/// </summary>
55-
[DxfCodeValue(70)]
56-
public MLineStyleFlags Flags { get; set; }
57-
58-
/// <summary>
59-
/// Style description
60-
/// </summary>
61-
/// <value>
62-
/// 255 characters maximum
63-
/// </value>
64-
[DxfCodeValue(3)]
65-
public string Description { get; set; }
97+
/// <inheritdoc/>
98+
public override string ObjectName => DxfFileToken.ObjectMLineStyle;
6699

67-
/// <summary>
68-
/// Fill color
69-
/// </summary>
70-
[DxfCodeValue(62)]
71-
public Color FillColor { get; set; } = Color.ByLayer;
100+
/// <inheritdoc/>
101+
public override ObjectType ObjectType => ObjectType.MLINESTYLE;
72102

73103
/// <summary>
74104
/// Start angle
75105
/// </summary>
76106
[DxfCodeValue(DxfReferenceType.IsAngle, 51)]
77107
public double StartAngle { get; set; } = System.Math.PI / 2;
78108

79-
/// <summary>
80-
/// End angle
81-
/// </summary>
82-
[DxfCodeValue(DxfReferenceType.IsAngle, 52)]
83-
public double EndAngle { get; set; } = System.Math.PI / 2;
109+
/// <inheritdoc/>
110+
public override string SubclassMarker => DxfSubclassMarker.MLineStyle;
84111

85112
/// <summary>
86-
/// Elements in the style
113+
/// Default multiline style name
87114
/// </summary>
88-
[DxfCodeValue(DxfReferenceType.Count, 71)]
89-
public List<MLineStyle.Element> Elements { get; } = new List<Element>();
115+
public const string DefaultName = "Standard";
90116

91-
internal MLineStyle() { }
117+
private List<Element> _elements = new List<Element>();
92118

93119
public MLineStyle(string name)
94120
{
95121
this.Name = name;
96122
}
123+
124+
internal MLineStyle()
125+
{
126+
}
127+
128+
/// <summary>
129+
/// Adds a segment to the current line style.
130+
/// </summary>
131+
/// <remarks>The method associates the specified element with the current line style and updates its line type
132+
/// using the document's line type collection, if available.</remarks>
133+
/// <param name="element">The element to add as a segment. The element must not already belong to another line style.</param>
134+
/// <exception cref="ArgumentException">Thrown if the <paramref name="element"/> is already assigned to another line style.</exception>
135+
public void AddElement(Element element)
136+
{
137+
if (element.Owner != null)
138+
throw new ArgumentException($"Element already assigned to a MLineStyle: {element.Owner.Name}");
139+
140+
element.LineType = CadObject.updateCollection(element.LineType, this.Document?.LineTypes);
141+
element.Owner = this;
142+
this._elements.Add(element);
143+
}
144+
145+
/// <inheritdoc/>
146+
public override CadObject Clone()
147+
{
148+
MLineStyle clone = (MLineStyle)base.Clone();
149+
150+
clone._elements = new List<Element>();
151+
foreach (var element in this._elements)
152+
{
153+
clone.AddElement(element.Clone());
154+
}
155+
156+
return clone;
157+
}
158+
159+
internal override void AssignDocument(CadDocument doc)
160+
{
161+
base.AssignDocument(doc);
162+
163+
foreach (var item in this._elements.Where(s => s.LineType != null))
164+
{
165+
item.AssignDocument(doc);
166+
}
167+
168+
doc.TextStyles.OnRemove += this.tableOnRemove;
169+
}
170+
171+
internal override void UnassignDocument()
172+
{
173+
this.Document.TextStyles.OnRemove -= this.tableOnRemove;
174+
175+
foreach (var item in this._elements.Where(s => s.LineType != null))
176+
{
177+
item.UnassignDocument();
178+
}
179+
180+
base.UnassignDocument();
181+
}
182+
183+
protected void tableOnRemove(object sender, CollectionChangedEventArgs e)
184+
{
185+
if (e.Item is LineType style)
186+
{
187+
foreach (var item in this._elements.Where(s => s.LineType != null))
188+
{
189+
if (item.LineType == style)
190+
{
191+
item.LineType = null;
192+
}
193+
}
194+
}
195+
}
97196
}
98-
}
197+
}

0 commit comments

Comments
 (0)