1 // Copyright (C) 2014-2023 CEA, EDF
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 email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_ConstraintRadius.h"
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Point.h>
25 #include <SketchPlugin_Tools.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());
79 // the value should to be computed here,
80 // not in the getAISObject in order to change the model value
81 // inside the object transaction. This is important for creating a constraint by preselection.
82 // The display of the presentation in this case happens after the transaction commit
83 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
84 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
85 if (!aFlyoutAttr->isInitialized())
86 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
90 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
92 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
95 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
96 double aRadius = circleRadius(aCyrcFeature);
101 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
102 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
104 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
105 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
106 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
107 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
108 double aShift = aRadius * 1.1;
109 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
110 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
111 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
112 aFlyoutAttr->setValue(aFPnt);
114 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
115 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
116 aFlyoutAttr->setValue(aStartAttr->pnt());
121 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
123 static const double kErrorResult = -1.;
127 std::shared_ptr<ModelAPI_Data> aData = data();
128 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
129 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
132 theCirc = ModelAPI_Feature::feature(anAttr->object());
133 std::string aKind = theCirc ? theCirc->getKind() : "";
134 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
137 DataPtr aCircData = theCirc->data();
138 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
139 if (aKind == SketchPlugin_Circle::ID()) {
140 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
141 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
142 return aCircRadius->value();
144 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
146 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
147 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
148 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
153 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
158 AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch(),
160 if (anAIS.get() && !thePrevious.get())
161 SketchPlugin_Tools::setDimensionColor(anAIS);
165 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
166 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
167 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
168 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
169 if (!aValueAttr->isInitialized()) {
170 // only if it is not initialized, try to compute the current value
171 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
172 double aRadius = circleRadius(aCyrcFeature);
173 if (aRadius > 0) { // set as value the radius of updated reference to another circle
174 aValueAttr->setValue(aRadius);
177 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
178 // Recalculate flyout point in local coordinates of the circle (or arc):
179 // coordinates are calculated according to center of the shape
180 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
181 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
184 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
185 attribute(SketchPlugin_Constraint::ENTITY_A()));
186 if (!aRefAttr || !aRefAttr->isObject())
188 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
189 if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
190 aFeature->getKind() != SketchPlugin_Circle::ID()))
193 std::string aCenterAttrName;
194 if (aFeature->getKind() == SketchPlugin_Circle::ID())
195 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
196 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
197 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
198 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
199 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
200 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
201 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
202 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
204 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
205 double aDist = aFlyoutPnt->distance(aCenter);
206 if (aDist < tolerance)
209 myFlyoutUpdate = true;
210 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
211 aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
212 myFlyoutUpdate = false;