]> SALOME platform Git repositories - modules/shaper.git/blob - src/InitializationPlugin/InitializationPlugin_Plugin.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom 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     std::list<FeaturePtr> aFeatures;
36
37     // the viewer update should be blocked in order to avoid the features blinking before they are
38     // hidden
39     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
40         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
41     Events_Loop::loop()->send(aMsg);
42
43     aFeatures.push_back(createPoint(aDoc));
44     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
45     aFeatures.push_back(createPlane(aDoc, 0., 1., 0.));
46     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
47     // for PartSet it is done outside of the transaction, so explicitly flush this creation
48     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
49
50     // hides the created features, the precondition is that the feature's results have been
51     // already built, so the createPlane/Points method calls the execute function for the planes
52     static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
53     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
54     for (; aFIter != aFeatures.cend(); aFIter++) {
55       FeaturePtr aPlane = *aFIter;
56       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
57       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
58       for (; aRIter != aResults.cend(); aRIter++) {
59         ModelAPI_EventCreator::get()->sendUpdated(*aRIter, HIDE_DISP);
60       }
61     }
62     Events_Loop::loop()->flush(HIDE_DISP);
63
64     // the viewer update should be unblocked in order to avoid the features blinking before they are
65     // hidden
66     aMsg = std::shared_ptr<Events_Message>(
67                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
68
69     Events_Loop::loop()->send(aMsg);
70
71   } else if (theMessage.get()) {
72     Events_Error::send(
73         std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
74             + theMessage->eventID().eventText());
75   }
76 }
77
78 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
79                                                     double theZ)
80 {
81   FeaturePtr aPlane = theDoc->addFeature("Plane");
82   aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
83   aPlane->real("A")->setValue(theX);
84   aPlane->real("B")->setValue(theY);
85   aPlane->real("C")->setValue(theZ);
86   aPlane->real("D")->setValue(0.);
87
88   if (theX) {
89     aPlane->data()->setName("Y0Z");
90   } else if (theY) {
91     aPlane->data()->setName("X0Z");
92   } else if (theZ) {
93     aPlane->data()->setName("X0Y");
94   }
95   aPlane->setInHistory(aPlane, false);  // don't show automatically created feature in the features history
96
97   // the plane should be executed in order to build the feature result immediatelly
98   // the results are to be hidden in the plugin
99   aPlane->execute();
100
101   return aPlane;
102 }
103
104 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
105 {
106   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
107   aPoint->real("x")->setValue(0.);
108   aPoint->real("y")->setValue(0.);
109   aPoint->real("z")->setValue(0.);
110   aPoint->data()->setName("Origin");
111   aPoint->setInHistory(aPoint, false);  // don't show automatically created feature in the features history
112
113   // the point should be executed in order to build the feature result immediatelly
114   // the results are to be hidden in the plugin
115   aPoint->execute();
116
117   return aPoint;
118 }