Salome HOME
Issue #273: Add copyright string
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintLength.cpp
4 // Created: 30 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintLength.h"
8 #include <SketchPlugin_Line.h>
9
10 #include <GeomDataAPI_Point2D.h>
11
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Result.h>
15
16 #include <GeomDataAPI_Point2D.h>
17
18 #include <GeomAPI_Lin2d.h>
19 #include <GeomAPI_Pnt2d.h>
20
21 #include <Config_PropManager.h>
22
23 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
24 {
25 }
26
27 void SketchPlugin_ConstraintLength::initAttributes()
28 {
29   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
30   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
32 }
33
34 void SketchPlugin_ConstraintLength::execute()
35 {
36   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
37       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
38   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
39   if (aFeature) {
40     // set length value
41     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
42         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
43     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
44         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
45
46     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
47
48     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
49     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
50     //if(!aValueAttr->isInitialized()) {
51     //  aValueAttr->setValue(aLenght);
52     //}
53   }
54 }
55
56 bool SketchPlugin_ConstraintLength::getPoints(
57   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
58   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
59   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
60 {
61   if (!sketch())
62     return false;
63   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
64   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
65       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
66   if (!anAttr)
67     return false;
68   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
69   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
70     return false;
71   DataPtr aData = aFeature->data();
72   theStartPoint = std::dynamic_pointer_cast<
73       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
74   theEndPoint = std::dynamic_pointer_cast<
75       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
76   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
77   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
78   return true;
79 }
80
81 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
82 {
83   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
84   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
85   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
86     return thePrevious; // not possible to show length because points are not defined
87
88   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
89   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
90       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
91   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
92   if (aFlyOutAttr->isInitialized()) {
93     aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
94   } else {
95     std::shared_ptr<GeomAPI_Lin2d> aLine = 
96       std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
97     double aDist = aPoint1->distance(aPoint2)/5.;
98     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
99     aFlyOutAttr->setValue(aFPnt);
100     aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
101   }
102   // value calculation
103   // TODO: has to be calculated on definition of reference object
104   double aDistance = aPoint1->distance(aPoint2);
105   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
106       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
107   double aValue = aDistance;
108   if (aValueAttr->isInitialized())
109     aValue = aValueAttr->value();
110   else 
111     aValueAttr->setValue(aValue);
112   // End TODO
113
114   AISObjectPtr anAIS = thePrevious;
115   if (!anAIS)
116     anAIS = AISObjectPtr(new GeomAPI_AISObject);
117   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
118
119   // Set color from preferences
120   std::vector<int> aRGB = 
121     Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
122   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
123   return anAIS;
124 }
125
126 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
127 {
128   std::shared_ptr<ModelAPI_Data> aData = data();
129   if (!aData->isValid())
130     return;
131
132   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
134   aPoint->move(theDeltaX, theDeltaY);
135 }
136
137 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
138   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
139   {
140     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
141       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
142     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
143       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
144       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
145       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
146         double aLength = aPoint1->distance(aPoint2);
147         aValueAttr->setValue(aLength);
148       }
149     }
150   }
151 }