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