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])
172 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
173 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
175 anAngle = fabs(aDirA->angle(aDirB)) * 180.0 / PI;
179 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
181 std::shared_ptr<ModelAPI_Data> aData = data();
182 if (!aData->isValid())
185 myFlyoutUpdate = true;
186 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
187 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
188 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
189 myFlyoutUpdate = false;
193 bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId)
195 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
200 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
201 GeomDataAPI_Point2D>(attribute(theAttributeId));
202 if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance)
205 DataPtr aData = data();
206 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
207 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
208 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
210 if ((aLineA.get() == NULL) || (aLineB.get() == NULL))
213 // Start and end points of lines
214 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215 aLineA->attribute(SketchPlugin_Line::START_ID()));
216 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217 aLineA->attribute(SketchPlugin_Line::END_ID()));
219 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
220 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
221 if (aStartA->distance(aEndA) < tolerance)
224 myFlyoutUpdate = true;
225 double aX = (aStartA->x() + aEndA->x()) / 2.;
226 double aY = (aStartA->y() + aEndA->y()) / 2.;
228 aFlyOutAttr->setValue(aX, aY);
229 myFlyoutUpdate = false;
235 // =============== Auxiliary functions ==================================
236 std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2)
238 // Start and end points of lines
239 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
240 theLine1->attribute(SketchPlugin_Line::START_ID()));
241 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
242 theLine1->attribute(SketchPlugin_Line::END_ID()));
244 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
245 theLine2->attribute(SketchPlugin_Line::START_ID()));
246 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
247 theLine2->attribute(SketchPlugin_Line::END_ID()));
249 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
250 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
251 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
252 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
253 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
254 std::shared_ptr<GeomAPI_Pnt2d>();
256 // Lines and their intersection point
257 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
258 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
259 return aLA->intersect(aLB);