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