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