1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintAngle.cpp
4 // Created: 19 August 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintAngle.h"
8 #include <SketchPlugin_Line.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <GeomDataAPI_Point2D.h>
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Lin2d.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_XY.h>
18 #include <SketcherPrs_Factory.h>
19 #include <SketcherPrs_Tools.h>
21 const double tolerance = 1.e-7;
22 #define PI 3.1415926535897932
25 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
27 myFlyoutUpdate = false;
30 void SketchPlugin_ConstraintAngle::initAttributes()
32 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
33 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
34 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
35 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
38 void SketchPlugin_ConstraintAngle::execute()
40 std::shared_ptr<ModelAPI_Data> aData = data();
42 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
43 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
44 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
47 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
48 aData->attribute(SketchPlugin_Constraint::VALUE()));
50 if (!anAttrValue->isInitialized()) {
51 double anAngle = calculateAngle();
52 anAttrValue->setValue(anAngle);
54 // the value should to be computed here, not in the getAISObject in order to change the model value
55 // inside the object transaction. This is important for creating a constraint by preselection.
56 // The display of the presentation in this case happens after the transaction commit
57 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
58 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
59 if(!aFlyOutAttr->isInitialized())
60 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
63 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
68 AISObjectPtr anAIS = thePrevious;
70 anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
73 // Set color from preferences
74 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
75 SKETCH_DIMENSION_COLOR);
76 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
80 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
82 std::shared_ptr<ModelAPI_Data> aData = data();
85 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
86 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
87 if (!aLineA || !aLineB)
90 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
91 theID == SketchPlugin_Constraint::ENTITY_B()) {
92 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
93 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
94 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
95 double anAngle = calculateAngle();
96 aValueAttr->setValue(anAngle);
98 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
99 // Recalculate flyout point in local coordinates
100 // coordinates are calculated according to the center of shapes intersection
101 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
102 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
106 double SketchPlugin_ConstraintAngle::calculateAngle()
108 double anAngle = 0.0;
110 std::shared_ptr<ModelAPI_Data> aData = data();
111 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
112 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
113 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
115 // Start and end points of lines
116 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
117 aLineA->attribute(SketchPlugin_Line::START_ID()));
118 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119 aLineA->attribute(SketchPlugin_Line::END_ID()));
121 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
122 aLineB->attribute(SketchPlugin_Line::START_ID()));
123 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124 aLineB->attribute(SketchPlugin_Line::END_ID()));
126 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
127 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
128 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
129 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
130 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
133 // Lines and their intersection point
134 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
135 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
136 std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
140 // Directions of lines
141 if (anInter->distance(aEndA) < tolerance)
143 if (anInter->distance(aEndB) < tolerance)
145 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
146 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
148 anAngle = aDirA->angle(aDirB) * 180.0 / PI;
152 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
154 std::shared_ptr<ModelAPI_Data> aData = data();
155 if (!aData->isValid())
158 myFlyoutUpdate = true;
159 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
160 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
161 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
162 myFlyoutUpdate = false;
166 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
168 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
173 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
174 GeomDataAPI_Point2D>(attribute(theAttributeId));
175 if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
178 DataPtr aData = data();
179 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
180 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
181 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
183 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
186 // Start and end points of lines
187 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188 aLineA->attribute(SketchPlugin_Line::START_ID()));
189 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aLineA->attribute(SketchPlugin_Line::END_ID()));
192 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
193 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
194 if (aStartA->distance(aEndA) < tolerance)
197 myFlyoutUpdate = true;
198 double aX = (aStartA->x() + aEndA->x()) / 2.;
199 double aY = (aStartA->y() + aEndA->y()) / 2.;
201 aFlyOutAttr->setValue(aX, aY);
202 myFlyoutUpdate = false;