]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
Salome HOME
Add SketchPlugin document template.
[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 #include <ModelAPI_Session.h>
39 #include <ModelAPI_Validator.h>
40
41 #include <Config_PropManager.h>
42
43 #include <math.h>
44
45 const double tolerance = 1e-7;
46
47
48 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
49 {
50   myFlyoutUpdate = false;
51 }
52
53 //*************************************************************************************
54 void SketchPlugin_ConstraintDistance::initAttributes()
55 {
56   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
57   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
58   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
60   data()->addAttribute(SIGNED(), ModelAPI_AttributeBoolean::typeId());
61
62   data()->addAttribute(SketchPlugin_ConstraintDistance::LOCATION_TYPE_ID(),
63                        ModelAPI_AttributeInteger::typeId());
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
65 }
66
67 void SketchPlugin_ConstraintDistance::colorConfigInfo(std::string& theSection, std::string& theName,
68                                                       std::string& theDefault)
69 {
70   theSection = "Visualization";
71   theName = "sketch_dimension_color";
72   theDefault = SKETCH_DIMENSION_COLOR;
73 }
74
75 //*************************************************************************************
76 void SketchPlugin_ConstraintDistance::execute()
77 {
78   std::shared_ptr<ModelAPI_Data> aData = data();
79   AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
80       aData->attribute(SketchPlugin_Constraint::VALUE()));
81
82   if(anAttrValue->isInitialized())
83     return;
84
85   double aDistance = calculateCurrentDistance();
86   if(aDistance >= 0)
87     anAttrValue->setValue(aDistance);
88 }
89
90 //*************************************************************************************
91 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
92 {
93   if (!sketch())
94     return thePrevious;
95
96   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
97                                                                       sketch()->coordinatePlane(),
98                                                                       thePrevious);
99   return anAIS;
100 }
101
102 double SketchPlugin_ConstraintDistance::calculateCurrentDistance()
103 {
104   double aDistance = -1.;
105
106   std::shared_ptr<ModelAPI_Data> aData = data();
107   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
108   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
109     SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
110   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
111       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
112
113   if (aPointA.get() && aPointB.get()) {  // both points
114     aDistance = aPointA->pnt()->distance(aPointB->pnt());
115   } else {
116     FeaturePtr aLineFeature;
117     std::shared_ptr<SketchPlugin_Line> aLine;
118     if (!aPointA.get() && aPointB.get()) {  //Line and point
119       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
120       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
121       if (aLine.get()) {
122         aDistance = aLine->distanceToPoint(aPointB->pnt());
123       }
124     } else if (aPointA.get() && !aPointB.get()) {  // Point and line
125       aLineFeature = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
126       aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aLineFeature);
127       if (aLine.get()) {
128         aDistance = aLine->distanceToPoint(aPointA->pnt());
129       }
130     }
131   }
132   return aDistance;
133 }
134
135 bool SketchPlugin_ConstraintDistance::areAttributesInitialized()
136 {
137   std::shared_ptr<ModelAPI_Data> aData = data();
138   std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
139   std::shared_ptr<GeomDataAPI_Point2D> aPointA =
140       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A(), aPlane);
141   std::shared_ptr<GeomDataAPI_Point2D> aPointB =
142       SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B(), aPlane);
143
144   if (!aPointA && !aPointB)
145     return false;
146   else if (aPointA || aPointB) {
147     FeaturePtr aLine;
148     if (!aPointA)
149       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
150     else if (!aPointB)
151       aLine = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
152     else // both points are initialized
153       return true;
154
155     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
156       return false;
157   }
158   return true;
159 }
160
161 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID)
162 {
163   if (theID == SketchPlugin_Constraint::ENTITY_A() ||
164       theID == SketchPlugin_Constraint::ENTITY_B())
165   {
166     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
167         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
168     if (!aValueAttr->isInitialized()) {
169       // only if it is not initialized, try to compute the current value
170       double aDistance = calculateCurrentDistance();
171       if (aDistance > 0) { // set as value the length of updated references
172         aValueAttr->setValue(aDistance);
173       }
174     }
175   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
176     // Recalculate flyout point in local coordinates of the distance constraint:
177     // the X coordinate is a length of projection of the flyout point on the
178     //                  line binding two distanced points
179     //                  or a line of projection of the distanced point onto the distanced segment
180     // the Y coordinate is a distance from the flyout point to the line
181     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
182         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
183         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
184     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
185
186     std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
187     std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
188         data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
189     std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
190         data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
191
192     std::shared_ptr<GeomAPI_XY> aStartPnt;
193     std::shared_ptr<GeomAPI_XY> aEndPnt;
194     if (aPointA && aPointB) {
195       aStartPnt = aPointA->pnt()->xy();
196       aEndPnt = aPointB->pnt()->xy();
197     } else if (aPointA) {
198       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
199           SketchPlugin_Constraint::ENTITY_B());
200       if (!aLine)
201         return;
202       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointA->pnt();
203       aStartPnt = aPoint->xy();
204       aEndPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
205     } else if (aPointB) {
206       FeaturePtr aLine = SketcherPrs_Tools::getFeatureLine(data(),
207           SketchPlugin_Constraint::ENTITY_A());
208       if (!aLine)
209         return;
210       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointB->pnt();
211       aStartPnt = SketcherPrs_Tools::getProjectionPoint(aLine, aPoint)->xy();
212       aEndPnt = aPoint->xy();
213     } else
214       return;
215
216     if (aEndPnt->distance(aStartPnt) < tolerance)
217       return;
218
219     myFlyoutUpdate = true;
220     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
221     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
222
223     double X = aFlyoutDir->dot(aLineDir->xy());
224     double Y = -aFlyoutDir->cross(aLineDir->xy());
225     aFlyoutAttr->setValue(X, Y);
226     myFlyoutUpdate = false;
227   }
228 }
229