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