]> SALOME platform Git repositories - modules/shaper.git/blob - src/InitializationPlugin/InitializationPlugin_Plugin.cpp
Salome HOME
Issue #1834: Fix length of lines
[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_AttributeBoolean.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeString.h>
10 #include <ModelAPI_AttributeSelection.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Result.h>
13
14 #include <Events_Message.h>
15 #include <Events_InfoMessage.h>
16
17 #include <memory>
18
19 // the only created instance of this plugin
20 static InitializationPlugin_Plugin* MY_INITIALIZATIONPLUGIN_INSTANCE =
21     new InitializationPlugin_Plugin();
22
23 InitializationPlugin_Plugin::InitializationPlugin_Plugin()
24 {
25   Events_Loop* aLoop = Events_Loop::loop();
26   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
27   aLoop->registerListener(this, kDocCreatedEvent, NULL, true);
28 }
29
30 void InitializationPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
31 {
32   const Events_ID kDocCreatedEvent = ModelAPI_DocumentCreatedMessage::eventId();
33   if (theMessage->eventID() == kDocCreatedEvent) {
34     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::dynamic_pointer_cast<
35         ModelAPI_DocumentCreatedMessage>(theMessage);
36     DocumentPtr aDoc = aMessage->document();
37
38     /// Issue 431: for the current moment create planes only in the module document,
39     /// Later if it is needed we may create special initial planes in Parts (may be different)
40     if (aDoc != ModelAPI_Session::get()->moduleDocument())
41       return;
42
43     std::list<FeaturePtr> aFeatures;
44
45     // the viewer update should be blocked in order to avoid the features blinking before they are
46     // hidden
47     std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
48         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
49     Events_Loop::loop()->send(aMsg);
50
51     FeaturePtr aOrigin = createPoint(aDoc, "Origin", 0., 0., 0.);
52     aFeatures.push_back(aOrigin);
53     aFeatures.push_back(createAxis(aDoc, aOrigin, 100., 0., 0.));
54     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 100., 0.));
55     aFeatures.push_back(createAxis(aDoc, aOrigin, 0., 0., 100.));
56     aFeatures.push_back(createPlane(aDoc, 1., 0., 0.));
57     aFeatures.push_back(createPlane(aDoc, 0., -1., 0.));
58     aFeatures.push_back(createPlane(aDoc, 0., 0., 1.));
59     // for PartSet it is done outside of the transaction, so explicitly flush this creation
60     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
61
62     // hides the created features, the precondition is that the feature's results have been
63     // already built, so the createPlane/Points method calls the execute function for the planes
64     std::list<FeaturePtr >::const_iterator aFIter = aFeatures.begin();
65     for (; aFIter != aFeatures.cend(); aFIter++) {
66       FeaturePtr aPlane = *aFIter;
67       const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aPlane->results();
68       std::list<ResultPtr >::const_iterator aRIter = aResults.begin();
69       for (; aRIter != aResults.cend(); aRIter++) {
70         (*aRIter)->setDisplayed(false);
71       }
72     }
73     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
74     // the viewer update should be unblocked in order to avoid the features blinking before they are
75     // hidden
76     aMsg = std::shared_ptr<Events_Message>(
77                   new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
78
79     Events_Loop::loop()->send(aMsg);
80   }
81 }
82
83 FeaturePtr InitializationPlugin_Plugin::createPlane(DocumentPtr theDoc, double theX, double theY,
84                                                     double theZ)
85 {
86   FeaturePtr aPlane = theDoc->addFeature("Plane");
87   aPlane->string("creation_method")->setValue("by_general_equation");
88   aPlane->real("A")->setValue(theX);
89   aPlane->real("B")->setValue(theY);
90   aPlane->real("C")->setValue(theZ);
91   aPlane->real("D")->setValue(0.);
92
93   if (theX) {
94     aPlane->data()->setName("YOZ");
95   } else if (theY) {
96     aPlane->data()->setName("XOZ");
97   } else if (theZ) {
98     aPlane->data()->setName("XOY");
99   }
100     // don't show automatically created feature in the features history
101   aPlane->setInHistory(aPlane, false);
102
103   // the plane should be executed in order to build the feature result immediatelly
104   // the results are to be hidden in the plugin
105   aPlane->execute();
106   // this flag is needed here to avoid setting it inside of the next transaction
107   // (may cause crash on redo of the first transaction in OCAF)
108   aPlane->data()->execState(ModelAPI_StateDone);
109   aPlane->firstResult()->data()->execState(ModelAPI_StateDone);
110
111   return aPlane;
112 }
113
114 FeaturePtr InitializationPlugin_Plugin::createPoint(DocumentPtr theDoc, const std::string& theName,
115                                                     double theX, double theY, double theZ)
116 {
117   std::shared_ptr<ModelAPI_Feature> aPoint = theDoc->addFeature("Point");
118   //aPoint->string("creation_method")->setValue("by_xyz");
119   aPoint->real("x")->setValue(theX);
120   aPoint->real("y")->setValue(theY);
121   aPoint->real("z")->setValue(theZ);
122   aPoint->data()->setName(theName);
123   // don't show automatically created feature in the features history
124   aPoint->setInHistory(aPoint, false);
125
126   // the point should be executed in order to build the feature result immediatelly
127   // the results are to be hidden in the plugin
128   aPoint->execute();
129   aPoint->data()->execState(ModelAPI_StateDone);
130   aPoint->firstResult()->data()->execState(ModelAPI_StateDone);
131
132   return aPoint;
133 }
134
135 FeaturePtr InitializationPlugin_Plugin::createAxis(DocumentPtr theDoc, FeaturePtr theOrigin,
136                                                    double theX, double theY, double theZ)
137 {
138   std::shared_ptr<ModelAPI_Feature> aAxis = theDoc->addFeature("Axis");
139   aAxis->string("CreationMethod")->setValue("AxisByPointAndDirection");
140
141   ResultPtr aResult = theOrigin->firstResult();
142   aAxis->selection("FirstPoint")->setValue(aResult, aResult->shape());
143
144   aAxis->real("X_Direction")->setValue(theX);
145   aAxis->real("Y_Direction")->setValue(theY);
146   aAxis->real("Z_Direction")->setValue(theZ);
147
148   if (theX != 0) {
149     aAxis->data()->setName("OX");
150   } else if (theY != 0) {
151     aAxis->data()->setName("OY");
152   } else if (theZ != 0) {
153     aAxis->data()->setName("OZ");
154   }
155    // don't show automatically created feature in the features history
156   aAxis->setInHistory(aAxis, false); 
157   aAxis->execute();
158   aAxis->data()->execState(ModelAPI_StateDone);
159   aAxis->firstResult()->data()->execState(ModelAPI_StateDone);
160
161   return aAxis;
162 }