Salome HOME
Temporary commit proposed by DBV: to make unit-tests working while the compsolids...
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_Plugin.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <InitializationPlugin_Plugin.h>
4
5 #include <ModelAPI_Session.h>
6 #include <ModelAPI_Document.h>
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_AttributeString.h>
9 #include <ModelAPI_Events.h>
10 #include <ModelAPI_Result.h>
11
12 #include <Events_Message.h>
13 #include <Events_Error.h>
14
15 #include <memory>
16
17 // the only created instance of this plugin
18 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
19     new InitializationPlugin_Plugin();
20
21 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
22 {
23   Events_Loop* aLoop = Events_Loop::loop();
24   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
25   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
26 }
27
28 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
29 {
30   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
31   if (theMessage->eventID() == kDocCreatedEvent) {
32     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
33         ModelAPI_DocumentCreatedMessage>(theMessage);
34     DocumentPtr aDoc = aMessage->document();
35
36     /// Issue 431: for the current moment create planes only in the module document,
37     /// Later if it is needed we may create special initial planes in Parts (may be different)
38     if (aDoc != ModelAPI_Session::get()->moduleDocument())
39       return;
40
41     std::list<FeaturePtr> aFeatures;
42
43     // the viewer update should be blocked in order to avoid the features blinking before they are
44     // hidden
45     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
46         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
47     Events_Loop::loop()->send(aMsg);
48
49     aFeatures.push_back(createPoint(aDoc));
50     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
51     aFeatures.push_back(createPlane(aDoc, 0., 1., 0.));
52     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
53     // for PartSet it is done outside of the transaction, so explicitly flush this creation
54     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
55
56     // hides the created features, the precondition is that the feature's results have been
57     // already built, so the createPlane/Points method calls the execute function for the planes
58     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
59     for (; aFIter != aFeatures.cend(); aFIter++) {
60       FeaturePtr aPlane = *aFIter;
61       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
62       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
63       for (; aRIter != aResults.cend(); aRIter++) {
64         (*aRIter)->setDisplayed(false);
65       }
66     }
67     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
68
69     // the viewer update should be unblocked in order to avoid the features blinking before they are
70     // hidden
71     aMsg = std::shared_ptr<Events_Message>(
72                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
73
74     Events_Loop::loop()->send(aMsg);
75
76   } else if (theMessage.get()) {
77     Events_Error::send(
78         std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
79             + theMessage->eventID().eventText());
80   }
81 }
82
83 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
84                                                     double theZ)
85 {
86   FeaturePtr aPlane = theDoc->addFeature("Plane");
87   aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
88   aPlane->real("A")->setValue(theX);
89   aPlane->real("B")->setValue(theY);
90   aPlane->real("C")->setValue(theZ);
91   aPlane->real("D")->setValue(0.);
92
93   if (theX) {
94     aPlane->data()->setName("YOZ");
95   } else if (theY) {
96     aPlane->data()->setName("XOZ");
97   } else if (theZ) {
98     aPlane->data()->setName("XOY");
99   }
100   aPlane->setInHistory(aPlane, false);  // don't show automatically created feature in the features history
101
102   // the plane should be executed in order to build the feature result immediatelly
103   // the results are to be hidden in the plugin
104   aPlane->execute();
105   // this flag is needed here to avoid setting it inside of the next transaction
106   // (may cause crash on redo of the first transaction in OCAF)
107   aPlane->data()->execState(ModelAPI_StateDone);
108   aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
109
110   return aPlane;
111 }
112
113 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
114 {
115   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
116   aPoint->real("x")->setValue(0.);
117   aPoint->real("y")->setValue(0.);
118   aPoint->real("z")->setValue(0.);
119   aPoint->data()->setName("Origin");
120   aPoint->setInHistory(aPoint, false);  // don't show automatically created feature in the features history
121
122   // the point should be executed in order to build the feature result immediatelly
123   // the results are to be hidden in the plugin
124   aPoint->execute();
125   aPoint->data()->execState(ModelAPI_StateDone);
126   aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
127
128   return aPoint;
129 }