Salome HOME
e585f971e036d7b379c9f96b1eaf44f916556329
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Import.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 "ExchangeAPI_Import.h"
21 //--------------------------------------------------------------------------------------
22 #include <ExchangePlugin_ImportPart.h>
23 //--------------------------------------------------------------------------------------
24 #include <ModelHighAPI_Dumper.h>
25 #include <ModelHighAPI_Services.h>
26 #include <ModelHighAPI_Tools.h>
27 //--------------------------------------------------------------------------------------
28 #include <algorithm>
29
30 ExchangeAPI_Import::ExchangeAPI_Import(
31     const std::shared_ptr<ModelAPI_Feature> & theFeature)
32 : ModelHighAPI_Interface(theFeature)
33 {
34   initialize();
35 }
36
37 ExchangeAPI_Import::ExchangeAPI_Import(
38     const std::shared_ptr<ModelAPI_Feature> & theFeature,
39     const std::string & theFilePath)
40 : ModelHighAPI_Interface(theFeature)
41 {
42   if (initialize())
43     setFilePath(theFilePath);
44 }
45
46 ExchangeAPI_Import::~ExchangeAPI_Import()
47 {
48
49 }
50
51 //--------------------------------------------------------------------------------------
52 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
53 {
54   fillAttribute(theFilePath, myfilePath);
55
56   execute();
57 }
58
59 //--------------------------------------------------------------------------------------
60 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
61 {
62   FeaturePtr aBase = feature();
63   std::string aPartName = theDumper.name(aBase->document());
64
65   std::string aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
66   std::string aFrom = "\\";
67   std::string aTo = "\\\\";
68   for(std::size_t aPos = aFilePath.find(aFrom);
69       aPos != std::string::npos;
70       aPos = aFilePath.find(aFrom, aPos)) {
71     aFilePath.replace(aPos, aFrom.size(), aTo);
72     aPos += aTo.size();
73   }
74
75   theDumper << aBase << " = model.addImport(" << aPartName << ", \""
76             << aFilePath << "\")" << std::endl;
77   // to make import have results
78   theDumper << "model.do()" << std::endl;
79
80   CompositeFeaturePtr aCompositeFeature =
81     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
82   if(aCompositeFeature.get()) {
83     int aNbOfSubs = aCompositeFeature->numberOfSubs();
84     for(int anIndex = 0; anIndex < aNbOfSubs; ++anIndex) {
85       std::string aSubFeatureGet =
86         theDumper.name(aBase) + ".subFeature(" + std::to_string((long long)anIndex) + ")";
87       theDumper.dumpSubFeatureNameAndColor(aSubFeatureGet, aCompositeFeature->subFeature(anIndex));
88     }
89   }
90 }
91
92 //--------------------------------------------------------------------------------------
93 ImportPtr addImport(
94     const std::shared_ptr<ModelAPI_Document> & thePart,
95     const std::string & theFilePath)
96 {
97   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
98   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
99 }
100
101 void importPart(const std::shared_ptr<ModelAPI_Document> & thePart,
102                 const std::string & theFilePath,
103                 const ModelHighAPI_Reference & theAfterThis)
104 {
105   static const bool THE_VISIBLE_FEATURE = false;
106   FeaturePtr aCurrentFeature;
107   if (theAfterThis.feature()) {
108     aCurrentFeature = thePart->currentFeature(THE_VISIBLE_FEATURE);
109     thePart->setCurrentFeature(theAfterThis.feature(), THE_VISIBLE_FEATURE);
110   }
111
112   FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ImportPart::ID());
113   aFeature->string(ExchangePlugin_ImportPart::FILE_PATH_ID())->setValue(theFilePath);
114   // restart transaction to execute and delete the macro-feature
115   apply();
116
117   // restore current feature
118   if (aCurrentFeature)
119     thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE);
120 }