Permalink
Browse files

Add DxfLinePattern builder

  • Loading branch information...
feragon committed Jun 20, 2017
1 parent 0a88c44 commit f226485d74dce1f64c190771db192ed2648f7aad
View
@@ -70,6 +70,7 @@ cad/builders/circle.cpp
cad/builders/point.cpp
cad/builders/insert.cpp
cad/builders/layer.cpp
cad/builders/linepattern.cpp
)
# HEADER FILES
@@ -161,6 +162,7 @@ cad/builders/point.h
cad/primitive/insert.h
cad/builders/insert.h
cad/builders/layer.h
cad/builders/linepattern.h
)
# LOG4CXX
@@ -0,0 +1,59 @@
#include "linepattern.h"
#include <cad/meta/dxflinepattern.h>
using namespace lc;
using namespace builder;
LinePatternBuilder::LinePatternBuilder() :
_name(),
_description() {
}
const std::string& LinePatternBuilder::name() const {
return _name;
}
const std::string& LinePatternBuilder::description() const {
return _description;
}
const std::vector<double>& LinePatternBuilder::path() const {
return _path;
}
LinePatternBuilder* LinePatternBuilder::setName(const std::string& name) {
_name = name;
return this;
}
LinePatternBuilder* LinePatternBuilder::setDescription(const std::string& description) {
_description = description;
return this;
}
LinePatternBuilder* LinePatternBuilder::setPath(const std::vector<double>& path) {
_path = path;
return this;
}
bool LinePatternBuilder::checkValues() {
return _name != "";
}
DxfLinePattern_CSPtr LinePatternBuilder::build() {
if(!checkValues()) {
throw "Missing values";
}
return DxfLinePattern_CSPtr(new DxfLinePattern(*this));
}
LinePatternBuilder* LinePatternBuilder::addElement(double element) {
_path.push_back(element);
return this;
}
@@ -0,0 +1,34 @@
#pragma once
#include <string>
#include <vector>
#include <memory>
namespace lc {
class DxfLinePattern;
using DxfLinePattern_CSPtr = std::shared_ptr<const DxfLinePattern>;
namespace builder {
class LinePatternBuilder {
public:
LinePatternBuilder();
const std::string& name() const;
const std::string& description() const;
const std::vector<double>& path() const;
LinePatternBuilder* setName(const std::string& name);
LinePatternBuilder* setDescription(const std::string& description);
LinePatternBuilder* setPath(const std::vector<double>& path);
LinePatternBuilder* addElement(double element);
bool checkValues();
DxfLinePattern_CSPtr build();
private:
std::string _name;
std::string _description;
std::vector<double> _path;
};
}
}
@@ -15,6 +15,14 @@ DxfLinePattern::DxfLinePattern(const std::string &name, const std::string &descr
// Continues has a path length of 0 assert(_path.size() > 0 && "Path length must be > 0");
}
DxfLinePattern::DxfLinePattern(const builder::LinePatternBuilder& builder) :
_name(builder.name()),
_description(builder.description()),
_path(builder.path()),
_length(calculatePathLength(builder.path())) {
}
double DxfLinePattern::calculatePathLength(const std::vector<double> &_path) {
return std::fabs(std::accumulate(_path.begin(), _path.end(), 0.));
}
@@ -9,13 +9,14 @@
#include <cassert>
#include <vector>
#include <map>
#include <cad/builders/linepattern.h>
namespace lc {
/**
* Represents a line style
*/
class DxfLinePattern : public DocumentMetaType, public EntityMetaType {
friend class builder::LinePatternBuilder;
public:
@@ -78,6 +79,8 @@ namespace lc {
const std::vector<double> lcPattern(double lineWidth = 1) const;
private:
DxfLinePattern(const builder::LinePatternBuilder& builder);
std::string _name;
std::string _description;
std::vector<double> _path;
@@ -8,6 +8,7 @@
#include <cad/primitive/insert.h>
#include <cad/dochelpers/documentimpl.h>
#include <cad/dochelpers/storagemanagerimpl.h>
#include <cad/meta/dxflinepattern.h>
TEST(BuilderTest, Line) {
auto layer = std::make_shared<lc::Layer>();
@@ -158,4 +159,26 @@ TEST(BuilderTest, Layer) {
EXPECT_EQ(lw.width(), layer->lineWidth().width());
EXPECT_TRUE(color == layer->color());
EXPECT_TRUE(layer->isFrozen());
}
TEST(BuilderTest, DxfLinePattern) {
auto builder = lc::builder::LinePatternBuilder();
auto name = "Test LP";
auto description = "Test";
std::vector<double> initialPath = {5, 0, 1, 2};
double lastElement = 3;
auto lp = builder
.setName(name)
->setDescription(description)
->setPath(initialPath)
->addElement(lastElement)
->build();
initialPath.push_back(lastElement);
EXPECT_EQ(name, lp->name());
EXPECT_EQ(description, lp->description());
EXPECT_EQ(initialPath, lp->path());
EXPECT_EQ(11, lp->length());
}

0 comments on commit f226485

Please sign in to comment.