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

DomCR / ACadSharp / 12952900945

24 Jan 2025 03:47PM UTC coverage: 76.119% (+0.09%) from 76.029%
12952900945

Pull #525

github

web-flow
Merge 0b1e965fd into f3555a8fa
Pull Request #525: Issue-524 Explode Hatch

5324 of 7724 branches covered (68.93%)

Branch coverage included in aggregate %.

2 of 52 new or added lines in 2 files covered. (3.85%)

127 existing lines in 12 files now uncovered.

21278 of 27224 relevant lines covered (78.16%)

39464.89 hits per line

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

92.5
/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.Diagnostics;
7
using System.Globalization;
8
using System.IO;
9
using System.Linq;
10
using System.Text;
11

12
namespace ACadSharp.Entities
13
{
14
        public partial class HatchPattern
15
        {
16
                public static HatchPattern Solid { get { return new HatchPattern("SOLID"); } }
2,739✔
17

18
                [DxfCodeValue(2)]
19
                public string Name { get; set; }
2,099✔
20

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

29
                [DxfCodeValue(DxfReferenceType.Count, 79)]
30
                public List<Line> Lines { get; set; } = new List<Line>();
15,969✔
31

32
                public HatchPattern(string name)
1,286✔
33
                {
1,286✔
34
                        this.Name = name;
1,286✔
35
                }
1,286✔
36

37
                public HatchPattern Clone()
38
                {
2✔
39
                        HatchPattern clone = (HatchPattern)this.MemberwiseClone();
2✔
40

41
                        clone.Lines = new List<Line>();
2✔
42
                        foreach (var item in this.Lines)
6!
UNCOV
43
                        {
×
UNCOV
44
                                clone.Lines.Add(item.Clone());
×
UNCOV
45
                        }
×
46

47
                        return clone;
2✔
48
                }
2✔
49

50
                /// <inheritdoc/>
51
                public override string ToString()
UNCOV
52
                {
×
UNCOV
53
                        return $"{this.Name}";
×
UNCOV
54
                }
×
55

56
                /// <summary>
57
                /// Load a collection of patterns from a .pat file.
58
                /// </summary>
59
                /// <param name="path"></param>
60
                /// <returns></returns>
61
                public static IEnumerable<HatchPattern> LoadFrom(string path)
62
                {
10✔
63
                        List<HatchPattern> patterns = new List<HatchPattern>();
10✔
64
                        HatchPattern current = null;
10✔
65

66
                        var lines = File.ReadLines(path)
10✔
67
                                .Select(line => line.Trim())
1,760✔
68
                                .Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith(";"))
1,760✔
69
                                .ToQueue();
10✔
70

71
                        List<int> leadingIndices = lines.Select((line, i) => (line, i))
1,762✔
72
                                .Where(t => t.line.StartsWith("*"))
1,752✔
73
                                .Select(t => t.i)
16✔
74
                                .OrderBy(i => i).ToList();
26✔
75

76
                        leadingIndices.Add(lines.Count);
10✔
77

78
                        while (lines.TryDequeue(out string line))
1,762✔
79
                        {
1,752✔
80
                                if (line.StartsWith("*"))
1,752✔
81
                                {
16✔
82
                                        int index = line.IndexOf(',');
16✔
83
                                        string noPrefix = line.Remove(0, 1);
16✔
84
                                        current = new HatchPattern(noPrefix.Substring(0, index - 1));
16✔
85
                                        current.Description = new string(noPrefix.Skip(index).ToArray()).Trim();
16✔
86

87
                                        patterns.Add(current);
16✔
88
                                }
16✔
89
                                else
90
                                {
1,736✔
91
                                        string[] data = line.Split(',');
1,736✔
92
                                        Line l = new Line();
1,736✔
93
                                        l.Angle = MathHelper.DegToRad(double.Parse(data[0], CultureInfo.InvariantCulture));
1,736✔
94
                                        l.BasePoint = new XY(double.Parse(data[1], CultureInfo.InvariantCulture), double.Parse(data[2], CultureInfo.InvariantCulture));
1,736✔
95

96
                                        XY offset = new XY(double.Parse(data[3], CultureInfo.InvariantCulture), double.Parse(data[4], CultureInfo.InvariantCulture));
1,736✔
97
                                        double cos = Math.Cos(l.Angle);
1,736✔
98
                                        double sin = Math.Sin(l.Angle);
1,736✔
99
                                        l.Offset = new XY(offset.X * cos - offset.Y * sin, offset.X * sin + offset.Y * cos);
1,736✔
100

101
                                        IEnumerable<string> dashes = data.Skip(5);
1,736✔
102
                                        if (dashes.Any())
1,736✔
103
                                        {
1,736✔
104
                                                l.DashLengths.AddRange(dashes.Select(d => double.Parse(d, CultureInfo.InvariantCulture)));
5,208✔
105
                                        }
1,736✔
106

107
                                        current.Lines.Add(l);
1,736✔
108
                                }
1,736✔
109
                        }
1,752✔
110

111
                        return patterns;
10✔
112
                }
10✔
113

114
                /// <summary>
115
                /// Write a pattern or a collection of patterns into a .pat file.
116
                /// </summary>
117
                /// <param name="filename"></param>
118
                /// <param name="patterns"></param>
119
                public static void SavePatterns(string filename, params IEnumerable<HatchPattern> patterns)
120
                {
1✔
121
                        using StreamWriter writer = File.CreateText(filename);
1✔
122

123
                        foreach (HatchPattern p in patterns)
5✔
124
                        {
1✔
125
                                writer.Write($"*{p.Name}");
1✔
126

127
                                if (!p.Description.IsNullOrEmpty())
1✔
128
                                {
1✔
129
                                        writer.Write($",{p.Description}");
1✔
130
                                }
1✔
131

132
                                writer.WriteLine();
1✔
133

134
                                foreach (Line line in p.Lines)
7✔
135
                                {
2✔
136
                                        StringBuilder sb = new StringBuilder();
2✔
137

138
                                        double angle = MathHelper.DegToRad(line.Angle);
2✔
139
                                        double cos = Math.Cos(line.Angle);
2✔
140
                                        double sin = Math.Sin(line.Angle);
2✔
141

142
                                        var offset = line.Offset;
2✔
143
                                        var vector2D = new XY(offset.X * cos - offset.Y * sin, offset.X * sin + offset.Y * cos);
2✔
144
                                        
145
                                        sb.Append(angle.ToString(CultureInfo.InvariantCulture));
2✔
146
                                        sb.Append(",");
2✔
147
                                        sb.Append(line.BasePoint.ToString(CultureInfo.InvariantCulture));
2✔
148
                                        sb.Append(",");
2✔
149
                                        sb.Append(vector2D.ToString(CultureInfo.InvariantCulture));
2✔
150

151
                                        if (line.DashLengths.Count > 0)
2✔
152
                                        {
1✔
153
                                                sb.Append(",");
1✔
154
                                                sb.Append(line.DashLengths[0].ToString(CultureInfo.InvariantCulture));
1✔
155
                                                for (int i = 1; i < line.DashLengths.Count; i++)
6✔
156
                                                {
2✔
157
                                                        sb.Append(",");
2✔
158
                                                        sb.Append(line.DashLengths[i].ToString(CultureInfo.InvariantCulture));
2✔
159
                                                }
2✔
160
                                        }
1✔
161

162
                                        writer.WriteLine(sb.ToString());
2✔
163
                                }
2✔
164

165
                        }
1✔
166
                }
2✔
167
        }
168
}
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

© 2026 Coveralls, Inc