Salome HOME
#2205 Ability to customize the arrows and texts of dimensions
[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 #include <ModelAPI_AttributeInteger.h>
36
37 const double tolerance = 1e-7;
38
39
40 SketchPlugin_ConstraintDistanceVertical::SketchPlugin_ConstraintDistanceVertical()
41   : SketchPlugin_ConstraintDistance()
42 {
43 }
44
45 //*************************************************************************************
46 void SketchPlugin_ConstraintDistanceVertical::initAttributes()
47 {
48   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
49   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
50   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
51   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
52
53   data()->addAttribute(SketchPlugin_ConstraintDistanceVertical::LOCATION_TYPE_ID(),
54                        ModelAPI_AttributeInteger::typeId());
55 }
56
57 //*************************************************************************************
58 void SketchPlugin_ConstraintDistanceVertical::execute()
59 {
60   AttributeDoublePtr anAttrValue = real(SketchPlugin_Constraint::VALUE());
61   if (anAttrValue->isInitialized() || !areAttributesInitialized())
62     return;
63
64   double aDistance = calculateCurrentDistance();
65   anAttrValue->setValue(aDistance);
66 }
67
68 //*************************************************************************************
69 AISObjectPtr SketchPlugin_ConstraintDistanceVertical::getAISObject(AISObjectPtr thePrevious)
70 {
71   if (!sketch())
72     return thePrevious;
73
74   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
75                                                                       sketch()->coordinatePlane(),
76                                                                       thePrevious);
77   return anAIS;
78 }
79
80 double SketchPlugin_ConstraintDistanceVertical::calculateCurrentDistance()
81 {
82   std::shared_ptr<ModelAPI_Data> aData = data();
83   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
84   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
85     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
86   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
87       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
88
89   return aPointB->y() - aPointA->y();
90 }
91
92 void SketchPlugin_ConstraintDistanceVertical::attributeChanged(const std::string& theID)
93 {
94   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
95       theID == SketchPlugin_Constraint::ENTITY_B())
96   {
97     AttributeDoublePtr aValueAttr = real(SketchPlugin_Constraint::VALUE());
98     if (!aValueAttr->isInitialized() && areAttributesInitialized()) {
99       // only if it is not initialized, try to compute the current value
100       double aDistance = calculateCurrentDistance();
101       aValueAttr->setValue(aDistance);
102     }
103   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
104     // Recalculate flyout point in local coordinates of the distance constraint:
105     // the X coordinate is a length of projection of the flyout point on the
106     //                  line binding two distanced points
107     //                  or a line of projection of the distanced point onto the distanced segment
108     // the Y coordinate is a distance from the flyout point to the line
109     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
110         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
112     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
113
114     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
115     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
116         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
117     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
118         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
119
120     std::shared_ptr<GeomAPI_XY> aStartPnt = aPointA->pnt()->xy();
121     std::shared_ptr<GeomAPI_XY> aEndPnt = aPointB->pnt()->xy();
122
123     if (aEndPnt->distance(aStartPnt) < tolerance)
124       return;
125
126     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aEndPnt);
127     myFlyoutUpdate = true;
128     double X =  aFlyoutDir->y(); // Dot on OY axis
129     double Y = -aFlyoutDir->x(); // Cross to OY axis
130     aFlyoutAttr->setValue(X, Y);
131     myFlyoutUpdate = false;
132   }
133 }