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

DomCR / ACadSharp / 29143042407

11 Jul 2026 06:34AM UTC coverage: 2.912% (-73.0%) from 75.918%
29143042407

push

github

web-flow
Merge pull request #1146 from ilCosmico/acds-read-payload-anchor

Read the AcDs payload area offset from the data segment header

264 of 12999 branches covered (2.03%)

Branch coverage included in aggregate %.

0 of 30 new or added lines in 1 file covered. (0.0%)

31243 existing lines in 465 files now uncovered.

1337 of 41984 relevant lines covered (3.18%)

5.38 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
        /// <summary>
18
        /// Description for this pattern.
19
        /// </summary>
20
        /// <remarks>
21
        /// The description is not saved in dxf or dwg files, its only used in the .pat files.
22
        /// </remarks>
UNCOV
23
        public string Description { get; set; }
×
24

25
        /// <summary>
26
        /// Gets or sets the collection of <see cref="Line"/> objects.
27
        /// </summary>
28
        /// <remarks>This property allows adding, removing, or modifying the lines in the collection. Changes to this
29
        /// collection directly affect the associated context.</remarks>
30
        [DxfCodeValue(DxfReferenceType.Count, 79)]
UNCOV
31
        public List<Line> Lines { get; set; } = new List<Line>();
×
32

33
        /// <summary>
34
        /// Name for this pattern.
35
        /// </summary>
36
        [DxfCodeValue(2)]
UNCOV
37
        public string Name { get; set; }
×
38

39
        /// <summary>
40
        /// Default constructor of a hatch pattern.
41
        /// </summary>
42
        /// <param name="name"></param>
UNCOV
43
        public HatchPattern(string name)
×
UNCOV
44
        {
×
UNCOV
45
                this.Name = name;
×
UNCOV
46
        }
×
47

48
        /// <summary>
49
        /// Load a collection of patterns from a .pat file.
50
        /// </summary>
51
        /// <param name="path"></param>
52
        /// <returns></returns>
53
        public static IEnumerable<HatchPattern> LoadFrom(string path)
UNCOV
54
        {
×
UNCOV
55
                List<HatchPattern> patterns = new List<HatchPattern>();
×
UNCOV
56
                HatchPattern current = null;
×
57

UNCOV
58
                var lines = File.ReadLines(path)
×
UNCOV
59
                        .Select(line => line.Trim())
×
UNCOV
60
                        .Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith(";"))
×
UNCOV
61
                        .ToQueue();
×
62

UNCOV
63
                List<int> leadingIndices = lines.Select((line, i) => (line, i))
×
UNCOV
64
                        .Where(t => t.line.StartsWith("*"))
×
UNCOV
65
                        .Select(t => t.i)
×
UNCOV
66
                        .OrderBy(i => i).ToList();
×
67

UNCOV
68
                leadingIndices.Add(lines.Count);
×
69

UNCOV
70
                while (lines.TryDequeue(out string line))
×
UNCOV
71
                {
×
UNCOV
72
                        if (line.StartsWith("*"))
×
UNCOV
73
                        {
×
UNCOV
74
                                int index = line.IndexOf(',');
×
UNCOV
75
                                string noPrefix = line.Remove(0, 1);
×
UNCOV
76
                                current = new HatchPattern(noPrefix.Substring(0, index - 1));
×
UNCOV
77
                                current.Description = new string(noPrefix.Skip(index).ToArray()).Trim();
×
78

UNCOV
79
                                patterns.Add(current);
×
UNCOV
80
                        }
×
81
                        else
UNCOV
82
                        {
×
83
                                //;angle, x, y, shift, offset, [dash, space, ...]
UNCOV
84
                                string[] data = line.Split(',');
×
UNCOV
85
                                Line l = new Line();
×
86
                                //angle
UNCOV
87
                                l.Angle = MathHelper.DegToRad(double.Parse(data[0], CultureInfo.InvariantCulture));
×
88
                                //x, y
UNCOV
89
                                l.BasePoint = new XY(double.Parse(data[1], CultureInfo.InvariantCulture), double.Parse(data[2], CultureInfo.InvariantCulture));
×
90

UNCOV
91
                                double shift = double.Parse(data[3], CultureInfo.InvariantCulture);
×
UNCOV
92
                                double offset = double.Parse(data[4], CultureInfo.InvariantCulture);
×
UNCOV
93
                                XY dir = new XY(shift, offset);
×
UNCOV
94
                                double cos = Math.Cos(l.Angle);
×
UNCOV
95
                                double sin = Math.Sin(l.Angle);
×
UNCOV
96
                                l.Offset = new XY(dir.X * cos - dir.Y * sin, dir.X * sin + dir.Y * cos);
×
97

UNCOV
98
                                IEnumerable<string> dashes = data.Skip(5);
×
UNCOV
99
                                if (dashes.Any())
×
UNCOV
100
                                {
×
UNCOV
101
                                        l.DashLengths.AddRange(dashes.Select(d => double.Parse(d, CultureInfo.InvariantCulture)));
×
UNCOV
102
                                }
×
103

UNCOV
104
                                current.Lines.Add(l);
×
UNCOV
105
                        }
×
UNCOV
106
                }
×
107

UNCOV
108
                return patterns;
×
UNCOV
109
        }
×
110

111
        /// <summary>
112
        /// Write a pattern or a collection of patterns into a .pat file.
113
        /// </summary>
114
        /// <param name="filename"></param>
115
        /// <param name="patterns"></param>
116
        public static void SavePatterns(string filename, params HatchPattern[] patterns)
UNCOV
117
        {
×
UNCOV
118
                using StreamWriter writer = File.CreateText(filename);
×
119

UNCOV
120
                foreach (HatchPattern p in patterns)
×
UNCOV
121
                {
×
UNCOV
122
                        writer.Write($"*{p.Name}");
×
123

UNCOV
124
                        if (!p.Description.IsNullOrEmpty())
×
UNCOV
125
                        {
×
UNCOV
126
                                writer.Write($",{p.Description}");
×
UNCOV
127
                        }
×
128

UNCOV
129
                        writer.WriteLine();
×
130

UNCOV
131
                        foreach (Line line in p.Lines)
×
UNCOV
132
                        {
×
UNCOV
133
                                StringBuilder sb = new StringBuilder();
×
134

UNCOV
135
                                double angle = MathHelper.DegToRad(line.Angle);
×
UNCOV
136
                                double cos = Math.Cos(0.0 - line.Angle);
×
UNCOV
137
                                double sin = Math.Sin(0.0 - line.Angle);
×
138

UNCOV
139
                                var v = new XY(line.Offset.X * cos - line.Offset.Y * sin, line.Offset.X * sin + line.Offset.Y * cos);
×
140

UNCOV
141
                                sb.Append(angle.ToString(CultureInfo.InvariantCulture));
×
UNCOV
142
                                sb.Append(",");
×
UNCOV
143
                                sb.Append(line.BasePoint.ToString(CultureInfo.InvariantCulture));
×
UNCOV
144
                                sb.Append(",");
×
UNCOV
145
                                sb.Append(v.ToString(CultureInfo.InvariantCulture));
×
146

UNCOV
147
                                if (line.DashLengths.Count > 0)
×
UNCOV
148
                                {
×
UNCOV
149
                                        sb.Append(",");
×
UNCOV
150
                                        sb.Append(line.DashLengths[0].ToString(CultureInfo.InvariantCulture));
×
UNCOV
151
                                        for (int i = 1; i < line.DashLengths.Count; i++)
×
UNCOV
152
                                        {
×
UNCOV
153
                                                sb.Append(",");
×
UNCOV
154
                                                sb.Append(line.DashLengths[i].ToString(CultureInfo.InvariantCulture));
×
UNCOV
155
                                        }
×
UNCOV
156
                                }
×
157

UNCOV
158
                                writer.WriteLine(sb.ToString());
×
UNCOV
159
                        }
×
UNCOV
160
                }
×
UNCOV
161
        }
×
162

163
        /// <summary>
164
        /// Clones the current pattern.
165
        /// </summary>
166
        /// <returns></returns>
167
        public HatchPattern Clone()
UNCOV
168
        {
×
UNCOV
169
                HatchPattern clone = (HatchPattern)this.MemberwiseClone();
×
170

UNCOV
171
                clone.Lines = new List<Line>();
×
UNCOV
172
                foreach (var item in this.Lines)
×
173
                {
×
174
                        clone.Lines.Add(item.Clone());
×
175
                }
×
176

UNCOV
177
                return clone;
×
UNCOV
178
        }
×
179

180
        /// <inheritdoc/>
181
        public override string ToString()
182
        {
×
183
                return $"{this.Name}";
×
184
        }
×
185

186
        /// <summary>
187
        /// Update the pattern geometry with a translation, rotation and scale.
188
        /// </summary>
189
        /// <param name="translation"></param>
190
        /// <param name="rotation"></param>
191
        /// <param name="scale"></param>
192
        public void Update(XY translation, double rotation, double scale)
UNCOV
193
        {
×
UNCOV
194
                var tr = Transform.CreateTranslation(translation.Convert<XYZ>());
×
UNCOV
195
                var sc = Transform.CreateScaling(new XYZ(scale));
×
UNCOV
196
                var rot = Transform.CreateRotation(XYZ.AxisZ, rotation);
×
197

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

UNCOV
200
                foreach (var line in Lines)
×
UNCOV
201
                {
×
UNCOV
202
                        line.Angle += rotation;
×
UNCOV
203
                        line.BasePoint = transform.ApplyTransform(line.BasePoint.Convert<XYZ>()).Convert<XY>();
×
UNCOV
204
                        line.Offset = transform.ApplyTransform(line.Offset.Convert<XYZ>()).Convert<XY>();
×
UNCOV
205
                        line.DashLengths = line.DashLengths.Select(d => d * scale).ToList();
×
UNCOV
206
                }
×
UNCOV
207
        }
×
208
}
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