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>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_Data.h>
32 #include <GeomAPI_Pnt2d.h>
33 #include <GeomAPI_Circ.h>
34 #include <GeomAPI_Circ2d.h>
35 #include <GeomAPI_Dir.h>
36 #include <GeomAPI_XY.h>
37 #include <GeomAPI_XYZ.h>
38 #include <GeomDataAPI_Point2D.h>
39 #include <GeomDataAPI_Dir.h>
41 #include <Config_PropManager.h>
43 const double tolerance = 1.e-7;
45 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
47 myFlyoutUpdate = false;
50 void SketchPlugin_ConstraintRadius::initAttributes()
52 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
53 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
54 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
57 void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName,
58 std::string& theDefault)
60 theSection = "Visualization";
61 theName = "sketch_dimension_color";
62 theDefault = SKETCH_DIMENSION_COLOR;
65 void SketchPlugin_ConstraintRadius::execute()
67 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
68 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
69 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
72 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
73 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
74 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
75 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
77 aRadius = anAttribute->value();
78 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
79 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
80 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
81 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
82 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
83 if (aCenterAttr && aStartAttr)
84 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
86 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
87 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
88 //if(!aValueAttr->isInitialized()) {
89 // aValueAttr->setValue(aRadius);
92 // the value should to be computed here,
93 // not in the getAISObject in order to change the model value
94 // inside the object transaction. This is important for creating a constraint by preselection.
95 // The display of the presentation in this case happens after the transaction commit
96 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
97 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
98 if (!aFlyoutAttr->isInitialized())
99 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
103 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
105 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
108 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
109 double aRadius = circleRadius(aCyrcFeature);
114 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
115 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
117 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
118 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
119 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
120 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
121 double aShift = aRadius * 1.1;
122 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
123 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
124 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
125 aFlyoutAttr->setValue(aFPnt);
127 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
128 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
129 aFlyoutAttr->setValue(aStartAttr->pnt());
134 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
136 static const double kErrorResult = -1.;
140 std::shared_ptr<ModelAPI_Data> aData = data();
141 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
142 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
145 theCirc = ModelAPI_Feature::feature(anAttr->object());
146 std::string aKind = theCirc ? theCirc->getKind() : "";
147 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
150 DataPtr aCircData = theCirc->data();
151 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
152 if (aKind == SketchPlugin_Circle::ID()) {
153 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
154 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
155 return aCircRadius->value();
157 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
158 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
159 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
160 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
161 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
166 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
171 AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
176 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
178 std::shared_ptr<ModelAPI_Data> aData = data();
179 if (!aData->isValid())
182 myFlyoutUpdate = true;
183 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
184 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
185 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
186 myFlyoutUpdate = false;
189 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
190 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
191 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
192 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
193 if (!aValueAttr->isInitialized()) {
194 // only if it is not initialized, try to compute the current value
195 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
196 double aRadius = circleRadius(aCyrcFeature);
197 if (aRadius > 0) { // set as value the radius of updated reference to another circle
198 aValueAttr->setValue(aRadius);
201 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
202 // Recalculate flyout point in local coordinates of the circle (or arc):
203 // coordinates are calculated according to center of the shape
204 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
205 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
208 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
209 attribute(SketchPlugin_Constraint::ENTITY_A()));
210 if (!aRefAttr || !aRefAttr->isObject())
212 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
213 if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
214 aFeature->getKind() != SketchPlugin_Circle::ID()))
217 std::string aCenterAttrName;
218 if (aFeature->getKind() == SketchPlugin_Circle::ID())
219 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
220 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
221 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
222 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
223 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
224 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
225 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
226 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
228 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
229 double aDist = aFlyoutPnt->distance(aCenter);
230 if (aDist < tolerance)
233 myFlyoutUpdate = true;
234 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
235 aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
236 myFlyoutUpdate = false;