1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_ConstraintDistance.h"
21 #include <SketchPlugin_Point.h>
22 #include <SketchPlugin_Circle.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_Tools.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_AttributeInteger.h>
37 #include <ModelAPI_Data.h>
38 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Validator.h>
41 #include <Config_PropManager.h>
45 const double tolerance = 1e-7;
48 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
50 myFlyoutUpdate = false;
53 //*************************************************************************************
54 void SketchPlugin_ConstraintDistance::initAttributes()
56 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
58 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
59 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
60 data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
62 data()->addAttribute(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID(),
63 ModelAPI_AttributeInteger::typeId());
64 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
67 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
68 std::string& theDefault)
70 theSection = "Visualization";
71 theName = "sketch_dimension_color";
72 theDefault = SKETCH_DIMENSION_COLOR;
75 //*************************************************************************************
76 void SketchPlugin_ConstraintDistance::execute()
78 std::shared_ptr<ModelAPI_Data> aData = data();
79 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
80 aData->attribute(SketchPlugin_Constraint::VALUE()));
82 if(anAttrValue->isInitialized())
85 double aDistance = calculateCurrentDistance();
87 anAttrValue->setValue(aDistance);
90 //*************************************************************************************
91 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
96 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
99 if (anAIS.get() && !thePrevious.get())
100 SketchPlugin_Tools::setDimensionColor(anAIS);
104 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
106 double aDistance = -1.;
108 std::shared_ptr<ModelAPI_Data> aData = data();
109 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
110 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
111 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
112 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
113 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
115 if (aPointA.get() && aPointB.get()) { // both points
116 aDistance = aPointA->pnt()->distance(aPointB->pnt());
118 FeaturePtr aLineFeature;
119 std::shared_ptr<SketchPlugin_Line> aLine;
120 if (!aPointA.get() && aPointB.get()) { //Line and point
121 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
122 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
124 aDistance = aLine->distanceToPoint(aPointB->pnt());
126 } else if (aPointA.get() && !aPointB.get()) { // Point and line
127 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
128 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
130 aDistance = aLine->distanceToPoint(aPointA->pnt());
137 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
139 std::shared_ptr<ModelAPI_Data> aData = data();
140 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
141 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
142 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
143 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
144 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
146 if (!aPointA && !aPointB)
148 else if (aPointA || aPointB) {
151 aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
153 aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
154 else // both points are initialized
157 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
163 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
165 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
166 theID == SketchPlugin_Constraint::ENTITY_B())
168 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
169 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
170 if (!aValueAttr->isInitialized()) {
171 // only if it is not initialized, try to compute the current value
172 double aDistance = calculateCurrentDistance();
173 if (aDistance > 0) { // set as value the length of updated references
174 aValueAttr->setValue(aDistance);
177 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
178 // Recalculate flyout point in local coordinates of the distance constraint:
179 // the X coordinate is a length of projection of the flyout point on the
180 // line binding two distanced points
181 // or a line of projection of the distanced point onto the distanced segment
182 // the Y coordinate is a distance from the flyout point to the line
183 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
184 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
186 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
188 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
189 std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
190 data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
191 std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
192 data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
194 std::shared_ptr<GeomAPI_XY> aStartPnt;
195 std::shared_ptr<GeomAPI_XY> aEndPnt;
196 if (aPointA && aPointB) {
197 aStartPnt = aPointA->pnt()->xy();
198 aEndPnt = aPointB->pnt()->xy();
199 } else if (aPointA) {
200 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
201 SketchPlugin_Constraint::ENTITY_B());
204 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
205 aStartPnt = aPoint->xy();
206 aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
207 } else if (aPointB) {
208 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
209 SketchPlugin_Constraint::ENTITY_A());
212 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
213 aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
214 aEndPnt = aPoint->xy();
218 if (aEndPnt->distance(aStartPnt) < tolerance)
221 myFlyoutUpdate = true;
222 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
223 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
225 double X = aFlyoutDir->dot(aLineDir->xy());
226 double Y = -aFlyoutDir->cross(aLineDir->xy());
227 aFlyoutAttr->setValue(X, Y);
228 myFlyoutUpdate = false;