Salome HOME
Merge remote-tracking branch 'remotes/origin/HighLevelDump'
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Import.cpp
1 // Name   : ExchangeAPI_Import.cpp
2 // Purpose: 
3 //
4 // History:
5 // 07/06/16 - Sergey POKHODENKO - Creation of the file
6
7 //--------------------------------------------------------------------------------------
8 #include "ExchangeAPI_Import.h"
9 //--------------------------------------------------------------------------------------
10 #include <ModelHighAPI_Dumper.h>
11 #include <ModelHighAPI_Tools.h>
12 //--------------------------------------------------------------------------------------
13 #include <algorithm>
14
15 ExchangeAPI_Import::ExchangeAPI_Import(
16     const std::shared_ptr<ModelAPI_Feature> & theFeature)
17 : ModelHighAPI_Interface(theFeature)
18 {
19   initialize();
20 }
21
22 ExchangeAPI_Import::ExchangeAPI_Import(
23     const std::shared_ptr<ModelAPI_Feature> & theFeature,
24     const std::string & theFilePath)
25 : ModelHighAPI_Interface(theFeature)
26 {
27   if (initialize())
28     setFilePath(theFilePath);
29 }
30
31 ExchangeAPI_Import::~ExchangeAPI_Import()
32 {
33
34 }
35
36 //--------------------------------------------------------------------------------------
37 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
38 {
39   fillAttribute(theFilePath, myfilePath);
40
41   execute();
42 }
43
44 //--------------------------------------------------------------------------------------
45 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
46 {
47   FeaturePtr aBase = feature();
48   std::string aPartName = theDumper.name(aBase->document());
49
50   std::string aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
51   std::string aFrom = "\\";
52   std::string aTo = "\\\\";
53   for(std::size_t aPos = aFilePath.find(aFrom);
54       aPos != std::string::npos;
55       aPos = aFilePath.find(aFrom, aPos)) {
56     aFilePath.replace(aPos, aFrom.size(), aTo);
57     aPos += aTo.size();
58   }
59
60   theDumper << aBase << " = model.addImport(" << aPartName << ", \""
61             << aFilePath << "\")" << std::endl;
62   // to make import have results
63   theDumper << "model.do()" << std::endl;
64
65   CompositeFeaturePtr aCompositeFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
66   if(aCompositeFeature.get()) {
67     int aNbOfSubs = aCompositeFeature->numberOfSubs();
68     for(int anIndex = 0; anIndex < aNbOfSubs; ++anIndex) {
69       std::string aSubFeatureGet = theDumper.name(aBase) + ".subFeature(" + std::to_string((long long)anIndex) + ")";
70       theDumper.dumpSubFeatureNameAndColor(aSubFeatureGet, aCompositeFeature->subFeature(anIndex));
71     }
72   }
73 }
74
75 //--------------------------------------------------------------------------------------
76 ImportPtr addImport(
77     const std::shared_ptr<ModelAPI_Document> & thePart,
78     const std::string & theFilePath)
79 {
80   // TODO(spo): check that thePart is not empty
81   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
82   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
83 }