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