Salome HOME
[EDF] (2023-T1) Sketch middle point constrain should create point if missing
[modules/shaper.git] / src / SketchAPI / SketchAPI_MacroMiddlePoint.cpp
1 // Copyright (C) 2019-2022  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_MacroMiddlePoint.h"
21
22 #include <ModelHighAPI_RefAttr.h>
23
24 #include <SketchPlugin_ConstraintMiddle.h>
25
26 #include <cmath>
27
28 SketchAPI_MacroMiddlePoint::SketchAPI_MacroMiddlePoint(
29     const std::shared_ptr<ModelAPI_Feature> & theFeature)
30 : SketchAPI_Point(theFeature)
31 {
32   ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
33   if (aConstraint)
34     initialize();
35 }
36
37 SketchAPI_MacroMiddlePoint::SketchAPI_MacroMiddlePoint(const std::shared_ptr<ModelAPI_Feature>& theFeature,
38   const ModelHighAPI_RefAttr& theLine, const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
39   : SketchAPI_Point(theFeature, thePoint)
40 {
41   createConstraint(theLine);
42 }
43
44 void SketchAPI_MacroMiddlePoint::createConstraint(const ModelHighAPI_RefAttr& theLine)
45 {
46   // Find sketch
47   CompositeFeaturePtr aSketch;
48   const std::set<AttributePtr>& aRefs = feature()->data()->refsToMe();
49   for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt)
50     if ((*anIt)->id() == SketchPlugin_Sketch::FEATURES_ID())
51     {
52       aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
53       break;
54     }
55   if (!aSketch)
56     return;
57
58   // Create middle constraint for line and poiunt
59   std::shared_ptr<ModelAPI_Feature> aConstrFeature =
60     aSketch->addFeature(SketchPlugin_ConstraintMiddle::ID());
61   aConstrFeature->string(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE())->setValue(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE_BY_LINE());
62   aConstrFeature->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(theLine.object());
63   aConstrFeature->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(coordinates());
64   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aConstrFeature->attribute(SketchPlugin_ConstraintMiddle::POINT_REF_ID()))->setValue(coordinates()->x(), coordinates()->y());
65
66   aConstrFeature->execute();
67
68   feature()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
69   feature()->reference(SketchPlugin_SketchEntity::PARENT_ID())->setValue(aConstrFeature);
70   feature()->execute();
71 }