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