Skip to content

Commit

Permalink
Merge pull request #91 from feragon/lpwidth
Browse files Browse the repository at this point in the history
DxfLinePattern line width support
  • Loading branch information
rvt committed Jul 22, 2016
2 parents d824af7 + 6ee1666 commit bbdbb80
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
20 changes: 15 additions & 5 deletions lckernel/cad/meta/dxflinepattern.cpp
Expand Up @@ -10,7 +10,7 @@
using namespace lc;

DxfLinePattern::DxfLinePattern(const std::string &name, const std::string &description, const std::vector<double> &path, const double length) :
_name(name), _description(description), _path(path), _lcPattern(generatePattern(path, length)), _length(length) {
_name(name), _description(description), _path(path), _length(length) {
assert(!StringHelper::isBlank(name) > 0 && "Name of DxfLinePattern must be given");
// Continues has a path length of 0 assert(_path.size() > 0 && "Path length must be > 0");
}
Expand All @@ -19,7 +19,7 @@ double DxfLinePattern::calculatePathLength(const std::vector<double> &_path) {
return std::fabs(std::accumulate(_path.begin(), _path.end(), 0.));
}

std::vector<double> DxfLinePattern::generatePattern(const std::vector<double> &dxfPattern, const double length) const {
std::vector<double> DxfLinePattern::generatePattern(const std::vector<double> &dxfPattern, const double length, const double lineWidth) const {
// DXF Linestyle Pattern is as follows
// 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
// w need to generate them as follows:
Expand All @@ -39,13 +39,13 @@ std::vector<double> DxfLinePattern::generatePattern(const std::vector<double> &d
for (auto d : dxfPattern) {
if (d == 0.) { // dot
if (isInk) {
dxfPat.push_back(.1);
dxfPat.push_back(lineWidth);
} else {
dxfPat.push_back(0.);
dxfPat.push_back(.1);
dxfPat.push_back(lineWidth);
isInk = !isInk;
}
d = last + 0.1;
d = last + 1;
} else if (d < 0.) { // skip
if (isInk) {
dxfPat.push_back(0.);
Expand Down Expand Up @@ -76,3 +76,13 @@ std::vector<double> DxfLinePattern::generatePattern(const std::vector<double> &d
return dxfPat;
}

const std::vector<double> DxfLinePattern::lcPattern(double lineWidth) const {
try {
return _lcPatterns.at(lineWidth);
}
catch (std::out_of_range &e) {
auto pattern = generatePattern(_path, _length, lineWidth);
_lcPatterns[lineWidth] = pattern;
return pattern;
}
}
13 changes: 8 additions & 5 deletions lckernel/cad/meta/dxflinepattern.h
Expand Up @@ -8,6 +8,7 @@
#include "cad/const.h"
#include <cassert>
#include <vector>
#include <map>

namespace lc {
/**
Expand Down Expand Up @@ -58,17 +59,19 @@ namespace lc {
return "_LINEPATTERN";
}

std::vector<double> generatePattern(const std::vector<double> & dxfPattern, const double length) const;
std::vector<double> generatePattern(const std::vector<double> & dxfPattern, const double length, const double lineWidth) const;

const std::vector<double> &lcPattern() const {
return _lcPattern;
}
const std::vector<double> lcPattern(double lineWidth = 1) const;

private:
std::string _name;
std::string _description;
std::vector<double> _path;
std::vector<double> _lcPattern;

//Might grow out of proportions if too much line sizes are rendered
//We should find a way to remove widths unrendered for x drawing cycles
mutable std::map<double, std::vector<double>> _lcPatterns;

double _length;
};
using DxfLinePattern_SPtr = std::shared_ptr<DxfLinePattern>;
Expand Down
31 changes: 14 additions & 17 deletions lcviewernoqt/documentcanvas.cpp
Expand Up @@ -361,28 +361,25 @@ void DocumentCanvas::drawEntity(LCVDrawItem_CSPtr entity) {
// Used to give the illusation from slightly thinner lines. Not sure yet what to d with it and if I will keep it
double alpha_compensation = 0.9;

// Decide on line width
// Decide on line width
// We multiply for now by 3 to ensure that 1mm lines will still appear thicker on screen
// TODO: Find a better algo
double width;
if (entityLineWidth != nullptr) {
// We multiply for now by 3 to ensure that 1mm lines will still appear thicker on screen
// TODO: Find a better algo
double width = entityLineWidth->width() * 1.5;
// Is this correct? May be we should decide on a different minimum width then 0.1, because may be on some devices 0.11 isn't visible?
painter.line_width(std::max(width, MINIMUM_READER_LINEWIDTH));
width = entityLineWidth->width() * 1.5;
} else {
// We multiply for now by 3 to ensure that 1mm lines will still appear thicker on screen
// TODO: Find a better algo
double width = layer->lineWidth().width() * 1.5;
// Is this correct? May be we should decide on a different minimum width then 0.1, because may be on some devices 0.11 isn't visible?
painter.line_width(std::max(width, MINIMUM_READER_LINEWIDTH));
width = layer->lineWidth().width() * 1.5;
}
// Is this correct? May be we should decide on a different minimum width then 0.1, because may be on some devices 0.11 isn't visible?
painter.line_width(std::max(width, MINIMUM_READER_LINEWIDTH));

if (entityLinePattern != nullptr && entityLinePattern->lcPattern().size()>0) {
const double* path = &entityLinePattern->lcPattern()[0];
painter.set_dash(path, entityLinePattern->lcPattern().size(), 0., true);
if (entityLinePattern != nullptr && entityLinePattern->lcPattern(width).size()>0) {
auto path = entityLinePattern->lcPattern(width);
painter.set_dash(&path[0], path.size(), 0., true);
}
else if(layer->linePattern() != nullptr && layer->linePattern()->lcPattern().size() > 0) {
const double* path = &layer->linePattern()->lcPattern()[0];
painter.set_dash(path, layer->linePattern()->lcPattern().size(), 0., true);
else if(layer->linePattern() != nullptr && layer->linePattern()->lcPattern(width).size() > 0) {
auto path = layer->linePattern()->lcPattern(width);
painter.set_dash(&path[0], path.size(), 0., true);
}

// Decide what color to render the entity into
Expand Down

0 comments on commit bbdbb80

Please sign in to comment.