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