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>
33 SketchAPI_ConstraintAngle::SketchAPI_ConstraintAngle(
34 const std::shared_ptr<ModelAPI_Feature> & theFeature)
35 : SketchAPI_Constraint(theFeature)
37 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
42 static FeaturePtr getFeatureLine(DataPtr theData, const std::string& theAttribute)
45 if (!theData.get() || !theData->isValid()) /// essential check as it is called in openGl thread)
48 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
49 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
51 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
52 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
59 static void calculatePossibleValuesOfAngle(FeaturePtr theFeature,
60 double& theAngleDirect, double& theAngleComplementary, double& theAngleBackward)
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());
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()));
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;
80 if (theAngleDirect > 180.0)
81 theAngleComplementary = theAngleDirect - 180.0;
83 theAngleComplementary = 180.0 - theAngleDirect;
86 static std::string angleTypeToString(FeaturePtr theFeature)
88 static const double TOLERANCE = 1.e-7;
89 double anAngleDirect, anAngleComplmentary, anAngleBackward;
90 calculatePossibleValuesOfAngle(theFeature, anAngleDirect, anAngleComplmentary, anAngleBackward);
92 AttributeDoublePtr aValueAttr = theFeature->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
94 double aDirectDiff = fabs(aValueAttr->value() - anAngleDirect);
95 double aComplementaryDiff = fabs(aValueAttr->value() - anAngleComplmentary);
96 double aBackwardDiff = fabs(aValueAttr->value() - anAngleBackward);
98 // find the minimal difference
100 if (aDirectDiff > TOLERANCE) {
101 if (aComplementaryDiff < aDirectDiff && aComplementaryDiff < aBackwardDiff)
102 aType = "Complementary";
103 else if (aBackwardDiff < aDirectDiff && aBackwardDiff < aComplementaryDiff)
109 void SketchAPI_ConstraintAngle::dump(ModelHighAPI_Dumper& theDumper) const
111 // postpone constraint until all its attributes be dumped
112 if (!areAllAttributesDumped(theDumper))
115 // calculate angle value as it was just applied to the attributes
116 FeaturePtr aBase = feature();
117 std::string aSetterSuffix = angleTypeToString(aBase);
119 const std::string& aSketchName = theDumper.parentName(aBase);
120 theDumper << aBase << " = " << aSketchName << "." << "setAngle" << aSetterSuffix << "(";
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;
131 AttributeDoublePtr aValueAttr = aBase->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
132 if (aValueAttr && aValueAttr->isInitialized())
133 theDumper << ", " << aValueAttr;
135 theDumper << ")" << std::endl;