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