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