LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dxflinepattern.cpp
Go to the documentation of this file.
1 //
2 // Created by R. van Twisk on 4/14/16.
3 //
4 
5 #include "dxflinepattern.h"
6 #include <cmath>
7 #include <numeric>
9 
10 using namespace lc;
11 
12 std::string DxfLinePattern::description() const {
13  return "";
14 }
15 
16 DxfLinePatternByValue::DxfLinePatternByValue(const std::string &name, const std::string &description, const std::vector<double> &path, const double length) :
17  _name(name), _description(description), _path(path), _length(length) {
18  assert(!StringHelper::isBlank(name) > 0 && "Name of DxfLinePatternByValue must be given");
19  // Continues has a path length of 0 assert(_path.size() > 0 && "Path length must be > 0");
20 }
21 
23  _name(builder.name()),
24  _description(builder.description()),
25  _path(builder.path()),
26  _length(calculatePathLength(builder.path())) {
27 
28 }
29 
30 double DxfLinePatternByValue::calculatePathLength(const std::vector<double> &_path) {
31  return std::fabs(std::accumulate(_path.begin(), _path.end(), 0.));
32 }
33 
34 std::vector<double> DxfLinePatternByValue::generatePattern(const std::vector<double> &dxfPattern, const double length, const double lineWidth) const {
35  // DXF Linestyle Pattern is as follows
36  // Parameters pattern – is a list of float values, elements > 0 are solid line segments, elements < 0 are gaps and elements = 0 are points. pattern[0] = total pattern length in drawing units
37  // w need to generate them as follows:
38  //double dashes[] = {50.0, /* ink */
39  // 10.0, /* skip */
40  // 10.0, /* ink */
41  // 10.0 /* skip*/
42  //};
43  std::vector<double> dxfPat;
44 
45  if (dxfPattern.size() == 0) {
46  return dxfPat;
47  }
48 
49  double last = dxfPattern.at(0);
50  bool isInk = true;
51  for (auto d : dxfPattern) {
52  if (d == 0.) { // dot
53  if (isInk) {
54  dxfPat.push_back(lineWidth);
55  } else {
56  dxfPat.push_back(0.);
57  dxfPat.push_back(lineWidth);
58  isInk = !isInk;
59  }
60  d = last + 1;
61  } else if (d < 0.) { // skip
62  if (isInk) {
63  dxfPat.push_back(0.);
64  dxfPat.push_back(std::fabs(d));
65  isInk = !isInk;
66  } else {
67  dxfPat.push_back(std::fabs(d));
68  }
69  } else { // ink
70  if (isInk) {
71  dxfPat.push_back(d);
72  } else {
73  dxfPat.push_back(0.);
74  dxfPat.push_back(d);
75  isInk = !isInk;
76  }
77  }
78  last = last + std::fabs(d);
79  isInk = !isInk;
80  }
81 
82  // Set length for this pattern
83  double mul = length / last;
84  for (unsigned int i = 0; i < dxfPat.size(); i++) {
85  dxfPat[i] = dxfPat[i] * mul;
86  }
87 
88  return dxfPat;
89 }
90 
91 const std::vector<double> DxfLinePatternByValue::lcPattern(double lineWidth) const {
92  try {
93  return _lcPatterns.at(lineWidth);
94  }
95  catch (std::out_of_range &e) {
96  auto pattern = generatePattern(_path, _length, lineWidth);
97  _lcPatterns[lineWidth] = pattern;
98  return pattern;
99  }
100 }
101 
102 const std::string DxfLinePatternByValue::name() const {
103  return _name;
104 }
105 
107  return _description;
108 }
109 
110 const std::vector<double>& DxfLinePatternByValue::path() const {
111  return _path;
112 }
113 
115  return _length;
116 }
117 
118 const std::string DxfLinePatternByBlock::name() const {
119  return "ByBlock";
120 }
static double calculatePathLength(const std::vector< double > &_path)
virtual std::string description() const
const std::vector< double > lcPattern(double lineWidth=1) const
Get cached LibreCAD compatible pattern.
Definition: cadentity.h:12
const std::vector< double > & path() const
const std::string name() const override
std::map< double, std::vector< double > > _lcPatterns
std::string description() const override
virtual const std::string name() const override
std::vector< double > _path
std::vector< double > generatePattern(const std::vector< double > &dxfPattern, const double length, const double lineWidth) const
Generate new LibreCAD compatible pattern.
static bool isBlank(const std::string str)
Definition: string_helper.h:49