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