1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_ConstraintLength.h"
22 #include <SketchPlugin_Line.h>
24 #include <GeomDataAPI_Point2D.h>
26 #include <SketcherPrs_Factory.h>
27 #include <SketcherPrs_Tools.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Result.h>
34 #include <GeomDataAPI_Point2D.h>
36 #include <GeomAPI_Dir2d.h>
37 #include <GeomAPI_Lin2d.h>
38 #include <GeomAPI_Pnt2d.h>
39 #include <GeomAPI_XY.h>
41 #include <Config_PropManager.h>
45 const double tolerance = 1e-7;
47 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
49 myFlyoutUpdate = false;
52 void SketchPlugin_ConstraintLength::initAttributes()
54 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
55 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
56 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
58 data()->addAttribute(SketchPlugin_ConstraintLength::LOCATION_TYPE_ID(),
59 ModelAPI_AttributeInteger::typeId());
60 data()->integer(LOCATION_TYPE_ID())->setValue(SketcherPrs_Tools::LOCATION_AUTOMATIC);
63 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
64 std::string& theDefault)
66 theSection = "Visualization";
67 theName = "sketch_dimension_color";
68 theDefault = SKETCH_DIMENSION_COLOR;
71 void SketchPlugin_ConstraintLength::execute()
73 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
74 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
75 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
78 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
79 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
80 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
81 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
83 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
85 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
86 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
87 //if(!aValueAttr->isInitialized()) {
88 // aValueAttr->setValue(aLenght);
91 // the value should to be computed here, not in the getAISObject
92 // in order to change the model value
93 // inside the object transaction. This is important for creating a constraint by preselection.
94 // The display of the presentation in this case happens after the transaction commit
95 AttributePtr aFlyOutAttribute =
96 data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
97 if (!aFlyOutAttribute->isInitialized()) {
98 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
103 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
105 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
108 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
109 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
110 if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
113 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
114 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
116 std::shared_ptr<GeomAPI_Lin2d> aLine =
117 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
118 if (!aFlyOutAttr->isInitialized() ||
119 (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
120 double aDist = aPoint1->distance(aPoint2)/5.;
121 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
122 aFlyOutAttr->setValue(aFPnt);
128 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
130 bool aResult = false;
131 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
132 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
133 if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
134 theValue = aPoint1->distance(aPoint2);
140 bool SketchPlugin_ConstraintLength::getPoints(
141 std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
142 std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
143 std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
147 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
148 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
151 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
152 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
154 DataPtr aData = aFeature->data();
155 theStartPoint = std::dynamic_pointer_cast<
156 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
157 theEndPoint = std::dynamic_pointer_cast<
158 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
159 thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
160 thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
164 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
169 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
170 sketch()->coordinatePlane(), thePrevious);
174 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
175 if (theID == SketchPlugin_Constraint::ENTITY_A())
177 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
178 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
179 if (!aValueAttr->isInitialized()) {
180 // only if it is not initialized, try to compute the current value
182 if (computeLenghtValue(aLength))
183 aValueAttr->setValue(aLength);
185 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
186 // Recalculate flyout point in local coordinates of the line:
187 // the X coordinate is a length of projection of the flyout point on the line
188 // the Y coordinate is a distance from the point to the line
189 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
190 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
191 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
192 AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
193 attribute(SketchPlugin_Constraint::ENTITY_A()));
194 if (!aLineAttr || !aLineAttr->isObject())
196 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
197 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
200 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
201 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
203 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
206 myFlyoutUpdate = true;
207 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
208 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
209 double X = aFlyoutDir->dot(aLineDir->xy());
210 double Y = -aFlyoutDir->cross(aLineDir->xy());
211 aFlyoutAttr->setValue(X, Y);
212 myFlyoutUpdate = false;