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();
41 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
42 aData->attribute(SketchPlugin_Constraint::VALUE()));
44 if(anAttrValue->isInitialized())
47 double anAngle = calculateAngle();
48 anAttrValue->setValue(anAngle);
51 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
56 AISObjectPtr anAIS = thePrevious;
58 anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
61 // Set color from preferences
62 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
63 SKETCH_DIMENSION_COLOR);
64 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
68 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
70 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
71 theID == SketchPlugin_Constraint::ENTITY_B()) {
72 std::shared_ptr<ModelAPI_Data> aData = data();
75 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
76 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
77 if (!aLineA || !aLineB)
80 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
81 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
82 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
83 double anAngle = calculateAngle();
84 aValueAttr->setValue(anAngle);
86 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
87 // Recalculate flyout point in local coordinates
88 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
89 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
93 double SketchPlugin_ConstraintAngle::calculateAngle()
97 std::shared_ptr<ModelAPI_Data> aData = data();
98 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
99 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
100 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
102 // Start and end points of lines
103 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104 aLineA->attribute(SketchPlugin_Line::START_ID()));
105 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106 aLineA->attribute(SketchPlugin_Line::END_ID()));
108 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
109 aLineB->attribute(SketchPlugin_Line::START_ID()));
110 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111 aLineB->attribute(SketchPlugin_Line::END_ID()));
113 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
114 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
115 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
116 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
117 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
120 // Lines and their intersection point
121 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
122 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
123 std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
127 // Directions of lines
128 if (anInter->distance(aEndA) < tolerance)
130 if (anInter->distance(aEndB) < tolerance)
132 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
133 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
135 anAngle = aDirA->angle(aDirB) * 180.0 / PI;
139 void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY)
141 std::shared_ptr<ModelAPI_Data> aData = data();
142 if (!aData->isValid())
145 myFlyoutUpdate = true;
146 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
147 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
148 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
149 myFlyoutUpdate = false;