Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[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 anAttrValue = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
37       aData->attribute(SketchPlugin_Constraint::VALUE()));
38
39   if(anAttrValue->isInitialized())
40     return;
41
42   double aDistance = calculateCurrentDistance();
43   if(aDistance >= 0) {
44     anAttrValue->setValue(aDistance);
45   }
46 }
47
48 //*************************************************************************************
49 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
50 {
51   if (!sketch())
52     return thePrevious;
53
54   boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
55
56   DataPtr aData = data();
57   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
58       aData, SketchPlugin_Constraint::ENTITY_A());
59   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
60       aData, SketchPlugin_Constraint::ENTITY_B());
61
62   boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
63   boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
64
65   if (aPoint_A && aPoint_B) {
66     aPnt_A = aPoint_A->pnt();
67     aPnt_B = aPoint_B->pnt();
68   } else if (!aPoint_A && aPoint_B) {
69     boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
70         aData, SketchPlugin_Constraint::ENTITY_A());
71     if (aLine) {
72       aPnt_B = aPoint_B->pnt();
73       aPnt_A = getProjectionPoint(aLine, aPnt_B);
74     }
75   } else if (aPoint_A && !aPoint_B) {
76     boost::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
77         aData, SketchPlugin_Constraint::ENTITY_B());
78     if (aLine) {
79       aPnt_A = aPoint_A->pnt();
80       aPnt_B = getProjectionPoint(aLine, aPnt_A);
81     }
82   }
83   if (!aPnt_A || !aPnt_B)
84     return AISObjectPtr();
85
86   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = boost::dynamic_pointer_cast<
87       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
88
89   boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
90   boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
91   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = boost::shared_ptr<GeomAPI_Pnt>();
92   if(aFlyOutAttr->isInitialized()) {
93     aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
94   } else {
95     boost::shared_ptr<GeomAPI_Lin2d> aLine = boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
96     double aDist = aPoint1->distance(aPoint2)/5.;
97     boost::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
98     aFlyOutAttr->setValue(aFPnt);
99     aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
100   }
101   // value calculation
102   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
103       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
104   double aValue = aValueAttr->value();
105   // Issue #196: checking the positivity of the distance constraint
106   // there is a validator for a distance constraint, that the value should be positive
107   // in case if an invalid value is set, the current distance value is shown
108   if (aValue <= 0)
109     aValue = calculateCurrentDistance();
110
111   AISObjectPtr anAIS = thePrevious;
112   if (!anAIS)
113     anAIS = AISObjectPtr(new GeomAPI_AISObject);
114   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
115
116   // Set color from preferences
117   std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color",
118                                                     DISTANCE_COLOR);
119   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
120   return anAIS;
121 }
122
123 //*************************************************************************************
124 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
125 {
126   boost::shared_ptr<ModelAPI_Data> aData = data();
127   if (!aData->isValid())
128     return;
129
130   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131       aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
132   aPoint->move(theDeltaX, theDeltaY);
133 }
134
135 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
136 {
137   double aDistance = -1.;
138
139   boost::shared_ptr<ModelAPI_Data> aData = data();
140   boost::shared_ptr<GeomDataAPI_Point2D> aPointA =
141     getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
142   boost::shared_ptr<GeomDataAPI_Point2D> aPointB =
143       getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
144
145   if (aPointA && aPointB) {  // both points
146     aDistance = aPointA->pnt()->distance(aPointB->pnt());
147   } else {
148     if (!aPointA && aPointB) {  //Line and point
149       boost::shared_ptr<SketchPlugin_Line> aLine =
150           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
151       if (aLine) {
152         aDistance = aLine->distanceToPoint(aPointB->pnt());
153       }
154     } else if (aPointA && !aPointB) {  // Point and line
155       boost::shared_ptr<SketchPlugin_Line> aLine =
156           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
157       if (aLine) {
158         aDistance = aLine->distanceToPoint(aPointA->pnt());
159       }
160     }
161   }
162   return aDistance;
163 }
164
165
166 //*************************************************************************************
167 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
168                                                        const std::string& theAttribute)
169 {
170   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
171
172   if (!theData)
173     return aPointAttr;
174
175   FeaturePtr aFeature;
176   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
177       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
178   if (anAttr)
179     aFeature = ModelAPI_Feature::feature(anAttr->object());
180
181   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
182     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
183         aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
184   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
185     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
186         aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
187   else if (anAttr->attr()) {
188     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
189   }
190   return aPointAttr;
191 }
192
193 //*************************************************************************************
194 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
195                                                     const std::string& theAttribute)
196 {
197   boost::shared_ptr<SketchPlugin_Line> aLine;
198   if (!theData)
199     return aLine;
200
201   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
202       ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
203   if (anAttr) {
204     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
205     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
206       aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
207     }
208   }
209   return aLine;
210 }
211
212 //*************************************************************************************
213 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
214     const boost::shared_ptr<SketchPlugin_Line>& theLine,
215     const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
216 {
217   boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
218   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219       aData->attribute(SketchPlugin_Line::START_ID()));
220   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
221       aData->attribute(SketchPlugin_Line::END_ID()));
222
223   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
224   return aLin2d.project(thePoint);
225 }