Salome HOME
updated copyright message
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_Plugin.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 <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 #include <Events_MessageBool.h>
37
38 #include <memory>
39
40 // the only created instance of this plugin
41 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
42     new InitializationPlugin_Plugin();
43
44 InitializationPlugin_Plugin::InitializationPlugin_Plugin() :
45   myCreatePartOnStart(false) // by default in TUI mode part is not created on start of PartSet
46 {
47   char* isUnitTest = getenv("SHAPER_UNIT_TEST_IN_PROGRESS");
48   myInitDataModel = (!isUnitTest || isUnitTest[0] != '1');
49
50   Events_Loop* aLoop = Events_Loop::loop();
51   static const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
52   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
53   static const Events_ID kCreatePartEvent = Events_Loop::eventByName(EVENT_CREATE_PART_ON_START);
54   aLoop->registerListener(this, kCreatePartEvent, NULL, true);
55
56   myEvalListener =
57     std::shared_ptr<InitializationPlugin_EvalListener>(new InitializationPlugin_EvalListener());
58 }
59
60 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
61 {
62   static const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
63   static const Events_ID kCreatePartEvent = Events_Loop::eventByName(EVENT_CREATE_PART_ON_START);
64   if (theMessage->eventID() == kDocCreatedEvent) {
65     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
66         ModelAPI_DocumentCreatedMessage>(theMessage);
67     DocumentPtr aDoc = aMessage->document();
68
69     /// Issue 431: for the current moment create planes only in the module document,
70     /// Later if it is needed we may create special initial planes in Parts (may be different)
71     SessionPtr aMgr = ModelAPI_Session::get();
72     if (aDoc != aMgr->moduleDocument() || aMgr->isLoading())
73       return;
74
75     if (myInitDataModel)
76       myEvalListener->initDataModel();
77
78     std::list<FeaturePtr> aFeatures;
79     aMgr->startOperation("Initialization");
80
81     // the viewer update should be blocked in order to avoid the features blinking before they are
82     // hidden
83     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
84         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
85     Events_Loop::loop()->send(aMsg);
86
87     FeaturePtr aOrigin = createPoint(aDoc, L"Origin", 0., 0., 0.);
88     aFeatures.push_back(aOrigin);
89     aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
90     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
91     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
92     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
93     aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
94     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
95     if (myCreatePartOnStart)
96       createPart(aDoc);
97     // for PartSet it is done outside of the transaction, so explicitly flush this creation
98     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
99
100     // hides the created features, the precondition is that the feature's results have been
101     // already built, so the createPlane/Points method calls the execute function for the planes
102     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
103     for (; aFIter != aFeatures.cend(); aFIter++) {
104       FeaturePtr aPlane = *aFIter;
105       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
106       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
107       for (; aRIter != aResults.cend(); aRIter++) {
108         (*aRIter)->setDisplayed(false);
109       }
110     }
111     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
112
113     aMgr->finishOperation(); // before last message flush to update the title, make it not-modified
114     aMgr->clearUndoRedo(); // to forbid undo of auxiliary elements and initial part
115
116     // the viewer update should be unblocked to avoid the features blinking before they are hidden
117     aMsg = std::shared_ptr<Events_Message>(
118                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
119
120     Events_Loop::loop()->send(aMsg);
121   }
122   else if (theMessage->eventID() == kCreatePartEvent)
123   {
124     std::shared_ptr<Events_MessageBool> aMsg = std::dynamic_pointer_cast<Events_MessageBool>(theMessage);
125     myCreatePartOnStart = aMsg->value();
126   }
127 }
128
129 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
130                                                     double theZ)
131 {
132   FeaturePtr aPlane = theDoc->addFeature("Plane");
133   aPlane->string("creation_method")->setValue("by_general_equation");
134   aPlane->real("A")->setValue(theX);
135   aPlane->real("B")->setValue(theY);
136   aPlane->real("C")->setValue(theZ);
137   aPlane->real("D")->setValue(0.);
138
139   if (theX) {
140     aPlane->data()->setName(L"YOZ");
141   } else if (theY) {
142     aPlane->data()->setName(L"XOZ");
143   } else if (theZ) {
144     aPlane->data()->setName(L"XOY");
145   }
146     // don't show automatically created feature in the features history
147   aPlane->setInHistory(aPlane, false);
148
149   // the plane should be executed in order to build the feature result immediatelly
150   // the results are to be hidden in the plugin
151   aPlane->execute();
152   // this flag is needed here to avoid setting it inside of the next transaction
153   // (may cause crash on redo of the first transaction in OCAF)
154   aPlane->data()->execState(ModelAPI_StateDone);
155   aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
156
157   return aPlane;
158 }
159
160 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::wstring& theName,
161                                                     double theX, double theY, double theZ)
162 {
163   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
164   AttributePointPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>
165     (aPoint->data()->attribute("point3d"));
166   aPointAttr->setValue(theX, theY, theZ);
167   aPoint->string("creation_method")->setValue("by_xyz");
168   aPoint->data()->setName(theName);
169   // don't show automatically created feature in the features history
170   aPoint->setInHistory(aPoint, false);
171
172   // the point should be executed in order to build the feature result immediatelly
173   // the results are to be hidden in the plugin
174   aPoint->execute();
175   aPoint->data()->execState(ModelAPI_StateDone);
176   aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
177
178   return aPoint;
179 }
180
181 FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
182                                                    double theX, double theY, double theZ)
183 {
184   std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
185   aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
186
187   ResultPtr aResult = theOrigin->firstResult();
188   aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
189
190   aAxis->real("X_Direction")->setValue(theX);
191   aAxis->real("Y_Direction")->setValue(theY);
192   aAxis->real("Z_Direction")->setValue(theZ);
193
194   if (theX != 0) {
195     aAxis->data()->setName(L"OX");
196   } else if (theY != 0) {
197     aAxis->data()->setName(L"OY");
198   } else if (theZ != 0) {
199     aAxis->data()->setName(L"OZ");
200   }
201    // don't show automatically created feature in the features history
202   aAxis->setInHistory(aAxis, false);
203   aAxis->execute();
204   aAxis->data()->execState(ModelAPI_StateDone);
205   aAxis->firstResult()->data()->execState(ModelAPI_StateDone);
206
207   return aAxis;
208 }
209
210 void InitializationPlugin_Plugin::createPart(DocumentPtr thePartSet)
211 {
212   std::shared_ptr<ModelAPI_Feature> aPart = thePartSet->addFeature("Part");
213   if (aPart.get())
214     aPart->execute(); // to initialize and activate this part document
215 }