]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
Merge branch 'Dev_2.8.0'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistance.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_ConstraintDistance.h"
22 #include <SketchPlugin_Point.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Line.h>
25
26 #include <SketcherPrs_Tools.h>
27 #include <SketcherPrs_Factory.h>
28
29 #include <GeomAPI_Dir2d.h>
30 #include <GeomAPI_Lin2d.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_XY.h>
33 #include <GeomDataAPI_Point2D.h>
34
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_Data.h>
38
39 #include <Config_PropManager.h>
40
41 #include <math.h>
42
43 const double tolerance = 1e-7;
44
45
46 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
47 {
48   myFlyoutUpdate = false;
49 }
50
51 //*************************************************************************************
52 void SketchPlugin_ConstraintDistance::initAttributes()
53 {
54   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
55   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
56   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
57   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
58   data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
59
60   data()->addAttribute(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID(),
61                        ModelAPI_AttributeInteger::typeId());
62   data()->integer(LOCATION_TYPE_ID())->setValue(SketcherPrs_Tools::LOCATION_AUTOMATIC);
63 }
64
65 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
66                                                       std::string& theDefault)
67 {
68   theSection = "Visualization";
69   theName = "sketch_dimension_color";
70   theDefault = SKETCH_DIMENSION_COLOR;
71 }
72
73 //*************************************************************************************
74 void SketchPlugin_ConstraintDistance::execute()
75 {
76   std::shared_ptr<ModelAPI_Data> aData = data();
77   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
78       aData->attribute(SketchPlugin_Constraint::VALUE()));
79
80   if(anAttrValue->isInitialized())
81     return;
82
83   double aDistance = calculateCurrentDistance();
84   if(aDistance >= 0)
85     anAttrValue->setValue(aDistance);
86 }
87
88 //*************************************************************************************
89 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
90 {
91   if (!sketch())
92     return thePrevious;
93
94   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
95                                                                       sketch()->coordinatePlane(),
96                                                                       thePrevious);
97   return anAIS;
98 }
99
100 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
101 {
102   double aDistance = -1.;
103
104   std::shared_ptr<ModelAPI_Data> aData = data();
105   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
106   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
107     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
108   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
109       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
110
111   if (aPointA.get() && aPointB.get()) {  // both points
112     aDistance = aPointA->pnt()->distance(aPointB->pnt());
113   } else {
114     FeaturePtr aLineFeature;
115     std::shared_ptr<SketchPlugin_Line> aLine;
116     if (!aPointA.get() && aPointB.get()) {  //Line and point
117       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
118       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
119       if (aLine.get()) {
120         aDistance = aLine->distanceToPoint(aPointB->pnt());
121       }
122     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
123       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
124       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
125       if (aLine.get()) {
126         aDistance = aLine->distanceToPoint(aPointA->pnt());
127       }
128     }
129   }
130   return aDistance;
131 }
132
133 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
134 {
135   std::shared_ptr<ModelAPI_Data> aData = data();
136   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
137   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
138       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
139   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
140       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
141
142   if (!aPointA && !aPointB)
143     return false;
144   else if (aPointA || aPointB) {
145     FeaturePtr aLine;
146     if (!aPointA)
147       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
148     else if (!aPointB)
149       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
150     else // both points are initialized
151       return true;
152
153     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
154       return false;
155   }
156   return true;
157 }
158
159 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
160 {
161   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
162       theID == SketchPlugin_Constraint::ENTITY_B())
163   {
164     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
165         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
166     if (!aValueAttr->isInitialized()) {
167       // only if it is not initialized, try to compute the current value
168       double aDistance = calculateCurrentDistance();
169       if (aDistance > 0) { // set as value the length of updated references
170         aValueAttr->setValue(aDistance);
171       }
172     }
173   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
174     // Recalculate flyout point in local coordinates of the distance constraint:
175     // the X coordinate is a length of projection of the flyout point on the
176     //                  line binding two distanced points
177     //                  or a line of projection of the distanced point onto the distanced segment
178     // the Y coordinate is a distance from the flyout point to the line
179     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
180         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
182     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
183
184     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
185     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
186         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
187     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
188         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
189
190     std::shared_ptr<GeomAPI_XY> aStartPnt;
191     std::shared_ptr<GeomAPI_XY> aEndPnt;
192     if (aPointA && aPointB) {
193       aStartPnt = aPointA->pnt()->xy();
194       aEndPnt = aPointB->pnt()->xy();
195     } else if (aPointA) {
196       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
197           SketchPlugin_Constraint::ENTITY_B());
198       if (!aLine)
199         return;
200       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
201       aStartPnt = aPoint->xy();
202       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
203     } else if (aPointB) {
204       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
205           SketchPlugin_Constraint::ENTITY_A());
206       if (!aLine)
207         return;
208       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
209       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
210       aEndPnt = aPoint->xy();
211     } else
212       return;
213
214     if (aEndPnt->distance(aStartPnt) < tolerance)
215       return;
216
217     myFlyoutUpdate = true;
218     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
219     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
220
221     double X = aFlyoutDir->dot(aLineDir->xy());
222     double Y = -aFlyoutDir->cross(aLineDir->xy());
223     aFlyoutAttr->setValue(X, Y);
224     myFlyoutUpdate = false;
225   }
226 }
227