]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintLength.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.cpp
1 // Copyright (C) 2014-2017  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<mailto: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
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Result.h>
30
31 #include <GeomDataAPI_Point2D.h>
32
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
37
38 #include <Config_PropManager.h>
39
40 #include <math.h>
41
42 const double tolerance = 1e-7;
43
44 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
45 {
46   myFlyoutUpdate = false;
47 }
48
49 void SketchPlugin_ConstraintLength::initAttributes()
50 {
51   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
52   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
53   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
54 }
55
56 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
57                                                     std::string& theDefault)
58 {
59   theSection = "Visualization";
60   theName = "sketch_dimension_color";
61   theDefault = SKETCH_DIMENSION_COLOR;
62 }
63
64 void SketchPlugin_ConstraintLength::execute()
65 {
66   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
67       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
68   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
69   if (aFeature) {
70     // set length value
71     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
72         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
73     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
74         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
75
76     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
77
78     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
79     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
80     //if(!aValueAttr->isInitialized()) {
81     //  aValueAttr->setValue(aLenght);
82     //}
83
84     // the value should to be computed here, not in the getAISObject
85     // in order to change the model value
86     // inside the object transaction. This is important for creating a constraint by preselection.
87     // The display of the presentation in this case happens after the transaction commit
88     AttributePtr aFlyOutAttribute =
89       data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
90     if (!aFlyOutAttribute->isInitialized()) {
91       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
92     }
93   }
94 }
95
96 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
97 {
98   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
99     return false;
100
101   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
102   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
103   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
104     return false;
105
106   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
107       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
108
109   std::shared_ptr<GeomAPI_Lin2d> aLine =
110     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
111   if (!aFlyOutAttr->isInitialized() ||
112       (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
113     double aDist = aPoint1->distance(aPoint2)/5.;
114     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
115     aFlyOutAttr->setValue(aFPnt);
116   }
117
118   return true;
119 }
120
121 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
122 {
123   bool aResult = false;
124   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
125   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
126   if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
127     theValue = aPoint1->distance(aPoint2);
128     aResult = true;
129   }
130   return aResult;
131 }
132
133 bool SketchPlugin_ConstraintLength::getPoints(
134   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
135   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
136   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
137 {
138   if (!sketch())
139     return false;
140   std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
141   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
142       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
143   if (!anAttr)
144     return false;
145   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
146   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
147     return false;
148   DataPtr aData = aFeature->data();
149   theStartPoint = std::dynamic_pointer_cast<
150       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
151   theEndPoint = std::dynamic_pointer_cast<
152       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
153   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
154   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
155   return true;
156 }
157
158 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
159 {
160   if (!sketch())
161     return thePrevious;
162
163   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
164     sketch()->coordinatePlane(), thePrevious);
165   return anAIS;
166 }
167
168 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
169 {
170   std::shared_ptr<ModelAPI_Data> aData = data();
171   if (!aData->isValid())
172     return;
173
174   AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
175       attribute(SketchPlugin_Constraint::ENTITY_A()));
176   if (!aLineAttr || !aLineAttr->isObject())
177     return;
178   FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
179   if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
180     return;
181
182   // Recalculate a shift of flyout point in terms of local coordinates
183   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
184   std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185       aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
186   std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187       aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
188   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
189   double dX = aDir->dot(aLineDir->xy());
190   double dY = -aDir->cross(aLineDir->xy());
191
192   myFlyoutUpdate = true;
193   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
195   aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
196   myFlyoutUpdate = false;
197 }
198
199 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
200   if (theID == SketchPlugin_Constraint::ENTITY_A())
201   {
202     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
203       ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
204     if (!aValueAttr->isInitialized()) {
205       // only if it is not initialized, try to compute the current value
206       double aLength;
207       if (computeLenghtValue(aLength))
208         aValueAttr->setValue(aLength);
209     }
210   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
211     myFlyoutUpdate = true;
212     // Recalculate flyout point in local coordinates of the line:
213     // the X coordinate is a length of projection of the flyout point on the line
214     // the Y coordinate is a distance from the point to the line
215     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
216         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
218     AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
219         attribute(SketchPlugin_Constraint::ENTITY_A()));
220     if (!aLineAttr || !aLineAttr->isObject())
221       return;
222     FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
223     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
224       return;
225
226     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
227     std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228         aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
229     std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
230         aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
231
232     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
233     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
234     double X = aFlyoutDir->dot(aLineDir->xy());
235     double Y = -aFlyoutDir->cross(aLineDir->xy());
236     aFlyoutAttr->setValue(X, Y);
237     myFlyoutUpdate = false;
238   }
239 }