Salome HOME
16a0451d4044c1987e970bf4b56adea306ed4e8a
[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
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeInteger.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Validator.h>
39
40 #include <Config_PropManager.h>
41
42 #include <math.h>
43
44 const double tolerance = 1e-7;
45
46
47 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
48 {
49   myFlyoutUpdate = false;
50 }
51
52 //*************************************************************************************
53 void SketchPlugin_ConstraintDistance::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   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
59   data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
60
61   data()->addAttribute(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID(),
62                        ModelAPI_AttributeInteger::typeId());
63   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
64 }
65
66 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
67                                                       std::string& theDefault)
68 {
69   theSection = "Visualization";
70   theName = "sketch_dimension_color";
71   theDefault = SKETCH_DIMENSION_COLOR;
72 }
73
74 //*************************************************************************************
75 void SketchPlugin_ConstraintDistance::execute()
76 {
77   std::shared_ptr<ModelAPI_Data> aData = data();
78   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
79       aData->attribute(SketchPlugin_Constraint::VALUE()));
80
81   if(anAttrValue->isInitialized())
82     return;
83
84   double aDistance = calculateCurrentDistance();
85   if(aDistance >= 0)
86     anAttrValue->setValue(aDistance);
87 }
88
89 //*************************************************************************************
90 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
91 {
92   if (!sketch())
93     return thePrevious;
94
95   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
96                                                                       sketch(),
97                                                                       thePrevious);
98   return anAIS;
99 }
100
101 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
102 {
103   double aDistance = -1.;
104
105   std::shared_ptr<ModelAPI_Data> aData = data();
106   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
107   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
108     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
109   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
110       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
111
112   if (aPointA.get() && aPointB.get()) {  // both points
113     aDistance = aPointA->pnt()->distance(aPointB->pnt());
114   } else {
115     FeaturePtr aLineFeature;
116     std::shared_ptr<SketchPlugin_Line> aLine;
117     if (!aPointA.get() && aPointB.get()) {  //Line and point
118       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
119       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
120       if (aLine.get()) {
121         aDistance = aLine->distanceToPoint(aPointB->pnt());
122       }
123     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
124       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
125       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
126       if (aLine.get()) {
127         aDistance = aLine->distanceToPoint(aPointA->pnt());
128       }
129     }
130   }
131   return aDistance;
132 }
133
134 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
135 {
136   std::shared_ptr<ModelAPI_Data> aData = data();
137   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
138   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
139       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
140   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
141       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
142
143   if (!aPointA && !aPointB)
144     return false;
145   else if (aPointA || aPointB) {
146     FeaturePtr aLine;
147     if (!aPointA)
148       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
149     else if (!aPointB)
150       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
151     else // both points are initialized
152       return true;
153
154     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
155       return false;
156   }
157   return true;
158 }
159
160 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
161 {
162   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
163       theID == SketchPlugin_Constraint::ENTITY_B())
164   {
165     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
166         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
167     if (!aValueAttr->isInitialized()) {
168       // only if it is not initialized, try to compute the current value
169       double aDistance = calculateCurrentDistance();
170       if (aDistance > 0) { // set as value the length of updated references
171         aValueAttr->setValue(aDistance);
172       }
173     }
174   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
175     // Recalculate flyout point in local coordinates of the distance constraint:
176     // the X coordinate is a length of projection of the flyout point on the
177     //                  line binding two distanced points
178     //                  or a line of projection of the distanced point onto the distanced segment
179     // the Y coordinate is a distance from the flyout point to the line
180     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
181         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
183     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
184
185     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
186     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
187         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
188     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
189         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
190
191     std::shared_ptr<GeomAPI_XY> aStartPnt;
192     std::shared_ptr<GeomAPI_XY> aEndPnt;
193     if (aPointA && aPointB) {
194       aStartPnt = aPointA->pnt()->xy();
195       aEndPnt = aPointB->pnt()->xy();
196     } else if (aPointA) {
197       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
198           SketchPlugin_Constraint::ENTITY_B());
199       if (!aLine)
200         return;
201       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
202       aStartPnt = aPoint->xy();
203       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
204     } else if (aPointB) {
205       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
206           SketchPlugin_Constraint::ENTITY_A());
207       if (!aLine)
208         return;
209       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
210       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
211       aEndPnt = aPoint->xy();
212     } else
213       return;
214
215     if (aEndPnt->distance(aStartPnt) < tolerance)
216       return;
217
218     myFlyoutUpdate = true;
219     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
220     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
221
222     double X = aFlyoutDir->dot(aLineDir->xy());
223     double Y = -aFlyoutDir->cross(aLineDir->xy());
224     aFlyoutAttr->setValue(X, Y);
225     myFlyoutUpdate = false;
226   }
227 }
228