1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_Line.h"
21 #include "SketchPlugin_Sketch.h"
22 #include "SketchPlugin_ConstraintCoincidence.h"
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_EventReentrantMessage.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeBoolean.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_Validator.h>
31 #include <ModelAPI_Session.h>
33 #include <GeomAPI_Edge.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt.h>
36 #include <GeomAPI_Pnt2d.h>
38 #include <GeomDataAPI_Point2D.h>
40 SketchPlugin_Line::SketchPlugin_Line()
41 : SketchPlugin_SketchEntity()
44 void SketchPlugin_Line::initAttributes()
46 SketchPlugin_SketchEntity::initAttributes();
47 /// new attributes should be added to end of the feature in order to provide
48 /// correct attribute values in previous saved studies
49 data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeDouble::typeId());
51 data()->addAttribute(PARENT_ID(), ModelAPI_AttributeReference::typeId());
52 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PARENT_ID());
55 void SketchPlugin_Line::initDerivedClassAttributes()
57 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
58 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
59 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
63 void SketchPlugin_Line::execute()
65 SketchPlugin_Sketch* aSketch = sketch();
67 // compute a start point in 3D view
68 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
69 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
70 // compute an end point in 3D view
71 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
72 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
73 if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
74 SketchPlugin_Sketch::createLine2DResult(this, aSketch, START_ID(), END_ID());
76 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
77 std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
78 <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(anId, this));
79 aMessage->setCreatedFeature(ModelAPI_Feature::feature(
80 data()->attribute(START_ID())->owner()));
81 Events_Loop::loop()->send(aMessage);
87 std::string SketchPlugin_Line::processEvent(const std::shared_ptr<Events_Message>& theMessage)
89 std::string aFilledAttributeName;
91 std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
92 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
93 if (aReentrantMessage.get()) {
94 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
96 // Initialize new line with first point equal to end of previous
97 std::shared_ptr<ModelAPI_Data> aSFData = aCreatedFeature->data();
98 std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
99 aSFData->attribute(SketchPlugin_Line::END_ID()));
100 std::shared_ptr<ModelAPI_Data> aNFData = data();
101 std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102 aNFData->attribute(SketchPlugin_Line::START_ID()));
103 aNPoint->setValue(aSPoint->x(), aSPoint->y());
104 SketchPlugin_ConstraintCoincidence::createCoincidenceFeature(sketch(), aSPoint, aNPoint);
106 return aFilledAttributeName;
110 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
114 std::shared_ptr<ModelAPI_Data> aData = data();
115 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
116 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
117 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
118 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
120 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
122 if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
123 std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
124 aDelta = aResult->distance(thePoint);
126 aDelta = aLin2d.distance(thePoint);
132 const std::string& SketchPlugin_Line::getKind()
134 static std::string MY_KIND = SketchPlugin_Line::ID();
138 bool SketchPlugin_Line::isFixed() {
139 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
142 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
143 // the second condition for unability to move external segments anywhere
144 // isCopy() is checked temporary for case when copied lines stored external id state
145 // to be removed after debug
146 if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
147 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
149 // empty shape in selection shows that the shape is equal to context
150 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
152 aSelection = anExtRes->shape();
154 // update arguments due to the selection value
155 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
156 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
157 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
158 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
159 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
160 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
161 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
162 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
166 else if (theID == START_ID() || theID == END_ID()) {
171 void SketchPlugin_Line::updateLenghtValue()
173 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
174 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
175 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
176 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
177 if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
178 double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
179 data()->real(LENGTH_ID())->setValue(aDistance);