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       Events_Loop::loop()->flush(anId);
78     }
79   }
80 }
81
82 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
83 {
84   std::shared_ptr<ModelAPI_Data> aData = data();
85   if (!aData->isValid())
86     return;
87
88   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
89     (aData->attribute(START_ID()));
90   aPoint1->move(theDeltaX, theDeltaY);
91
92   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
93     (aData->attribute(END_ID()));
94   aPoint2->move(theDeltaX, theDeltaY);
95 }
96
97 std::string SketchPlugin_Line::processEvent(const std::shared_ptr<Events_Message>& theMessage)
98 {
99   std::string aFilledAttributeName;
100
101   std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
102         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
103   if (aReentrantMessage.get()) {
104     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
105
106     // Initialize new line with first point equal to end of previous
107     std::shared_ptr<ModelAPI_Data> aSFData = aCreatedFeature->data();
108     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
110     std::shared_ptr<ModelAPI_Data> aNFData = data();
111     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
112                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
113     aNPoint->setValue(aSPoint->x(), aSPoint->y());
114     SketchPlugin_ConstraintCoincidence::createCoincidenceFeature(sketch(), aSPoint, aNPoint);
115
116     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
117                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
118     aNPoint->setValue(aSPoint->x(), aSPoint->y());
119
120   }
121   return aFilledAttributeName;
122 }
123
124 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
125 {
126   double aDelta = 0;
127
128   std::shared_ptr<ModelAPI_Data> aData = data();
129   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
130     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
131   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
132     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
133
134   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
135
136   if (false/*projection*/) {  // TODO: if it has not been necessary, remove this block
137     std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
138     aDelta = aResult->distance(thePoint);
139   } else {  // distance
140     aDelta = aLin2d.distance(thePoint);
141   }
142
143   return aDelta;
144 }
145
146 const std::string& SketchPlugin_Line::getKind()
147 {
148   static std::string MY_KIND = SketchPlugin_Line::ID();
149   return MY_KIND;
150 }
151
152 bool SketchPlugin_Line::isFixed() {
153   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
154 }
155
156 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
157   // the second condition for unability to move external segments anywhere
158   // isCopy() is checked temporary for case when copied lines stored external id state
159   // to be removed after debug
160   if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
161     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
162     if (!aSelection) {
163       // empty shape in selection shows that the shape is equal to context
164       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
165       if (anExtRes)
166         aSelection = anExtRes->shape();
167     }
168     // update arguments due to the selection value
169     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
170       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
171       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
172         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
173       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
174       std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
175         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
176       anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
177       updateLenghtValue();
178     }
179   }
180   else if (theID == START_ID() || theID == END_ID()) {
181     updateLenghtValue();
182   }
183 }
184
185 void SketchPlugin_Line::updateLenghtValue()
186 {
187   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
188       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
189   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
190       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
191   if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
192     double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
193     data()->real(LENGTH_ID())->setValue(aDistance);
194   }
195 }