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_ConstraintAngle.h"
22 #include <SketchPlugin_Line.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeInteger.h>
27 #include <GeomDataAPI_Point2D.h>
29 #include <GeomAPI_Angle2d.h>
30 #include <GeomAPI_Dir2d.h>
31 #include <GeomAPI_Lin2d.h>
32 #include <GeomAPI_Pnt2d.h>
33 #include <GeomAPI_XY.h>
35 #include <SketcherPrs_Factory.h>
36 #include <SketcherPrs_Tools.h>
40 const double tolerance = 1.e-7;
41 #define PI 3.1415926535897932
43 /// \brief Calculate intersection point of two lines
44 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
47 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
49 myFlyoutUpdate = false;
52 void SketchPlugin_ConstraintAngle::initAttributes()
54 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
56 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
57 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
59 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(),
60 ModelAPI_AttributeDouble::typeId());
61 data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(),
62 ModelAPI_AttributeInteger::typeId());
64 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(),
65 ModelAPI_AttributeBoolean::typeId());
66 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(),
67 ModelAPI_AttributeBoolean::typeId());
70 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
71 std::string& theDefault)
73 theSection = "Visualization";
74 theName = "sketch_dimension_color";
75 theDefault = SKETCH_DIMENSION_COLOR;
78 void SketchPlugin_ConstraintAngle::execute()
80 std::shared_ptr<ModelAPI_Data> aData = data();
82 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA =
83 aData->refattr(SketchPlugin_Constraint::ENTITY_A());
84 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB =
85 aData->refattr(SketchPlugin_Constraint::ENTITY_B());
86 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
89 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
90 aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
92 if (!anAttrValue->isInitialized()) {
93 double anAngle = calculateAngle();
94 anAttrValue->setValue(anAngle);
95 updateConstraintValueByAngleValue();
97 // the value should to be computed here, not in the
98 // getAISObject in order to change the model value
99 // inside the object transaction. This is important for creating a constraint by preselection.
100 // The display of the presentation in this case happens after the transaction commit
101 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
102 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
103 if(!aFlyOutAttr->isInitialized())
104 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
107 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
112 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
117 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
119 std::shared_ptr<ModelAPI_Data> aData = data();
122 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
123 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
124 if (!aLineA || !aLineB)
127 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
128 theID == SketchPlugin_Constraint::ENTITY_B()) {
129 AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
130 AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE());
131 // only if one of attributes is not initialized, try to compute the current value
132 if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) {
133 if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized())
134 // initialize base value of constraint
135 updateConstraintValueByAngleValue();
136 double anAngle = calculateAngle();
137 aValueAttr->setValue(anAngle);
138 updateConstraintValueByAngleValue();
140 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
141 // Recalculate flyout point in local coordinates
142 // coordinates are calculated according to the center of shapes intersection
143 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
144 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
147 std::shared_ptr<ModelAPI_Data> aData = data();
148 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
150 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
152 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
154 // Intersection of lines
155 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
159 myFlyoutUpdate = true;
160 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
161 if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
162 aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
163 myFlyoutUpdate = false;
165 else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
166 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
167 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
168 double anAngle = calculateAngle();
169 if (aValueAttr->text().empty())
170 aValueAttr->setValue(anAngle);
172 aValueAttr = std::dynamic_pointer_cast<
173 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
174 aValueAttr->setValue(anAngle);
177 else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
178 updateConstraintValueByAngleValue();
182 double SketchPlugin_ConstraintAngle::calculateAngle()
184 std::shared_ptr<ModelAPI_Data> aData = data();
185 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
186 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
187 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
189 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aLineA->attribute(SketchPlugin_Line::START_ID()));
191 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aLineA->attribute(SketchPlugin_Line::END_ID()));
193 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194 aLineB->attribute(SketchPlugin_Line::START_ID()));
195 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
196 aLineB->attribute(SketchPlugin_Line::END_ID()));
198 std::shared_ptr<GeomAPI_Angle2d> anAng;
199 if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
200 !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
201 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
202 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
204 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
205 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
206 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
207 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
208 anAng = std::shared_ptr<GeomAPI_Angle2d>(
209 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
211 double anAngle = anAng->angleDegree();
212 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
213 ModelAPI_AttributeDouble>(data()->attribute(VALUE()));
214 std::shared_ptr<ModelAPI_AttributeDouble> anAngleValueAttr = std::dynamic_pointer_cast<
215 ModelAPI_AttributeDouble>(data()->attribute(ANGLE_VALUE_ID()));
216 if (!aValueAttr->isInitialized())
217 aValueAttr->setValue(anAngle);
218 /// an angle value should be corrected by the current angle type
219 anAngle = getAngleForType(anAngleValueAttr->text().empty() ?
220 anAngle : anAngleValueAttr->value());
221 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
222 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
226 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
228 double anAngle = theAngle;
230 std::shared_ptr<ModelAPI_Data> aData = data();
231 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
232 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
233 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
234 switch (anAngleType) {
235 case SketcherPrs_Tools::ANGLE_DIRECT:
238 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
239 if (theAngle > 180 || isPreviousValueObtuse)
240 anAngle = theAngle - 180.0;
242 anAngle = 180.0 - theAngle;
248 case SketcherPrs_Tools::ANGLE_BACKWARD:
249 anAngle = 360.0 - theAngle;
257 void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
259 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
260 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
261 double anAngle = aValueAttr->value();
263 /// an angle value should be corrected by the current angle type
264 aValueAttr = std::dynamic_pointer_cast<
265 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
266 if (!aValueAttr->isInitialized())
268 anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
269 aValueAttr->setValue(anAngle);
272 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
274 std::shared_ptr<ModelAPI_Data> aData = data();
275 if (!aData->isValid())
278 myFlyoutUpdate = true;
279 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
280 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
281 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
282 myFlyoutUpdate = false;
286 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
288 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
293 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
294 GeomDataAPI_Point2D>(attribute(theAttributeId));
295 if (aFlyOutAttr->isInitialized() &&
296 (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
299 DataPtr aData = data();
300 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
301 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
302 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
304 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
307 // Start and end points of lines
308 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
309 aLineA->attribute(SketchPlugin_Line::START_ID()));
310 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
311 aLineA->attribute(SketchPlugin_Line::END_ID()));
313 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
314 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
315 if (aStartA->distance(aEndA) < tolerance)
318 myFlyoutUpdate = true;
319 double aX = (aStartA->x() + aEndA->x()) / 2.;
320 double aY = (aStartA->y() + aEndA->y()) / 2.;
322 aFlyOutAttr->setValue(aX, aY);
323 myFlyoutUpdate = false;
329 // =============== Auxiliary functions ==================================
330 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
332 // Start and end points of lines
333 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
334 theLine1->attribute(SketchPlugin_Line::START_ID()));
335 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
336 theLine1->attribute(SketchPlugin_Line::END_ID()));
338 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
339 theLine2->attribute(SketchPlugin_Line::START_ID()));
340 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
341 theLine2->attribute(SketchPlugin_Line::END_ID()));
343 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
344 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
345 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
346 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
347 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
348 std::shared_ptr<GeomAPI_Pnt2d>();
350 // Lines and their intersection point
351 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
352 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
353 return aLA->intersect(aLB);