]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintMiddle.cpp
Salome HOME
[bos #37570] [CEA] SHAPER hdf tests are failing. Fix Sphere feature attributes order.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintMiddle.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 "SketchPlugin_ConstraintMiddle.h"
21
22 #include "SketcherPrs_Factory.h"
23
24 #include <GeomDataAPI_Point2D.h>
25
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_AttributeString.h>
28
29 #include <ModelAPI_Events.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Tools.h>
32
33 SketchPlugin_ConstraintMiddle::SketchPlugin_ConstraintMiddle()
34 {
35 }
36
37 // Create new point for Middle constraint
38 void SketchPlugin_ConstraintMiddle::CreatePoint()
39 {
40   // Wait all objects being created, then send update events
41   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
42   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
43   if (isUpdateFlushed)
44     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
45
46   auto aTrPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()));
47
48   if (!myPoint)
49   {
50     // Get last subfeature (constraintMiddle) for set as parent
51     FeaturePtr aCurrentFeature = sketch()->subFeature(sketch()->numberOfSubs() - 1);
52     keepCurrentFeature();
53     myPoint = sketch()->addFeature(SketchPlugin_Point::ID());
54     myPoint->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
55     restoreCurrentFeature();
56
57     myPoint->reference(SketchPlugin_Point::PARENT_ID())->setValue(aCurrentFeature);
58   }
59
60   AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
61     myPoint->attribute(SketchPlugin_Point::COORD_ID()));
62   aCoord->setValue(aTrPnt->pnt());
63
64   myPoint->execute();
65
66   // Init second attr for constraint
67   refattr(SketchPlugin_Constraint::ENTITY_B())->setObject(myPoint);
68
69
70   if (isUpdateFlushed)
71     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
72 }
73
74 void SketchPlugin_ConstraintMiddle::initAttributes()
75 {
76   data()->addAttribute(SketchPlugin_ConstraintMiddle::MIDDLE_TYPE(), ModelAPI_AttributeString::typeId());
77
78   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
79   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
80
81   data()->addAttribute(POINT_REF_ID(), GeomDataAPI_Point2D::typeId());
82
83   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), POINT_REF_ID());
84 }
85
86 void SketchPlugin_ConstraintMiddle::execute()
87 {
88   if (string(MIDDLE_TYPE())->value() == MIDDLE_TYPE_BY_LINE())
89   {
90     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()))->setValue(1., 1.);
91     AttributeRefAttrPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
92       data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
93
94     auto aRefAttr = data()->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
95     if (!aRefAttr.get())
96       return;
97
98
99     if (!attribute(ENTITY_B())->isInitialized())
100       CreatePoint(); // Create new point
101   }
102 }
103
104 void SketchPlugin_ConstraintMiddle::attributeChanged(const std::string& theID)
105 {
106   if (theID == MIDDLE_TYPE())
107   {
108     SketchPlugin_Tools::resetAttribute(this, ENTITY_A());
109     SketchPlugin_Tools::resetAttribute(this, ENTITY_B());
110   }
111   else if (theID == POINT_REF_ID())
112   {
113     if (!myPoint)
114       return;
115
116     auto aTrPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(POINT_REF_ID()));
117     AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118       myPoint->attribute(SketchPlugin_Point::COORD_ID()));
119     aCoord->setValue(aTrPnt->pnt());
120
121     myPoint->execute();
122   }
123 }
124
125 AISObjectPtr SketchPlugin_ConstraintMiddle::getAISObject(AISObjectPtr thePrevious)
126 {
127   if (!sketch())
128     return thePrevious;
129   AISObjectPtr anAIS = SketcherPrs_Factory::middleConstraint(this, sketch(),
130                                                              thePrevious);
131
132   return anAIS;
133 }