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_ConstraintAngle.h"
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Tools.h>
23 #include <SketcherPrs_Tools.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAPI_Angle2d.h>
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
38 #include <SketcherPrs_Factory.h>
39 #include <SketcherPrs_Tools.h>
43 const double tolerance = 1.e-7;
44 #define PI 3.1415926535897932
46 /// \brief Calculate intersection point of two lines
47 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
50 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
52 myFlyoutUpdate = false;
55 void SketchPlugin_ConstraintAngle::initAttributes()
57 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
59 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
60 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
62 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID(),
63 ModelAPI_AttributeDouble::typeId());
64 data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(),
65 ModelAPI_AttributeInteger::typeId());
67 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID(),
68 ModelAPI_AttributeBoolean::typeId());
69 data()->addAttribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID(),
70 ModelAPI_AttributeBoolean::typeId());
72 data()->addAttribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID(),
73 ModelAPI_AttributeInteger::typeId());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
77 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
78 std::string& theDefault)
80 theSection = "Visualization";
81 theName = "sketch_dimension_color";
82 theDefault = SKETCH_DIMENSION_COLOR;
85 void SketchPlugin_ConstraintAngle::execute()
87 std::shared_ptr<ModelAPI_Data> aData = data();
89 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA =
90 aData->refattr(SketchPlugin_Constraint::ENTITY_A());
91 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB =
92 aData->refattr(SketchPlugin_Constraint::ENTITY_B());
93 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
96 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
97 aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
99 if (!anAttrValue->isInitialized()) {
100 double anAngle = calculateAngle();
101 anAttrValue->setValue(anAngle);
102 updateConstraintValueByAngleValue();
104 // the value should to be computed here, not in the
105 // getAISObject in order to change the model value
106 // inside the object transaction. This is important for creating a constraint by preselection.
107 // The display of the presentation in this case happens after the transaction commit
108 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
109 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
110 if(!aFlyOutAttr->isInitialized())
111 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
114 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
119 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch(),
121 if (anAIS.get() && !thePrevious.get())
122 SketchPlugin_Tools::setDimensionColor(anAIS);
126 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
128 std::shared_ptr<ModelAPI_Data> aData = data();
131 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
132 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
133 if (!aLineA || !aLineB)
136 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
137 theID == SketchPlugin_Constraint::ENTITY_B()) {
138 AttributeDoublePtr aValueAttr = real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
139 AttributeDoublePtr aConstrValueAttr = real(SketchPlugin_Constraint::VALUE());
140 // only if one of attributes is not initialized, try to compute the current value
141 if (!aValueAttr->isInitialized() || !aConstrValueAttr->isInitialized()) {
142 if (aValueAttr->isInitialized() && !aConstrValueAttr->isInitialized())
143 // initialize base value of constraint
144 updateConstraintValueByAngleValue();
145 double anAngle = calculateAngle();
146 aValueAttr->setValue(anAngle);
147 updateConstraintValueByAngleValue();
149 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
150 // Recalculate flyout point in local coordinates
151 // coordinates are calculated according to the center of shapes intersection
152 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
153 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
156 std::shared_ptr<ModelAPI_Data> aData = data();
157 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
159 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
161 SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
163 // Intersection of lines
164 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
168 myFlyoutUpdate = true;
169 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
170 if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
171 aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
172 myFlyoutUpdate = false;
174 else if (theID == SketchPlugin_ConstraintAngle::TYPE_ID()) {
175 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
176 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
177 double anAngle = calculateAngle();
178 if (aValueAttr->text().empty())
179 aValueAttr->setValue(anAngle);
181 aValueAttr = std::dynamic_pointer_cast<
182 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::VALUE()));
183 aValueAttr->setValue(anAngle);
186 else if (theID == SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()) {
187 updateConstraintValueByAngleValue();
191 double SketchPlugin_ConstraintAngle::calculateAngle()
193 std::shared_ptr<ModelAPI_Data> aData = data();
194 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
195 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
196 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
198 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
199 aLineA->attribute(SketchPlugin_Line::START_ID()));
200 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201 aLineA->attribute(SketchPlugin_Line::END_ID()));
202 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203 aLineB->attribute(SketchPlugin_Line::START_ID()));
204 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
205 aLineB->attribute(SketchPlugin_Line::END_ID()));
207 std::shared_ptr<GeomAPI_Angle2d> anAng;
208 if (!attribute(ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
209 !attribute(ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
210 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
211 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
213 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
214 bool isReversed1 = boolean(ANGLE_REVERSED_FIRST_LINE_ID())->value();
215 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
216 bool isReversed2 = boolean(ANGLE_REVERSED_SECOND_LINE_ID())->value();
217 anAng = std::shared_ptr<GeomAPI_Angle2d>(
218 new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
220 double anAngle = anAng->angleDegree();
221 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
222 ModelAPI_AttributeDouble>(data()->attribute(VALUE()));
223 std::shared_ptr<ModelAPI_AttributeDouble> anAngleValueAttr = std::dynamic_pointer_cast<
224 ModelAPI_AttributeDouble>(data()->attribute(ANGLE_VALUE_ID()));
225 if (!aValueAttr->isInitialized())
226 aValueAttr->setValue(anAngle);
227 /// an angle value should be corrected by the current angle type
228 anAngle = getAngleForType(anAngleValueAttr->text().empty() ?
229 anAngle : anAngleValueAttr->value());
230 boolean(ANGLE_REVERSED_FIRST_LINE_ID())->setValue(anAng->isReversed(0));
231 boolean(ANGLE_REVERSED_SECOND_LINE_ID())->setValue(anAng->isReversed(1));
235 double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle, bool isPreviousValueObtuse)
237 double anAngle = theAngle;
239 std::shared_ptr<ModelAPI_Data> aData = data();
240 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
241 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
242 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
243 switch (anAngleType) {
244 case SketcherPrs_Tools::ANGLE_DIRECT:
247 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
248 if (theAngle > 180 || isPreviousValueObtuse)
249 anAngle = theAngle - 180.0;
251 anAngle = 180.0 - theAngle;
257 case SketcherPrs_Tools::ANGLE_BACKWARD:
258 anAngle = 360.0 - theAngle;
266 void SketchPlugin_ConstraintAngle::updateConstraintValueByAngleValue()
268 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
269 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID()));
270 double anAngle = aValueAttr->value();
272 /// an angle value should be corrected by the current angle type
273 aValueAttr = std::dynamic_pointer_cast<
274 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
275 if (!aValueAttr->isInitialized())
277 anAngle = getAngleForType(anAngle, aValueAttr->value() > 180.0);
278 aValueAttr->setValue(anAngle);
281 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
283 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
288 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
289 GeomDataAPI_Point2D>(attribute(theAttributeId));
290 if (aFlyOutAttr->isInitialized() &&
291 (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance))
294 DataPtr aData = data();
295 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
296 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
297 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
299 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
302 // Start and end points of lines
303 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
304 aLineA->attribute(SketchPlugin_Line::START_ID()));
305 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
306 aLineA->attribute(SketchPlugin_Line::END_ID()));
308 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
309 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
310 if (aStartA->distance(aEndA) < tolerance)
313 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
314 aLineB->attribute(SketchPlugin_Line::START_ID()));
315 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
316 aLineB->attribute(SketchPlugin_Line::END_ID()));
318 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
319 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
320 if (aStartB->distance(aEndB) < tolerance)
323 myFlyoutUpdate = true;
324 double aX = (aStartA->x() + aEndA->x() + aStartB->x() + aEndB->x()) / 4.;
325 double aY = (aStartA->y() + aEndA->y() + aStartB->y() + aEndB->y()) / 4.;
327 aFlyOutAttr->setValue(aX, aY);
328 myFlyoutUpdate = false;
334 // =============== Auxiliary functions ==================================
335 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
337 // Start and end points of lines
338 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
339 theLine1->attribute(SketchPlugin_Line::START_ID()));
340 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
341 theLine1->attribute(SketchPlugin_Line::END_ID()));
343 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
344 theLine2->attribute(SketchPlugin_Line::START_ID()));
345 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
346 theLine2->attribute(SketchPlugin_Line::END_ID()));
348 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
349 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
350 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
351 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
352 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
353 std::shared_ptr<GeomAPI_Pnt2d>();
355 // Lines and their intersection point
356 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
357 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
358 return aLA->intersect(aLB);