Salome HOME
Commented options in Point feature;
[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 // the only created instance of this plugin
20 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
21     new InitializationPlugin_Plugin();
22
23 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
24 {
25   Events_Loop* aLoop = Events_Loop::loop();
26   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
27   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
28 }
29
30 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
31 {
32   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
33   if (theMessage->eventID() == kDocCreatedEvent) {
34     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
35         ModelAPI_DocumentCreatedMessage>(theMessage);
36     DocumentPtr aDoc = aMessage->document();
37
38     /// Issue 431: for the current moment create planes only in the module document,
39     /// Later if it is needed we may create special initial planes in Parts (may be different)
40     if (aDoc != ModelAPI_Session::get()->moduleDocument())
41       return;
42
43     std::list<FeaturePtr> aFeatures;
44
45     // the viewer update should be blocked in order to avoid the features blinking before they are
46     // hidden
47     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
48         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
49     Events_Loop::loop()->send(aMsg);
50
51     FeaturePtr aOrigin = createPoint(aDoc, "Origin", 0., 0., 0.);
52     aFeatures.push_back(aOrigin);
53     aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
54     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
55     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
56     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
57     aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
58     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
59     // for PartSet it is done outside of the transaction, so explicitly flush this creation
60     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
61
62     // hides the created features, the precondition is that the feature's results have been
63     // already built, so the createPlane/Points method calls the execute function for the planes
64     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
65     for (; aFIter != aFeatures.cend(); aFIter++) {
66       FeaturePtr aPlane = *aFIter;
67       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
68       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
69       for (; aRIter != aResults.cend(); aRIter++) {
70         (*aRIter)->setDisplayed(false);
71       }
72     }
73     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
74
75     // the viewer update should be unblocked in order to avoid the features blinking before they are
76     // hidden
77     aMsg = std::shared_ptr<Events_Message>(
78                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
79
80     Events_Loop::loop()->send(aMsg);
81
82   } else if (theMessage.get()) {
83     Events_InfoMessage("InitializationPlugin_Plugin",
84         "InitializationPlugin_Plugin::processEvent: unhandled message caught: %1")
85             .arg(theMessage->eventID().eventText()).send();
86   }
87 }
88
89 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
90                                                     double theZ)
91 {
92   FeaturePtr aPlane = theDoc->addFeature("Plane");
93   aPlane->string("creation_method")->setValue("by_general_equation");
94   aPlane->real("A")->setValue(theX);
95   aPlane->real("B")->setValue(theY);
96   aPlane->real("C")->setValue(theZ);
97   aPlane->real("D")->setValue(0.);
98
99   if (theX) {
100     aPlane->data()->setName("YOZ");
101   } else if (theY) {
102     aPlane->data()->setName("XOZ");
103   } else if (theZ) {
104     aPlane->data()->setName("XOY");
105   }
106   aPlane->setInHistory(aPlane, false);  // don't show automatically created feature in the features history
107
108   // the plane should be executed in order to build the feature result immediatelly
109   // the results are to be hidden in the plugin
110   aPlane->execute();
111   // this flag is needed here to avoid setting it inside of the next transaction
112   // (may cause crash on redo of the first transaction in OCAF)
113   aPlane->data()->execState(ModelAPI_StateDone);
114   aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
115
116   return aPlane;
117 }
118
119 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::string& theName,
120                                                     double theX, double theY, double theZ)
121 {
122   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
123   //aPoint->string("creation_method")->setValue("by_xyz");
124   aPoint->real("x")->setValue(theX);
125   aPoint->real("y")->setValue(theY);
126   aPoint->real("z")->setValue(theZ);
127   aPoint->data()->setName(theName);
128   aPoint->setInHistory(aPoint, false);  // don't show automatically created feature in the features history
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   aAxis->setInHistory(aAxis, false);  // don't show automatically created feature in the features history
160   aAxis->execute();
161   aAxis->data()->execState(ModelAPI_StateDone);
162   aAxis->firstResult()->data()->execState(ModelAPI_StateDone);
163
164   return aAxis;
165 }