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 #include <GeomAlgoAPI_Tools.h>
32 //--------------------------------------------------------------------------------------
35 ExchangeAPI_Import::ExchangeAPI_Import(
36 const std::shared_ptr<ModelAPI_Feature> & theFeature)
37 : ModelHighAPI_Interface(theFeature)
42 ExchangeAPI_Import::ExchangeAPI_Import(
43 const std::shared_ptr<ModelAPI_Feature> & theFeature,
44 const std::string & theFilePath)
45 : ModelHighAPI_Interface(theFeature)
48 setFilePath(theFilePath);
51 ExchangeAPI_Import::ExchangeAPI_Import(
52 const std::shared_ptr<ModelAPI_Feature> & theFeature,
53 const std::string & theFilePath,
54 const bool theScalInterUnits,
55 const bool theMaterials,
57 : ModelHighAPI_Interface(theFeature)
60 setParameters(theFilePath, theScalInterUnits, theMaterials, theColor);
63 ExchangeAPI_Import::~ExchangeAPI_Import()
68 //--------------------------------------------------------------------------------------
69 void ExchangeAPI_Import::setParameters(const std::string & theFilePath,
70 const bool theScalInterUnits,
71 const bool theMaterials,
74 fillAttribute(theFilePath, mystepFilePath);
75 fillAttribute("STEP", myimportType);
76 fillAttribute(theScalInterUnits, myscalInterUnits);
77 fillAttribute(theMaterials,mymaterials);
78 fillAttribute(theColor,mycolors);
82 //--------------------------------------------------------------------------------------
83 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
86 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFilePath);
87 if (anExtension == "STEP" || anExtension == "STP") {
88 setParameters(theFilePath,true,false,false);
90 fillAttribute(theFilePath, myfilePath);
91 fillAttribute(anExtension, myimportType);
96 //--------------------------------------------------------------------------------------
97 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
99 FeaturePtr aBase = feature();
100 std::string aPartName = theDumper.name(aBase->document());
102 AttributeStringPtr aImportTypeAttr =
103 aBase->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
104 std::string aFormat = aImportTypeAttr->value();
105 std::string aFilePath;
106 if (aFormat == "STEP" || aFormat == "STP")
108 aFilePath = aBase->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID())->value();
110 aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
113 std::string aFrom = "\\";
114 std::string aTo = "\\\\";
115 for(std::size_t aPos = aFilePath.find(aFrom);
116 aPos != std::string::npos;
117 aPos = aFilePath.find(aFrom, aPos)) {
118 aFilePath.replace(aPos, aFrom.size(), aTo);
121 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
122 if (anExtension == "STP" || anExtension == "STEP"){
123 theDumper << aBase << " = model.addImportSTEP(" << aPartName << ", \""
124 << aFilePath << "\"" ;
126 theDumper << ", " << scalInterUnits()->value()
127 << ", " << materials()->value()
128 << ", " << colors()->value() << ")"<< std::endl;
130 theDumper << aBase << " = model.addImport(" << aPartName << ", \""
131 << aFilePath << "\")" << std::endl;
134 // to make import have results
135 theDumper << "model.do()" << std::endl;
137 CompositeFeaturePtr aCompositeFeature =
138 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aBase);
139 if (aCompositeFeature.get()) {
140 int aNbOfSubs = aCompositeFeature->numberOfSubs();
141 for(int anIndex = 0; anIndex < aNbOfSubs; ++anIndex) {
142 std::string aSubFeatureGet =
143 theDumper.name(aBase) + ".subFeature(" + std::to_string((long long)anIndex) + ")";
144 theDumper.dumpSubFeatureNameAndColor(aSubFeatureGet, aCompositeFeature->subFeature(anIndex));
149 //--------------------------------------------------------------------------------------
151 const std::shared_ptr<ModelAPI_Document> & thePart,
152 const std::string & theFilePath)
154 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
155 return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
158 ImportPtr addImportSTEP(
159 const std::shared_ptr<ModelAPI_Document> & thePart,
160 const std::string & theFilePath,
161 const bool theScalInterUnits,
162 const bool theMaterials,
163 const bool theColor )
165 std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
166 return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath,
167 theScalInterUnits, theMaterials, theColor));
170 void importPart(const std::shared_ptr<ModelAPI_Document> & thePart,
171 const std::string & theFilePath,
172 const ModelHighAPI_Reference & theAfterThis)
174 static const bool THE_VISIBLE_FEATURE = false;
175 FeaturePtr aCurrentFeature;
176 if (theAfterThis.feature()) {
177 aCurrentFeature = thePart->currentFeature(THE_VISIBLE_FEATURE);
178 thePart->setCurrentFeature(theAfterThis.feature(), THE_VISIBLE_FEATURE);
181 FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ImportPart::ID());
182 aFeature->string(ExchangePlugin_ImportPart::FILE_PATH_ID())->setValue(theFilePath);
184 // specify the ID of selected document
185 int aTargetPartIndex = 0;
186 SessionPtr aSession = ModelAPI_Session::get();
187 if (aSession->moduleDocument() == thePart) {
188 // Importing to PartSet has 2 choices: import directly to PartSet (if possible)
189 // or create a new part. Because then importing to existing part the document
190 // has to be specified explicitly.
191 // As a result, parse the list of possible target documents and generate new part
192 // if the import document is not applicable on PartSet level
193 // (there is no 'PartSet' in the list of applicable documents).
194 AttributeStringArrayPtr aDocsList =
195 aFeature->stringArray(ExchangePlugin_ImportPart::TARGET_PARTS_LIST_ID());
196 if (aDocsList->size() > 1 && aDocsList->value(1) == "PartSet")
197 aTargetPartIndex = 1;
199 aFeature->integer(ExchangePlugin_ImportPart::TARGET_PART_ID())->setValue(aTargetPartIndex);
201 // restart transaction to execute and delete the macro-feature
204 // restore current feature
206 thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE);