1 // Copyright (C) 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 // File: SketchPlugin_ConstraintDistanceHorizontal.cpp
22 // Created: 2 May 2017
23 // Author: Artem ZHIDKOV
25 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
27 #include <SketcherPrs_Tools.h>
28 #include <SketcherPrs_Factory.h>
30 #include <GeomAPI_Dir2d.h>
31 #include <GeomAPI_XY.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeInteger.h>
37 const double tolerance = 1e-7;
40 SketchPlugin_ConstraintDistanceHorizontal::SketchPlugin_ConstraintDistanceHorizontal()
41 : SketchPlugin_ConstraintDistance()
45 //*************************************************************************************
46 void SketchPlugin_ConstraintDistanceHorizontal::initAttributes()
48 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
49 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
50 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
51 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
53 data()->addAttribute(SketchPlugin_ConstraintDistanceHorizontal::LOCATION_TYPE_ID(),
54 ModelAPI_AttributeInteger::typeId());
55 data()->integer(LOCATION_TYPE_ID())->setValue(SketcherPrs_Tools::LOCATION_AUTOMATIC);
58 //*************************************************************************************
59 void SketchPlugin_ConstraintDistanceHorizontal::execute()
61 AttributeDoublePtr anAttrValue = real(SketchPlugin_Constraint::VALUE());
62 if (anAttrValue->isInitialized() || !areAttributesInitialized())
65 double aDistance = calculateCurrentDistance();
66 anAttrValue->setValue(aDistance);
69 //*************************************************************************************
70 AISObjectPtr SketchPlugin_ConstraintDistanceHorizontal::getAISObject(AISObjectPtr thePrevious)
75 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
76 sketch()->coordinatePlane(),
81 double SketchPlugin_ConstraintDistanceHorizontal::calculateCurrentDistance()
83 std::shared_ptr<ModelAPI_Data> aData = data();
84 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
85 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
86 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
87 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
88 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
90 return aPointB->x() - aPointA->x();
93 void SketchPlugin_ConstraintDistanceHorizontal::attributeChanged(const std::string& theID)
95 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
96 theID == SketchPlugin_Constraint::ENTITY_B())
98 AttributeDoublePtr aValueAttr = real(SketchPlugin_Constraint::VALUE());
99 if (!aValueAttr->isInitialized() && areAttributesInitialized()) {
100 // only if it is not initialized, try to compute the current value
101 double aDistance = calculateCurrentDistance();
102 aValueAttr->setValue(aDistance);
104 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
105 // Recalculate flyout point in local coordinates of the distance constraint:
106 // the X coordinate is a length of projection of the flyout point on the
107 // line binding two distanced points
108 // or a line of projection of the distanced point onto the distanced segment
109 // the Y coordinate is a distance from the flyout point to the line
110 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
111 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
112 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
113 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
115 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
116 std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
117 data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
118 std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
119 data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
121 std::shared_ptr<GeomAPI_XY> aStartPnt = aPointA->pnt()->xy();
122 std::shared_ptr<GeomAPI_XY> aEndPnt = aPointB->pnt()->xy();
123 if (aEndPnt->distance(aStartPnt) < tolerance)
126 myFlyoutUpdate = true;
127 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aEndPnt);
128 double X = aFlyoutDir->x(); // Dot on OX axis
129 double Y = aFlyoutDir->y(); // Cross to OX axis
130 aFlyoutAttr->setValue(X, Y);
131 myFlyoutUpdate = false;