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