]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintLength.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git 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 <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     // the value should to be computed here, not in the getAISObject in order to change the model value
55     // inside the object transaction. This is important for creating a constraint by preselection.
56     // The display of the presentation in this case happens after the transaction commit
57     AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
58     if (!aFlyOutAttribute->isInitialized()) {
59       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
60     }
61   }
62 }
63
64 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
65 {
66   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
67     return false;
68
69   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
70   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
71   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
72     return false;
73
74   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
75       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
76
77   std::shared_ptr<GeomAPI_Lin2d> aLine = 
78     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
79   double aDist = aPoint1->distance(aPoint2)/5.;
80   std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
81   aFlyOutAttr->setValue(aFPnt);
82
83   return true;
84 }
85
86 bool SketchPlugin_ConstraintLength::getPoints(
87   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
88   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
89   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
90 {
91   if (!sketch())
92     return false;
93   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
94   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
95       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
96   if (!anAttr)
97     return false;
98   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
99   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
100     return false;
101   DataPtr aData = aFeature->data();
102   theStartPoint = std::dynamic_pointer_cast<
103       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
104   theEndPoint = std::dynamic_pointer_cast<
105       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
106   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
107   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
108   return true;
109 }
110
111 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
112 {
113   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
114   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
115   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
116     return thePrevious; // not possible to show length because points are not defined
117
118   AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
119   if (!aFlyOutAttribute->isInitialized()) {
120     return thePrevious; // not possible to show length because points are not defined
121   }
122   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
123                                                           GeomDataAPI_Point2D>(aFlyOutAttribute);
124   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
125   // value calculation
126   // TODO: has to be calculated on definition of reference object
127   double aDistance = aPoint1->distance(aPoint2);
128   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
129       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
130   double aValue = aDistance;
131   if (aValueAttr->isInitialized())
132     aValue = aValueAttr->value();
133   else 
134     aValueAttr->setValue(aValue);
135   // End TODO
136
137   AISObjectPtr anAIS = thePrevious;
138   if (!anAIS)
139     anAIS = AISObjectPtr(new GeomAPI_AISObject);
140   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
141   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
142
143   // Set color from preferences
144   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
145                                                     SKETCH_DIMENSION_COLOR);
146   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
147   return anAIS;
148 }
149
150 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
151 {
152   std::shared_ptr<ModelAPI_Data> aData = data();
153   if (!aData->isValid())
154     return;
155
156   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
157       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
158   aPoint->move(theDeltaX, theDeltaY);
159 }
160
161 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
162   if (theID == SketchPlugin_Constraint::ENTITY_A()) 
163   {
164     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
165       ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
166     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
167       std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
168       std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
169       if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
170         double aLength = aPoint1->distance(aPoint2);
171         aValueAttr->setValue(aLength);
172       }
173     }
174   }
175 }