Salome HOME
Merge remote-tracking branch 'remotes/origin/EDF_2020_Lot2'
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_Plugin.cpp
1 // Copyright (C) 2014-2020  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 <InitializationPlugin_Plugin.h>
21
22 #include <InitializationPlugin_EvalListener.h>
23 #include <ModelAPI_Session.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeSelection.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Result.h>
31
32 #include <GeomDataAPI_Point.h>
33
34 #include <Events_Message.h>
35 #include <Events_InfoMessage.h>
36
37 #include <memory>
38
39 // the only created instance of this plugin
40 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
41     new InitializationPlugin_Plugin();
42
43 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
44 {
45   Events_Loop* aLoop = Events_Loop::loop();
46   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
47   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
48
49   myEvalListener =
50     std::shared_ptr<InitializationPlugin_EvalListener>(new InitializationPlugin_EvalListener());
51 }
52
53 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
54 {
55   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
56   if (theMessage->eventID() == kDocCreatedEvent) {
57     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
58         ModelAPI_DocumentCreatedMessage>(theMessage);
59     DocumentPtr aDoc = aMessage->document();
60
61     /// Issue 431: for the current moment create planes only in the module document,
62     /// Later if it is needed we may create special initial planes in Parts (may be different)
63     if (aDoc != ModelAPI_Session::get()->moduleDocument())
64       return;
65
66     myEvalListener->initDataModel();
67
68     std::list<FeaturePtr> aFeatures;
69
70     // the viewer update should be blocked in order to avoid the features blinking before they are
71     // hidden
72     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
73         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
74     Events_Loop::loop()->send(aMsg);
75
76     FeaturePtr aOrigin = createPoint(aDoc, L"Origin", 0., 0., 0.);
77     aFeatures.push_back(aOrigin);
78     aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
79     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
80     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
81     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
82     aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
83     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
84     // for PartSet it is done outside of the transaction, so explicitly flush this creation
85     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
86
87     // hides the created features, the precondition is that the feature's results have been
88     // already built, so the createPlane/Points method calls the execute function for the planes
89     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
90     for (; aFIter != aFeatures.cend(); aFIter++) {
91       FeaturePtr aPlane = *aFIter;
92       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
93       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
94       for (; aRIter != aResults.cend(); aRIter++) {
95         (*aRIter)->setDisplayed(false);
96       }
97     }
98     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
99     // the viewer update should be unblocked in order to avoid the features blinking before they are
100     // hidden
101     aMsg = std::shared_ptr<Events_Message>(
102                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
103
104     Events_Loop::loop()->send(aMsg);
105   }
106 }
107
108 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
109                                                     double theZ)
110 {
111   FeaturePtr aPlane = theDoc->addFeature("Plane");
112   aPlane->string("creation_method")->setValue("by_general_equation");
113   aPlane->real("A")->setValue(theX);
114   aPlane->real("B")->setValue(theY);
115   aPlane->real("C")->setValue(theZ);
116   aPlane->real("D")->setValue(0.);
117
118   if (theX) {
119     aPlane->data()->setName(L"YOZ");
120   } else if (theY) {
121     aPlane->data()->setName(L"XOZ");
122   } else if (theZ) {
123     aPlane->data()->setName(L"XOY");
124   }
125     // don't show automatically created feature in the features history
126   aPlane->setInHistory(aPlane, false);
127
128   // the plane should be executed in order to build the feature result immediatelly
129   // the results are to be hidden in the plugin
130   aPlane->execute();
131   // this flag is needed here to avoid setting it inside of the next transaction
132   // (may cause crash on redo of the first transaction in OCAF)
133   aPlane->data()->execState(ModelAPI_StateDone);
134   aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
135
136   return aPlane;
137 }
138
139 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::wstring& theName,
140                                                     double theX, double theY, double theZ)
141 {
142   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
143   AttributePointPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>
144     (aPoint->data()->attribute("point3d"));
145   aPointAttr->setValue(theX, theY, theZ);
146   aPoint->string("creation_method")->setValue("by_xyz");
147   aPoint->data()->setName(theName);
148   // don't show automatically created feature in the features history
149   aPoint->setInHistory(aPoint, false);
150
151   // the point should be executed in order to build the feature result immediatelly
152   // the results are to be hidden in the plugin
153   aPoint->execute();
154   aPoint->data()->execState(ModelAPI_StateDone);
155   aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
156
157   return aPoint;
158 }
159
160 FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
161                                                    double theX, double theY, double theZ)
162 {
163   std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
164   aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
165
166   ResultPtr aResult = theOrigin->firstResult();
167   aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
168
169   aAxis->real("X_Direction")->setValue(theX);
170   aAxis->real("Y_Direction")->setValue(theY);
171   aAxis->real("Z_Direction")->setValue(theZ);
172
173   if (theX != 0) {
174     aAxis->data()->setName(L"OX");
175   } else if (theY != 0) {
176     aAxis->data()->setName(L"OY");
177   } else if (theZ != 0) {
178     aAxis->data()->setName(L"OZ");
179   }
180    // don't show automatically created feature in the features history
181   aAxis->setInHistory(aAxis, false);
182   aAxis->execute();
183   aAxis->data()->execState(ModelAPI_StateDone);
184   aAxis->firstResult()->data()->execState(ModelAPI_StateDone);
185
186   return aAxis;
187 }