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 <SketcherPrs_Tools.h>
13 #include <SketcherPrs_Factory.h>
15 #include <GeomAPI_Lin2d.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomDataAPI_Point2D.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Data.h>
22 #include <Config_PropManager.h>
24 const double tolerance = 1e-7;
27 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
31 //*************************************************************************************
32 void SketchPlugin_ConstraintDistance::initAttributes()
34 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
35 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
36 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
37 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
40 //*************************************************************************************
41 void SketchPlugin_ConstraintDistance::execute()
43 std::shared_ptr<ModelAPI_Data> aData = data();
44 AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
45 aData->attribute(SketchPlugin_Constraint::VALUE()));
47 if(anAttrValue->isInitialized())
50 double aDistance = calculateCurrentDistance();
52 anAttrValue->setValue(aDistance);
55 // the value should to be computed here, not in the getAISObject in order to change the model value
56 // inside the object transaction. This is important for creating a constraint by preselection.
57 // The display of the presentation in this case happens after the transaction commit
58 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
59 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
60 if(!aFlyOutAttr->isInitialized())
61 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
64 bool SketchPlugin_ConstraintDistance::compute(const std::string& theAttributeId)
66 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
72 DataPtr aData = data();
73 std::shared_ptr<GeomDataAPI_Point2D> aPoint_A = SketcherPrs_Tools::getFeaturePoint(
74 aData, SketchPlugin_Constraint::ENTITY_A());
75 std::shared_ptr<GeomDataAPI_Point2D> aPoint_B = SketcherPrs_Tools::getFeaturePoint(
76 aData, SketchPlugin_Constraint::ENTITY_B());
78 std::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
79 std::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
81 if (aPoint_A && aPoint_B) {
82 aPnt_A = aPoint_A->pnt();
83 aPnt_B = aPoint_B->pnt();
84 } else if (!aPoint_A && aPoint_B) {
85 //std::shared_ptr<SketchPlugin_Line> aLine = SketcherPrs_Tools::getFeatureLine(
86 // aData, SketchPlugin_Constraint::ENTITY_A());
88 // aPnt_B = aPoint_B->pnt();
89 // aPnt_A = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_B);
91 } else if (aPoint_A && !aPoint_B) {
92 //std::shared_ptr<SketchPlugin_Line> aLine = SketcherPrs_Tools::getFeatureLine(
93 // aData, SketchPlugin_Constraint::ENTITY_B());
95 // aPnt_A = aPoint_A->pnt();
96 // aPnt_B = SketcherPrs_Tools::getProjectionPoint(aLine, aPnt_A);
99 if (!aPnt_A || !aPnt_B)
102 std::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = std::dynamic_pointer_cast<
103 GeomDataAPI_Point2D>(aData->attribute(theAttributeId));
105 std::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
106 std::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
107 // it is not possible to create lin2d on the points with equal position
108 if (aPoint1->distance(aPoint1) < tolerance)
111 std::shared_ptr<GeomAPI_Lin2d> aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPnt_A, aPnt_B));
112 double aDist = aPoint1->distance(aPoint2)/5.;
113 std::shared_ptr<GeomAPI_Pnt2d> aFPnt = aLine->shiftedLocation(aDist);
114 aFlyOutAttr->setValue(aFPnt);
119 //*************************************************************************************
120 AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevious)
125 AISObjectPtr anAIS = thePrevious;
127 anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this, sketch()->coordinatePlane());
130 // Set color from preferences
131 std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
132 SKETCH_DIMENSION_COLOR);
133 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
137 //*************************************************************************************
138 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
140 std::shared_ptr<ModelAPI_Data> aData = data();
141 if (!aData->isValid())
144 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145 aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
146 aPoint->move(theDeltaX, theDeltaY);
149 double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const
151 double aDistance = -1.;
153 std::shared_ptr<ModelAPI_Data> aData = data();
154 std::shared_ptr<GeomDataAPI_Point2D> aPointA =
155 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
156 std::shared_ptr<GeomDataAPI_Point2D> aPointB =
157 SketcherPrs_Tools::getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
159 if (aPointA && aPointB) { // both points
160 aDistance = aPointA->pnt()->distance(aPointB->pnt());
162 // if (!aPointA && aPointB) { //Line and point
163 // std::shared_ptr<SketchPlugin_Line> aLine =
164 // SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
166 // aDistance = aLine->distanceToPoint(aPointB->pnt());
168 // } else if (aPointA && !aPointB) { // Point and line
169 // std::shared_ptr<SketchPlugin_Line> aLine =
170 // SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
172 // aDistance = aLine->distanceToPoint(aPointA->pnt());
179 void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) {
180 if (theID == SketchPlugin_Constraint::ENTITY_A() ||
181 theID == SketchPlugin_Constraint::ENTITY_B())
183 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
184 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
185 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
186 double aDistance = calculateCurrentDistance();
187 if (aDistance > 0) { // set as value the length of updated references
188 aValueAttr->setValue(aDistance);