Salome HOME
Update dimensions on current plane of sketcher
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistanceAlongDir.cpp
1 // Copyright (C) 2017-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 // File:    SketchPlugin_ConstraintDistanceAlongDir.cpp
21 // Created: 24 October 2017
22 // Author:  Artem ZHIDKOV
23
24 #include <SketchPlugin_ConstraintDistanceAlongDir.h>
25
26 #include <SketcherPrs_Tools.h>
27 #include <SketcherPrs_Factory.h>
28
29 #include <GeomAPI_Dir2d.h>
30 #include <GeomAPI_XY.h>
31 #include <GeomDataAPI_Point2D.h>
32
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeInteger.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Validator.h>
37
38 #include <cmath>
39
40 const double tolerance = 1e-7;
41
42
43 SketchPlugin_ConstraintDistanceAlongDir::SketchPlugin_ConstraintDistanceAlongDir()
44   : SketchPlugin_ConstraintDistance(),
45     myValue(-1.e100),
46     myValueUpdate(false)
47 {
48 }
49
50 //*************************************************************************************
51 void SketchPlugin_ConstraintDistanceAlongDir::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   data()->addAttribute(LOCATION_TYPE_ID(), ModelAPI_AttributeInteger::typeId());
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
60
61   data()->addAttribute(DISTANCE_VALUE_ID(), ModelAPI_AttributeDouble::typeId());
62
63   data()->addAttribute(NEGATIVE_TYPE_ID(), ModelAPI_AttributeBoolean::typeId());
64   boolean(NEGATIVE_TYPE_ID())->setValue(false);
65 }
66
67 //*************************************************************************************
68 void SketchPlugin_ConstraintDistanceAlongDir::execute()
69 {
70   AttributeDoublePtr anAttrValue = real(SketchPlugin_Constraint::VALUE());
71   if (anAttrValue->isInitialized() || !areAttributesInitialized())
72     return;
73
74   double aDistance = calculateCurrentDistance();
75   anAttrValue->setValue(aDistance);
76 }
77
78 //*************************************************************************************
79 AISObjectPtr SketchPlugin_ConstraintDistanceAlongDir::getAISObject(AISObjectPtr thePrevious)
80 {
81   if (!sketch())
82     return thePrevious;
83
84   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
85                                                                       sketch(),
86                                                                       thePrevious);
87   return anAIS;
88 }
89
90 void SketchPlugin_ConstraintDistanceAlongDir::attributeChanged(const std::string& theID)
91 {
92   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
93       theID == SketchPlugin_Constraint::ENTITY_B())
94   {
95     AttributeDoublePtr aValueAttr = real(SketchPlugin_Constraint::VALUE());
96     if (!aValueAttr->isInitialized() && areAttributesInitialized()) {
97       // only if it is not initialized, try to compute the current value
98       double aDistance = calculateCurrentDistance();
99       aValueAttr->setValue(aDistance);
100     }
101   } else if (theID == SketchPlugin_Constraint::VALUE() && !myValueUpdate) {
102     myValueUpdate = true;
103     // value of the distance shown to the user should be always positive
104     AttributeDoublePtr aDistanceValueAttr = real(DISTANCE_VALUE_ID());
105     double aConstraintValue = real(SketchPlugin_Constraint::VALUE())->value();
106     aDistanceValueAttr->setValue(fabs(aConstraintValue));
107     myValueUpdate = false;
108   } else if (theID == DISTANCE_VALUE_ID() && !myValueUpdate){
109     myValueUpdate = true;
110     // update value of the distance according to the value set by user
111     double aDistanceValue = real(DISTANCE_VALUE_ID())->value();
112     AttributeDoublePtr aConstraintValueAttr = real(SketchPlugin_Constraint::VALUE());
113     if (aConstraintValueAttr->value() < 0.0)
114       aDistanceValue = -aDistanceValue;
115     aConstraintValueAttr->setValue(aDistanceValue);
116     myValueUpdate = false;
117   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
118     // Recalculate flyout point in local coordinates of the distance constraint:
119     // the X coordinate is a length of projection of the flyout point on the
120     //                  line binding two distanced points
121     //                  or a line of projection of the distanced point onto the distanced segment
122     // the Y coordinate is a distance from the flyout point to the line
123     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
124         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
125         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
126     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
127
128     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
129     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
130         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
131     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
132         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
133
134     std::shared_ptr<GeomAPI_XY> aStartPnt = aPointA->pnt()->xy();
135     std::shared_ptr<GeomAPI_XY> aEndPnt = aPointB->pnt()->xy();
136
137     if (aEndPnt->distance(aStartPnt) < tolerance)
138       return;
139
140     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aEndPnt);
141     myFlyoutUpdate = true;
142     updateFlyoutPoint();
143     myFlyoutUpdate = false;
144   }
145 }