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