Salome HOME
Issue #1652 Add a real widget displaying the length of the line in the panel: correct...
[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 <ModelAPI_Data.h>
10 #include <ModelAPI_ResultConstruction.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_AttributeBoolean.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_Validator.h>
15 #include <ModelAPI_Session.h>
16
17 #include <GeomAPI_Pnt.h>
18 #include <GeomAPI_Lin2d.h>
19 #include <GeomAPI_Pnt2d.h>
20
21 #include <GeomAlgoAPI_EdgeBuilder.h>
22 #include <GeomDataAPI_Point2D.h>
23
24 using namespace std;
25
26 SketchPlugin_Line::SketchPlugin_Line()
27     : SketchPlugin_SketchEntity()
28 {}
29
30 void SketchPlugin_Line::initAttributes()
31 {
32   SketchPlugin_SketchEntity::initAttributes();
33   /// new attributes should be added to end of the feature in order to provide
34   /// correct attribute values in previous saved studies
35   data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeDouble::typeId());
36 }
37
38 void SketchPlugin_Line::initDerivedClassAttributes()
39 {
40   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
41   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
42   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
43   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
44 }
45
46 void SketchPlugin_Line::execute()
47 {
48   SketchPlugin_Sketch* aSketch = sketch();
49   if (aSketch) {
50     // compute a start point in 3D view
51     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
52         GeomDataAPI_Point2D>(data()->attribute(START_ID()));
53     // compute an end point in 3D view
54     std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
55         GeomDataAPI_Point2D>(data()->attribute(END_ID()));
56     if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
57       std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
58       std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
59       //std::cout<<"Execute line "<<aStart->x()<<" "<<aStart->y()<<" "<<aStart->z()<<" - "
60       //  <<anEnd->x()<<" "<<anEnd->y()<<" "<<anEnd->z()<<std::endl;
61       // make linear edge
62       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
63       // store the result
64       std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(
65           data());
66       aConstr->setShape(anEdge);
67       aConstr->setIsInHistory(false);
68       setResult(aConstr);
69     }
70   }
71 }
72
73 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
74 {
75   std::shared_ptr<ModelAPI_Data> aData = data();
76   if (!aData->isValid())
77     return;
78
79   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
80     (aData->attribute(START_ID()));
81   aPoint1->move(theDeltaX, theDeltaY);
82
83   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
84     (aData->attribute(END_ID()));
85   aPoint2->move(theDeltaX, theDeltaY);
86 }
87
88 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
89 {
90   double aDelta = 0;
91
92   std::shared_ptr<ModelAPI_Data> aData = data();
93   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = 
94     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
95   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = 
96     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
97
98   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
99
100   if (false/*projection*/) {  // TODO: if it has not been necessary, remove this block
101     std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
102     aDelta = aResult->distance(thePoint);
103   } else {  // distance
104     aDelta = aLin2d.distance(thePoint);
105   }
106
107   return aDelta;
108 }
109
110 const std::string& SketchPlugin_Line::getKind()
111 {
112   static std::string MY_KIND = SketchPlugin_Line::ID();
113   return MY_KIND;
114 }
115
116 bool SketchPlugin_Line::isFixed() {
117   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
118 }
119
120 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
121   // the second condition for unability to move external segments anywhere
122   // isCopy() is checked temporary for case when copied lines stored external id state
123   // to be removed after debug
124   if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
125     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
126      // update arguments due to the selection value
127     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
128       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
129       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
130         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
131       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
132       std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
133         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
134       anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
135       updateLenghtValue();
136     }
137   }
138   else if (theID == START_ID() || theID == END_ID()) {
139     updateLenghtValue();
140   }
141 }
142
143 void SketchPlugin_Line::updateLenghtValue()
144 {
145   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
146       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
147   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
148       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
149   if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
150     double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
151     data()->real(LENGTH_ID())->setValue(aDistance);
152   }
153 }