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