]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_ConstraintAngle.cpp
Salome HOME
Fix incorrect treating a constraint Angle type while dumping (issue #2893)
[modules/shaper.git] / src / SketchAPI / SketchAPI_ConstraintAngle.cpp
1 // Copyright (C) 2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchAPI_ConstraintAngle.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24
25 #include <GeomAPI_Angle2d.h>
26 #include <GeomAPI_Lin2d.h>
27
28 #include <SketchPlugin_ConstraintAngle.h>
29 #include <SketchPlugin_Line.h>
30
31 #include <cmath>
32
33 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
34     const std::shared_ptr<ModelAPI_Feature> & theFeature)
35 : SketchAPI_Constraint(theFeature)
36 {
37   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
38   if (aConstraint)
39     initialize();
40 }
41
42 static FeaturePtr getFeatureLine(DataPtr theData, const std::string& theAttribute)
43 {
44   FeaturePtr aLine;
45   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
46     return aLine;
47
48   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
49     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
50   if (anAttr) {
51     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
52     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
53       return aFeature;
54     }
55   }
56   return aLine;
57 }
58
59 static void calculatePossibleValuesOfAngle(FeaturePtr theFeature,
60     double& theAngleDirect, double& theAngleComplementary, double& theAngleBackward)
61 {
62   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
63   FeaturePtr aLineA = getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
64   FeaturePtr aLineB = getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
65
66   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
67     aLineA->attribute(SketchPlugin_Line::START_ID()));
68   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
69     aLineA->attribute(SketchPlugin_Line::END_ID()));
70   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
71     aLineB->attribute(SketchPlugin_Line::START_ID()));
72   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
73     aLineB->attribute(SketchPlugin_Line::END_ID()));
74
75   std::shared_ptr<GeomAPI_Angle2d> anAng(new GeomAPI_Angle2d(
76       aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
77   theAngleDirect = anAng->angleDegree();
78   theAngleBackward = 360.0 - theAngleDirect;
79
80   if (theAngleDirect > 180.0)
81     theAngleComplementary = theAngleDirect - 180.0;
82   else
83     theAngleComplementary = 180.0 - theAngleDirect;
84 }
85
86 static std::string angleTypeToString(FeaturePtr theFeature)
87 {
88   static const double TOLERANCE = 1.e-7;
89   double anAngleDirect, anAngleComplmentary, anAngleBackward;
90   calculatePossibleValuesOfAngle(theFeature, anAngleDirect, anAngleComplmentary, anAngleBackward);
91
92   AttributeDoublePtr aValueAttr = theFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
93
94   double aDirectDiff = fabs(aValueAttr->value() - anAngleDirect);
95   double aComplementaryDiff = fabs(aValueAttr->value() - anAngleComplmentary);
96   double aBackwardDiff = fabs(aValueAttr->value() - anAngleBackward);
97
98   // find the minimal difference
99   std::string aType;
100   if (aDirectDiff > TOLERANCE) {
101     if (aComplementaryDiff < aDirectDiff && aComplementaryDiff < aBackwardDiff)
102       aType = "Complementary";
103     else if (aBackwardDiff < aDirectDiff && aBackwardDiff < aComplementaryDiff)
104       aType = "Backward";
105   }
106   return aType;
107 }
108
109 void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
110 {
111   // postpone constraint until all its attributes be dumped
112   if (!areAllAttributesDumped(theDumper))
113     return;
114
115   // calculate angle value as it was just applied to the attributes
116   FeaturePtr aBase = feature();
117   std::string aSetterSuffix = angleTypeToString(aBase);
118
119   const std::string& aSketchName = theDumper.parentName(aBase);
120   theDumper << aBase << " = " << aSketchName << "." << "setAngle" << aSetterSuffix << "(";
121
122   bool isFirstAttr = true;
123   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
124     AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
125     if (aRefAttr && aRefAttr->isInitialized()) {
126       theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
127       isFirstAttr = false;
128     }
129   }
130
131   AttributeDoublePtr aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
132   if (aValueAttr && aValueAttr->isInitialized())
133     theDumper << ", " << aValueAttr;
134
135   theDumper << ")" << std::endl;
136 }