1 // Copyright (C) 2014-2019 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 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>
26 #include <SketcherPrs_Factory.h>
27 #include <SketcherPrs_Tools.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_Circ.h>
37 #include <GeomAPI_Circ2d.h>
38 #include <GeomAPI_Dir.h>
39 #include <GeomAPI_XY.h>
40 #include <GeomAPI_XYZ.h>
41 #include <GeomDataAPI_Point2D.h>
42 #include <GeomDataAPI_Dir.h>
44 #include <Config_PropManager.h>
46 const double tolerance = 1.e-7;
48 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
50 myFlyoutUpdate = false;
53 void SketchPlugin_ConstraintRadius::initAttributes()
55 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
57 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
59 data()->addAttribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID(),
60 ModelAPI_AttributeInteger::typeId());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
64 void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName,
65 std::string& theDefault)
67 theSection = "Visualization";
68 theName = "sketch_dimension_color";
69 theDefault = SKETCH_DIMENSION_COLOR;
72 void SketchPlugin_ConstraintRadius::execute()
74 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
75 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
76 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
79 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
80 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
81 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
82 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
84 aRadius = anAttribute->value();
85 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
86 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
87 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
88 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
89 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
90 if (aCenterAttr && aStartAttr)
91 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
93 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
94 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
95 //if(!aValueAttr->isInitialized()) {
96 // aValueAttr->setValue(aRadius);
99 // the value should to be computed here,
100 // not in the getAISObject in order to change the model value
101 // inside the object transaction. This is important for creating a constraint by preselection.
102 // The display of the presentation in this case happens after the transaction commit
103 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
104 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
105 if (!aFlyoutAttr->isInitialized())
106 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
110 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
112 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
115 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
116 double aRadius = circleRadius(aCyrcFeature);
121 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
122 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
124 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
125 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
127 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
128 double aShift = aRadius * 1.1;
129 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
130 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
131 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
132 aFlyoutAttr->setValue(aFPnt);
134 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
135 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
136 aFlyoutAttr->setValue(aStartAttr->pnt());
141 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
143 static const double kErrorResult = -1.;
147 std::shared_ptr<ModelAPI_Data> aData = data();
148 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
149 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
152 theCirc = ModelAPI_Feature::feature(anAttr->object());
153 std::string aKind = theCirc ? theCirc->getKind() : "";
154 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
157 DataPtr aCircData = theCirc->data();
158 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
159 if (aKind == SketchPlugin_Circle::ID()) {
160 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
161 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
162 return aCircRadius->value();
164 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
166 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
167 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
168 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
173 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
178 AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
183 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
184 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
185 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
186 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
187 if (!aValueAttr->isInitialized()) {
188 // only if it is not initialized, try to compute the current value
189 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
190 double aRadius = circleRadius(aCyrcFeature);
191 if (aRadius > 0) { // set as value the radius of updated reference to another circle
192 aValueAttr->setValue(aRadius);
195 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
196 // Recalculate flyout point in local coordinates of the circle (or arc):
197 // coordinates are calculated according to center of the shape
198 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
199 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
202 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
203 attribute(SketchPlugin_Constraint::ENTITY_A()));
204 if (!aRefAttr || !aRefAttr->isObject())
206 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
207 if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
208 aFeature->getKind() != SketchPlugin_Circle::ID()))
211 std::string aCenterAttrName;
212 if (aFeature->getKind() == SketchPlugin_Circle::ID())
213 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
214 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
215 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
216 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
217 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
218 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
219 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
220 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
222 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
223 double aDist = aFlyoutPnt->distance(aCenter);
224 if (aDist < tolerance)
227 myFlyoutUpdate = true;
228 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
229 aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
230 myFlyoutUpdate = false;