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