Salome HOME
Merge remote-tracking branch 'origin/Dev_0.6'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Line.cpp
1 // File:        SketchPlugin_Line.cxx
2 // Created:     27 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "SketchPlugin_Line.h"
6 #include "SketchPlugin_Sketch.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_ResultConstruction.h>
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_Session.h>
12
13 #include <GeomAPI_Pnt.h>
14 #include <GeomAPI_Lin2d.h>
15 #include <GeomAPI_Pnt2d.h>
16
17 #include <GeomAlgoAPI_EdgeBuilder.h>
18 #include <GeomDataAPI_Point2D.h>
19
20 using namespace std;
21
22 SketchPlugin_Line::SketchPlugin_Line()
23     : SketchPlugin_Feature()
24 {}
25
26 void SketchPlugin_Line::initAttributes()
27 {
28   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::type());
29   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::type());
30   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
31   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
32 }
33
34 void SketchPlugin_Line::execute()
35 {
36   SketchPlugin_Sketch* aSketch = sketch();
37   if (aSketch) {
38     // compute a start point in 3D view
39     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
40         GeomDataAPI_Point2D>(data()->attribute(START_ID()));
41     // compute an end point in 3D view
42     std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
43         GeomDataAPI_Point2D>(data()->attribute(END_ID()));
44     if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
45       std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
46       std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
47       //std::cout<<"Execute line "<<aStart->x()<<" "<<aStart->y()<<" "<<aStart->z()<<" - "
48       //  <<anEnd->x()<<" "<<anEnd->y()<<" "<<anEnd->z()<<std::endl;
49       // make linear edge
50       std::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
51       // store the result
52       std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(
53           data());
54       aConstr->setShape(anEdge);
55       aConstr->setIsInHistory(false);
56       setResult(aConstr);
57     }
58   }
59 }
60
61 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
62 {
63   std::shared_ptr<ModelAPI_Data> aData = data();
64   if (!aData->isValid())
65     return;
66
67   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
68     (aData->attribute(START_ID()));
69   aPoint1->move(theDeltaX, theDeltaY);
70
71   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>
72     (aData->attribute(END_ID()));
73   aPoint2->move(theDeltaX, theDeltaY);
74 }
75
76 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
77 {
78   double aDelta = 0;
79
80   std::shared_ptr<ModelAPI_Data> aData = data();
81   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = 
82     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
83   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = 
84     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
85
86   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
87
88   if (false/*projection*/) {  // TODO: if it has not been necessary, remove this block
89     std::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
90     aDelta = aResult->distance(thePoint);
91   } else {  // distance
92     aDelta = aLin2d.distance(thePoint);
93   }
94
95   return aDelta;
96 }
97
98 bool SketchPlugin_Line::isFixed() {
99   return data()->selection(EXTERNAL_ID())->context();
100 }
101
102 void SketchPlugin_Line::attributeChanged(const std::string& theID) {
103   if (theID == EXTERNAL_ID()) {
104     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
105      // update arguments due to the selection value
106     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
107       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
108       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
109         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
110       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
111       std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
112         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
113       anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
114     }
115   }
116 }