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