1 // Copyright (C) 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 "SketchAPI_ConstraintAngle.h"
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
25 #include <GeomAPI_Angle2d.h>
26 #include <GeomAPI_Lin2d.h>
28 #include <SketchPlugin_ConstraintAngle.h>
29 #include <SketchPlugin_Line.h>
31 #include <SketcherPrs_Tools.h>
35 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
36 const std::shared_ptr<ModelAPI_Feature> & theFeature)
37 : SketchAPI_Constraint(theFeature)
39 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
44 static FeaturePtr getFeatureLine(DataPtr theData, const std::string& theAttribute)
47 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
50 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
51 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
53 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
54 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
61 static void calculatePossibleValuesOfAngle(FeaturePtr theFeature,
62 double& theAngleDirect, double& theAngleComplementary, double& theAngleBackward)
64 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
65 FeaturePtr aLineA = getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
66 FeaturePtr aLineB = getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
68 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
69 aLineA->attribute(SketchPlugin_Line::START_ID()));
70 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
71 aLineA->attribute(SketchPlugin_Line::END_ID()));
72 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
73 aLineB->attribute(SketchPlugin_Line::START_ID()));
74 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
75 aLineB->attribute(SketchPlugin_Line::END_ID()));
77 std::shared_ptr<GeomAPI_Angle2d> anAng(new GeomAPI_Angle2d(
78 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
79 theAngleDirect = anAng->angleDegree();
80 theAngleBackward = 360.0 - theAngleDirect;
82 if (theAngleDirect > 180.0)
83 theAngleComplementary = theAngleDirect - 180.0;
85 theAngleComplementary = 180.0 - theAngleDirect;
88 static std::string angleTypeToString(FeaturePtr theFeature)
90 static const double TOLERANCE = 1.e-7;
91 double anAngleDirect, anAngleComplmentary, anAngleBackward;
92 calculatePossibleValuesOfAngle(theFeature, anAngleDirect, anAngleComplmentary, anAngleBackward);
94 AttributeDoublePtr aValueAttr = theFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
95 double aDirectDiff = fabs(aValueAttr->value() - anAngleDirect);
96 double aComplementaryDiff = fabs(aValueAttr->value() - anAngleComplmentary);
97 double aBackwardDiff = fabs(aValueAttr->value() - anAngleBackward);
99 AttributeIntegerPtr aTypeAttr = theFeature->integer(SketchPlugin_ConstraintAngle::TYPE_ID());
100 bool isDirect = aTypeAttr->value() == SketcherPrs_Tools::ANGLE_DIRECT;
101 bool isComplementary = aTypeAttr->value() == SketcherPrs_Tools::ANGLE_COMPLEMENTARY;
102 bool isBackward = aTypeAttr->value() == SketcherPrs_Tools::ANGLE_BACKWARD;
104 // find the minimal difference
106 if (isDirect && aDirectDiff < TOLERANCE) {
108 // This case is empty and going the first to check the direct angle before the others.
110 else if (isComplementary && aComplementaryDiff < TOLERANCE)
111 aType = "Complementary";
112 else if (isBackward && aBackwardDiff < TOLERANCE)
115 if (aComplementaryDiff < aDirectDiff && aComplementaryDiff < aBackwardDiff)
116 aType = "Complementary";
117 else if (aBackwardDiff < aDirectDiff && aBackwardDiff < aComplementaryDiff)
123 void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
125 // postpone constraint until all its attributes be dumped
126 if (!areAllAttributesDumped(theDumper))
129 // calculate angle value as it was just applied to the attributes
130 FeaturePtr aBase = feature();
131 std::string aSetterSuffix = angleTypeToString(aBase);
133 const std::string& aSketchName = theDumper.parentName(aBase);
134 theDumper << aBase << " = " << aSketchName << "." << "setAngle" << aSetterSuffix << "(";
136 bool isFirstAttr = true;
137 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
138 AttributeRefAttrPtr aRefAttr = aBase->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
139 if (aRefAttr && aRefAttr->isInitialized()) {
140 theDumper << (isFirstAttr ? "" : ", ") << aRefAttr;
145 AttributeDoublePtr aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
146 if (aValueAttr && aValueAttr->isInitialized())
147 theDumper << ", " << aValueAttr;
149 theDumper << ")" << std::endl;