Salome HOME
ab8bba03646e1e80c09640c3a7bbd73c8d6b4390
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.cpp
1 // File:    SketchPlugin_ConstraintDistance.cpp
2 // Created: 23 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintDistance.h"
6 #include <SketchPlugin_Point.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
9
10 #include <GeomAPI_Lin2d.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomDataAPI_Point2D.h>
13
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Data.h>
16
17 #include <Config_PropManager.h>
18
19 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
20 {
21 }
22
23 //*************************************************************************************
24 void SketchPlugin_ConstraintDistance::initAttributes()
25 {
26   data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::type());
27   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
28   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
29   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
30 }
31
32 //*************************************************************************************
33 void SketchPlugin_ConstraintDistance::execute()
34 {
35   boost::shared_ptr<ModelAPI_Data> aData = data();
36   AttributeDoublePtr anAttr_Value =
37     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
38       aData->attribute(SketchPlugin_Constraint::VALUE()));
39
40   if (!anAttr_Value->isInitialized()) {
41     boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = 
42       getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
43     boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = 
44       getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
45   
46     if (aPoint_A && aPoint_B) { // both points
47         anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
48     } else {
49       if (!aPoint_A && aPoint_B) { //Line and point
50         boost::shared_ptr<SketchPlugin_Line> aLine = 
51           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
52         if (aLine)
53           anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
54       } else if (aPoint_A && !aPoint_B) { // Point and line
55         boost::shared_ptr<SketchPlugin_Line> aLine = 
56           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
57         if(aLine)
58           anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
59       }
60     }
61   }
62 }
63
64 //*************************************************************************************
65 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
66 {
67   if (!sketch())
68     return thePrevious;
69
70   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
71
72   DataPtr aData = data();
73   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
74   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
75
76   boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
77   boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
78
79   if (aPoint_A && aPoint_B) {
80     aPnt_A = aPoint_A->pnt();
81     aPnt_B = aPoint_B->pnt();
82   } else if (!aPoint_A && aPoint_B) {
83     boost::shared_ptr<SketchPlugin_Line> aLine = 
84       getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
85     if (aLine) {
86       aPnt_B = aPoint_B->pnt();
87       aPnt_A = getProjectionPoint(aLine, aPnt_B);
88     }
89   } else if (aPoint_A && !aPoint_B) {
90     boost::shared_ptr<SketchPlugin_Line> aLine = 
91       getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
92     if (aLine) {
93       aPnt_A = aPoint_A->pnt();
94       aPnt_B = getProjectionPoint(aLine, aPnt_A);
95     }
96   }
97   if (!aPnt_A || !aPnt_B)
98     return thePrevious;
99
100   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
101     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
102
103   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
104   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
105   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ? 
106                                               sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : 
107                                               boost::shared_ptr<GeomAPI_Pnt>();
108
109   // value calculation
110   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
111     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
112   double aValue = aValueAttr->value();
113
114   AISObjectPtr anAIS = thePrevious;
115   if (!anAIS)
116     anAIS = AISObjectPtr(new GeomAPI_AISObject);
117   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
118
119   // Set color from preferences
120   std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color", DISTANCE_COLOR);
121   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
122   return anAIS;
123 }
124
125 //*************************************************************************************
126 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
127 {
128   boost::shared_ptr<ModelAPI_Data> aData = data();
129   if (!aData->isValid())
130     return;
131
132   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
133         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
134   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
135 }
136
137 //*************************************************************************************
138 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
139                                                        const std::string& theAttribute)
140 {
141   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
142
143   if (!theData)
144     return aPointAttr;
145
146   FeaturePtr aFeature;
147   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
148     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
149   if (anAttr)
150     aFeature = ModelAPI_Feature::feature(anAttr->object());
151
152   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
153     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
154                                            (aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
155   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
156     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
157                                           (aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
158   else if (anAttr->attr()) {
159       aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
160   }
161   return aPointAttr;
162 }
163
164 //*************************************************************************************
165 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute)
166 {
167   boost::shared_ptr<SketchPlugin_Line> aLine;
168   if (!theData)
169     return aLine;
170
171   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
172     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
173   if (anAttr) {
174     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
175     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
176       aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
177     }
178   }
179   return aLine;
180 }
181
182 //*************************************************************************************
183 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine, 
184                                                           const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
185 {
186   boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
187   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
188         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189         aData->attribute(SketchPlugin_Line::START_ID()));
190   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
191         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192         aData->attribute(SketchPlugin_Line::END_ID()));
193
194   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
195   return aLin2d.project(thePoint);
196 }