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