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();
35 std::list<FeaturePtr> aFeatures;
37 aFeatures.push_back(createPoint(aDoc));
38 aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
39 aFeatures.push_back(createPlane(aDoc, 0., 1., 0.));
40 aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
41 // for PartSet it is done outside of the transaction, so explicitly flush this creation
42 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
44 // hides the created features, the precondition is that the feature's results have been
45 // already built, so the createPlane/Points method calls the execute function for the planes
46 static Events_ID HIDE_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOHIDE);
47 std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
48 for (; aFIter != aFeatures.cend(); aFIter++) {
49 FeaturePtr aPlane = *aFIter;
50 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
51 std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
52 for (; aRIter != aResults.cend(); aRIter++) {
53 ModelAPI_EventCreator::get()->sendUpdated(*aRIter, HIDE_DISP);
56 Events_Loop::loop()->flush(HIDE_DISP);
58 } else if (theMessage.get()) {
60 std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
61 + theMessage->eventID().eventText());
65 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
68 FeaturePtr aPlane = theDoc->addFeature("Plane");
69 aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
70 aPlane->real("A")->setValue(theX);
71 aPlane->real("B")->setValue(theY);
72 aPlane->real("C")->setValue(theZ);
73 aPlane->real("D")->setValue(0.);
76 aPlane->data()->setName("Y0Z");
78 aPlane->data()->setName("X0Z");
80 aPlane->data()->setName("X0Y");
82 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
84 // the plane should be executed in order to build the feature result immediatelly
85 // the results are to be hidden in the plugin
91 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
93 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
94 aPoint->real("x")->setValue(0.);
95 aPoint->real("y")->setValue(0.);
96 aPoint->real("z")->setValue(0.);
97 aPoint->data()->setName("Origin");
98 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history
100 // the point should be executed in order to build the feature result immediatelly
101 // the results are to be hidden in the plugin