Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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 <SketcherPrs_Factory.h>
13
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Result.h>
17
18 #include <GeomDataAPI_Point2D.h>
19
20 #include <GeomAPI_Lin2d.h>
21 #include <GeomAPI_Pnt2d.h>
22
23 #include <Config_PropManager.h>
24
25 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
26 {
27 }
28
29 void SketchPlugin_ConstraintLength::initAttributes()
30 {
31   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
32   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
33   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
34 }
35
36 void SketchPlugin_ConstraintLength::execute()
37 {
38   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
39       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
40   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
41   if (aFeature) {
42     // set length value
43     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
44         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
45     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
46         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
47
48     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
49
50     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
51     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
52     //if(!aValueAttr->isInitialized()) {
53     //  aValueAttr->setValue(aLenght);
54     //}
55
56     // the value should to be computed here, not in the getAISObject in order to change the model value
57     // inside the object transaction. This is important for creating a constraint by preselection.
58     // The display of the presentation in this case happens after the transaction commit
59     AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
60     if (!aFlyOutAttribute->isInitialized()) {
61       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
62     }
63   }
64 }
65
66 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
67 {
68   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
69     return false;
70
71   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
72   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
73   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
74     return false;
75
76   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
77       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
78
79   std::shared_ptr<GeomAPI_Lin2d> aLine = 
80     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
81   double aDist = aPoint1->distance(aPoint2)/5.;
82   std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
83   aFlyOutAttr->setValue(aFPnt);
84
85   return true;
86 }
87
88 bool SketchPlugin_ConstraintLength::getPoints(
89   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
90   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
91   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
92 {
93   if (!sketch())
94     return false;
95   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
96   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
97       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
98   if (!anAttr)
99     return false;
100   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
101   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
102     return false;
103   DataPtr aData = aFeature->data();
104   theStartPoint = std::dynamic_pointer_cast<
105       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
106   theEndPoint = std::dynamic_pointer_cast<
107       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
108   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
109   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
110   return true;
111 }
112
113 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
114 {
115   if (!sketch())
116     return thePrevious;
117
118   AISObjectPtr anAIS = thePrevious;
119   if (!anAIS) {
120     anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane());
121   }
122
123   // Set color from preferences
124   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
125                                                     SKETCH_DIMENSION_COLOR);
126   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
127   return anAIS;
128 }
129
130 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
131 {
132   std::shared_ptr<ModelAPI_Data> aData = data();
133   if (!aData->isValid())
134     return;
135
136   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
137       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
138   aPoint->move(theDeltaX, theDeltaY);
139 }
140
141 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
142   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
143   {
144     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
145       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
146     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
147       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
148       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
149       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
150         double aLength = aPoint1->distance(aPoint2);
151         aValueAttr->setValue(aLength);
152       }
153     }
154   }
155 }