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