1 // File: SketchPlugin_ConstraintLength.cpp
2 // Created: 30 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintLength.h"
6 #include <SketchPlugin_Line.h>
8 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Result.h>
14 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Lin2d.h>
17 #include <GeomAPI_Pnt2d.h>
19 #include <Config_PropManager.h>
21 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
25 void SketchPlugin_ConstraintLength::initAttributes()
27 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
28 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
29 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
32 void SketchPlugin_ConstraintLength::execute()
34 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
35 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
36 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
39 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
40 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
41 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
42 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
44 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
46 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
47 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
48 //if(!aValueAttr->isInitialized()) {
49 // aValueAttr->setValue(aLenght);
54 bool SketchPlugin_ConstraintLength::getPoints(
55 std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
56 std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
57 std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
61 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
62 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
63 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
66 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
67 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
69 DataPtr aData = aFeature->data();
70 theStartPoint = std::dynamic_pointer_cast<
71 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
72 theEndPoint = std::dynamic_pointer_cast<
73 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
74 thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
75 thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
79 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
81 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
82 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
83 if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
84 return thePrevious; // not possible to show length because points are not defined
86 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
87 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
88 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
89 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = std::shared_ptr<GeomAPI_Pnt>();
90 if (aFlyOutAttr->isInitialized()) {
91 aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y());
93 std::shared_ptr<GeomAPI_Lin2d> aLine =
94 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
95 double aDist = aPoint1->distance(aPoint2)/5.;
96 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
97 aFlyOutAttr->setValue(aFPnt);
98 aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
101 // TODO: has to be calculated on definition of reference object
102 double aDistance = aPoint1->distance(aPoint2);
103 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
104 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
105 double aValue = aDistance;
106 if (aValueAttr->isInitialized())
107 aValue = aValueAttr->value();
109 aValueAttr->setValue(aValue);
112 AISObjectPtr anAIS = thePrevious;
114 anAIS = AISObjectPtr(new GeomAPI_AISObject);
115 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
117 // Set color from preferences
118 std::vector<int> aRGB =
119 Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR);
120 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
124 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
126 std::shared_ptr<ModelAPI_Data> aData = data();
127 if (!aData->isValid())
130 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
132 aPoint->move(theDeltaX, theDeltaY);
135 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
136 if (theID == SketchPlugin_Constraint::ENTITY_A())
138 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
139 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
140 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
141 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
142 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
143 if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
144 double aLength = aPoint1->distance(aPoint2);
145 aValueAttr->setValue(aLength);