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