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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "SketchPlugin_ConstraintLength.h"
21 #include <SketchPlugin_Line.h>
23 #include <GeomDataAPI_Point2D.h>
25 #include <SketcherPrs_Factory.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Result.h>
31 #include <GeomDataAPI_Point2D.h>
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
38 #include <Config_PropManager.h>
42 const double tolerance = 1e-7;
44 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
46 myFlyoutUpdate = false;
49 void SketchPlugin_ConstraintLength::initAttributes()
51 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
52 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
53 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
56 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
57 std::string& theDefault)
59 theSection = "Visualization";
60 theName = "sketch_dimension_color";
61 theDefault = SKETCH_DIMENSION_COLOR;
64 void SketchPlugin_ConstraintLength::execute()
66 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
67 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
68 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
71 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
72 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
73 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
74 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
76 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
78 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
79 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
80 //if(!aValueAttr->isInitialized()) {
81 // aValueAttr->setValue(aLenght);
84 // the value should to be computed here, not in the getAISObject
85 // in order to change the model value
86 // inside the object transaction. This is important for creating a constraint by preselection.
87 // The display of the presentation in this case happens after the transaction commit
88 AttributePtr aFlyOutAttribute =
89 data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
90 if (!aFlyOutAttribute->isInitialized()) {
91 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
96 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
98 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
101 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
102 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
103 if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
106 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
107 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
109 std::shared_ptr<GeomAPI_Lin2d> aLine =
110 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
111 if (!aFlyOutAttr->isInitialized() ||
112 (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
113 double aDist = aPoint1->distance(aPoint2)/5.;
114 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
115 aFlyOutAttr->setValue(aFPnt);
121 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
123 bool aResult = false;
124 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
125 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
126 if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
127 theValue = aPoint1->distance(aPoint2);
133 bool SketchPlugin_ConstraintLength::getPoints(
134 std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
135 std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
136 std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
140 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
141 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
142 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
145 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
146 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
148 DataPtr aData = aFeature->data();
149 theStartPoint = std::dynamic_pointer_cast<
150 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
151 theEndPoint = std::dynamic_pointer_cast<
152 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
153 thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
154 thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
158 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
163 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
164 sketch()->coordinatePlane(), thePrevious);
168 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
170 std::shared_ptr<ModelAPI_Data> aData = data();
171 if (!aData->isValid())
174 AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
175 attribute(SketchPlugin_Constraint::ENTITY_A()));
176 if (!aLineAttr || !aLineAttr->isObject())
178 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
179 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
182 // Recalculate a shift of flyout point in terms of local coordinates
183 std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
184 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
186 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
187 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
188 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
189 double dX = aDir->dot(aLineDir->xy());
190 double dY = -aDir->cross(aLineDir->xy());
192 myFlyoutUpdate = true;
193 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
194 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
195 aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
196 myFlyoutUpdate = false;
199 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
200 if (theID == SketchPlugin_Constraint::ENTITY_A())
202 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
203 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
204 if (!aValueAttr->isInitialized()) {
205 // only if it is not initialized, try to compute the current value
207 if (computeLenghtValue(aLength))
208 aValueAttr->setValue(aLength);
210 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
211 myFlyoutUpdate = true;
212 // Recalculate flyout point in local coordinates of the line:
213 // the X coordinate is a length of projection of the flyout point on the line
214 // the Y coordinate is a distance from the point to the line
215 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
216 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
218 AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
219 attribute(SketchPlugin_Constraint::ENTITY_A()));
220 if (!aLineAttr || !aLineAttr->isObject())
222 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
223 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
226 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
227 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
229 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
230 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
232 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
233 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
234 double X = aFlyoutDir->dot(aLineDir->xy());
235 double Y = -aFlyoutDir->cross(aLineDir->xy());
236 aFlyoutAttr->setValue(X, Y);
237 myFlyoutUpdate = false;