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