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