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 std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
59 for (; aFIter != aFeatures.cend(); aFIter++) {
60 FeaturePtr aPlane = *aFIter;
61 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
62 std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
63 for (; aRIter != aResults.cend(); aRIter++) {
64 (*aRIter)->setDisplayed(false);
67 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
69 // the viewer update should be unblocked in order to avoid the features blinking before they are
71 aMsg = std::shared_ptr<Events_Message>(
72 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
74 Events_Loop::loop()->send(aMsg);
76 } else if (theMessage.get()) {
78 std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
79 + theMessage->eventID().eventText());
83 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
86 FeaturePtr aPlane = theDoc->addFeature("Plane");
87 aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
88 aPlane->real("A")->setValue(theX);
89 aPlane->real("B")->setValue(theY);
90 aPlane->real("C")->setValue(theZ);
91 aPlane->real("D")->setValue(0.);
94 aPlane->data()->setName("YOZ");
96 aPlane->data()->setName("XOZ");
98 aPlane->data()->setName("XOY");
100 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
102 // the plane should be executed in order to build the feature result immediatelly
103 // the results are to be hidden in the plugin
105 // this flag is needed here to avoid setting it inside of the next transaction
106 // (may cause crash on redo of the first transaction in OCAF)
107 aPlane->data()->execState(ModelAPI_StateDone);
108 aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
113 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
115 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
116 aPoint->real("x")->setValue(0.);
117 aPoint->real("y")->setValue(0.);
118 aPoint->real("z")->setValue(0.);
119 aPoint->data()->setName("Origin");
120 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history
122 // the point should be executed in order to build the feature result immediatelly
123 // the results are to be hidden in the plugin
125 aPoint->data()->execState(ModelAPI_StateDone);
126 aPoint->firstResult()->data()->execState(ModelAPI_StateDone);