Permalink
Browse files

Fix insert bounding box

  • Loading branch information...
feragon committed Jun 24, 2017
1 parent 9868d99 commit fd8a55322b9c084eb7dce97ecf275091c263a803
Showing with 52 additions and 7 deletions.
  1. +46 −7 lckernel/cad/primitive/insert.cpp
  2. +6 −0 lckernel/cad/primitive/insert.h
@@ -9,15 +9,28 @@ Insert::Insert(Insert_CSPtr other, bool sameID) :
_position(other->_position),
_displayBlock(other->_displayBlock) {
calculateBoundingBox();
_document->addEntityEvent().connect<Insert, &Insert::on_addEntityEvent>(this);
_document->removeEntityEvent().connect<Insert, &Insert::on_removeEntityEvent>(this);
}
Insert::Insert(const builder::InsertBuilder& builder) :
CADEntity(builder),
_document(builder.document()),
_position(builder.coordinate()),
_displayBlock(builder.displayBlock()) {
calculateBoundingBox();
_document->addEntityEvent().connect<Insert, &Insert::on_addEntityEvent>(this);
_document->removeEntityEvent().connect<Insert, &Insert::on_removeEntityEvent>(this);
}
Insert::~Insert() {
document()->addEntityEvent().disconnect<Insert, &Insert::on_addEntityEvent>(this);
document()->removeEntityEvent().disconnect<Insert, &Insert::on_removeEntityEvent>(this);
}
const Block_CSPtr& Insert::displayBlock() const {
return _displayBlock;
@@ -57,13 +70,7 @@ CADEntity_CSPtr Insert::mirror(const geo::Coordinate& axis1, const geo::Coordina
}
const geo::Area Insert::boundingBox() const {
geo::Area area;
for(auto entity : _document->entitiesByBlock(_displayBlock).asVector()) {
area.merge(entity->boundingBox());
}
return area;
return _boundingBox;
}
CADEntity_CSPtr Insert::modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo) const {
@@ -116,3 +123,35 @@ geo::Coordinate Insert::nearestPointOnPath(const geo::Coordinate& coord) const {
const Document_SPtr& Insert::document() const {
return _document;
}
void Insert::calculateBoundingBox() {
auto entities = _document->entitiesByBlock(_displayBlock).asVector();
if(entities.empty()) {
_boundingBox = geo::Area(_position, _position);
return;
}
auto offset = _position - displayBlock()->base();
auto it = entities.begin();
_boundingBox = (*it)->move(offset)->boundingBox();
it++;
while (it != entities.end()) {
_boundingBox.merge((*it)->move(offset)->boundingBox());
it++;
}
}
void Insert::on_addEntityEvent(const lc::AddEntityEvent& event) {
if(event.entity()->block() == _displayBlock) {
calculateBoundingBox();
}
}
void Insert::on_removeEntityEvent(const lc::RemoveEntityEvent& event) {
if(event.entity()->block() == _displayBlock) {
calculateBoundingBox();
}
}
@@ -14,6 +14,7 @@ namespace lc {
public:
Insert(Insert_CSPtr other, bool sameID = false);
~Insert();
const Block_CSPtr& displayBlock() const;
const geo::Coordinate& position() const;
@@ -40,10 +41,15 @@ namespace lc {
private:
Insert(const builder::InsertBuilder& builder);
void calculateBoundingBox();
void on_addEntityEvent(const lc::AddEntityEvent&);
void on_removeEntityEvent(const lc::RemoveEntityEvent&);
Document_SPtr _document;
geo::Coordinate _position;
Block_CSPtr _displayBlock;
geo::Area _boundingBox;
};
using Insert_SPtr = std::shared_ptr<Insert>;

0 comments on commit fd8a553

Please sign in to comment.