]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
Improve movement of the dimension when value is 0.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.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_ConstraintDistance.h"
21 #include <SketchPlugin_Point.h>
22 #include <SketchPlugin_Circle.h>
23 #include <SketchPlugin_Line.h>
24
25 #include <SketcherPrs_Tools.h>
26 #include <SketcherPrs_Factory.h>
27
28 #include <GeomAPI_Dir2d.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <GeomDataAPI_Dir.h>
34
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_Data.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Validator.h>
40
41 #include <Config_PropManager.h>
42
43 #include <math.h>
44
45 const double tolerance = 1e-7;
46
47
48 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
49 {
50   myFlyoutUpdate = false;
51 }
52
53 //*************************************************************************************
54 void SketchPlugin_ConstraintDistance::initAttributes()
55 {
56   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
58   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
60   data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
61
62   data()->addAttribute(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID(),
63                        ModelAPI_AttributeInteger::typeId());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
65
66   AttributePtr anAttr = data()->addAttribute(SketchPlugin_ConstraintDistance::DIRECTION_ID(),
67                                              GeomDataAPI_Dir::typeId());
68   anAttr->setIsArgument(false);
69   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIRECTION_ID());
70 }
71
72 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
73                                                       std::string& theDefault)
74 {
75   theSection = "Visualization";
76   theName = "sketch_dimension_color";
77   theDefault = SKETCH_DIMENSION_COLOR;
78 }
79
80 //*************************************************************************************
81 void SketchPlugin_ConstraintDistance::execute()
82 {
83   std::shared_ptr<ModelAPI_Data> aData = data();
84   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
85       aData->attribute(SketchPlugin_Constraint::VALUE()));
86
87   if(anAttrValue->isInitialized())
88     return;
89
90   double aDistance = calculateCurrentDistance();
91   if(aDistance >= 0)
92     anAttrValue->setValue(aDistance);
93 }
94
95 //*************************************************************************************
96 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
97 {
98   if (!sketch())
99     return thePrevious;
100
101   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
102                                                                       sketch(),
103                                                                       thePrevious);
104   return anAIS;
105 }
106
107 static std::shared_ptr<GeomAPI_Lin2d> getLine(DataPtr theData, const std::string& theAttrName)
108 {
109   FeaturePtr aLineFeature = SketcherPrs_Tools::getFeatureLine(theData, theAttrName);
110   if (!aLineFeature)
111     return GeomLine2dPtr();
112
113   std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114       aLineFeature->attribute(SketchPlugin_Line::START_ID()));
115   std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116       aLineFeature->attribute(SketchPlugin_Line::END_ID()));
117
118   return GeomLine2dPtr(new GeomAPI_Lin2d(aStart->x(), aStart->y(), aEnd->x(), aEnd->y()));
119 }
120
121 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
122 {
123   double aDistance = -1.;
124
125   std::shared_ptr<ModelAPI_Data> aData = data();
126   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
127   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
128     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
129   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
130       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
131
132   GeomPnt2dPtr aGeomPntA, aGeomPntB;
133   GeomLine2dPtr aLine;
134   if (aPointA.get() && aPointB.get()) {  // both points
135     aGeomPntA = aPointA->pnt();
136     aGeomPntB = aPointB->pnt();
137   } else {
138     if (!aPointA.get() && aPointB.get()) {  //Line and point
139       aLine = getLine(aData, SketchPlugin_Constraint::ENTITY_A());
140       aGeomPntB = aPointB->pnt();
141       aGeomPntA = aLine ? aLine->project(aGeomPntB) : GeomPnt2dPtr();
142     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
143       aLine = getLine(aData, SketchPlugin_Constraint::ENTITY_B());
144       aGeomPntA = aPointA->pnt();
145       aGeomPntB = aLine ? aLine->project(aGeomPntA) : GeomPnt2dPtr();
146     }
147   }
148
149   if (aGeomPntA.get() && aGeomPntB.get()) {
150     aDistance = aGeomPntA->distance(aGeomPntB);
151     if (aDistance < tolerance)
152       aDistance = 0.0;
153   }
154
155   // keep the direction between arguments for processing of the zero distance
156   std::shared_ptr<GeomDataAPI_Dir> aPointPointDir =
157       std::dynamic_pointer_cast<GeomDataAPI_Dir>(attribute(DIRECTION_ID()));
158   if (aDistance > tolerance)
159     aPointPointDir->setValue(aGeomPntB->x() - aGeomPntA->x(), aGeomPntB->y() - aGeomPntA->y(), 0.0);
160   else if (aLine) {
161     GeomDir2dPtr aLineDir = aLine->direction();
162     aPointPointDir->setValue(-aLineDir->y(), aLineDir->x(), 0.0);
163   }
164
165   return aDistance;
166 }
167
168 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
169 {
170   std::shared_ptr<ModelAPI_Data> aData = data();
171   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
172   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
173       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
174   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
175       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
176
177   if (!aPointA && !aPointB)
178     return false;
179   else if (aPointA || aPointB) {
180     FeaturePtr aLine;
181     if (!aPointA)
182       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
183     else if (!aPointB)
184       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
185     else // both points are initialized
186       return true;
187
188     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
189       return false;
190   }
191   return true;
192 }
193
194 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
195 {
196   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
197       theID == SketchPlugin_Constraint::ENTITY_B())
198   {
199     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
200         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
201     if (!aValueAttr->isInitialized()) {
202       // only if it is not initialized, try to compute the current value
203       double aDistance = calculateCurrentDistance();
204       if (aDistance >= 0) { // set as value the length of updated references
205         aValueAttr->setValue(aDistance);
206       }
207     }
208   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
209     // Recalculate flyout point in local coordinates of the distance constraint:
210     // the X coordinate is a length of projection of the flyout point on the
211     //                  line binding two distanced points
212     //                  or a line of projection of the distanced point onto the distanced segment
213     // the Y coordinate is a distance from the flyout point to the line
214     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
215         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
216         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
217     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
218
219     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
220     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
221         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
222     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
223         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
224
225     std::shared_ptr<GeomAPI_XY> aStartPnt;
226     std::shared_ptr<GeomAPI_XY> aEndPnt;
227     if (aPointA && aPointB) {
228       aStartPnt = aPointA->pnt()->xy();
229       aEndPnt = aPointB->pnt()->xy();
230     } else if (aPointA) {
231       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
232           SketchPlugin_Constraint::ENTITY_B());
233       if (!aLine)
234         return;
235       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
236       aStartPnt = aPoint->xy();
237       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
238     } else if (aPointB) {
239       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
240           SketchPlugin_Constraint::ENTITY_A());
241       if (!aLine)
242         return;
243       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
244       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
245       aEndPnt = aPoint->xy();
246     } else
247       return;
248
249     myFlyoutUpdate = true;
250     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
251     if (aEndPnt->distance(aStartPnt) >= tolerance) {
252       std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
253       double X = aFlyoutDir->dot(aLineDir->xy());
254       double Y = -aFlyoutDir->cross(aLineDir->xy());
255       aFlyoutAttr->setValue(X, Y);
256     }
257     else
258       aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
259     myFlyoutUpdate = false;
260   }
261 }
262