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>
23 const double tolerance = 1.e-7;
24 #define PI 3.1415926535897932
27 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
29 myFlyoutUpdate = false;
32 void SketchPlugin_ConstraintAngle::initAttributes()
34 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
35 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
37 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
40 void SketchPlugin_ConstraintAngle::execute()
42 std::shared_ptr<ModelAPI_Data> aData = data();
44 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
45 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
46 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
49 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
50 aData->attribute(SketchPlugin_Constraint::VALUE()));
52 if (!anAttrValue->isInitialized()) {
53 double anAngle = calculateAngle();
54 anAttrValue->setValue(anAngle);
56 // the value should to be computed here, not in the getAISObject in order to change the model value
57 // inside the object transaction. This is important for creating a constraint by preselection.
58 // The display of the presentation in this case happens after the transaction commit
59 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
60 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
61 if(!aFlyOutAttr->isInitialized())
62 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
65 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
70 AISObjectPtr anAIS = thePrevious;
72 anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
75 // Set color from preferences
76 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
77 SKETCH_DIMENSION_COLOR);
78 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
82 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
84 std::shared_ptr<ModelAPI_Data> aData = data();
87 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
88 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
89 if (!aLineA || !aLineB)
92 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
93 theID == SketchPlugin_Constraint::ENTITY_B()) {
94 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
95 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
96 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
97 double anAngle = calculateAngle();
98 aValueAttr->setValue(anAngle);
100 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
101 // Recalculate flyout point in local coordinates
102 // coordinates are calculated according to the center of shapes intersection
103 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
104 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
108 double SketchPlugin_ConstraintAngle::calculateAngle()
110 double anAngle = 0.0;
112 std::shared_ptr<ModelAPI_Data> aData = data();
113 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
114 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
115 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
117 // Start and end points of lines
118 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119 aLineA->attribute(SketchPlugin_Line::START_ID()));
120 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121 aLineA->attribute(SketchPlugin_Line::END_ID()));
123 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124 aLineB->attribute(SketchPlugin_Line::START_ID()));
125 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
126 aLineB->attribute(SketchPlugin_Line::END_ID()));
128 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
129 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
130 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
131 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
132 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
135 // Lines and their intersection point
136 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
137 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
138 std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
142 // Directions of lines
143 if (anInter->distance(aEndA) < tolerance)
145 if (anInter->distance(aEndB) < tolerance)
147 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
148 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
150 anAngle = aDirA->angle(aDirB) * 180.0 / PI;
154 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
156 std::shared_ptr<ModelAPI_Data> aData = data();
157 if (!aData->isValid())
160 myFlyoutUpdate = true;
161 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
162 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
163 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
164 myFlyoutUpdate = false;
168 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
170 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
175 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
176 GeomDataAPI_Point2D>(attribute(theAttributeId));
177 if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
180 DataPtr aData = data();
181 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
182 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
183 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
185 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
188 // Start and end points of lines
189 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aLineA->attribute(SketchPlugin_Line::START_ID()));
191 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aLineA->attribute(SketchPlugin_Line::END_ID()));
194 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
195 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
196 if (aStartA->distance(aEndA) < tolerance)
199 myFlyoutUpdate = true;
200 double aX = (aStartA->x() + aEndA->x()) / 2.;
201 double aY = (aStartA->y() + aEndA->y()) / 2.;
203 aFlyOutAttr->setValue(aX, aY);
204 myFlyoutUpdate = false;