11using ACadSharp . Attributes ;
2+ using ACadSharp . Tables ;
3+ using CSMath ;
4+ using System ;
25using System . Collections . Generic ;
6+ using System . Linq ;
37
48namespace 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