• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp / 17737836230

15 Sep 2025 03:12PM UTC coverage: 2.092% (-76.2%) from 78.245%
17737836230

push

github

web-flow
Merge pull request #790 from DomCR/addflag-refactor

addflag refactor

141 of 9225 branches covered (1.53%)

Branch coverage included in aggregate %.

0 of 93 new or added lines in 10 files covered. (0.0%)

24910 existing lines in 372 files now uncovered.

724 of 32119 relevant lines covered (2.25%)

5.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ACadSharp/Entities/HatchPattern.cs
1
using ACadSharp.Attributes;
2
using CSMath;
3
using CSUtilities.Extensions;
4
using System;
5
using System.Collections.Generic;
6
using System.Globalization;
7
using System.IO;
8
using System.Linq;
9
using System.Text;
10

11
namespace ACadSharp.Entities
12
{
13
        public partial class HatchPattern
14
        {
UNCOV
15
                public static HatchPattern Solid { get { return new HatchPattern("SOLID"); } }
×
16

17
                [DxfCodeValue(2)]
UNCOV
18
                public string Name { get; set; }
×
19

20
                /// <summary>
21
                /// Description for this pattern.
22
                /// </summary>
23
                /// <remarks>
24
                /// The description is not saved in dxf or dwg files, its only used in the .pat files.
25
                /// </remarks>
UNCOV
26
                public string Description { get; set; }
×
27

28
                [DxfCodeValue(DxfReferenceType.Count, 79)]
UNCOV
29
                public List<Line> Lines { get; set; } = new List<Line>();
×
30

31
                /// <summary>
32
                /// Default constructor of a hatch pattern.
33
                /// </summary>
34
                /// <param name="name"></param>
UNCOV
35
                public HatchPattern(string name)
×
UNCOV
36
                {
×
UNCOV
37
                        this.Name = name;
×
UNCOV
38
                }
×
39

40
                /// <summary>
41
                /// Update the pattern geometry with a translation, rotation and scale.
42
                /// </summary>
43
                /// <param name="translation"></param>
44
                /// <param name="rotation"></param>
45
                /// <param name="scale"></param>
46
                public void Update(XY translation, double rotation, double scale)
UNCOV
47
                {
×
UNCOV
48
                        var tr = Transform.CreateTranslation(translation.Convert<XYZ>());
×
UNCOV
49
                        var sc = Transform.CreateScaling(new XYZ(scale));
×
UNCOV
50
                        var rot = Transform.CreateRotation(XYZ.AxisZ, rotation);
×
51

UNCOV
52
                        var transform = new Transform(tr.Matrix * sc.Matrix * rot.Matrix);
×
53

UNCOV
54
                        foreach (var line in Lines)
×
UNCOV
55
                        {
×
UNCOV
56
                                line.Angle += rotation;
×
UNCOV
57
                                line.BasePoint = transform.ApplyTransform(line.BasePoint.Convert<XYZ>()).Convert<XY>();
×
UNCOV
58
                                line.Offset = transform.ApplyTransform(line.Offset.Convert<XYZ>()).Convert<XY>();
×
UNCOV
59
                                line.DashLengths = line.DashLengths.Select(d => d * scale).ToList();
×
UNCOV
60
                        }
×
UNCOV
61
                }
×
62

63
                /// <summary>
64
                /// Clones the current pattern.
65
                /// </summary>
66
                /// <returns></returns>
67
                public HatchPattern Clone()
UNCOV
68
                {
×
UNCOV
69
                        HatchPattern clone = (HatchPattern)this.MemberwiseClone();
×
70

UNCOV
71
                        clone.Lines = new List<Line>();
×
UNCOV
72
                        foreach (var item in this.Lines)
×
73
                        {
×
74
                                clone.Lines.Add(item.Clone());
×
75
                        }
×
76

UNCOV
77
                        return clone;
×
UNCOV
78
                }
×
79

80
                /// <inheritdoc/>
81
                public override string ToString()
82
                {
×
83
                        return $"{this.Name}";
×
84
                }
×
85

86
                /// <summary>
87
                /// Load a collection of patterns from a .pat file.
88
                /// </summary>
89
                /// <param name="path"></param>
90
                /// <returns></returns>
91
                public static IEnumerable<HatchPattern> LoadFrom(string path)
UNCOV
92
                {
×
UNCOV
93
                        List<HatchPattern> patterns = new List<HatchPattern>();
×
UNCOV
94
                        HatchPattern current = null;
×
95

UNCOV
96
                        var lines = File.ReadLines(path)
×
UNCOV
97
                                .Select(line => line.Trim())
×
UNCOV
98
                                .Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith(";"))
×
UNCOV
99
                                .ToQueue();
×
100

UNCOV
101
                        List<int> leadingIndices = lines.Select((line, i) => (line, i))
×
UNCOV
102
                                .Where(t => t.line.StartsWith("*"))
×
UNCOV
103
                                .Select(t => t.i)
×
UNCOV
104
                                .OrderBy(i => i).ToList();
×
105

UNCOV
106
                        leadingIndices.Add(lines.Count);
×
107

UNCOV
108
                        while (lines.TryDequeue(out string line))
×
UNCOV
109
                        {
×
UNCOV
110
                                if (line.StartsWith("*"))
×
UNCOV
111
                                {
×
UNCOV
112
                                        int index = line.IndexOf(',');
×
UNCOV
113
                                        string noPrefix = line.Remove(0, 1);
×
UNCOV
114
                                        current = new HatchPattern(noPrefix.Substring(0, index - 1));
×
UNCOV
115
                                        current.Description = new string(noPrefix.Skip(index).ToArray()).Trim();
×
116

UNCOV
117
                                        patterns.Add(current);
×
UNCOV
118
                                }
×
119
                                else
UNCOV
120
                                {
×
UNCOV
121
                                        string[] data = line.Split(',');
×
UNCOV
122
                                        Line l = new Line();
×
UNCOV
123
                                        l.Angle = MathHelper.DegToRad(double.Parse(data[0], CultureInfo.InvariantCulture));
×
UNCOV
124
                                        l.BasePoint = new XY(double.Parse(data[1], CultureInfo.InvariantCulture), double.Parse(data[2], CultureInfo.InvariantCulture));
×
125

UNCOV
126
                                        XY offset = new XY(double.Parse(data[3], CultureInfo.InvariantCulture), double.Parse(data[4], CultureInfo.InvariantCulture));
×
UNCOV
127
                                        double cos = Math.Cos(l.Angle);
×
UNCOV
128
                                        double sin = Math.Sin(l.Angle);
×
UNCOV
129
                                        l.Offset = new XY(offset.X * cos - offset.Y * sin, offset.X * sin + offset.Y * cos);
×
130

UNCOV
131
                                        IEnumerable<string> dashes = data.Skip(5);
×
UNCOV
132
                                        if (dashes.Any())
×
UNCOV
133
                                        {
×
UNCOV
134
                                                l.DashLengths.AddRange(dashes.Select(d => double.Parse(d, CultureInfo.InvariantCulture)));
×
UNCOV
135
                                        }
×
136

UNCOV
137
                                        current.Lines.Add(l);
×
UNCOV
138
                                }
×
UNCOV
139
                        }
×
140

UNCOV
141
                        return patterns;
×
UNCOV
142
                }
×
143

144
                /// <summary>
145
                /// Write a pattern or a collection of patterns into a .pat file.
146
                /// </summary>
147
                /// <param name="filename"></param>
148
                /// <param name="patterns"></param>
149
                public static void SavePatterns(string filename, params HatchPattern[] patterns)
UNCOV
150
                {
×
UNCOV
151
                        using StreamWriter writer = File.CreateText(filename);
×
152

UNCOV
153
                        foreach (HatchPattern p in patterns)
×
UNCOV
154
                        {
×
UNCOV
155
                                writer.Write($"*{p.Name}");
×
156

UNCOV
157
                                if (!p.Description.IsNullOrEmpty())
×
UNCOV
158
                                {
×
UNCOV
159
                                        writer.Write($",{p.Description}");
×
UNCOV
160
                                }
×
161

UNCOV
162
                                writer.WriteLine();
×
163

UNCOV
164
                                foreach (Line line in p.Lines)
×
UNCOV
165
                                {
×
UNCOV
166
                                        StringBuilder sb = new StringBuilder();
×
167

UNCOV
168
                                        double angle = MathHelper.DegToRad(line.Angle);
×
UNCOV
169
                                        double cos = Math.Cos(line.Angle);
×
UNCOV
170
                                        double sin = Math.Sin(line.Angle);
×
171

UNCOV
172
                                        var offset = line.Offset;
×
UNCOV
173
                                        var vector2D = new XY(offset.X * cos - offset.Y * sin, offset.X * sin + offset.Y * cos);
×
174

UNCOV
175
                                        sb.Append(angle.ToString(CultureInfo.InvariantCulture));
×
UNCOV
176
                                        sb.Append(",");
×
UNCOV
177
                                        sb.Append(line.BasePoint.ToString(CultureInfo.InvariantCulture));
×
UNCOV
178
                                        sb.Append(",");
×
UNCOV
179
                                        sb.Append(vector2D.ToString(CultureInfo.InvariantCulture));
×
180

UNCOV
181
                                        if (line.DashLengths.Count > 0)
×
UNCOV
182
                                        {
×
UNCOV
183
                                                sb.Append(",");
×
UNCOV
184
                                                sb.Append(line.DashLengths[0].ToString(CultureInfo.InvariantCulture));
×
UNCOV
185
                                                for (int i = 1; i < line.DashLengths.Count; i++)
×
UNCOV
186
                                                {
×
UNCOV
187
                                                        sb.Append(",");
×
UNCOV
188
                                                        sb.Append(line.DashLengths[i].ToString(CultureInfo.InvariantCulture));
×
UNCOV
189
                                                }
×
UNCOV
190
                                        }
×
191

UNCOV
192
                                        writer.WriteLine(sb.ToString());
×
UNCOV
193
                                }
×
194

UNCOV
195
                        }
×
UNCOV
196
                }
×
197
        }
198
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc