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