Salome HOME
Issues #2027, #2024, #2063, #2067: reentrant message to fill new operation by result...
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Line.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Line.cxx
4 // Created:     27 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "SketchPlugin_Line.h"
8 #include "SketchPlugin_Sketch.h"
9 #include "SketchPlugin_ConstraintCoincidence.h"
10
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_EventReentrantMessage.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_AttributeSelection.h>
15 #include <ModelAPI_AttributeBoolean.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_Session.h>
19
20 #include <GeomAPI_Pnt.h>
21 #include <GeomAPI_Lin2d.h>
22 #include <GeomAPI_Pnt2d.h>
23
24 #include <GeomAlgoAPI_EdgeBuilder.h>
25 #include <GeomDataAPI_Point2D.h>
26
27 SketchPlugin_Line::SketchPlugin_Line()
28     : SketchPlugin_SketchEntity()
29 {}
30
31 void SketchPlugin_Line::initAttributes()
32 {
33   SketchPlugin_SketchEntity::initAttributes();
34   /// new attributes should be added to end of the feature in order to provide
35   /// correct attribute values in previous saved studies
36   data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeDouble::typeId());
37 }
38
39 void SketchPlugin_Line::initDerivedClassAttributes()
40 {
41   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
42   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
43   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
44   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
45 }
46
47 void SketchPlugin_Line::execute()
48 {
49   SketchPlugin_Sketch* aSketch = sketch();
50   if (aSketch) {
51     // compute a start point in 3D view
52     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
53         GeomDataAPI_Point2D>(data()->attribute(START_ID()));
54     // compute an end point in 3D view
55     std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
56         GeomDataAPI_Point2D>(data()->attribute(END_ID()));
57     if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
58       std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
59       std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
60       //std::cout<<"Execute line "<<aStart->x()<<" "<<aStart->y()<<" "<<aStart->z()<<" - "
61       //  <<anEnd->x()<<" "<<anEnd->y()<<" "<<anEnd->z()<<std::endl;
62       // make linear edge
63       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
64       // store the result
65       std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(
66           data());
67       aConstr->setShape(anEdge);
68       aConstr->setIsInHistory(false);
69       setResult(aConstr);
70
71       static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
72       std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
73           <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(anId, 0));
74       aMessage->setCreatedFeature(ModelAPI_Feature::feature(
75                                   data()->attribute(START_ID())->owner()));
76       Events_Loop::loop()->send(aMessage);
77     }
78   }
79 }
80
81 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
82 {
83   std::shared_ptr<ModelAPI_Data> aData = data();
84   if (!aData->isValid())
85     return;
86
87   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
88     (aData->attribute(START_ID()));
89   aPoint1->move(theDeltaX, theDeltaY);
90
91   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
92     (aData->attribute(END_ID()));
93   aPoint2->move(theDeltaX, theDeltaY);
94 }
95
96 std::string SketchPlugin_Line::processEvent(const std::shared_ptr<Events_Message>& theMessage)
97 {
98   std::string aFilledAttributeName;
99
100   std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
101         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
102   if (aReentrantMessage.get()) {
103     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
104
105     // Initialize new line with first point equal to end of previous
106     std::shared_ptr<ModelAPI_Data> aSFData = aCreatedFeature->data();
107     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
109     std::shared_ptr<ModelAPI_Data> aNFData = data();
110     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
112     aNPoint->setValue(aSPoint->x(), aSPoint->y());
113     SketchPlugin_ConstraintCoincidence::createCoincidenceFeature(sketch(), aSPoint, aNPoint);
114
115     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
117     aNPoint->setValue(aSPoint->x(), aSPoint->y());
118
119   }
120   return aFilledAttributeName;
121 }
122
123 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
124 {
125   double aDelta = 0;
126
127   std::shared_ptr<ModelAPI_Data> aData = data();
128   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
129     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
130   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
131     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
132
133   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
134
135   if (false/*projection*/) {  // TODO: if it has not been necessary, remove this block
136     std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
137     aDelta = aResult->distance(thePoint);
138   } else {  // distance
139     aDelta = aLin2d.distance(thePoint);
140   }
141
142   return aDelta;
143 }
144
145 const std::string& SketchPlugin_Line::getKind()
146 {
147   static std::string MY_KIND = SketchPlugin_Line::ID();
148   return MY_KIND;
149 }
150
151 bool SketchPlugin_Line::isFixed() {
152   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
153 }
154
155 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
156   // the second condition for unability to move external segments anywhere
157   // isCopy() is checked temporary for case when copied lines stored external id state
158   // to be removed after debug
159   if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
160     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
161     if (!aSelection) {
162       // empty shape in selection shows that the shape is equal to context
163       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
164       if (anExtRes)
165         aSelection = anExtRes->shape();
166     }
167     // update arguments due to the selection value
168     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
169       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
170       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
171         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
172       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
173       std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
174         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
175       anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
176       updateLenghtValue();
177     }
178   }
179   else if (theID == START_ID() || theID == END_ID()) {
180     updateLenghtValue();
181   }
182 }
183
184 void SketchPlugin_Line::updateLenghtValue()
185 {
186   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
187       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
188   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
189       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
190   if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
191     double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
192     data()->real(LENGTH_ID())->setValue(aDistance);
193   }
194 }