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 =
18 new InitializationPlugin_Plugin();
20 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
22 Events_Loop* aLoop = Events_Loop::loop();
23 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
24 aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
27 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
29 const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
30 if (theMessage->eventID() == kDocCreatedEvent) {
31 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
32 ModelAPI_DocumentCreatedMessage>(theMessage);
33 DocumentPtr aDoc = aMessage->document();
35 createPlane(aDoc, 1., 0., 0.);
36 createPlane(aDoc, 0., 1., 0.);
37 createPlane(aDoc, 0., 0., 1.);
38 // for PartSet it is done outside of the transaction, so explicitly flush this creation
39 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
40 } else if (theMessage.get()) {
42 std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
43 + theMessage->eventID().eventText());
47 void InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
50 std::shared_ptr<ModelAPI_Feature> aPlane = theDoc->addFeature("Plane");
51 aPlane->string("CreationMethod")->setValue("PlaneByGeneralEquation");
52 aPlane->real("A")->setValue(theX);
53 aPlane->real("B")->setValue(theY);
54 aPlane->real("C")->setValue(theZ);
55 aPlane->real("D")->setValue(0.);
58 aPlane->data()->setName("Y0Z");
60 aPlane->data()->setName("X0Z");
62 aPlane->data()->setName("X0Y");
64 aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
67 void InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
69 std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
70 aPoint->real("x")->setValue(0.);
71 aPoint->real("y")->setValue(0.);
72 aPoint->real("z")->setValue(0.);
73 aPoint->data()->setName("Origin");
74 aPoint->setInHistory(aPoint, false); // don't show automatically created feature in the features history