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