Salome HOME
Update coordinates of a point coincided with line, arc or circle (issue #883)
[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 = thePrevious;
49   if (!anAIS) {
50     anAIS = SketcherPrs_Factory::coincidentConstraint(this, sketch()->coordinatePlane());
51   }
52   return anAIS;
53 }
54
55 void SketchPlugin_ConstraintCoincidence::attributeChanged(const std::string& theID)
56 {
57   if (theID == ENTITY_A() || theID == ENTITY_B()) {
58     AttributeRefAttrPtr anAttrA = 
59         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_A()));
60     AttributeRefAttrPtr anAttrB = 
61         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(ENTITY_B()));
62
63     if (!anAttrA || !anAttrB || (!anAttrA->isObject() && !anAttrB->isObject()))
64       return;
65
66     if (anAttrA->isObject()) {
67       AttributeRefAttrPtr aTransient = anAttrA;
68       anAttrA = anAttrB;
69       anAttrB = aTransient;
70     }
71
72     std::shared_ptr<GeomDataAPI_Point2D> aPoint =
73         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrA->attr());
74     FeaturePtr aFeature;
75     if (anAttrB->object())
76       aFeature = ModelAPI_Feature::feature(anAttrB->object());
77
78     // Adjust position of a point lying on a line or arc
79     if (aPoint && aFeature)
80       adjustPointOnEntity(aPoint, aFeature);
81   }
82 }
83
84
85
86
87 // =======   Auxiliary functions   ================================================
88 void adjustPointOnEntity(std::shared_ptr<GeomDataAPI_Point2D> thePoint, FeaturePtr theFeature)
89 {
90   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
91     // project point on line
92     std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93         theFeature->attribute(SketchPlugin_Line::START_ID()));
94     std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
95         theFeature->attribute(SketchPlugin_Line::END_ID()));
96
97     std::shared_ptr<GeomAPI_Lin2d> aLine(new GeomAPI_Lin2d(aStart->pnt(), aEnd->pnt()));
98     std::shared_ptr<GeomAPI_Pnt2d> aProjected = aLine->project(thePoint->pnt());
99     thePoint->setValue(aProjected);
100   }
101   else if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
102     // project point on circle
103     std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104         theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
105     double aRadius = theFeature->real(SketchPlugin_Circle::RADIUS_ID())->value();
106
107     std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(1.0, 0.0));
108     std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aDir, aRadius));
109
110     std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
111     thePoint->setValue(aProjected);
112   }
113   else if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
114     // project point on arc
115     std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
116         theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
117     std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118         theFeature->attribute(SketchPlugin_Arc::START_ID()));
119
120     std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter->pnt(), aStart->pnt()));
121     std::shared_ptr<GeomAPI_Pnt2d> aProjected = aCircle->project(thePoint->pnt());
122     thePoint->setValue(aProjected);
123   }
124 }