]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchAPI/SketchAPI_ConstraintAngle.cpp
Salome HOME
cdfa37b6dcd2b94283290938f4925625c7c11bf7
[modules/shaper.git] / src / SketchAPI / SketchAPI_ConstraintAngle.cpp
1 // Copyright (C) 2019-20xx  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_ConstraintAngle.h"
22
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
25
26 #include <GeomAPI_Angle2d.h>
27 #include <GeomAPI_Lin2d.h>
28
29 #include <SketchPlugin_ConstraintAngle.h>
30 #include <SketchPlugin_Line.h>
31
32 #include <cmath>
33
34 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : SketchAPI_Constraint(theFeature)
37 {
38   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
39   if (aConstraint)
40     initialize();
41 }
42
43 static FeaturePtr getFeatureLine(DataPtr theData, const std::string& theAttribute)
44 {
45   FeaturePtr aLine;
46   if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
47     return aLine;
48
49   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
50     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
51   if (anAttr) {
52     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
53     if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
54       return aFeature;
55     }
56   }
57   return aLine;
58 }
59
60 static void calculatePossibleValuesOfAngle(FeaturePtr theFeature,
61     double& theAngleDirect, double& theAngleComplementary, double& theAngleBackward)
62 {
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());
66
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()));
75
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;
80
81   if (theAngleDirect > 180.0)
82     theAngleComplementary = theAngleDirect - 180.0;
83   else
84     theAngleComplementary = 180.0 - theAngleDirect;
85 }
86
87 static std::string angleTypeToString(FeaturePtr theFeature)
88 {
89   static const double TOLERANCE = 1.e-7;
90   double anAngleDirect, anAngleComplmentary, anAngleBackward;
91   calculatePossibleValuesOfAngle(theFeature, anAngleDirect, anAngleComplmentary, anAngleBackward);
92
93   AttributeDoublePtr aValueAttr = theFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
94
95   std::string aType;
96   if (fabs(aValueAttr->value() - anAngleDirect) < TOLERANCE) {
97     // Nothing to do.
98     // This case is empty and going the first to check the direct angle before the others.
99   }
100   else if (fabs(aValueAttr->value() - anAngleComplmentary) < TOLERANCE)
101     aType = "Complementary";
102   else if (fabs(aValueAttr->value() - anAngleBackward) < TOLERANCE)
103     aType = "Backward";
104   return aType;
105 }
106
107 void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
108 {
109   // postpone constraint until all its attributes be dumped
110   if (!areAllAttributesDumped(theDumper))
111     return;
112
113   // calculate angle value as it was just applied to the attributes
114   FeaturePtr aBase = feature();
115   std::string aSetterSuffix = angleTypeToString(aBase);
116
117   const std::string& aSketchName = theDumper.parentName(aBase);
118   theDumper << aBase << " = " << aSketchName << "." << "setAngle" << aSetterSuffix << "(";
119
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;
125       isFirstAttr = false;
126     }
127   }
128
129   AttributeDoublePtr aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
130   if (aValueAttr && aValueAttr->isInitialized())
131     theDumper << ", " << aValueAttr;
132
133   theDumper << ")" << std::endl;
134 }