Salome HOME
f12ea5dc6d3059230ddd7bc9498c957b2ddb4fe9
[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 #include <GeomAlgoAPI_Tools.h>
32 //--------------------------------------------------------------------------------------
33 #include <algorithm>
34
35 ExchangeAPI_Import::ExchangeAPI_Import(
36     const std::shared_ptr<ModelAPI_Feature> & theFeature)
37 : ModelHighAPI_Interface(theFeature)
38 {
39   initialize();
40 }
41
42 ExchangeAPI_Import::ExchangeAPI_Import(
43     const std::shared_ptr<ModelAPI_Feature> & theFeature,
44     const std::string & theFilePath)
45 : ModelHighAPI_Interface(theFeature)
46 {
47   if (initialize())
48     setFilePath(theFilePath);
49 }
50
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,
56     const bool theColor)
57 : ModelHighAPI_Interface(theFeature)
58 {
59   if (initialize())
60     setParameters(theFilePath, theScalInterUnits, theMaterials, theColor);
61 }
62
63 ExchangeAPI_Import::~ExchangeAPI_Import()
64 {
65
66 }
67
68 //--------------------------------------------------------------------------------------
69 void ExchangeAPI_Import::setParameters(const std::string & theFilePath,
70                                        const bool theScalInterUnits,
71                                        const bool theMaterials,
72                                        const bool theColor)
73 {
74   fillAttribute(theFilePath, mystepFilePath);
75   fillAttribute("STEP", myimportType);
76   fillAttribute(theScalInterUnits, myscalInterUnits);
77   fillAttribute(theMaterials,mymaterials);
78   fillAttribute(theColor,mycolors);
79   execute();
80 }
81
82 //--------------------------------------------------------------------------------------
83 void ExchangeAPI_Import::setFilePath(const std::string & theFilePath)
84 {
85
86   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFilePath);
87   if (anExtension == "STEP" || anExtension == "STP") {
88     setParameters(theFilePath,true,false,false);
89   } else {
90     fillAttribute(theFilePath, myfilePath);
91     fillAttribute(anExtension, myimportType);
92     execute();
93   }
94 }
95
96 //--------------------------------------------------------------------------------------
97 void ExchangeAPI_Import::dump(ModelHighAPI_Dumper& theDumper) const
98 {
99   FeaturePtr aBase = feature();
100   std::string aPartName = theDumper.name(aBase->document());
101
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")
107   {
108     aFilePath = aBase->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID())->value();
109   } else {
110     aFilePath = aBase->string(ExchangePlugin_ImportFeature::FILE_PATH_ID())->value();
111   }
112
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);
119     aPos += aTo.size();
120   }
121   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(aFilePath);
122   if (anExtension == "STP" || anExtension == "STEP"){
123       theDumper << aBase << " = model.addImportSTEP(" << aPartName << ", \""
124                 << aFilePath << "\"" ;
125
126       theDumper << ", " << scalInterUnits()->value()
127                 << ", " << materials()->value()
128                 << ", " << colors()->value() << ")"<< std::endl;
129   } else {
130       theDumper << aBase << " = model.addImport(" << aPartName << ", \""
131             << aFilePath << "\")" << std::endl;
132   }
133
134   // to make import have results
135   theDumper << "model.do()" << std::endl;
136
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));
145     }
146   }
147 }
148
149 //--------------------------------------------------------------------------------------
150 ImportPtr addImport(
151     const std::shared_ptr<ModelAPI_Document> & thePart,
152     const std::string & theFilePath)
153 {
154   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
155   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath));
156 }
157
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 )
164 {
165   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ExchangeAPI_Import::ID());
166   return ImportPtr(new ExchangeAPI_Import(aFeature, theFilePath,
167                                           theScalInterUnits, theMaterials, theColor));
168 }
169
170 void importPart(const std::shared_ptr<ModelAPI_Document> & thePart,
171                 const std::string & theFilePath,
172                 const ModelHighAPI_Reference & theAfterThis)
173 {
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);
179   }
180
181   FeaturePtr aFeature = thePart->addFeature(ExchangePlugin_ImportPart::ID());
182   aFeature->string(ExchangePlugin_ImportPart::FILE_PATH_ID())->setValue(theFilePath);
183
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;
198   }
199   aFeature->integer(ExchangePlugin_ImportPart::TARGET_PART_ID())->setValue(aTargetPartIndex);
200
201   // restart transaction to execute and delete the macro-feature
202   apply();
203
204   // restore current feature
205   if (aCurrentFeature)
206     thePart->setCurrentFeature(aCurrentFeature, THE_VISIBLE_FEATURE);
207 }