Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_ConstraintLength.h"
21 #include <SketchPlugin_Line.h>
22
23 #include <GeomDataAPI_Point2D.h>
24
25 #include <SketcherPrs_Factory.h>
26 #include <SketcherPrs_Tools.h>
27
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Result.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
34
35 #include <GeomDataAPI_Point2D.h>
36
37 #include <GeomAPI_Dir2d.h>
38 #include <GeomAPI_Lin2d.h>
39 #include <GeomAPI_Pnt2d.h>
40 #include <GeomAPI_XY.h>
41
42 #include <Config_PropManager.h>
43
44 #include <math.h>
45
46 const double tolerance = 1e-7;
47
48 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
49 {
50   myFlyoutUpdate = false;
51 }
52
53 void SketchPlugin_ConstraintLength::initAttributes()
54 {
55   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
56   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
57   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
58
59   data()->addAttribute(SketchPlugin_ConstraintLength::LOCATION_TYPE_ID(),
60                        ModelAPI_AttributeInteger::typeId());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
62 }
63
64 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
65                                                     std::string& theDefault)
66 {
67   theSection = "Visualization";
68   theName = "sketch_dimension_color";
69   theDefault = SKETCH_DIMENSION_COLOR;
70 }
71
72 void SketchPlugin_ConstraintLength::execute()
73 {
74   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
75       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
76   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
77   if (aFeature) {
78     // set length value
79     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
80         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
81     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
82         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
83
84     if (aPoint1.get() && aPoint2.get()) {
85       double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
86
87       //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
88       //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
89       //if(!aValueAttr->isInitialized()) {
90       //  aValueAttr->setValue(aLenght);
91       //}
92
93       // the value should to be computed here, not in the getAISObject
94       // in order to change the model value
95       // inside the object transaction. This is important for creating a constraint by preselection.
96       // The display of the presentation in this case happens after the transaction commit
97       AttributePtr aFlyOutAttribute =
98         data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
99       if (!aFlyOutAttribute->isInitialized()) {
100         compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
101       }
102     }
103   }
104 }
105
106 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
107 {
108   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
109     return false;
110
111   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
112   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
113   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
114     return false;
115
116   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
117       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
118
119   std::shared_ptr<GeomAPI_Lin2d> aLine =
120     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
121   if (!aFlyOutAttr->isInitialized() ||
122       (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
123     double aDist = aPoint1->distance(aPoint2)/5.;
124     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
125     aFlyOutAttr->setValue(aFPnt);
126   }
127
128   return true;
129 }
130
131 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
132 {
133   bool aResult = false;
134   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
135   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
136   if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
137     theValue = aPoint1->distance(aPoint2);
138     aResult = true;
139   }
140   return aResult;
141 }
142
143 bool SketchPlugin_ConstraintLength::getPoints(
144   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
145   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
146   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
147 {
148   if (!sketch())
149     return false;
150   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
151       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
152   if (!anAttr)
153     return false;
154   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
155   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
156     return false;
157   DataPtr aData = aFeature->data();
158   theStartPoint = std::dynamic_pointer_cast<
159       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
160   theEndPoint = std::dynamic_pointer_cast<
161       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
162   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
163   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
164   return true;
165 }
166
167 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
168 {
169   if (!sketch())
170     return thePrevious;
171
172   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
173     sketch()->coordinatePlane(), thePrevious);
174   return anAIS;
175 }
176
177 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
178   if (theID == SketchPlugin_Constraint::ENTITY_A())
179   {
180     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
181       ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
182     if (!aValueAttr->isInitialized()) {
183       // only if it is not initialized, try to compute the current value
184       double aLength;
185       if (computeLenghtValue(aLength))
186         aValueAttr->setValue(aLength);
187     }
188   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
189     // Recalculate flyout point in local coordinates of the line:
190     // the X coordinate is a length of projection of the flyout point on the line
191     // the Y coordinate is a distance from the point to the line
192     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
193         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
195     AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
196         attribute(SketchPlugin_Constraint::ENTITY_A()));
197     if (!aLineAttr || !aLineAttr->isObject())
198       return;
199     FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
200     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
201       return;
202
203     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
204     std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
205         aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
206     std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
207         aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
208
209     myFlyoutUpdate = true;
210     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
211     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
212     double X = aFlyoutDir->dot(aLineDir->xy());
213     double Y = -aFlyoutDir->cross(aLineDir->xy());
214     aFlyoutAttr->setValue(X, Y);
215     myFlyoutUpdate = false;
216   }
217 }