Salome HOME
Make initialization plugin:
[modules/shaper.git] / src / InitializationPlugin / InitializationPlugin_Plugin.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <InitializationPlugin_Plugin.h>
4
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
11 #include <Events_Message.h>
12 #include <Events_Error.h>
13
14 #include <memory>
15
16 // the only created instance of this plugin
17 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE = new InitializationPlugin_Plugin();
18
19 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
20 {
21   Events_Loop* aLoop = Events_Loop::loop();
22   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
23   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
24 }
25
26 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
27 {
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();
33     createPoint(aDoc);
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()) {
40     Events_Error::send(
41       std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ") + 
42       theMessage->eventID().eventText());
43   }
44 }
45
46 void InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theA, double theB, double theC)
47 {
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.);
54
55   if (theA) {
56     aPlane->data()->setName("Y0Z");
57   } else if (theB) {
58     aPlane->data()->setName("X0Z");
59   } else if (theC) {
60     aPlane->data()->setName("X0Y");
61   }
62   aPlane->setInHistory(aPlane, false); // don't show automatically created feature in the features history
63 }
64
65 void InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
66 {
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
73 }