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