Salome HOME
6691ee51bba8ad4a231d9cf4953c9e4d5ce363ec
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistanceVertical.cpp
1 // Copyright (C) 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 // File:    SketchPlugin_ConstraintDistanceVertical.cpp
22 // Created: 10 May 2017
23 // Author:  Artem ZHIDKOV
24
25 #include <SketchPlugin_ConstraintDistanceVertical.h>
26
27 #include <SketcherPrs_Tools.h>
28 #include <SketcherPrs_Factory.h>
29
30 #include <GeomAPI_Dir2d.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomDataAPI_Point2D.h>
33
34 #include <ModelAPI_AttributeDouble.h>
35
36 const double tolerance = 1e-7;
37
38
39 SketchPlugin_ConstraintDistanceVertical::SketchPlugin_ConstraintDistanceVertical()
40   : SketchPlugin_ConstraintDistance()
41 {
42 }
43
44 //*************************************************************************************
45 void SketchPlugin_ConstraintDistanceVertical::initAttributes()
46 {
47   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
48   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
49   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
50   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
51 }
52
53 //*************************************************************************************
54 void SketchPlugin_ConstraintDistanceVertical::execute()
55 {
56   AttributeDoublePtr anAttrValue = real(SketchPlugin_Constraint::VALUE());
57   if (anAttrValue->isInitialized() || !areAttributesInitialized())
58     return;
59
60   double aDistance = calculateCurrentDistance();
61   anAttrValue->setValue(aDistance);
62 }
63
64 //*************************************************************************************
65 AISObjectPtr SketchPlugin_ConstraintDistanceVertical::getAISObject(AISObjectPtr thePrevious)
66 {
67   if (!sketch())
68     return thePrevious;
69
70   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
71                                                                       sketch()->coordinatePlane(),
72                                                                       thePrevious);
73   return anAIS;
74 }
75
76 //*************************************************************************************
77 void SketchPlugin_ConstraintDistanceVertical::move(double theDeltaX, double theDeltaY)
78 {
79   std::shared_ptr<ModelAPI_Data> aData = data();
80   if (!aData->isValid())
81     return;
82
83   // Recalculate a shift of flyout point in terms of local coordinates
84   std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
85   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
86   std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
87       data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
88   std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
89       data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
90
91   if (!aPointA || !aPointB)
92     return;
93
94   std::shared_ptr<GeomAPI_XY> aStartPnt = aPointA->pnt()->xy();
95   std::shared_ptr<GeomAPI_XY> aEndPnt = aPointB->pnt()->xy();
96
97   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
98   double dX = aDir->dot(aLineDir->xy());
99   double dY = -aDir->cross(aLineDir->xy());
100
101   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
103   myFlyoutUpdate = true;
104   if (aPoint->isInitialized()) {
105     aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
106   } else {
107     aPoint->setValue(dX, dY);
108   }
109   myFlyoutUpdate = false;
110 }
111
112 double SketchPlugin_ConstraintDistanceVertical::calculateCurrentDistance()
113 {
114   std::shared_ptr<ModelAPI_Data> aData = data();
115   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
116   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
117     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
118   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
119       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
120
121   return aPointB->y() - aPointA->y();
122 }
123
124 void SketchPlugin_ConstraintDistanceVertical::attributeChanged(const std::string& theID)
125 {
126   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
127       theID == SketchPlugin_Constraint::ENTITY_B())
128   {
129     AttributeDoublePtr aValueAttr = real(SketchPlugin_Constraint::VALUE());
130     if (!aValueAttr->isInitialized() && areAttributesInitialized()) {
131       // only if it is not initialized, try to compute the current value
132       double aDistance = calculateCurrentDistance();
133       aValueAttr->setValue(aDistance);
134     }
135   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
136     myFlyoutUpdate = true;
137     // Recalculate flyout point in local coordinates of the distance constraint:
138     // the X coordinate is a length of projection of the flyout point on the
139     //                  line binding two distanced points
140     //                  or a line of projection of the distanced point onto the distanced segment
141     // the Y coordinate is a distance from the flyout point to the line
142     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
143         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
144         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
145     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
146
147     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
148     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
149         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
150     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
151         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
152
153     std::shared_ptr<GeomAPI_XY> aStartPnt = aPointA->pnt()->xy();
154     std::shared_ptr<GeomAPI_XY> aEndPnt = aPointB->pnt()->xy();
155
156     if (aEndPnt->distance(aStartPnt) < tolerance)
157       return;
158
159     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
160     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
161
162     double X = aFlyoutDir->dot(aLineDir->xy());
163     double Y = -aFlyoutDir->cross(aLineDir->xy());
164     aFlyoutAttr->setValue(X, Y);
165     myFlyoutUpdate = false;
166   }
167 }