Salome HOME
Issue #2024 Redesign of circle and arc of circle: message of reentrant Line should...
[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, this));
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   return aFilledAttributeName;
116 }
117
118 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
119 {
120   double aDelta = 0;
121
122   std::shared_ptr<ModelAPI_Data> aData = data();
123   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
124     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
125   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
126     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
127
128   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
129
130   if (false/*projection*/) {  // TODO: if it has not been necessary, remove this block
131     std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
132     aDelta = aResult->distance(thePoint);
133   } else {  // distance
134     aDelta = aLin2d.distance(thePoint);
135   }
136
137   return aDelta;
138 }
139
140 const std::string& SketchPlugin_Line::getKind()
141 {
142   static std::string MY_KIND = SketchPlugin_Line::ID();
143   return MY_KIND;
144 }
145
146 bool SketchPlugin_Line::isFixed() {
147   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
148 }
149
150 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
151   // the second condition for unability to move external segments anywhere
152   // isCopy() is checked temporary for case when copied lines stored external id state
153   // to be removed after debug
154   if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
155     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
156     if (!aSelection) {
157       // empty shape in selection shows that the shape is equal to context
158       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
159       if (anExtRes)
160         aSelection = anExtRes->shape();
161     }
162     // update arguments due to the selection value
163     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
164       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
165       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
166         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
167       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
168       std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
169         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
170       anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
171       updateLenghtValue();
172     }
173   }
174   else if (theID == START_ID() || theID == END_ID()) {
175     updateLenghtValue();
176   }
177 }
178
179 void SketchPlugin_Line::updateLenghtValue()
180 {
181   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
182       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
183   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
184       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
185   if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
186     double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
187     data()->real(LENGTH_ID())->setValue(aDistance);
188   }
189 }