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