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());
57 data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
60 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
61 std::string& theDefault)
63 theSection = "Visualization";
64 theName = "sketch_dimension_color";
65 theDefault = SKETCH_DIMENSION_COLOR;
68 //*************************************************************************************
69 void SketchPlugin_ConstraintDistance::execute()
71 std::shared_ptr<ModelAPI_Data> aData = data();
72 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
73 aData->attribute(SketchPlugin_Constraint::VALUE()));
75 if(anAttrValue->isInitialized())
78 double aDistance = calculateCurrentDistance();
80 anAttrValue->setValue(aDistance);
83 //*************************************************************************************
84 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
89 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
90 sketch()->coordinatePlane(),
95 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
97 double aDistance = -1.;
99 std::shared_ptr<ModelAPI_Data> aData = data();
100 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
101 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
102 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
103 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
104 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
106 if (aPointA.get() && aPointB.get()) { // both points
107 aDistance = aPointA->pnt()->distance(aPointB->pnt());
109 FeaturePtr aLineFeature;
110 std::shared_ptr<SketchPlugin_Line> aLine;
111 if (!aPointA.get() && aPointB.get()) { //Line and point
112 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
113 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
115 aDistance = aLine->distanceToPoint(aPointB->pnt());
117 } else if (aPointA.get() && !aPointB.get()) { // Point and line
118 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
119 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
121 aDistance = aLine->distanceToPoint(aPointA->pnt());
128 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
130 std::shared_ptr<ModelAPI_Data> aData = data();
131 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
132 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
133 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
134 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
135 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
137 if (!aPointA && !aPointB)
139 else if (aPointA || aPointB) {
142 aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
144 aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
145 else // both points are initialized
148 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
154 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
156 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
157 theID == SketchPlugin_Constraint::ENTITY_B())
159 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
160 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
161 if (!aValueAttr->isInitialized()) {
162 // only if it is not initialized, try to compute the current value
163 double aDistance = calculateCurrentDistance();
164 if (aDistance > 0) { // set as value the length of updated references
165 aValueAttr->setValue(aDistance);
168 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
169 // Recalculate flyout point in local coordinates of the distance constraint:
170 // the X coordinate is a length of projection of the flyout point on the
171 // line binding two distanced points
172 // or a line of projection of the distanced point onto the distanced segment
173 // the Y coordinate is a distance from the flyout point to the line
174 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
175 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
177 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
179 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
180 std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
181 data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
182 std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
183 data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
185 std::shared_ptr<GeomAPI_XY> aStartPnt;
186 std::shared_ptr<GeomAPI_XY> aEndPnt;
187 if (aPointA && aPointB) {
188 aStartPnt = aPointA->pnt()->xy();
189 aEndPnt = aPointB->pnt()->xy();
190 } else if (aPointA) {
191 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
192 SketchPlugin_Constraint::ENTITY_B());
195 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
196 aStartPnt = aPoint->xy();
197 aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
198 } else if (aPointB) {
199 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
200 SketchPlugin_Constraint::ENTITY_A());
203 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
204 aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
205 aEndPnt = aPoint->xy();
209 if (aEndPnt->distance(aStartPnt) < tolerance)
212 myFlyoutUpdate = true;
213 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
214 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
216 double X = aFlyoutDir->dot(aLineDir->xy());
217 double Y = -aFlyoutDir->cross(aLineDir->xy());
218 aFlyoutAttr->setValue(X, Y);
219 myFlyoutUpdate = false;