Salome HOME
Compilation fix
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintCoincidence.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintCoincidence.cpp
4 // Created: 08 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintCoincidence.h"
8
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Point.h>
13
14 #include <SketcherPrs_Factory.h>
15
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Data.h>
18 #include <GeomDataAPI_Dir.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Point2D.h>
21
22 #include <GeomAPI_Circ2d.h>
23 #include <GeomAPI_Dir2d.h>
24 #include <GeomAPI_Lin2d.h>
25
26 /// \brief Update coordinates of the point to be correctly placed on the feature
27 static void adjustPointOnEntity(std::shared_ptr<GeomDataAPI_Point2D> thePoint, FeaturePtr theFeature);
28
29 SketchPlugin_ConstraintCoincidence::SketchPlugin_ConstraintCoincidence()
30 {
31 }
32
33 void SketchPlugin_ConstraintCoincidence::initAttributes()
34 {
35   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
37 }
38
39 void SketchPlugin_ConstraintCoincidence::execute()
40 {
41 }
42
43 AISObjectPtr SketchPlugin_ConstraintCoincidence::getAISObject(AISObjectPtr thePrevious)
44 {
45   if (!sketch())
46     return thePrevious;
47
48   AISObjectPtr anAIS = SketcherPrs_Factory::coincidentConstraint(this, sketch()->coordinatePlane(),
49                                                                  thePrevious);
50   return anAIS;
51 }
52
53 void SketchPlugin_ConstraintCoincidence::attributeChanged(const std::string& theID)
54 {
55   if (theID == ENTITY_A() || theID == ENTITY_B()) {
56     AttributeRefAttrPtr anAttrA = 
57         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_A()));
58     AttributeRefAttrPtr anAttrB = 
59         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_B()));
60
61     if (!anAttrA || !anAttrB || (!anAttrA->isObject() && !anAttrB->isObject()))
62       return;
63
64     if (anAttrA->isObject()) {
65       AttributeRefAttrPtr aTransient = anAttrA;
66       anAttrA = anAttrB;
67       anAttrB = aTransient;
68     }
69
70     std::shared_ptr<GeomDataAPI_Point2D> aPoint =
71         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrA->attr());
72     FeaturePtr aFeature;
73     if (anAttrB->object())
74       aFeature = ModelAPI_Feature::feature(anAttrB->object());
75
76     // Adjust position of a point lying on a line or arc
77     if (aPoint && aFeature)
78       adjustPointOnEntity(aPoint, aFeature);
79   }
80 }
81
82
83
84
85 // =======   Auxiliary functions   ================================================
86 void adjustPointOnEntity(std::shared_ptr<GeomDataAPI_Point2D> thePoint, FeaturePtr theFeature)
87 {
88   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
89     // project point on line
90     std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
91         theFeature->attribute(SketchPlugin_Line::START_ID()));
92     std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93         theFeature->attribute(SketchPlugin_Line::END_ID()));
94
95     std::shared_ptr<GeomAPI_Lin2d> aLine(new GeomAPI_Lin2d(aStart->pnt(), aEnd->pnt()));
96     std::shared_ptr<GeomAPI_Pnt2d> aProjected = aLine->project(thePoint->pnt());
97     thePoint->setValue(aProjected);
98   }
99   else if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
100     // project point on circle
101     std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102         theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
103     double aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID())->value();
104
105     std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(1.0, 0.0));
106     std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aDir, aRadius));
107
108     std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
109     thePoint->setValue(aProjected);
110   }
111   else if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
112     // project point on arc
113     std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114         theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
115     std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116         theFeature->attribute(SketchPlugin_Arc::START_ID()));
117
118     std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aStart->pnt()));
119     std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
120     thePoint->setValue(aProjected);
121   }
122 }