1 // Copyright (C) 2014-2024 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
33 //--------------------------------------------------------------------------------------
34 std::shared_ptr<ModelAPI_Document> moduleDocument()
36 return ModelAPI_Session::get()->moduleDocument();
39 //--------------------------------------------------------------------------------------
40 std::shared_ptr<ModelAPI_Document> activeDocument()
42 return ModelAPI_Session::get()->activeDocument();
45 //--------------------------------------------------------------------------------------
46 std::shared_ptr<GeomAPI_Ax3> defaultPlane( const std::wstring& theName )
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));
61 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(o, x, n));
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)
68 static const double aTol = 1.e-10;
70 if (fabs(theOrigin->x()) > aTol || fabs(theOrigin->y()) > aTol || fabs(theOrigin->z()) > aTol)
71 return std::wstring();
74 if (fabs(theNormal->x()) < aTol &&
75 fabs(theDirX->x() - 1.0) < aTol && fabs(theDirX->y()) < aTol && fabs(theDirX->z()) < aTol) {
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");
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");
89 return std::wstring();
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));
99 //--------------------------------------------------------------------------------------
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;
110 std::ostringstream aTransactionName;
111 aTransactionName << "Operation_" << aTransactionID;
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
119 ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>
120 (aSession->moduleDocument()->object(ModelAPI_ResultPart::group(), 0));
121 if (aPartRes.get() && aPartRes->isActivated())
123 DocumentPtr aPartDoc = aPartRes->partDoc();
124 if (aPartDoc.get() && aPartDoc->size(ModelAPI_Feature::group()) == 0) // no features in part
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();
138 aSession->startOperation(aTransactionName.str());
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");
147 FeaturePtr aLastFeat =
148 std::dynamic_pointer_cast<ModelAPI_Feature>(anActive->object("Features", aSize - 1));
149 anActive->setCurrentFeature(aLastFeat, true);
152 ModelAPI_Session::get()->finishOperation();
153 // to update data tree in the end of dumped script execution
154 ModelAPI_EventCreator::get()->sendReordered(FeaturePtr());
158 auto aSession = ModelAPI_Session::get();
159 aSession->finishOperation();
160 // start the next operation
164 void updateFeatures()
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));
171 //--------------------------------------------------------------------------------------
174 ModelAPI_Session::get()->undo();
178 ModelAPI_Session::get()->redo();
181 //--------------------------------------------------------------------------------------
184 ModelAPI_Session::get()->closeAll();
187 //--------------------------------------------------------------------------------------