Salome HOME
Update copyrights
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Services.cpp
1 // Copyright (C) 2014-2019  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 "ModelHighAPI_Services.h"
21 //--------------------------------------------------------------------------------------
22 #include <GeomAPI_Ax3.h>
23 #include <GeomAPI_Pnt.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Events.h>
28
29 #include <cmath>
30
31 //--------------------------------------------------------------------------------------
32 std::shared_ptr<ModelAPI_Document> moduleDocument()
33 {
34   return ModelAPI_Session::get()->moduleDocument();
35 }
36
37 //--------------------------------------------------------------------------------------
38 std::shared_ptr<ModelAPI_Document> activeDocument()
39 {
40   return ModelAPI_Session::get()->activeDocument();
41 }
42
43 //--------------------------------------------------------------------------------------
44 std::shared_ptr<GeomAPI_Ax3> defaultPlane( const std::string& theName )
45 {
46   std::shared_ptr<GeomAPI_Pnt> o(new GeomAPI_Pnt(0, 0, 0));
47   std::shared_ptr<GeomAPI_Dir> n, x;
48   if (theName == "XOY") {
49       n.reset(new GeomAPI_Dir(0, 0, 1));
50       x.reset(new GeomAPI_Dir(1, 0, 0));
51   } else if (theName == "XOZ") {
52       n.reset(new GeomAPI_Dir(0, -1, 0));
53       x.reset(new GeomAPI_Dir(1, 0, 0));
54   } else if (theName == "YOZ") {
55       n.reset(new GeomAPI_Dir(1, 0, 0));
56       x.reset(new GeomAPI_Dir(0, 1, 0));
57   }
58
59   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(o, x, n));
60 }
61
62 std::string defaultPlane(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
63                          const std::shared_ptr<GeomAPI_Dir>& theNormal,
64                          const std::shared_ptr<GeomAPI_Dir>& theDirX)
65 {
66   static const double aTol = 1.e-10;
67
68   if (fabs(theOrigin->x()) > aTol || fabs(theOrigin->y()) > aTol || fabs(theOrigin->z()) > aTol)
69     return std::string();
70
71   // XOY or XOZ
72   if (fabs(theNormal->x()) < aTol &&
73       fabs(theDirX->x() - 1.0) < aTol && fabs(theDirX->y()) < aTol && fabs(theDirX->z()) < aTol) {
74     // XOY
75     if (fabs(theNormal->y()) < aTol && fabs(theNormal->z() - 1.0) < aTol)
76       return std::string("XOY");
77     else if (fabs(theNormal->y() + 1.0) < aTol && fabs(theNormal->z()) < aTol)
78       return std::string("XOZ");
79   }
80   // YOZ
81   else if (fabs(theNormal->x() - 1.0) < aTol &&
82            fabs(theNormal->y()) < aTol && fabs(theNormal->z()) < aTol &&
83            fabs(theDirX->x()) < aTol && fabs(theDirX->y() - 1.0) < aTol &&
84            fabs(theDirX->z()) < aTol)
85     return std::string("YOZ");
86
87   return std::string();
88 }
89
90 std::shared_ptr<ModelAPI_Result> standardPlane(const std::string & theName){
91   DocumentPtr aPartSet = ModelAPI_Session::get()->moduleDocument();
92   // searching for the construction element
93   return std::dynamic_pointer_cast<ModelAPI_Result>(
94     aPartSet->objectByName(ModelAPI_ResultConstruction::group(), theName));
95 }
96
97 //--------------------------------------------------------------------------------------
98 void begin()
99 {
100   ModelAPI_Session::get()->startOperation();
101 }
102
103 void end()
104 {
105   // some operations make the current feature not the last one (like "galeries" change parameters)
106   DocumentPtr anActive = ModelAPI_Session::get()->activeDocument();
107   int aSize = anActive->size("Features");
108   if (aSize > 0) {
109     FeaturePtr aLastFeat =
110       std::dynamic_pointer_cast<ModelAPI_Feature>(anActive->object("Features", aSize - 1));
111     anActive->setCurrentFeature(aLastFeat, true);
112   }
113
114   ModelAPI_Session::get()->finishOperation();
115   // to update data tree in the end of dumped script execution
116   ModelAPI_EventCreator::get()->sendReordered(FeaturePtr());
117 }
118 void apply()
119 {
120   auto aSession = ModelAPI_Session::get();
121   aSession->finishOperation();
122   aSession->startOperation();
123 }
124
125 void updateFeatures()
126 {
127   Events_Loop* aLoop = Events_Loop::loop();
128   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
129   aLoop->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
130 }
131
132 //--------------------------------------------------------------------------------------
133 void undo()
134 {
135   ModelAPI_Session::get()->undo();
136 }
137 void redo()
138 {
139   ModelAPI_Session::get()->redo();
140 }
141
142 //--------------------------------------------------------------------------------------
143 void reset()
144 {
145   ModelAPI_Session::get()->closeAll();
146 }
147
148 //--------------------------------------------------------------------------------------