Salome HOME
Test case for Pipe naming (issue #17281)
[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 <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Tools.h>
24 //--------------------------------------------------------------------------------------
25 #include <algorithm>
26
27 ExchangeAPI_Import::ExchangeAPI_Import(
28     const std::shared_ptr<ModelAPI_Feature> & theFeature)
29 : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
34 ExchangeAPI_Import::ExchangeAPI_Import(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature,
36     const std::string & theFilePath)
37 : ModelHighAPI_Interface(theFeature)
38 {
39   if (initialize())
40     setFilePath(theFilePath);
41 }
42
43 ExchangeAPI_Import::~ExchangeAPI_Import()
44 {
45
46 }
47
48 //--------------------------------------------------------------------------------------
49 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
50 {
51   fillAttribute(theFilePath, myfilePath);
52
53   execute();
54 }
55
56 //--------------------------------------------------------------------------------------
57 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
58 {
59   FeaturePtr aBase = feature();
60   std::string aPartName = theDumper.name(aBase->document());
61
62   std::string aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
63   std::string aFrom = "\\";
64   std::string aTo = "\\\\";
65   for(std::size_t aPos = aFilePath.find(aFrom);
66       aPos != std::string::npos;
67       aPos = aFilePath.find(aFrom, aPos)) {
68     aFilePath.replace(aPos, aFrom.size(), aTo);
69     aPos += aTo.size();
70   }
71
72   theDumper << aBase << " = model.addImport(" << aPartName << ", \""
73             << aFilePath << "\")" << std::endl;
74   // to make import have results
75   theDumper << "model.do()" << std::endl;
76
77   CompositeFeaturePtr aCompositeFeature =
78     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
79   if(aCompositeFeature.get()) {
80     int aNbOfSubs = aCompositeFeature->numberOfSubs();
81     for(int anIndex = 0; anIndex < aNbOfSubs; ++anIndex) {
82       std::string aSubFeatureGet =
83         theDumper.name(aBase) + ".subFeature(" + std::to_string((long long)anIndex) + ")";
84       theDumper.dumpSubFeatureNameAndColor(aSubFeatureGet, aCompositeFeature->subFeature(anIndex));
85     }
86   }
87 }
88
89 //--------------------------------------------------------------------------------------
90 ImportPtr addImport(
91     const std::shared_ptr<ModelAPI_Document> & thePart,
92     const std::string & theFilePath)
93 {
94   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
95   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
96 }