1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintDistance.cpp
4 // Created: 23 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintDistance.h"
8 #include <SketchPlugin_Point.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_Line.h>
12 #include <GeomAPI_Lin2d.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Data.h>
19 #include <Config_PropManager.h>
21 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
25 //*************************************************************************************
26 void SketchPlugin_ConstraintDistance::initAttributes()
28 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
29 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
30 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
34 //*************************************************************************************
35 void SketchPlugin_ConstraintDistance::execute()
37 std::shared_ptr<ModelAPI_Data> aData = data();
38 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
39 aData->attribute(SketchPlugin_Constraint::VALUE()));
41 if(anAttrValue->isInitialized())
44 double aDistance = calculateCurrentDistance();
46 anAttrValue->setValue(aDistance);
50 //*************************************************************************************
51 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
56 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
58 DataPtr aData = data();
59 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(
60 aData, SketchPlugin_Constraint::ENTITY_A());
61 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(
62 aData, SketchPlugin_Constraint::ENTITY_B());
64 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
65 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
67 if (aPoint_A && aPoint_B) {
68 aPnt_A = aPoint_A->pnt();
69 aPnt_B = aPoint_B->pnt();
70 } else if (!aPoint_A && aPoint_B) {
71 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
72 aData, SketchPlugin_Constraint::ENTITY_A());
74 aPnt_B = aPoint_B->pnt();
75 aPnt_A = getProjectionPoint(aLine, aPnt_B);
77 } else if (aPoint_A && !aPoint_B) {
78 std::shared_ptr<SketchPlugin_Line> aLine = getFeatureLine(
79 aData, SketchPlugin_Constraint::ENTITY_B());
81 aPnt_A = aPoint_A->pnt();
82 aPnt_B = getProjectionPoint(aLine, aPnt_A);
85 if (!aPnt_A || !aPnt_B)
86 return AISObjectPtr();
88 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
89 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
91 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
92 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
93 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
94 if(aFlyOutAttr->isInitialized()) {
95 aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
97 std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
98 double aDist = aPoint1->distance(aPoint2)/5.;
99 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
100 aFlyOutAttr->setValue(aFPnt);
101 aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
104 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
105 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
107 // Issue #196: checking the positivity of the distance constraint
108 // there is a validator for a distance constraint, that the value should be positive
109 // in case if an invalid value is set, the current distance value is shown
110 if (aValueAttr->isInitialized())
111 aValue = aValueAttr->value();
113 AISObjectPtr anAIS = thePrevious;
115 anAIS = AISObjectPtr(new GeomAPI_AISObject);
116 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
118 // Set color from preferences
119 std::vector<int> aRGB = Config_PropManager::color("Visualization", "distance_color",
121 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
125 //*************************************************************************************
126 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
128 std::shared_ptr<ModelAPI_Data> aData = data();
129 if (!aData->isValid())
132 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
134 aPoint->move(theDeltaX, theDeltaY);
137 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
139 double aDistance = -1.;
141 std::shared_ptr<ModelAPI_Data> aData = data();
142 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
143 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
144 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
145 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
147 if (aPointA && aPointB) { // both points
148 aDistance = aPointA->pnt()->distance(aPointB->pnt());
150 if (!aPointA && aPointB) { //Line and point
151 std::shared_ptr<SketchPlugin_Line> aLine =
152 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
154 aDistance = aLine->distanceToPoint(aPointB->pnt());
156 } else if (aPointA && !aPointB) { // Point and line
157 std::shared_ptr<SketchPlugin_Line> aLine =
158 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
160 aDistance = aLine->distanceToPoint(aPointA->pnt());
167 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
168 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
169 theID == SketchPlugin_Constraint::ENTITY_B())
171 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
172 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
173 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
174 double aDistance = calculateCurrentDistance();
175 if (aDistance > 0) { // set as value the length of updated references
176 aValueAttr->setValue(aDistance);
182 //*************************************************************************************
183 std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
184 const std::string& theAttribute)
186 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
192 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
193 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
195 aFeature = ModelAPI_Feature::feature(anAttr->object());
197 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
198 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
199 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
200 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
201 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
203 else if (anAttr->attr()) {
204 aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
209 //*************************************************************************************
210 std::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData,
211 const std::string& theAttribute)
213 std::shared_ptr<SketchPlugin_Line> aLine;
217 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
218 ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
220 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
221 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
222 aLine = std::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
228 //*************************************************************************************
229 std::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(
230 const std::shared_ptr<SketchPlugin_Line>& theLine,
231 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
233 std::shared_ptr<ModelAPI_Data> aData = theLine->data();
234 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
235 aData->attribute(SketchPlugin_Line::START_ID()));
236 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
237 aData->attribute(SketchPlugin_Line::END_ID()));
239 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
240 return aLin2d.project(thePoint);