LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lc::DxfLinePatternByValue Class Reference

#include <dxflinepattern.h>

Inheritance diagram for lc::DxfLinePatternByValue:
Collaboration diagram for lc::DxfLinePatternByValue:

Public Member Functions

 DxfLinePatternByValue ()
 
 DxfLinePatternByValue (const std::string &_name, const std::string &_description, const std::vector< double > &_path, const double length)
 
virtual ~DxfLinePatternByValue ()=default
 
const std::string name () const override
 
std::string description () const override
 
const std::vector< double > & path () const
 
double length () const
 
std::vector< double > generatePattern (const std::vector< double > &dxfPattern, const double length, const double lineWidth) const
 Generate new LibreCAD compatible pattern. More...
 
const std::vector< double > lcPattern (double lineWidth=1) const
 Get cached LibreCAD compatible pattern. More...
 
- Public Member Functions inherited from lc::DxfLinePattern
virtual const std::string metaTypeID () const override
 
virtual const std::string id () const override
 
- Public Member Functions inherited from lc::MetaType
 MetaType ()
 
virtual ~MetaType ()=default
 

Static Public Member Functions

static double calculatePathLength (const std::vector< double > &_path)
 
- Static Public Member Functions inherited from lc::DxfLinePattern
static std::string LCMETANAME ()
 

Private Member Functions

 DxfLinePatternByValue (const builder::LinePatternBuilder &builder)
 

Private Attributes

std::string _name
 
std::string _description
 
std::vector< double > _path
 
std::map< double, std::vector
< double > > 
_lcPatterns
 
double _length
 

Friends

class builder::LinePatternBuilder
 

Detailed Description

Definition at line 35 of file dxflinepattern.h.

Constructor & Destructor Documentation

lc::DxfLinePatternByValue::DxfLinePatternByValue ( )
inline

Definition at line 38 of file dxflinepattern.h.

38 {}
DxfLinePatternByValue::DxfLinePatternByValue ( const std::string &  _name,
const std::string &  _description,
const std::vector< double > &  _path,
const double  length 
)

Definition at line 16 of file dxflinepattern.cpp.

16  :
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 }
const std::vector< double > & path() const
const std::string name() const override
std::string description() const override
std::vector< double > _path
static bool isBlank(const std::string str)
Definition: string_helper.h:49
virtual lc::DxfLinePatternByValue::~DxfLinePatternByValue ( )
virtualdefault
DxfLinePatternByValue::DxfLinePatternByValue ( const builder::LinePatternBuilder builder)
private

Definition at line 22 of file dxflinepattern.cpp.

22  :
23  _name(builder.name()),
24  _description(builder.description()),
25  _path(builder.path()),
26  _length(calculatePathLength(builder.path())) {
27 
28 }
static double calculatePathLength(const std::vector< double > &_path)
const std::vector< double > & path() const
Definition: linepattern.cpp:25
const std::string & description() const
Definition: linepattern.cpp:21
std::vector< double > _path
const std::string & name() const
Definition: linepattern.cpp:17

Member Function Documentation

double DxfLinePatternByValue::calculatePathLength ( const std::vector< double > &  _path)
static

Calculates the total length of a path

Definition at line 30 of file dxflinepattern.cpp.

30  {
31  return std::fabs(std::accumulate(_path.begin(), _path.end(), 0.));
32 }
std::vector< double > _path
std::string DxfLinePatternByValue::description ( ) const
overridevirtual

Reimplemented from lc::DxfLinePattern.

Definition at line 106 of file dxflinepattern.cpp.

106  {
107  return _description;
108 }
std::vector< double > DxfLinePatternByValue::generatePattern ( const std::vector< double > &  dxfPattern,
const double  length,
const double  lineWidth 
) const

Generate new LibreCAD compatible pattern.

Parameters
dxfPatternPattern from DXF file
lengthLength of DXF pattern
lineWidthWidth of the line (for dots size)
Returns
New line pattern Generate a new pattern compatible with LibreCAD from a DXF pattern.

Definition at line 34 of file dxflinepattern.cpp.

34  {
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 }
const std::vector< double > DxfLinePatternByValue::lcPattern ( double  lineWidth = 1) const

Get cached LibreCAD compatible pattern.

Parameters
lineWidthWidth of the line (for dots size)
Returns
New or cached line pattern Get LibreCAD compatible pattern from cache and generate it if needed.

Definition at line 91 of file dxflinepattern.cpp.

91  {
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 }
std::map< double, std::vector< double > > _lcPatterns
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.
double lc::DxfLinePatternByValue::length ( ) const

Definition at line 114 of file dxflinepattern.cpp.

114  {
115  return _length;
116 }
const std::string DxfLinePatternByValue::name ( ) const
overridevirtual

Name of this meta type, for layer this can be '0' 'My Layer' etc, for LineStyles this can be 'DASHDOT' .

Implements lc::DocumentMetaType.

Definition at line 102 of file dxflinepattern.cpp.

102  {
103  return _name;
104 }
const std::vector< double > & DxfLinePatternByValue::path ( ) const

Definition at line 110 of file dxflinepattern.cpp.

110  {
111  return _path;
112 }
std::vector< double > _path

Friends And Related Function Documentation

friend class builder::LinePatternBuilder
friend

Definition at line 36 of file dxflinepattern.h.

Member Data Documentation

std::string lc::DxfLinePatternByValue::_description
private

Definition at line 74 of file dxflinepattern.h.

std::map<double, std::vector<double> > lc::DxfLinePatternByValue::_lcPatterns
mutableprivate

Definition at line 79 of file dxflinepattern.h.

double lc::DxfLinePatternByValue::_length
private

Definition at line 81 of file dxflinepattern.h.

std::string lc::DxfLinePatternByValue::_name
private

Definition at line 73 of file dxflinepattern.h.

std::vector<double> lc::DxfLinePatternByValue::_path
private

Definition at line 75 of file dxflinepattern.h.


The documentation for this class was generated from the following files: