1 // Copyright (C) 2019-20xx 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 "SketchAPI_ConstraintAngle.h"
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
26 #include <GeomAPI_Angle2d.h>
27 #include <GeomAPI_Lin2d.h>
29 #include <SketchPlugin_ConstraintAngle.h>
30 #include <SketchPlugin_Line.h>
34 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
35 const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : SketchAPI_Constraint(theFeature)
38 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
43 static FeaturePtr getFeatureLine(DataPtr theData, const std::string& theAttribute)
46 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
49 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
50 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
52 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
53 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
60 static void calculatePossibleValuesOfAngle(FeaturePtr theFeature,
61 double& theAngleDirect, double& theAngleComplementary, double& theAngleBackward)
63 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
64 FeaturePtr aLineA = getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
65 FeaturePtr aLineB = getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
67 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
68 aLineA->attribute(SketchPlugin_Line::START_ID()));
69 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
70 aLineA->attribute(SketchPlugin_Line::END_ID()));
71 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
72 aLineB->attribute(SketchPlugin_Line::START_ID()));
73 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
74 aLineB->attribute(SketchPlugin_Line::END_ID()));
76 std::shared_ptr<GeomAPI_Angle2d> anAng(new GeomAPI_Angle2d(
77 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
78 theAngleDirect = anAng->angleDegree();
79 theAngleBackward = 360.0 - theAngleDirect;
81 if (theAngleDirect > 180.0)
82 theAngleComplementary = theAngleDirect - 180.0;
84 theAngleComplementary = 180.0 - theAngleDirect;
87 static std::string angleTypeToString(FeaturePtr theFeature)
89 static const double TOLERANCE = 1.e-7;
90 double anAngleDirect, anAngleComplmentary, anAngleBackward;
91 calculatePossibleValuesOfAngle(theFeature, anAngleDirect, anAngleComplmentary, anAngleBackward);
93 AttributeDoublePtr aValueAttr = theFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
96 if (fabs(aValueAttr->value() - anAngleDirect) < TOLERANCE) {
98 // This case is empty and going the first to check the direct angle before the others.
100 else if (fabs(aValueAttr->value() - anAngleComplmentary) < TOLERANCE)
101 aType = "Complementary";
102 else if (fabs(aValueAttr->value() - anAngleBackward) < TOLERANCE)
107 void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
109 // postpone constraint until all its attributes be dumped
110 if (!areAllAttributesDumped(theDumper))
113 // calculate angle value as it was just applied to the attributes
114 FeaturePtr aBase = feature();
115 std::string aSetterSuffix = angleTypeToString(aBase);
117 const std::string& aSketchName = theDumper.parentName(aBase);
118 theDumper << aBase << " = " << aSketchName << "." << "setAngle" << aSetterSuffix << "(";
120 bool isFirstAttr = true;
121 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
122 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
123 if (aRefAttr && aRefAttr->isInitialized()) {
124 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
129 AttributeDoublePtr aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
130 if (aValueAttr && aValueAttr->isInitialized())
131 theDumper << ", " << aValueAttr;
133 theDumper << ")" << std::endl;