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