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