]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
de504f56ea4b1ca99cc88607c51d4dba6df46f09
[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
18 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
19 {
20 }
21
22 //*************************************************************************************
23 void SketchPlugin_ConstraintDistance::initAttributes()
24 {
25   data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::type());
26   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
27   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
28   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
29 }
30
31 //*************************************************************************************
32 void SketchPlugin_ConstraintDistance::execute()
33 {
34   boost::shared_ptr<ModelAPI_Data> aData = data();
35   AttributeDoublePtr anAttr_Value =
36     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 = 
41       getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
42     boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = 
43       getFeaturePoint(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 = 
50           getFeatureLine(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 = 
55           getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
56         if(aLine)
57           anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
58       }
59     }
60   }
61 }
62
63 //*************************************************************************************
64 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
65                     boost::shared_ptr<GeomAPI_AISObject> 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   boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
115   if (!anAIS)
116     anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
117   anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
118   return anAIS;
119 }
120
121 //*************************************************************************************
122 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
123 {
124   boost::shared_ptr<ModelAPI_Data> aData = data();
125   if (!aData->isValid())
126     return;
127
128   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
129         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
130   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
131 }
132
133 //*************************************************************************************
134 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
135                                                        const std::string& theAttribute)
136 {
137   boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
138
139   if (!theData)
140     return aPointAttr;
141
142   FeaturePtr aFeature;
143   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
144     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
145   if (anAttr)
146     aFeature = ModelAPI_Feature::feature(anAttr->object());
147
148   if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
149     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
150                                            (aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
151   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
152     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
153                                           (aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
154   else if (anAttr->attr()) {
155       aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
156   }
157   return aPointAttr;
158 }
159
160 //*************************************************************************************
161 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute)
162 {
163   boost::shared_ptr<SketchPlugin_Line> aLine;
164   if (!theData)
165     return aLine;
166
167   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
168     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
169   if (anAttr) {
170     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
171     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
172       aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
173     }
174   }
175   return aLine;
176 }
177
178 //*************************************************************************************
179 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine, 
180                                                           const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
181 {
182   boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
183   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
184         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185         aData->attribute(SketchPlugin_Line::START_ID()));
186   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
187         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188         aData->attribute(SketchPlugin_Line::END_ID()));
189
190   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
191   return aLin2d.project(thePoint);
192 }