1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintLength.cpp
4 // Created: 30 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintLength.h"
8 #include <SketchPlugin_Line.h>
10 #include <GeomDataAPI_Point2D.h>
12 #include <SketcherPrs_Factory.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Result.h>
18 #include <GeomDataAPI_Point2D.h>
20 #include <GeomAPI_Dir2d.h>
21 #include <GeomAPI_Lin2d.h>
22 #include <GeomAPI_Pnt2d.h>
23 #include <GeomAPI_XY.h>
25 #include <Config_PropManager.h>
29 const double tolerance = 1e-7;
31 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
33 myFlyoutUpdate = false;
36 void SketchPlugin_ConstraintLength::initAttributes()
38 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
39 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
40 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
43 void SketchPlugin_ConstraintLength::colorConfigInfo(std::string& theSection, std::string& theName,
44 std::string& theDefault)
46 theSection = "Visualization";
47 theName = "sketch_dimension_color";
48 theDefault = SKETCH_DIMENSION_COLOR;
51 void SketchPlugin_ConstraintLength::execute()
53 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
54 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
55 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
58 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
59 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
60 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
61 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
63 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
65 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
66 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
67 //if(!aValueAttr->isInitialized()) {
68 // aValueAttr->setValue(aLenght);
71 // the value should to be computed here, not in the getAISObject
72 // in order to change the model value
73 // inside the object transaction. This is important for creating a constraint by preselection.
74 // The display of the presentation in this case happens after the transaction commit
75 AttributePtr aFlyOutAttribute =
76 data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
77 if (!aFlyOutAttribute->isInitialized()) {
78 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
83 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
85 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
88 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
89 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
90 if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
93 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
94 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
96 std::shared_ptr<GeomAPI_Lin2d> aLine =
97 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
98 if (!aFlyOutAttr->isInitialized() ||
99 (fabs(aFlyOutAttr->x()) < tolerance && fabs(aFlyOutAttr->y()) < tolerance)) {
100 double aDist = aPoint1->distance(aPoint2)/5.;
101 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
102 aFlyOutAttr->setValue(aFPnt);
108 bool SketchPlugin_ConstraintLength::computeLenghtValue(double& theValue)
110 bool aResult = false;
111 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
112 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
113 if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
114 theValue = aPoint1->distance(aPoint2);
120 bool SketchPlugin_ConstraintLength::getPoints(
121 std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
122 std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
123 std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
127 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
128 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
129 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
132 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
133 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
135 DataPtr aData = aFeature->data();
136 theStartPoint = std::dynamic_pointer_cast<
137 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
138 theEndPoint = std::dynamic_pointer_cast<
139 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
140 thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
141 thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
145 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
150 AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
151 sketch()->coordinatePlane(), thePrevious);
155 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
157 std::shared_ptr<ModelAPI_Data> aData = data();
158 if (!aData->isValid())
161 AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
162 attribute(SketchPlugin_Constraint::ENTITY_A()));
163 if (!aLineAttr || !aLineAttr->isObject())
165 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
166 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
169 // Recalculate a shift of flyout point in terms of local coordinates
170 std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(theDeltaX, theDeltaY));
171 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
172 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
173 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
174 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
175 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
176 double dX = aDir->dot(aLineDir->xy());
177 double dY = -aDir->cross(aLineDir->xy());
179 myFlyoutUpdate = true;
180 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
182 aPoint->setValue(aPoint->x() + dX, aPoint->y() + dY);
183 myFlyoutUpdate = false;
186 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
187 if (theID == SketchPlugin_Constraint::ENTITY_A())
189 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
190 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
191 if (!aValueAttr->isInitialized()) {
192 // only if it is not initialized, try to compute the current value
194 if (computeLenghtValue(aLength))
195 aValueAttr->setValue(aLength);
197 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
198 myFlyoutUpdate = true;
199 // Recalculate flyout point in local coordinates of the line:
200 // the X coordinate is a length of projection of the flyout point on the line
201 // the Y coordinate is a distance from the point to the line
202 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
203 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
204 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
205 AttributeRefAttrPtr aLineAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
206 attribute(SketchPlugin_Constraint::ENTITY_A()));
207 if (!aLineAttr || !aLineAttr->isObject())
209 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
210 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
213 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
214 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
216 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
219 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
220 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aStartPnt);
221 double X = aFlyoutDir->dot(aLineDir->xy());
222 double Y = -aFlyoutDir->cross(aLineDir->xy());
223 aFlyoutAttr->setValue(X, Y);
224 myFlyoutUpdate = false;