1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 //--------------------------------------------------------------------------------------
34 ExchangeAPI_Import::ExchangeAPI_Import(
35 const std::shared_ptr<ModelAPI_Feature> & theFeature)
36 : ModelHighAPI_Interface(theFeature)
41 ExchangeAPI_Import::ExchangeAPI_Import(
42 const std::shared_ptr<ModelAPI_Feature> & theFeature,
43 const std::string & theFilePath)
44 : ModelHighAPI_Interface(theFeature)
47 setFilePath(theFilePath);
50 ExchangeAPI_Import::~ExchangeAPI_Import()
55 //--------------------------------------------------------------------------------------
56 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
58 fillAttribute(theFilePath, myfilePath);
63 //--------------------------------------------------------------------------------------
64 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
66 FeaturePtr aBase = feature();
67 std::string aPartName = theDumper.name(aBase->document());
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);
79 theDumper << aBase << " = model.addImport(" << aPartName << ", \""
80 << aFilePath << "\")" << std::endl;
81 // to make import have results
82 theDumper << "model.do()" << std::endl;
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));
96 //--------------------------------------------------------------------------------------
98 const std::shared_ptr<ModelAPI_Document> & thePart,
99 const std::string & theFilePath)
101 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
102 return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
105 void importPart(const std::shared_ptr<ModelAPI_Document> & thePart,
106 const std::string & theFilePath,
107 const ModelHighAPI_Reference & theAfterThis)
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);
116 FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ImportPart::ID());
117 aFeature->string(ExchangePlugin_ImportPart::FILE_PATH_ID())->setValue(theFilePath);
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;
134 aFeature->integer(ExchangePlugin_ImportPart::TARGET_PART_ID())->setValue(aTargetPartIndex);
136 // restart transaction to execute and delete the macro-feature
139 // restore current feature
141 thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE);