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_Lin2d.h>
21 #include <GeomAPI_Pnt2d.h>
23 #include <Config_PropManager.h>
25 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
29 void SketchPlugin_ConstraintLength::initAttributes()
31 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
32 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
33 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36 void SketchPlugin_ConstraintLength::execute()
38 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
39 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
40 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
43 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<
44 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
45 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<
46 GeomDataAPI_Point2D>(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
48 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
50 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
51 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
52 //if(!aValueAttr->isInitialized()) {
53 // aValueAttr->setValue(aLenght);
56 // the value should to be computed here, not in the getAISObject in order to change the model value
57 // inside the object transaction. This is important for creating a constraint by preselection.
58 // The display of the presentation in this case happens after the transaction commit
59 AttributePtr aFlyOutAttribute = data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
60 if (!aFlyOutAttribute->isInitialized()) {
61 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
66 bool SketchPlugin_ConstraintLength::compute(const std::string& theAttributeId)
68 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
71 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
72 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
73 if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint))
76 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
77 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
79 std::shared_ptr<GeomAPI_Lin2d> aLine =
80 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStartPoint->pnt(), anEndPoint->pnt()));
81 double aDist = aPoint1->distance(aPoint2)/5.;
82 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
83 aFlyOutAttr->setValue(aFPnt);
88 bool SketchPlugin_ConstraintLength::getPoints(
89 std::shared_ptr<GeomAPI_Pnt>& thePoint1, std::shared_ptr<GeomAPI_Pnt>& thePoint2,
90 std::shared_ptr<GeomDataAPI_Point2D>& theStartPoint,
91 std::shared_ptr<GeomDataAPI_Point2D>& theEndPoint)
95 std::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
96 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
97 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
100 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
101 if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID())
103 DataPtr aData = aFeature->data();
104 theStartPoint = std::dynamic_pointer_cast<
105 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
106 theEndPoint = std::dynamic_pointer_cast<
107 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
108 thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y());
109 thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y());
113 AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious)
118 AISObjectPtr anAIS = thePrevious;
120 anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane());
123 // Set color from preferences
124 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
125 SKETCH_DIMENSION_COLOR);
126 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
130 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
132 std::shared_ptr<ModelAPI_Data> aData = data();
133 if (!aData->isValid())
136 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
137 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
138 aPoint->move(theDeltaX, theDeltaY);
141 void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) {
142 if (theID == SketchPlugin_Constraint::ENTITY_A())
144 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
145 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
146 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
147 std::shared_ptr<GeomAPI_Pnt> aPoint1, aPoint2;
148 std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, anEndPoint;
149 if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) {
150 double aLength = aPoint1->distance(aPoint2);
151 aValueAttr->setValue(aLength);