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>
25 #include <SketcherPrs_Tools.h>
26 #include <SketcherPrs_Factory.h>
28 #include <GeomAPI_Dir2d.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeInteger.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Validator.h>
40 #include <Config_PropManager.h>
44 const double tolerance = 1e-7;
47 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
49 myFlyoutUpdate = false;
52 //*************************************************************************************
53 void SketchPlugin_ConstraintDistance::initAttributes()
55 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
57 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
58 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
59 data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
61 data()->addAttribute(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID(),
62 ModelAPI_AttributeInteger::typeId());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
66 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
67 std::string& theDefault)
69 theSection = "Visualization";
70 theName = "sketch_dimension_color";
71 theDefault = SKETCH_DIMENSION_COLOR;
74 //*************************************************************************************
75 void SketchPlugin_ConstraintDistance::execute()
77 std::shared_ptr<ModelAPI_Data> aData = data();
78 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
79 aData->attribute(SketchPlugin_Constraint::VALUE()));
81 if(anAttrValue->isInitialized())
84 double aDistance = calculateCurrentDistance();
86 anAttrValue->setValue(aDistance);
89 //*************************************************************************************
90 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
95 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
101 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
103 double aDistance = -1.;
105 std::shared_ptr<ModelAPI_Data> aData = data();
106 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
107 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
108 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
109 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
110 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
112 if (aPointA.get() && aPointB.get()) { // both points
113 aDistance = aPointA->pnt()->distance(aPointB->pnt());
115 FeaturePtr aLineFeature;
116 std::shared_ptr<SketchPlugin_Line> aLine;
117 if (!aPointA.get() && aPointB.get()) { //Line and point
118 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
119 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
121 aDistance = aLine->distanceToPoint(aPointB->pnt());
123 } else if (aPointA.get() && !aPointB.get()) { // Point and line
124 aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
125 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
127 aDistance = aLine->distanceToPoint(aPointA->pnt());
134 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
136 std::shared_ptr<ModelAPI_Data> aData = data();
137 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
138 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
139 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
140 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
141 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
143 if (!aPointA && !aPointB)
145 else if (aPointA || aPointB) {
148 aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
150 aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
151 else // both points are initialized
154 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
160 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
162 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
163 theID == SketchPlugin_Constraint::ENTITY_B())
165 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
166 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
167 if (!aValueAttr->isInitialized()) {
168 // only if it is not initialized, try to compute the current value
169 double aDistance = calculateCurrentDistance();
170 if (aDistance > 0) { // set as value the length of updated references
171 aValueAttr->setValue(aDistance);
174 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
175 // Recalculate flyout point in local coordinates of the distance constraint:
176 // the X coordinate is a length of projection of the flyout point on the
177 // line binding two distanced points
178 // or a line of projection of the distanced point onto the distanced segment
179 // the Y coordinate is a distance from the flyout point to the line
180 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
181 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
183 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
185 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
186 std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
187 data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
188 std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
189 data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
191 std::shared_ptr<GeomAPI_XY> aStartPnt;
192 std::shared_ptr<GeomAPI_XY> aEndPnt;
193 if (aPointA && aPointB) {
194 aStartPnt = aPointA->pnt()->xy();
195 aEndPnt = aPointB->pnt()->xy();
196 } else if (aPointA) {
197 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
198 SketchPlugin_Constraint::ENTITY_B());
201 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
202 aStartPnt = aPoint->xy();
203 aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
204 } else if (aPointB) {
205 FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
206 SketchPlugin_Constraint::ENTITY_A());
209 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
210 aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
211 aEndPnt = aPoint->xy();
215 if (aEndPnt->distance(aStartPnt) < tolerance)
218 myFlyoutUpdate = true;
219 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
220 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
222 double X = aFlyoutDir->dot(aLineDir->xy());
223 double Y = -aFlyoutDir->cross(aLineDir->xy());
224 aFlyoutAttr->setValue(X, Y);
225 myFlyoutUpdate = false;