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 <ModelAPI_AttributeInteger.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Dir2d.h>
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_XY.h>
20 #include <SketcherPrs_Factory.h>
21 #include <SketcherPrs_Tools.h>
25 const double tolerance = 1.e-7;
26 #define PI 3.1415926535897932
28 /// \brief Calculate intersection point of two lines
29 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
32 SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle()
34 myFlyoutUpdate = false;
37 void SketchPlugin_ConstraintAngle::initAttributes()
39 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
40 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
41 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
42 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
44 data()->addAttribute(SketchPlugin_ConstraintAngle::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
47 void SketchPlugin_ConstraintAngle::colorConfigInfo(std::string& theSection, std::string& theName,
48 std::string& theDefault)
50 theSection = "Visualization";
51 theName = "sketch_dimension_color";
52 theDefault = SKETCH_DIMENSION_COLOR;
55 void SketchPlugin_ConstraintAngle::execute()
57 std::shared_ptr<ModelAPI_Data> aData = data();
59 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
60 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
61 if (!anAttrA->isInitialized() || !anAttrB->isInitialized())
64 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
65 aData->attribute(SketchPlugin_Constraint::VALUE()));
67 if (!anAttrValue->isInitialized()) {
68 double anAngle = calculateAngle();
69 anAttrValue->setValue(anAngle);
71 // the value should to be computed here, not in the getAISObject in order to change the model value
72 // inside the object transaction. This is important for creating a constraint by preselection.
73 // The display of the presentation in this case happens after the transaction commit
74 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
75 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
76 if(!aFlyOutAttr->isInitialized())
77 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
80 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
85 AISObjectPtr anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane(),
90 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
92 std::shared_ptr<ModelAPI_Data> aData = data();
95 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
96 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
97 if (!aLineA || !aLineB)
100 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
101 theID == SketchPlugin_Constraint::ENTITY_B()) {
102 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
103 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
104 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
105 double anAngle = calculateAngle();
106 aValueAttr->setValue(anAngle);
108 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
109 // Recalculate flyout point in local coordinates
110 // coordinates are calculated according to the center of shapes intersection
111 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
112 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
114 std::shared_ptr<ModelAPI_Data> aData = data();
115 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
116 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
117 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
119 // Intersection of lines
120 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
124 myFlyoutUpdate = true;
125 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy());
126 if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance)
127 aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y());
128 myFlyoutUpdate = false;
132 double SketchPlugin_ConstraintAngle::calculateAngle()
134 double anAngle = 0.0;
136 std::shared_ptr<ModelAPI_Data> aData = data();
137 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
138 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
139 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
141 // Intersection of lines
142 std::shared_ptr<GeomAPI_Pnt2d> anInter = intersect(aLineA, aLineB);
146 // Start and end points of lines
147 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148 aLineA->attribute(SketchPlugin_Line::START_ID()));
149 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150 aLineA->attribute(SketchPlugin_Line::END_ID()));
152 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
153 aLineB->attribute(SketchPlugin_Line::START_ID()));
154 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
155 aLineB->attribute(SketchPlugin_Line::END_ID()));
157 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
158 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
159 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
160 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
162 double aDist[2][2] = {
163 { anInter->distance(aStartA), anInter->distance(aEndA) },
164 { anInter->distance(aStartB), anInter->distance(aEndB) }
167 // Directions of lines
168 if (aDist[0][0] > aDist[0][1])
170 if (aDist[1][0] > aDist[1][1])
173 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
174 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
176 double aDirAngle = aDirA->angle(aDirB);
178 aDirAngle += 2.0 * PI;
179 anAngle = fabs(aDirAngle) * 180.0 / PI;
180 //anAngle = fabs(aDirA->angle(aDirB)) * 180.0 / PI;
185 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
187 std::shared_ptr<ModelAPI_Data> aData = data();
188 if (!aData->isValid())
191 myFlyoutUpdate = true;
192 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
193 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
194 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
195 myFlyoutUpdate = false;
199 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
201 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
206 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
207 GeomDataAPI_Point2D>(attribute(theAttributeId));
208 if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
211 DataPtr aData = data();
212 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
213 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
214 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
216 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
219 // Start and end points of lines
220 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221 aLineA->attribute(SketchPlugin_Line::START_ID()));
222 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
223 aLineA->attribute(SketchPlugin_Line::END_ID()));
225 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
226 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
227 if (aStartA->distance(aEndA) < tolerance)
230 myFlyoutUpdate = true;
231 double aX = (aStartA->x() + aEndA->x()) / 2.;
232 double aY = (aStartA->y() + aEndA->y()) / 2.;
234 aFlyOutAttr->setValue(aX, aY);
235 myFlyoutUpdate = false;
241 // =============== Auxiliary functions ==================================
242 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
244 // Start and end points of lines
245 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
246 theLine1->attribute(SketchPlugin_Line::START_ID()));
247 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
248 theLine1->attribute(SketchPlugin_Line::END_ID()));
250 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
251 theLine2->attribute(SketchPlugin_Line::START_ID()));
252 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
253 theLine2->attribute(SketchPlugin_Line::END_ID()));
255 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
256 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
257 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
258 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
259 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
260 std::shared_ptr<GeomAPI_Pnt2d>();
262 // Lines and their intersection point
263 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
264 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
265 return aLA->intersect(aLB);