Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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     static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
59     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
60     for (; aFIter != aFeatures.cend(); aFIter++) {
61       FeaturePtr aPlane = *aFIter;
62       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
63       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
64       for (; aRIter != aResults.cend(); aRIter++) {
65         ModelAPI_EventCreator::get()->sendUpdated(*aRIter, HIDE_DISP);
66       }
67     }
68     Events_Loop::loop()->flush(HIDE_DISP);
69
70     // the viewer update should be unblocked in order to avoid the features blinking before they are
71     // hidden
72     aMsg = std::shared_ptr<Events_Message>(
73                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
74
75     Events_Loop::loop()->send(aMsg);
76
77   } else if (theMessage.get()) {
78     Events_Error::send(
79         std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
80             + theMessage->eventID().eventText());
81   }
82 }
83
84 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
85                                                     double theZ)
86 {
87   FeaturePtr aPlane = theDoc->addFeature("Plane");
88   aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
89   aPlane->real("A")->setValue(theX);
90   aPlane->real("B")->setValue(theY);
91   aPlane->real("C")->setValue(theZ);
92   aPlane->real("D")->setValue(0.);
93
94   if (theX) {
95     aPlane->data()->setName("Y0Z");
96   } else if (theY) {
97     aPlane->data()->setName("X0Z");
98   } else if (theZ) {
99     aPlane->data()->setName("X0Y");
100   }
101   aPlane->setInHistory(aPlane, false);  // don't show automatically created feature in the features history
102
103   // the plane should be executed in order to build the feature result immediatelly
104   // the results are to be hidden in the plugin
105   aPlane->execute();
106
107   return aPlane;
108 }
109
110 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
111 {
112   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
113   aPoint->real("x")->setValue(0.);
114   aPoint->real("y")->setValue(0.);
115   aPoint->real("z")->setValue(0.);
116   aPoint->data()->setName("Origin");
117   aPoint->setInHistory(aPoint, false);  // don't show automatically created feature in the features history
118
119   // the point should be executed in order to build the feature result immediatelly
120   // the results are to be hidden in the plugin
121   aPoint->execute();
122
123   return aPoint;
124 }