1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <InitializationPlugin_Plugin.h>
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>
12 #include <Events_Message.h>
13 #include <Events_Error.h>
17 // the only created instance of this plugin
18 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
19 new InitializationPlugin_Plugin();
21 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
23 Events_Loop* aLoop = Events_Loop::loop();
24 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
25 aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
28 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
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();
36 /// Issue 431: for the current moment create planes only in the module document,
37 /// Later if it is needed we may create special initial planes in Parts (may be different)
38 if (aDoc != ModelAPI_Session::get()->moduleDocument())
41 std::list<FeaturePtr> aFeatures;
43 // the viewer update should be blocked in order to avoid the features blinking before they are
45 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
46 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
47 Events_Loop::loop()->send(aMsg);
49 aFeatures.push_back(createPoint(aDoc));
50 aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
51 aFeatures.push_back(createPlane(aDoc, 0., 1., 0.));
52 aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
53 // for PartSet it is done outside of the transaction, so explicitly flush this creation
54 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
56 // hides the created features, the precondition is that the feature's results have been
57 // already built, so the createPlane/Points method calls the execute function for the planes
58 static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
59 std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
60 for (; aFIter != aFeatures.cend(); aFIter++) {
61 FeaturePtr aPlane = *aFIter;
62 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
63 std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
64 for (; aRIter != aResults.cend(); aRIter++) {
65 ModelAPI_EventCreator::get()->sendUpdated(*aRIter, HIDE_DISP);
68 Events_Loop::loop()->flush(HIDE_DISP);
70 // the viewer update should be unblocked in order to avoid the features blinking before they are
72 aMsg = std::shared_ptr<Events_Message>(
73 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
75 Events_Loop::loop()->send(aMsg);
77 } else if (theMessage.get()) {
79 std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
80 + theMessage->eventID().eventText());
84 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
87 FeaturePtr aPlane = theDoc->addFeature("Plane");
88 aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
89 aPlane->real("A")->setValue(theX);
90 aPlane->real("B")->setValue(theY);
91 aPlane->real("C")->setValue(theZ);
92 aPlane->real("D")->setValue(0.);
95 aPlane->data()->setName("Y0Z");
97 aPlane->data()->setName("X0Z");
99 aPlane->data()->setName("X0Y");
101 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
103 // the plane should be executed in order to build the feature result immediatelly
104 // the results are to be hidden in the plugin
110 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
112 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
113 aPoint->real("x")->setValue(0.);
114 aPoint->real("y")->setValue(0.);
115 aPoint->real("z")->setValue(0.);
116 aPoint->data()->setName("Origin");
117 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history
119 // the point should be executed in order to build the feature result immediatelly
120 // the results are to be hidden in the plugin