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