1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_ConstraintDistance.h"
22 #include <SketchPlugin_Point.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Line.h>
26 #include <SketcherPrs_Tools.h>
27 #include <SketcherPrs_Factory.h>
29 #include <GeomAPI_Dir2d.h>
30 #include <GeomAPI_Lin2d.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_XY.h>
33 #include <GeomDataAPI_Point2D.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_Data.h>
38 #include <Config_PropManager.h>
42 const double tolerance = 1e-7;
45 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
47 myFlyoutUpdate = false;
50 //*************************************************************************************
51 void SketchPlugin_ConstraintDistance::initAttributes()
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());
59 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
60 std::string& theDefault)
62 theSection = "Visualization";
63 theName = "sketch_dimension_color";
64 theDefault = SKETCH_DIMENSION_COLOR;
67 //*************************************************************************************
68 void SketchPlugin_ConstraintDistance::execute()
70 std::shared_ptr<ModelAPI_Data> aData = data();
71 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
72 aData->attribute(SketchPlugin_Constraint::VALUE()));
74 if(anAttrValue->isInitialized())
77 double aDistance = calculateCurrentDistance();
79 anAttrValue->setValue(aDistance);
82 //*************************************************************************************
83 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
88 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
89 sketch()->coordinatePlane(),
94 //*************************************************************************************
95 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
97 std::shared_ptr<ModelAPI_Data> aData = data();
98 if (!aData->isValid())
101 // Recalculate a shift of flyout point in terms of local coordinates
102 std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
103 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
104 std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
105 data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
106 std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
107 data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
109 std::shared_ptr<GeomAPI_XY> aStartPnt;
110 std::shared_ptr<GeomAPI_XY> aEndPnt;
111 if (aPointA && aPointB) {
112 aStartPnt = aPointA->pnt()->xy();
113 aEndPnt = aPointB->pnt()->xy();
114 } else if (aPointA) {
115 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
116 SketchPlugin_Constraint::ENTITY_B());
119 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
120 aStartPnt = aPoint->xy();
121 aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
122 } else if (aPointB) {
123 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
124 SketchPlugin_Constraint::ENTITY_A());
127 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
128 aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
129 aEndPnt = aPoint->xy();
133 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
134 double dX = aDir->dot(aLineDir->xy());
135 double dY = -aDir->cross(aLineDir->xy());
137 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
138 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
139 myFlyoutUpdate = true;
140 if (aPoint->isInitialized()) {
141 aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
143 aPoint->setValue(dX, dY);
145 myFlyoutUpdate = false;
148 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
150 double aDistance = -1.;
152 std::shared_ptr<ModelAPI_Data> aData = data();
153 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
154 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
155 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
156 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
157 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
159 if (aPointA.get() && aPointB.get()) { // both points
160 aDistance = aPointA->pnt()->distance(aPointB->pnt());
162 FeaturePtr aLineFeature;
163 std::shared_ptr<SketchPlugin_Line> aLine;
164 if (!aPointA.get() && aPointB.get()) { //Line and point
165 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
166 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
168 aDistance = aLine->distanceToPoint(aPointB->pnt());
170 } else if (aPointA.get() && !aPointB.get()) { // Point and line
171 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
172 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
174 aDistance = aLine->distanceToPoint(aPointA->pnt());
181 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
183 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
184 theID == SketchPlugin_Constraint::ENTITY_B())
186 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
187 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
188 if (!aValueAttr->isInitialized()) {
189 // only if it is not initialized, try to compute the current value
190 double aDistance = calculateCurrentDistance();
191 if (aDistance > 0) { // set as value the length of updated references
192 aValueAttr->setValue(aDistance);
195 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
196 myFlyoutUpdate = true;
197 // Recalculate flyout point in local coordinates of the distance constraint:
198 // the X coordinate is a length of projection of the flyout point on the
199 // line binding two distanced points
200 // or a line of projection of the distanced point onto the distanced segment
201 // the Y coordinate is a distance from the flyout point to the line
202 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
203 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
205 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
207 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
208 std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
209 data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
210 std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
211 data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
213 std::shared_ptr<GeomAPI_XY> aStartPnt;
214 std::shared_ptr<GeomAPI_XY> aEndPnt;
215 if (aPointA && aPointB) {
216 aStartPnt = aPointA->pnt()->xy();
217 aEndPnt = aPointB->pnt()->xy();
218 } else if (aPointA) {
219 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
220 SketchPlugin_Constraint::ENTITY_B());
223 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
224 aStartPnt = aPoint->xy();
225 aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
226 } else if (aPointB) {
227 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
228 SketchPlugin_Constraint::ENTITY_A());
231 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
232 aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
233 aEndPnt = aPoint->xy();
237 if (aEndPnt->distance(aStartPnt) < tolerance)
240 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
241 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
243 double X = aFlyoutDir->dot(aLineDir->xy());
244 double Y = -aFlyoutDir->cross(aLineDir->xy());
245 aFlyoutAttr->setValue(X, Y);
246 myFlyoutUpdate = false;