1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_ConstraintRadius.h"
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_Circle.h>
25 #include <SketchPlugin_Point.h>
27 #include <SketcherPrs_Factory.h>
28 #include <SketcherPrs_Tools.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_AttributeInteger.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
36 #include <GeomAPI_Pnt2d.h>
37 #include <GeomAPI_Circ.h>
38 #include <GeomAPI_Circ2d.h>
39 #include <GeomAPI_Dir.h>
40 #include <GeomAPI_XY.h>
41 #include <GeomAPI_XYZ.h>
42 #include <GeomDataAPI_Point2D.h>
43 #include <GeomDataAPI_Dir.h>
45 #include <Config_PropManager.h>
47 const double tolerance = 1.e-7;
49 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
51 myFlyoutUpdate = false;
54 void SketchPlugin_ConstraintRadius::initAttributes()
56 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
57 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
58 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
60 data()->addAttribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID(),
61 ModelAPI_AttributeInteger::typeId());
62 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
65 void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName,
66 std::string& theDefault)
68 theSection = "Visualization";
69 theName = "sketch_dimension_color";
70 theDefault = SKETCH_DIMENSION_COLOR;
73 void SketchPlugin_ConstraintRadius::execute()
75 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
76 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
77 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
80 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
81 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
82 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
83 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
85 aRadius = anAttribute->value();
86 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
87 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
88 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
89 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
90 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
91 if (aCenterAttr && aStartAttr)
92 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
94 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
95 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
96 //if(!aValueAttr->isInitialized()) {
97 // aValueAttr->setValue(aRadius);
100 // the value should to be computed here,
101 // not in the getAISObject in order to change the model value
102 // inside the object transaction. This is important for creating a constraint by preselection.
103 // The display of the presentation in this case happens after the transaction commit
104 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
105 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
106 if (!aFlyoutAttr->isInitialized())
107 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
111 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
113 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
116 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
117 double aRadius = circleRadius(aCyrcFeature);
122 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
123 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
125 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
126 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
127 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
129 double aShift = aRadius * 1.1;
130 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
131 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
132 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
133 aFlyoutAttr->setValue(aFPnt);
135 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
136 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
137 aFlyoutAttr->setValue(aStartAttr->pnt());
142 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
144 static const double kErrorResult = -1.;
148 std::shared_ptr<ModelAPI_Data> aData = data();
149 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
150 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
153 theCirc = ModelAPI_Feature::feature(anAttr->object());
154 std::string aKind = theCirc ? theCirc->getKind() : "";
155 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
158 DataPtr aCircData = theCirc->data();
159 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
160 if (aKind == SketchPlugin_Circle::ID()) {
161 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
162 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
163 return aCircRadius->value();
165 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
166 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
167 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
168 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
169 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
174 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
179 AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
184 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
185 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
186 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
187 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
188 if (!aValueAttr->isInitialized()) {
189 // only if it is not initialized, try to compute the current value
190 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
191 double aRadius = circleRadius(aCyrcFeature);
192 if (aRadius > 0) { // set as value the radius of updated reference to another circle
193 aValueAttr->setValue(aRadius);
196 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
197 // Recalculate flyout point in local coordinates of the circle (or arc):
198 // coordinates are calculated according to center of the shape
199 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
200 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
203 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
204 attribute(SketchPlugin_Constraint::ENTITY_A()));
205 if (!aRefAttr || !aRefAttr->isObject())
207 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
208 if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
209 aFeature->getKind() != SketchPlugin_Circle::ID()))
212 std::string aCenterAttrName;
213 if (aFeature->getKind() == SketchPlugin_Circle::ID())
214 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
215 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
216 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
217 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
218 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
219 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
220 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
221 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
223 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
224 double aDist = aFlyoutPnt->distance(aCenter);
225 if (aDist < tolerance)
228 myFlyoutUpdate = true;
229 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
230 aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
231 myFlyoutUpdate = false;