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>
11 #include <Events_Message.h>
12 #include <Events_Error.h>
16 // the only created instance of this plugin
17 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE = new InitializationPlugin_Plugin();
19 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
21 Events_Loop* aLoop = Events_Loop::loop();
22 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
23 aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
26 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
28 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
29 if (theMessage->eventID() == kDocCreatedEvent) {
30 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage =
31 std::dynamic_pointer_cast<ModelAPI_DocumentCreatedMessage>(theMessage);
32 DocumentPtr aDoc = aMessage->document();
34 createPlane(aDoc, 1., 0., 0.);
35 createPlane(aDoc, 0., 1., 0.);
36 createPlane(aDoc, 0., 0., 1.);
37 // for PartSet it is done outside of the transaction, so explicitly flush this creation
38 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
39 } else if (theMessage.get()) {
41 std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ") +
42 theMessage->eventID().eventText());
46 void InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theA, double theB, double theC)
48 std::shared_ptr<ModelAPI_Feature> aPlane = theDoc->addFeature("Plane");
49 aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
50 aPlane->real("A")->setValue(theA);
51 aPlane->real("B")->setValue(theB);
52 aPlane->real("C")->setValue(theC);
53 aPlane->real("D")->setValue(0.);
56 aPlane->data()->setName("Y0Z");
58 aPlane->data()->setName("X0Z");
60 aPlane->data()->setName("X0Y");
62 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
65 void InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
67 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
68 aPoint->real("x")->setValue(0.);
69 aPoint->real("y")->setValue(0.);
70 aPoint->real("z")->setValue(0.);
71 aPoint->data()->setName("Origin");
72 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history