]> SALOME platform Git repositories - modules/shaper.git/blob - src/InitializationPlugin/InitializationPlugin_Plugin.cpp
Salome HOME
Code documentation and formating for 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 =
18     new InitializationPlugin_Plugin();
19
20 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
21 {
22   Events_Loop* aLoop = Events_Loop::loop();
23   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
24   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
25 }
26
27 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
28 {
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();
34     createPoint(aDoc);
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()) {
41     Events_Error::send(
42         std::string("InitializationPlugin_Plugin::processEvent: unhandled message caught: ")
43             + theMessage->eventID().eventText());
44   }
45 }
46
47 void InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
48                                               double theZ)
49 {
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.);
56
57   if (theX) {
58     aPlane->data()->setName("Y0Z");
59   } else if (theY) {
60     aPlane->data()->setName("X0Z");
61   } else if (theZ) {
62     aPlane->data()->setName("X0Y");
63   }
64   aPlane->setInHistory(aPlane, false);  // don't show automatically created feature in the features history
65 }
66
67 void InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc)
68 {
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
75 }