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