Salome HOME
Copyright update 2020
[modules/shaper.git] / src / ExchangeAPI / ExchangeAPI_Import.cpp
1 // Copyright (C) 2014-2020  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 <ModelAPI_AttributeStringArray.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 //--------------------------------------------------------------------------------------
32 #include <algorithm>
33
34 ExchangeAPI_Import::ExchangeAPI_Import(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : ModelHighAPI_Interface(theFeature)
37 {
38   initialize();
39 }
40
41 ExchangeAPI_Import::ExchangeAPI_Import(
42     const std::shared_ptr<ModelAPI_Feature> & theFeature,
43     const std::string & theFilePath)
44 : ModelHighAPI_Interface(theFeature)
45 {
46   if (initialize())
47     setFilePath(theFilePath);
48 }
49
50 ExchangeAPI_Import::~ExchangeAPI_Import()
51 {
52
53 }
54
55 //--------------------------------------------------------------------------------------
56 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
57 {
58   fillAttribute(theFilePath, myfilePath);
59
60   execute();
61 }
62
63 //--------------------------------------------------------------------------------------
64 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
65 {
66   FeaturePtr aBase = feature();
67   std::string aPartName = theDumper.name(aBase->document());
68
69   std::string aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
70   std::string aFrom = "\\";
71   std::string aTo = "\\\\";
72   for(std::size_t aPos = aFilePath.find(aFrom);
73       aPos != std::string::npos;
74       aPos = aFilePath.find(aFrom, aPos)) {
75     aFilePath.replace(aPos, aFrom.size(), aTo);
76     aPos += aTo.size();
77   }
78
79   theDumper << aBase << " = model.addImport(" << aPartName << ", \""
80             << aFilePath << "\")" << std::endl;
81   // to make import have results
82   theDumper << "model.do()" << std::endl;
83
84   CompositeFeaturePtr aCompositeFeature =
85     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
86   if(aCompositeFeature.get()) {
87     int aNbOfSubs = aCompositeFeature->numberOfSubs();
88     for(int anIndex = 0; anIndex < aNbOfSubs; ++anIndex) {
89       std::string aSubFeatureGet =
90         theDumper.name(aBase) + ".subFeature(" + std::to_string((long long)anIndex) + ")";
91       theDumper.dumpSubFeatureNameAndColor(aSubFeatureGet, aCompositeFeature->subFeature(anIndex));
92     }
93   }
94 }
95
96 //--------------------------------------------------------------------------------------
97 ImportPtr addImport(
98     const std::shared_ptr<ModelAPI_Document> & thePart,
99     const std::string & theFilePath)
100 {
101   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
102   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
103 }
104
105 void importPart(const std::shared_ptr<ModelAPI_Document> & thePart,
106                 const std::string & theFilePath,
107                 const ModelHighAPI_Reference & theAfterThis)
108 {
109   static const bool THE_VISIBLE_FEATURE = false;
110   FeaturePtr aCurrentFeature;
111   if (theAfterThis.feature()) {
112     aCurrentFeature = thePart->currentFeature(THE_VISIBLE_FEATURE);
113     thePart->setCurrentFeature(theAfterThis.feature(), THE_VISIBLE_FEATURE);
114   }
115
116   FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ImportPart::ID());
117   aFeature->string(ExchangePlugin_ImportPart::FILE_PATH_ID())->setValue(theFilePath);
118
119   // specify the ID of selected document
120   int aTargetPartIndex = 0;
121   SessionPtr aSession = ModelAPI_Session::get();
122   if (aSession->moduleDocument() == thePart) {
123     // Importing to PartSet has 2 choices: import directly to PartSet (if possible)
124     // or create a new part. Because then importing to existing part the document
125     // has to be specified explicitly.
126     // As a result, parse the list of possible target documents and generate new part
127     // if the import document is not applicable on PartSet level
128     // (there is no 'PartSet' in the list of applicable documents).
129     AttributeStringArrayPtr aDocsList =
130         aFeature->stringArray(ExchangePlugin_ImportPart::TARGET_PARTS_LIST_ID());
131     if (aDocsList->size() > 1 && aDocsList->value(1) == "PartSet")
132       aTargetPartIndex = 1;
133   }
134   aFeature->integer(ExchangePlugin_ImportPart::TARGET_PART_ID())->setValue(aTargetPartIndex);
135
136   // restart transaction to execute and delete the macro-feature
137   apply();
138
139   // restore current feature
140   if (aCurrentFeature)
141     thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE);
142 }