Salome HOME
bos #26444 Improve treatment of parameters
[modules/shaper.git] / src / ModelAPI / ModelAPI_Events.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModelAPI.h>
21 #include <ModelAPI_Events.h>
22
23 #include <GeomAPI_Pnt2d.h>
24 #include <GeomAPI_Shape.h>
25
26 //#define DEBUG_OBJECT_MOVED_MESSAGE
27 #ifdef DEBUG_OBJECT_MOVED_MESSAGE
28 #include <iostream>
29 #endif
30
31 ModelAPI_ObjectUpdatedMessage::ModelAPI_ObjectUpdatedMessage(const Events_ID theID,
32                                                              const void* theSender)
33     : Events_MessageGroup(theID, theSender)
34 {}
35
36 ModelAPI_ObjectUpdatedMessage::~ModelAPI_ObjectUpdatedMessage()
37 {}
38
39 ModelAPI_ObjectDeletedMessage::ModelAPI_ObjectDeletedMessage(const Events_ID theID,
40                                                              const void* theSender)
41     : Events_MessageGroup(theID, theSender)
42 {}
43
44 ModelAPI_ObjectDeletedMessage::~ModelAPI_ObjectDeletedMessage()
45 {}
46
47 ModelAPI_OrderUpdatedMessage::ModelAPI_OrderUpdatedMessage(const Events_ID theID,
48                                                            const void* theSender)
49     : Events_Message(theID, theSender)
50 {}
51
52 ModelAPI_OrderUpdatedMessage::~ModelAPI_OrderUpdatedMessage()
53 {}
54
55 // used by GUI only
56 // LCOV_EXCL_START
57 ModelAPI_FeatureStateMessage::ModelAPI_FeatureStateMessage(const Events_ID theID,
58                                                            const void* theSender)
59  : Events_Message(theID, theSender)
60 {
61   myCurrentFeature = std::shared_ptr<ModelAPI_Feature>();
62 }
63
64 ModelAPI_FeatureStateMessage::~ModelAPI_FeatureStateMessage()
65 {}
66
67 std::shared_ptr<ModelAPI_Feature> ModelAPI_FeatureStateMessage::feature() const
68 {
69   return myCurrentFeature;
70 }
71
72 void ModelAPI_FeatureStateMessage::setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature)
73 {
74   myCurrentFeature = theFeature;
75 }
76
77 bool ModelAPI_FeatureStateMessage::hasState(const std::string& theKey) const
78 {
79   return myFeatureState.find(theKey) != myFeatureState.end();
80 }
81
82 bool ModelAPI_FeatureStateMessage::state(const  std::string& theFeatureId, bool theDefault) const
83 {
84   if(hasState(theFeatureId)) {
85     return myFeatureState.at(theFeatureId);
86   }
87   return theDefault;
88 }
89
90 void ModelAPI_FeatureStateMessage::setState(const std::string& theFeatureId, bool theValue)
91 {
92   myFeatureState[theFeatureId] = theValue;
93 }
94
95 std::list<std::string> ModelAPI_FeatureStateMessage::features() const
96 {
97   std::list<std::string> result;
98   std::map<std::string, bool>::const_iterator it = myFeatureState.begin();
99   for( ; it != myFeatureState.end(); ++it) {
100     result.push_back(it->first);
101   }
102   return result;
103 }
104 // LCOV_EXCL_STOP
105
106 ModelAPI_DocumentCreatedMessage::ModelAPI_DocumentCreatedMessage(
107   const Events_ID theID, const void* theSender)
108 : Events_Message(theID, theSender)
109 {}
110
111 ModelAPI_DocumentCreatedMessage::~ModelAPI_DocumentCreatedMessage()
112 {}
113
114 DocumentPtr ModelAPI_DocumentCreatedMessage::document() const
115 {
116   return myDocument;
117 }
118
119 void ModelAPI_DocumentCreatedMessage::setDocument(DocumentPtr theDocument)
120 {
121   myDocument = theDocument;
122 }
123
124 ModelAPI_AttributeEvalMessage::ModelAPI_AttributeEvalMessage(
125   const Events_ID theID, const void* theSender)
126 : Events_Message(theID, theSender)
127 {}
128
129 ModelAPI_AttributeEvalMessage::~ModelAPI_AttributeEvalMessage()
130 {}
131
132 AttributePtr ModelAPI_AttributeEvalMessage::attribute() const
133 {
134   return myAttribute;
135 }
136
137 void ModelAPI_AttributeEvalMessage::setAttribute(AttributePtr theAttribute)
138 {
139   myAttribute = theAttribute;
140 }
141
142 ModelAPI_ParameterEvalMessage::ModelAPI_ParameterEvalMessage(
143   const Events_ID theID, const void* theSender)
144   : Events_Message(theID, theSender), myIsProcessed(false)
145 {}
146
147 ModelAPI_ParameterEvalMessage::~ModelAPI_ParameterEvalMessage()
148 {}
149
150 FeaturePtr ModelAPI_ParameterEvalMessage::parameter() const
151 {
152   return myParam;
153 }
154
155 void ModelAPI_ParameterEvalMessage::setParameter(FeaturePtr theParam)
156 {
157   myParam = theParam;
158 }
159
160 void ModelAPI_ParameterEvalMessage::setResults(
161     const std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
162     const double theResult, const std::string& theError)
163 {
164   myParamsList = theParamsList;
165   myResult = theResult;
166   myError = theError;
167   myIsProcessed = true;
168 }
169
170 bool ModelAPI_ParameterEvalMessage::isProcessed()
171 {
172   return myIsProcessed;
173 }
174
175 const std::list<std::shared_ptr<ModelAPI_ResultParameter> >&
176   ModelAPI_ParameterEvalMessage::params() const
177 {
178   return myParamsList;
179 }
180
181 const double& ModelAPI_ParameterEvalMessage::result() const
182 {
183   return myResult;
184 }
185
186 const std::string& ModelAPI_ParameterEvalMessage::error() const
187 {
188   return myError;
189 }
190
191 /// Creates an empty message
192 ModelAPI_ImportParametersMessage::ModelAPI_ImportParametersMessage(const Events_ID theID, const void* theSender)
193   :Events_Message(theID, theSender)
194 {
195
196 }
197
198 ModelAPI_ImportParametersMessage::~ModelAPI_ImportParametersMessage()
199 {
200 }
201
202 std::string ModelAPI_ImportParametersMessage::filename() const
203 {
204   return myFilename;
205 }
206
207 void ModelAPI_ImportParametersMessage::setFilename(std::string theFilename)
208 {
209   myFilename = theFilename;
210 }
211
212 ModelAPI_BuildEvalMessage::ModelAPI_BuildEvalMessage(
213   const Events_ID theID, const void* theSender)
214   : Events_Message(theID, theSender), myIsProcessed(false)
215 {}
216
217 ModelAPI_BuildEvalMessage::~ModelAPI_BuildEvalMessage()
218 {}
219
220 FeaturePtr ModelAPI_BuildEvalMessage::parameter() const
221 {
222   return myParam;
223 }
224
225 void ModelAPI_BuildEvalMessage::setParameter(FeaturePtr theParam)
226 {
227   myParam = theParam;
228 }
229
230 void ModelAPI_BuildEvalMessage::setResults(
231             const std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
232             const std::string& theError)
233 {
234   myParamsList = theParamsList;
235   myError = theError;
236   myIsProcessed = true;
237 }
238
239 const std::list<std::shared_ptr<ModelAPI_ResultParameter> >&
240   ModelAPI_BuildEvalMessage::params() const
241 {
242   return myParamsList;
243 }
244
245 bool ModelAPI_BuildEvalMessage::isProcessed()
246 {
247   return myIsProcessed;
248 }
249
250 const std::string& ModelAPI_BuildEvalMessage::error() const
251 {
252   return myError;
253 }
254
255 ModelAPI_ComputePositionsMessage::ModelAPI_ComputePositionsMessage(
256   const Events_ID theID, const void* theSender)
257   : Events_Message(theID, theSender)
258 {}
259
260 ModelAPI_ComputePositionsMessage::~ModelAPI_ComputePositionsMessage()
261 {}
262
263 const std::wstring& ModelAPI_ComputePositionsMessage::expression() const
264 {
265   return myExpression;
266 }
267
268 const std::wstring& ModelAPI_ComputePositionsMessage::parameter() const
269 {
270   return myParamName;
271 }
272
273 void ModelAPI_ComputePositionsMessage::set(
274   const std::wstring& theExpression, const std::wstring& theParameter)
275 {
276   myExpression = theExpression;
277   myParamName = theParameter;
278 }
279
280 void ModelAPI_ComputePositionsMessage::setPositions(
281   const std::list<std::pair<int, int> >& thePositions)
282 {
283   myPositions = thePositions;
284 }
285
286 const std::list<std::pair<int, int> >& ModelAPI_ComputePositionsMessage::positions() const
287 {
288   return myPositions;
289 }
290
291
292 ModelAPI_ObjectRenamedMessage::ModelAPI_ObjectRenamedMessage(const Events_ID theID,
293                                                              const void* theSender)
294 : Events_Message(theID, theSender)
295 {}
296
297 ModelAPI_ObjectRenamedMessage::~ModelAPI_ObjectRenamedMessage()
298 {}
299
300 void ModelAPI_ObjectRenamedMessage::send(ObjectPtr theObject,
301                                          const std::wstring& theOldName,
302                                          const std::wstring& theNewName,
303                                          const void* theSender)
304 {
305   std::shared_ptr<ModelAPI_ObjectRenamedMessage> aMessage(
306     new ModelAPI_ObjectRenamedMessage(eventId(), theSender));
307   aMessage->setObject(theObject);
308   aMessage->setOldName(theOldName);
309   aMessage->setNewName(theNewName);
310   Events_Loop::loop()->send(aMessage);
311 }
312
313 ObjectPtr ModelAPI_ObjectRenamedMessage::object() const
314 {
315   return myObject;
316 }
317
318 void ModelAPI_ObjectRenamedMessage::setObject(ObjectPtr theObject)
319 {
320   myObject = theObject;
321 }
322
323 std::wstring ModelAPI_ObjectRenamedMessage::oldName() const
324 {
325   return myOldName;
326 }
327
328 void ModelAPI_ObjectRenamedMessage::setOldName(const std::wstring& theOldName)
329 {
330   myOldName = theOldName;
331 }
332
333 std::wstring ModelAPI_ObjectRenamedMessage::newName() const
334 {
335   return myNewName;
336 }
337
338 void ModelAPI_ObjectRenamedMessage::setNewName(const std::wstring& theNewName)
339 {
340   myNewName = theNewName;
341 }
342
343 ModelAPI_ReplaceParameterMessage::ModelAPI_ReplaceParameterMessage(const Events_ID theID,
344                                                                    const void* theSender)
345 : Events_Message(theID, theSender)
346 {}
347
348 ModelAPI_ReplaceParameterMessage::~ModelAPI_ReplaceParameterMessage()
349 {}
350
351 void ModelAPI_ReplaceParameterMessage::send(ObjectPtr theObject,
352                                             const void* theSender)
353 {
354   std::shared_ptr<ModelAPI_ReplaceParameterMessage> aMessage(
355       new ModelAPI_ReplaceParameterMessage(eventId(), theSender));
356   aMessage->setObject(theObject);
357   Events_Loop::loop()->send(aMessage);
358 }
359
360 ObjectPtr ModelAPI_ReplaceParameterMessage::object() const
361 {
362   return myObject;
363 }
364
365 void ModelAPI_ReplaceParameterMessage::setObject(ObjectPtr theObject)
366 {
367   myObject = theObject;
368 }
369
370
371 // =====   ModelAPI_SolverFailedMessage   =====
372 ModelAPI_SolverFailedMessage::ModelAPI_SolverFailedMessage(const Events_ID theID,
373                                                            const void* theSender)
374   : Events_Message(theID, theSender),
375     myDOF(-1)
376 {}
377
378 ModelAPI_SolverFailedMessage::~ModelAPI_SolverFailedMessage()
379 {}
380
381 void ModelAPI_SolverFailedMessage::setObjects(const std::set<ObjectPtr>& theObjects)
382 {
383   myObjects = theObjects;
384 }
385
386 const std::set<ObjectPtr>& ModelAPI_SolverFailedMessage::objects() const
387 {
388   return myObjects;
389 }
390
391
392 // =====   ModelAPI_ObjectMovedMessage   =====
393 ModelAPI_ObjectMovedMessage::ModelAPI_ObjectMovedMessage(const void* theSender)
394   : Events_Message(Events_Loop::eventByName(EVENT_OBJECT_MOVED), theSender)
395 {}
396
397 void ModelAPI_ObjectMovedMessage::setMovedObject(const ObjectPtr& theMovedObject)
398 {
399   myMovedObject = theMovedObject;
400   myMovedAttribute = AttributePtr();
401 }
402
403 void ModelAPI_ObjectMovedMessage::setMovedAttribute(const AttributePtr& theMovedAttribute,
404                                                     const int thePointIndex)
405 {
406   myMovedAttribute = theMovedAttribute;
407   myMovedObject = ObjectPtr();
408   myMovedPointIndex = thePointIndex;
409 }
410
411 void ModelAPI_ObjectMovedMessage::setOriginalPosition(double theX, double theY)
412 {
413   myOriginalPosition = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
414 #ifdef DEBUG_OBJECT_MOVED_MESSAGE
415   std::cout << "setOriginalPosition: " << myOriginalPosition->x() << ", "
416                                        << myOriginalPosition->y() << std::endl;
417 #endif
418 }
419
420 void ModelAPI_ObjectMovedMessage::setOriginalPosition(
421     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
422 {
423   myOriginalPosition = thePoint;
424 #ifdef DEBUG_OBJECT_MOVED_MESSAGE
425   std::cout << "setOriginalPosition: " << myOriginalPosition->x() << ", "
426                                        << myOriginalPosition->y() << std::endl;
427 #endif
428 }
429
430 void ModelAPI_ObjectMovedMessage::setCurrentPosition(double theX, double theY)
431 {
432   myCurrentPosition = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
433 #ifdef DEBUG_OBJECT_MOVED_MESSAGE
434   std::cout << "setCurrentPosition: " << myCurrentPosition->x() << ", " << myCurrentPosition->y()
435             << ", myCurrentPosition - myOriginalPosition: "
436             << myCurrentPosition->x() - myOriginalPosition->x() << ", "
437             << myCurrentPosition->y() - myOriginalPosition->y() << std::endl;
438 #endif
439 }
440
441 void ModelAPI_ObjectMovedMessage::setCurrentPosition(
442     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
443 {
444   myCurrentPosition = thePoint;
445 #ifdef DEBUG_OBJECT_MOVED_MESSAGE
446   std::cout << "setCurrentPosition: " << myCurrentPosition->x() << ", " << myCurrentPosition->y()
447             << ", myCurrentPosition - myOriginalPosition: "
448             << myCurrentPosition->x() - myOriginalPosition->x() << ", "
449             << myCurrentPosition->y() - myOriginalPosition->y() << std::endl;
450 #endif
451 }
452
453
454 // =====   ModelAPI_ShapesFailedMessage   =====
455 ModelAPI_ShapesFailedMessage::ModelAPI_ShapesFailedMessage(const Events_ID theID,
456                                                                const void* theSender)
457   : Events_Message(theID, theSender)
458 {}
459
460 ModelAPI_ShapesFailedMessage::~ModelAPI_ShapesFailedMessage()
461 {}
462
463 void ModelAPI_ShapesFailedMessage::setShapes(const ListOfShape& theShapes)
464 {
465   myShapes = theShapes;
466 }
467
468 const ListOfShape& ModelAPI_ShapesFailedMessage::shapes() const
469 {
470   return myShapes;
471 }