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()
29 void SketchPlugin_ConstraintAngle::initAttributes()
31 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
32 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
33 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
36 void SketchPlugin_ConstraintAngle::execute()
38 std::shared_ptr<ModelAPI_Data> aData = data();
39 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
40 aData->attribute(SketchPlugin_Constraint::VALUE()));
42 if(anAttrValue->isInitialized())
45 double anAngle = calculateAngle();
46 anAttrValue->setValue(anAngle);
49 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
54 AISObjectPtr anAIS = thePrevious;
56 anAIS = SketcherPrs_Factory::angleConstraint(this, sketch()->coordinatePlane());
61 void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID)
63 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
64 theID == SketchPlugin_Constraint::ENTITY_B()) {
65 std::shared_ptr<ModelAPI_Data> aData = data();
68 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
69 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
70 if (!aLineA || !aLineB)
73 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
74 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
75 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
76 double anAngle = calculateAngle();
77 aValueAttr->setValue(anAngle);
82 double SketchPlugin_ConstraintAngle::calculateAngle()
86 std::shared_ptr<ModelAPI_Data> aData = data();
87 std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
88 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
89 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
91 // Start and end points of lines
92 std::shared_ptr<GeomDataAPI_Point2D> aPointA1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93 aLineA->attribute(SketchPlugin_Line::START_ID()));
94 std::shared_ptr<GeomDataAPI_Point2D> aPointA2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
95 aLineA->attribute(SketchPlugin_Line::END_ID()));
97 std::shared_ptr<GeomDataAPI_Point2D> aPointB1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
98 aLineB->attribute(SketchPlugin_Line::START_ID()));
99 std::shared_ptr<GeomDataAPI_Point2D> aPointB2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100 aLineB->attribute(SketchPlugin_Line::END_ID()));
102 std::shared_ptr<GeomAPI_Pnt2d> aStartA = aPointA1->pnt();
103 std::shared_ptr<GeomAPI_Pnt2d> aEndA = aPointA2->pnt();
104 std::shared_ptr<GeomAPI_Pnt2d> aStartB = aPointB1->pnt();
105 std::shared_ptr<GeomAPI_Pnt2d> aEndB = aPointB2->pnt();
106 if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance)
109 // Lines and their intersection point
110 std::shared_ptr<GeomAPI_Lin2d> aLA(new GeomAPI_Lin2d(aStartA, aEndA));
111 std::shared_ptr<GeomAPI_Lin2d> aLB(new GeomAPI_Lin2d(aStartB, aEndB));
112 std::shared_ptr<GeomAPI_Pnt2d> anInter = aLA->intersect(aLB);
116 // Directions of lines
117 if (anInter->distance(aEndA) < tolerance)
119 if (anInter->distance(aEndB) < tolerance)
121 std::shared_ptr<GeomAPI_Dir2d> aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy())));
122 std::shared_ptr<GeomAPI_Dir2d> aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy())));
124 anAngle = aDirA->angle(aDirB) * 180.0 / PI;