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