Salome HOME
Issue #2206 Avoid the ability to cancel the current sketch when saving,
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintLength.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_ConstraintLength.h"
22 #include <SketchPlugin_Line.h>
23
24 #include <GeomDataAPI_Point2D.h>
25
26 #include <SketcherPrs_Factory.h>
27
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Result.h>
32
33 #include <GeomDataAPI_Point2D.h>
34
35 #include <GeomAPI_Dir2d.h>
36 #include <GeomAPI_Lin2d.h>
37 #include <GeomAPI_Pnt2d.h>
38 #include <GeomAPI_XY.h>
39
40 #include <Config_PropManager.h>
41
42 #include <math.h>
43
44 const double tolerance = 1e-7;
45
46 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
47 {
48   myFlyoutUpdate = false;
49 }
50
51 void SketchPlugin_ConstraintLength::initAttributes()
52 {
53   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
54   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
55   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
56
57   data()->addAttribute(SketchPlugin_ConstraintLength::LOCATION_TYPE_ID(),
58                        ModelAPI_AttributeInteger::typeId());
59 }
60
61 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
62                                                     std::string& theDefault)
63 {
64   theSection = "Visualization";
65   theName = "sketch_dimension_color";
66   theDefault = SKETCH_DIMENSION_COLOR;
67 }
68
69 void SketchPlugin_ConstraintLength::execute()
70 {
71   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
72       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
73   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
74   if (aFeature) {
75     // set length value
76     std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
77         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
78     std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
79         GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
80
81     double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
82
83     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
84     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
85     //if(!aValueAttr->isInitialized()) {
86     //  aValueAttr->setValue(aLenght);
87     //}
88
89     // the value should to be computed here, not in the getAISObject
90     // in order to change the model value
91     // inside the object transaction. This is important for creating a constraint by preselection.
92     // The display of the presentation in this case happens after the transaction commit
93     AttributePtr aFlyOutAttribute =
94       data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
95     if (!aFlyOutAttribute->isInitialized()) {
96       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
97     }
98   }
99 }
100
101 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
102 {
103   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
104     return false;
105
106   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
107   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
108   if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
109     return false;
110
111   std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
112       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
113
114   std::shared_ptr<GeomAPI_Lin2d> aLine =
115     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
116   if (!aFlyOutAttr->isInitialized() ||
117       (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
118     double aDist = aPoint1->distance(aPoint2)/5.;
119     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
120     aFlyOutAttr->setValue(aFPnt);
121   }
122
123   return true;
124 }
125
126 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
127 {
128   bool aResult = false;
129   std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
130   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
131   if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
132     theValue = aPoint1->distance(aPoint2);
133     aResult = true;
134   }
135   return aResult;
136 }
137
138 bool SketchPlugin_ConstraintLength::getPoints(
139   std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
140   std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
141   std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
142 {
143   if (!sketch())
144     return false;
145   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
146       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
147   if (!anAttr)
148     return false;
149   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
150   if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
151     return false;
152   DataPtr aData = aFeature->data();
153   theStartPoint = std::dynamic_pointer_cast<
154       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
155   theEndPoint = std::dynamic_pointer_cast<
156       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
157   thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
158   thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
159   return true;
160 }
161
162 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
163 {
164   if (!sketch())
165     return thePrevious;
166
167   AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
168     sketch()->coordinatePlane(), thePrevious);
169   return anAIS;
170 }
171
172 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
173   if (theID == SketchPlugin_Constraint::ENTITY_A())
174   {
175     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
176       ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
177     if (!aValueAttr->isInitialized()) {
178       // only if it is not initialized, try to compute the current value
179       double aLength;
180       if (computeLenghtValue(aLength))
181         aValueAttr->setValue(aLength);
182     }
183   } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
184     // Recalculate flyout point in local coordinates of the line:
185     // the X coordinate is a length of projection of the flyout point on the line
186     // the Y coordinate is a distance from the point to the line
187     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
188         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189         attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
190     AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
191         attribute(SketchPlugin_Constraint::ENTITY_A()));
192     if (!aLineAttr || !aLineAttr->isObject())
193       return;
194     FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
195     if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
196       return;
197
198     std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
199     std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200         aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
201     std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202         aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
203
204     myFlyoutUpdate = true;
205     std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
206     std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
207     double X = aFlyoutDir->dot(aLineDir->xy());
208     double Y = -aFlyoutDir->cross(aLineDir->xy());
209     aFlyoutAttr->setValue(X, Y);
210     myFlyoutUpdate = false;
211   }
212 }