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