LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
insert.cpp
Go to the documentation of this file.
1 #include "insert.h"
2 
3 using namespace lc;
4 using namespace entity;
5 
6 Insert::Insert(Insert_CSPtr other, bool sameID) :
7  CADEntity(other, sameID),
8  _document(other->_document),
9  _position(other->_position),
10  _displayBlock(other->_displayBlock) {
11 
13 
14  _document->addEntityEvent().connect<Insert, &Insert::on_addEntityEvent>(this);
15  _document->removeEntityEvent().connect<Insert, &Insert::on_removeEntityEvent>(this);
16 }
17 
19  CADEntity(builder),
20  _document(builder.document()),
21  _position(builder.coordinate()),
22  _displayBlock(builder.displayBlock()) {
23 
25 
26  _document->addEntityEvent().connect<Insert, &Insert::on_addEntityEvent>(this);
27  _document->removeEntityEvent().connect<Insert, &Insert::on_removeEntityEvent>(this);
28 }
29 
31  document()->addEntityEvent().disconnect<Insert, &Insert::on_addEntityEvent>(this);
32  document()->removeEntityEvent().disconnect<Insert, &Insert::on_removeEntityEvent>(this);
33 }
34 
35 const Block_CSPtr& Insert::displayBlock() const {
36  return _displayBlock;
37 }
38 
40  return _position;
41 }
42 
43 CADEntity_CSPtr Insert::move(const geo::Coordinate& offset) const {
44  auto newEntity = std::make_shared<Insert>(shared_from_this(), true);
45  newEntity->_position = _position + offset;
46 
47  return newEntity;
48 }
49 
50 CADEntity_CSPtr Insert::copy(const geo::Coordinate& offset) const {
51  auto newEntity = std::make_shared<Insert>(shared_from_this());
52  newEntity->_position = _position + offset;
53 
54  return newEntity;
55 }
56 
57 CADEntity_CSPtr Insert::rotate(const geo::Coordinate& rotation_center, const double rotation_angle) const {
58  //TODO
59  return shared_from_this();
60 }
61 
62 CADEntity_CSPtr Insert::scale(const geo::Coordinate& scale_center, const geo::Coordinate& scale_factor) const {
63  //TODO
64  return shared_from_this();
65 }
66 
67 CADEntity_CSPtr Insert::mirror(const geo::Coordinate& axis1, const geo::Coordinate& axis2) const {
68  //TODO
69  return shared_from_this();
70 }
71 
73  return _boundingBox;
74 }
75 
76 CADEntity_CSPtr Insert::modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, Block_CSPtr block) const {
77  auto builder = builder::InsertBuilder();
78 
79  builder.copy(shared_from_this());
80  builder.setLayer(layer);
81  builder.setMetaInfo(metaInfo);
82  builder.setBlock(block);
83 
84  return builder.build();
85 }
86 
87 void Insert::dispatch(EntityDispatch& dispatch) const {
88  //TODO
89 }
90 
91 std::map<unsigned int, geo::Coordinate> entity::Insert::dragPoints() const {
92  auto result = std::map<unsigned int, geo::Coordinate>();
93 
94  result.insert(std::pair<unsigned int, geo::Coordinate>(0, _position));
95 
96  return result;
97 }
98 
99 entity::CADEntity_CSPtr entity::Insert::setDragPoints(std::map<unsigned int, lc::geo::Coordinate> dragPoints) const {
100  try {
101  auto newEntity = std::make_shared<Insert>(shared_from_this(), true);
102  newEntity->_position = dragPoints.at(0);
103 
104  return newEntity;
105  }
106  catch(std::out_of_range& e) {
107  return shared_from_this();
108  }
109 }
110 
111 std::vector<lc::EntityCoordinate> entity::Insert::snapPoints(const geo::Coordinate& coord,
112  const SimpleSnapConstrain& simpleSnapConstrain,
113  double minDistanceToSnap,
114  int maxNumberOfSnapPoints) const {
115  std::vector<EntityCoordinate> points;
116 
117  if (simpleSnapConstrain.constrain() & SimpleSnapConstrain::LOGICAL) {
118  points.emplace_back(_position, 0);
119  }
120 
121  Snapable::snapPointsCleanup(points, coord, maxNumberOfSnapPoints, minDistanceToSnap);
122  return points;
123 }
124 
126  return _position;
127 }
128 
129 const Document_SPtr& Insert::document() const {
130  return _document;
131 }
132 
134  auto entities = _document->entitiesByBlock(_displayBlock).asVector();
135 
136  if(entities.empty()) {
138  return;
139  }
140 
141  auto offset = _position - displayBlock()->base();
142  auto it = entities.begin();
143 
144  _boundingBox = (*it)->move(offset)->boundingBox();
145  it++;
146 
147  while (it != entities.end()) {
148  _boundingBox = _boundingBox.merge((*it)->move(offset)->boundingBox());
149  it++;
150  }
151 }
152 
154  if(event.entity()->block() == _displayBlock) {
156  }
157 }
158 
160  if(event.entity()->block() == _displayBlock) {
162  }
163 }
virtual std::vector< EntityCoordinate > snapPoints(const geo::Coordinate &coord, const SimpleSnapConstrain &simpleSnapConstrain, double minDistanceToSnap, int maxNumberOfSnapPoints) const override
Find a number of snap points the line has available This function returns a ordered list...
Definition: insert.cpp:111
entity::CADEntity_CSPtr entity() const
Returns the ID.
CADEntity_CSPtr setDragPoints(std::map< unsigned int, lc::geo::Coordinate > dragPoints) const override
Return modified entity.
Definition: insert.cpp:99
static const uint16_t LOGICAL
Definition: snapconstrain.h:22
Block_CSPtr _displayBlock
Definition: insert.h:53
Insert(Insert_CSPtr other, bool sameID=false)
Definition: insert.cpp:6
Area merge(const Area &other) const
merge two area's and expand if required to largest containing area
Definition: geoarea.h:156
const Block_CSPtr & displayBlock() const
Definition: insert.cpp:35
void dispatch(EntityDispatch &dispatch) const override
Definition: insert.cpp:87
Definition: cadentity.h:12
Document_SPtr _document
Definition: insert.h:51
std::map< unsigned int, geo::Coordinate > dragPoints() const override
Get all points of the entity that can be dragged.
Definition: insert.cpp:91
CADEntity_CSPtr scale(const geo::Coordinate &scale_center, const geo::Coordinate &scale_factor) const override
Function implementation for Scale.
Definition: insert.cpp:62
static void snapPointsCleanup(std::vector< EntityCoordinate > &points, const geo::Coordinate &reference, const unsigned int maxNumberOfSnapPoints, const double minDistanceToSnap)
Definition: snapable.h:53
const geo::Area boundingBox() const override
boundingBox Return the bounding box of this entity. This should be as tight around the entity as poss...
Definition: insert.cpp:72
CADEntity_CSPtr modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, Block_CSPtr block) const override
modify Return a new entity with the same ID bit with possible modified metainfo and/pr layer informat...
Definition: insert.cpp:76
CADEntity_CSPtr mirror(const geo::Coordinate &axis1, const geo::Coordinate &axis2) const override
Definition: insert.cpp:67
CADEntity_CSPtr copy(const geo::Coordinate &offset) const override
Function for Copy.
Definition: insert.cpp:50
const geo::Coordinate & position() const
Definition: insert.cpp:39
void calculateBoundingBox()
Definition: insert.cpp:133
const Document_SPtr & document() const
Definition: insert.cpp:129
virtual geo::Coordinate nearestPointOnPath(const geo::Coordinate &coord) const override
Find the nearest point on the path for this entity for the coordinate coord The path of a entity that...
Definition: insert.cpp:125
geo::Coordinate _position
Definition: insert.h:52
CADEntity_CSPtr move(const geo::Coordinate &offset) const override
Function for Move.
Definition: insert.cpp:43
geo::Area _boundingBox
Definition: insert.h:54
void on_addEntityEvent(const lc::AddEntityEvent &)
Definition: insert.cpp:153
void on_removeEntityEvent(const lc::RemoveEntityEvent &)
Definition: insert.cpp:159
const uint16_t constrain() const
Definition: snapconstrain.h:47
CADEntity_CSPtr rotate(const geo::Coordinate &rotation_center, const double rotation_angle) const override
Function implementation for rotate.
Definition: insert.cpp:57
const entity::CADEntity_CSPtr entity() const
Returns the entity without cast.