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_ConstraintLength.h"
22 #include <SketchPlugin_Line.h>
24 #include <GeomDataAPI_Point2D.h>
26 #include <SketcherPrs_Factory.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Result.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
35 #include <GeomDataAPI_Point2D.h>
37 #include <GeomAPI_Dir2d.h>
38 #include <GeomAPI_Lin2d.h>
39 #include <GeomAPI_Pnt2d.h>
40 #include <GeomAPI_XY.h>
42 #include <Config_PropManager.h>
46 const double tolerance = 1e-7;
48 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
50 myFlyoutUpdate = false;
53 void SketchPlugin_ConstraintLength::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());
59 data()->addAttribute(SketchPlugin_ConstraintLength::LOCATION_TYPE_ID(),
60 ModelAPI_AttributeInteger::typeId());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
64 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
65 std::string& theDefault)
67 theSection = "Visualization";
68 theName = "sketch_dimension_color";
69 theDefault = SKETCH_DIMENSION_COLOR;
72 void SketchPlugin_ConstraintLength::execute()
74 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
75 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
76 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
79 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
80 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
81 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
82 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
84 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
86 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
87 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
88 //if(!aValueAttr->isInitialized()) {
89 // aValueAttr->setValue(aLenght);
92 // the value should to be computed here, not in the getAISObject
93 // in order to change the model value
94 // inside the object transaction. This is important for creating a constraint by preselection.
95 // The display of the presentation in this case happens after the transaction commit
96 AttributePtr aFlyOutAttribute =
97 data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
98 if (!aFlyOutAttribute->isInitialized()) {
99 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
104 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
106 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
109 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
110 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
111 if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
114 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
115 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
117 std::shared_ptr<GeomAPI_Lin2d> aLine =
118 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
119 if (!aFlyOutAttr->isInitialized() ||
120 (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
121 double aDist = aPoint1->distance(aPoint2)/5.;
122 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
123 aFlyOutAttr->setValue(aFPnt);
129 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
131 bool aResult = false;
132 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
133 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
134 if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
135 theValue = aPoint1->distance(aPoint2);
141 bool SketchPlugin_ConstraintLength::getPoints(
142 std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
143 std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
144 std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
148 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
149 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
152 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
153 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
155 DataPtr aData = aFeature->data();
156 theStartPoint = std::dynamic_pointer_cast<
157 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
158 theEndPoint = std::dynamic_pointer_cast<
159 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
160 thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
161 thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
165 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
170 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
171 sketch()->coordinatePlane(), thePrevious);
175 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
176 if (theID == SketchPlugin_Constraint::ENTITY_A())
178 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
179 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
180 if (!aValueAttr->isInitialized()) {
181 // only if it is not initialized, try to compute the current value
183 if (computeLenghtValue(aLength))
184 aValueAttr->setValue(aLength);
186 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
187 // Recalculate flyout point in local coordinates of the line:
188 // the X coordinate is a length of projection of the flyout point on the line
189 // the Y coordinate is a distance from the point to the line
190 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
191 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
193 AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
194 attribute(SketchPlugin_Constraint::ENTITY_A()));
195 if (!aLineAttr || !aLineAttr->isObject())
197 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
198 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
201 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
202 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
204 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
205 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
207 myFlyoutUpdate = true;
208 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
209 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
210 double X = aFlyoutDir->dot(aLineDir->xy());
211 double Y = -aFlyoutDir->cross(aLineDir->xy());
212 aFlyoutAttr->setValue(X, Y);
213 myFlyoutUpdate = false;