]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_STEPImportXCAF.cpp
Salome HOME
Copyright update 2021
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STEPImportXCAF.cpp
1 // Copyright (C) 2014-2021  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 <GeomAlgoAPI_STEPImportXCAF.h>
21
22 #include <BRep_Builder.hxx>
23
24 #include <Interface_EntityIterator.hxx>
25 #include <Interface_Graph.hxx>
26 #include <Interface_InterfaceModel.hxx>
27
28 #include <Quantity_Color.hxx>
29
30 #include <StepRepr_DescriptiveRepresentationItem.hxx>
31 #include <StepRepr_ProductDefinitionShape.hxx>
32 #include <StepRepr_PropertyDefinitionRepresentation.hxx>
33 #include <StepRepr_Representation.hxx>
34
35 #include <TDataStd_Name.hxx>
36 #include <TDF_ChildIDIterator.hxx>
37 #include <TDocStd_Document.hxx>
38 #include <TopExp.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopoDS.hxx>
41 #include <Transfer_TransientProcess.hxx>
42 #include <TransferBRep.hxx>
43
44 #include <XCAFApp_Application.hxx>
45 #include <XCAFDoc_DocumentTool.hxx>
46 #include <XCAFDoc_Location.hxx>
47 #include <XCAFDoc_ShapeTool.hxx>
48 #include <XSControl_TransferReader.hxx>
49 #include <XSControl_WorkSession.hxx>
50
51 #include <Locale_Convert.h>
52
53 //=============================================================================
54 TopoDS_Shape getShape(const Handle(Standard_Transient) &theEnti,
55                       const Handle(Transfer_TransientProcess) &theTP)
56 {
57   TopoDS_Shape aResult;
58   Handle(Transfer_Binder) aBinder = theTP->Find(theEnti);
59
60   if (aBinder.IsNull()) {
61     return aResult;
62   }
63
64   aResult = TransferBRep::ShapeResult(aBinder);
65
66   return aResult;
67 }
68
69 //=============================================================================
70 std::shared_ptr<GeomAPI_Shape> readAttributes(STEPCAFControl_Reader &theReader,
71                                std::shared_ptr<ModelAPI_ResultBody> theResultBody,
72                                const bool  theIsMaterials,
73                                std::map< std::wstring,std::list<std::wstring>> &theMaterialShape,
74                                std::string& theError)
75 {
76   // dummy XCAF Application to handle the STEP XCAF Document
77   Handle(XCAFApp_Application) dummy_app = XCAFApp_Application::GetApplication();
78   // XCAF Document to contain the STEP/IGES file itself
79   Handle(TDocStd_Document) adoc;
80
81   dummy_app->NewDocument( TCollection_ExtendedString("MDTV-CAF"), adoc);
82   // transfer STEP/IGES into the document, and get the main label
83   theReader.Transfer(adoc);
84   TDF_Label mainLabel = adoc->Main();
85   Handle_XCAFDoc_ShapeTool shapeTool = XCAFDoc_DocumentTool::ShapeTool(mainLabel);
86   Handle_XCAFDoc_ColorTool colorTool = XCAFDoc_DocumentTool::ColorTool(mainLabel);
87   Handle(XCAFDoc_MaterialTool) materialTool = XCAFDoc_DocumentTool::MaterialTool(mainLabel);
88   // traverse the labels recursively to set attributes on shapes
89   setShapeAttributes(shapeTool, colorTool, materialTool, mainLabel,
90                      TopLoc_Location(),theResultBody,theMaterialShape,false);
91
92   std::shared_ptr<GeomAPI_Shape> ageom =  setgeom(shapeTool,mainLabel,theError);
93
94   STEPControl_Reader aReader = theReader.ChangeReader();
95
96   // BEGIN: reading materials of sub-shapes from file
97   if (theIsMaterials) {
98     TopTools_IndexedMapOfShape anIndices;
99     TopExp::MapShapes(ageom->impl<TopoDS_Shape>(), anIndices);
100
101     Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
102     Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
103     if (!TR.IsNull()) {
104       Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
105
106       Standard_Integer nb = Model->NbEntities();
107
108       for (Standard_Integer ie = 1; ie <= nb; ie++) {
109         Handle(Standard_Transient) enti = Model->Value(ie);
110
111         // Store materials.
112         storeMaterial(theResultBody,enti, anIndices, TP, mainLabel,theMaterialShape);
113       }
114     }
115   }
116   if (adoc->CanClose() == CDM_CCS_OK)
117     adoc->Close();
118   return ageom;
119 }
120
121 //=============================================================================
122 std::shared_ptr<GeomAPI_Shape> setgeom(const Handle(XCAFDoc_ShapeTool) &theShapeTool,
123                                        const TDF_Label &theLabel,
124                                        std::string& theError)
125 {
126   BRep_Builder aB;
127   TopoDS_Compound aCompound;
128   aB.MakeCompound(aCompound);
129
130   TDF_LabelSequence aFrshapes;
131   theShapeTool->GetShapes(aFrshapes);
132
133   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
134
135   if (aFrshapes.Length() == 0) {
136       aGeomShape->setImpl(new TopoDS_Shape());
137       return aGeomShape;
138   } else if (aFrshapes.Length() == 1) {
139     TopoDS_Shape shape = theShapeTool->GetShape(aFrshapes.Value(1));
140     aGeomShape->setImpl(new TopoDS_Shape(shape));
141     return aGeomShape;
142   } else {
143     for (Standard_Integer i=1; i<aFrshapes.Length(); i++) {
144       TopoDS_Shape aS = theShapeTool->GetShape(aFrshapes.Value(i));
145       TDF_Label aLabel = theShapeTool->FindShape(aS, Standard_False);
146       if ( (!aLabel.IsNull()) && (theShapeTool->IsShape(aLabel)) ) {
147         if (theShapeTool->IsFree(aLabel) ) {
148           if (aS.IsNull()) {
149             continue;
150           } else {
151             if (!theShapeTool->IsReference(aLabel) ){
152               for(TDF_ChildIterator anIt(aLabel); anIt.More(); anIt.Next()) {
153                 aB.Add(aCompound, theShapeTool->GetShape(anIt.Value()) );
154               }
155             } else {
156               aB.Add(aCompound, aS);
157             }
158           }
159         }
160       }
161     }
162
163     TopoDS_Shape aShape = aCompound;
164     // Check if any BRep entity has been read, there must be at least a vertex
165     if (!TopExp_Explorer( aShape, TopAbs_VERTEX ).More()) {
166       theError = "No geometrical data in the imported file.";
167       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
168       aGeomShape->setImpl(new TopoDS_Shape());
169       return aGeomShape;
170     }
171
172     aGeomShape->setImpl(new TopoDS_Shape(aShape));
173     return aGeomShape;
174   }
175 }
176 //=============================================================================
177 void setShapeAttributes(const Handle(XCAFDoc_ShapeTool) &theShapeTool,
178                         const Handle(XCAFDoc_ColorTool) &theColorTool,
179                         const Handle(XCAFDoc_MaterialTool) &theMaterialTool,
180                         const TDF_Label &theLabel,
181                         const TopLoc_Location &theLoc,
182                         std::shared_ptr<ModelAPI_ResultBody> theResultBody,
183                         std::map< std::wstring,std::list<std::wstring>> &theMaterialShape,
184                         bool theIsRef)
185 {
186   std::wstring aShapeName;
187   Handle(TDataStd_Name) aN;
188
189   if (theLabel.FindAttribute(TDataStd_Name::GetID(), aN)) {
190     TCollection_ExtendedString aName = aN->Get();
191
192     aShapeName =  Locale::Convert::toWString(TCollection_AsciiString(aName).ToCString()) ;
193   }
194
195   TopLoc_Location aPartLoc = theLoc;
196   Handle(XCAFDoc_Location) al;
197   if (theLabel.FindAttribute(XCAFDoc_Location::GetID(), al)) {
198     if (theIsRef)
199       aPartLoc = aPartLoc * al->Get();
200     else
201       aPartLoc = al->Get();
202   }
203
204   TDF_Label aRef;
205   if (theShapeTool->IsReference(theLabel) && theShapeTool->GetReferredShape(theLabel, aRef)) {
206
207     setShapeAttributes( theShapeTool, theColorTool, theMaterialTool, aRef,
208                         aPartLoc,theResultBody,theMaterialShape,true);
209   }
210
211   if (theShapeTool->IsSimpleShape(theLabel) && (theIsRef || theShapeTool->IsFree(theLabel))) {
212
213     TopoDS_Shape aShape = theShapeTool->GetShape(theLabel);
214
215     std::shared_ptr<GeomAPI_Shape> aShapeGeom(new GeomAPI_Shape);
216     if (!theLoc.IsIdentity()) {
217         aShape.Move(theLoc);
218     }
219     aShapeGeom->setImpl(new TopoDS_Shape(aShape));
220     aShapeName = theResultBody->addShapeName(aShapeGeom, aShapeName);
221
222
223     aShape.Location(theIsRef ? theLoc : aPartLoc);
224     int aDim =
225       (aShape.ShapeType() == TopAbs_VERTEX) ?
226         0 :
227         (aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) ?
228         1 :
229         (aShape.ShapeType() == TopAbs_FACE ||
230          aShape.ShapeType() == TopAbs_SHELL) ? 2 :3;
231
232     Handle(TCollection_HAsciiString) aMatName;
233     Handle(TCollection_HAsciiString) aMatDescription;
234     Standard_Real aMatDensity;
235     Handle(TCollection_HAsciiString) aMatDensName;
236     Handle(TCollection_HAsciiString) aMatDensValType;
237
238     if (theMaterialTool->GetMaterial(theLabel, aMatName, aMatDescription, aMatDensity,
239                                  aMatDensName, aMatDensValType)) {
240       std::wstring aNameMaterial = Locale::Convert::toWString(aMatName->ToCString());
241
242       theMaterialShape[aNameMaterial].push_back(aShapeName);
243     }
244
245     Quantity_Color aCol;
246     if (theColorTool->GetColor(theLabel, XCAFDoc_ColorGen, aCol)) {
247       double r = aCol.Red(), g = aCol.Green(), b = aCol.Blue();
248       std::vector<int> ColRGB = {int(r*255),int(g*255),int(b*255)};
249       theResultBody->addShapeColor(aShapeName, ColRGB);
250     } else if (theColorTool->GetColor(theLabel, XCAFDoc_ColorSurf, aCol)) {
251       double r = aCol.Red(), g = aCol.Green(), b = aCol.Blue();
252       std::vector<int> aColRGB = {int(r*255),int(g*255),int(b*255)};
253       theResultBody->addShapeColor(aShapeName, aColRGB);
254     } else if (theColorTool->GetColor(theLabel, XCAFDoc_ColorCurv, aCol)) {
255       double aR = aCol.Red(), aG = aCol.Green(), aB = aCol.Blue();
256       std::vector<int> aColRGB = {int(aR*255),int(aG*255),int(aB*255)};
257       theResultBody->addShapeColor(aShapeName, aColRGB);
258     }
259     // check explicit coloring of boundary entities
260     if (aDim == 3) {
261       TopExp_Explorer aXp2(aShape, TopAbs_FACE);
262       while (aXp2.More()) {
263         if (theColorTool->GetColor(aXp2.Current(), XCAFDoc_ColorGen, aCol) ||
264           theColorTool->GetColor(aXp2.Current(), XCAFDoc_ColorSurf, aCol) ||
265           theColorTool->GetColor(aXp2.Current(), XCAFDoc_ColorCurv, aCol)) {
266           double aR = aCol.Red(), aG = aCol.Green(), aB = aCol.Blue();
267           TopoDS_Face aFace = TopoDS::Face(aXp2.Current());
268           std::vector<int> aColRGB = {int(aR*255),int(aG*255),int(aB*255)};
269           std::wstringstream aNameFace;
270           TopoDS_Shape aShapeface = aXp2.Current();
271           if (!theLoc.IsIdentity()){
272                   aShapeface.Move(theLoc);
273           }
274           aShapeGeom->setImpl(new TopoDS_Shape(aShapeface));
275           theResultBody->addShapeColor(
276           theResultBody->addShapeName(aShapeGeom , aNameFace.str()), aColRGB);
277         }
278         aXp2.Next();
279       }
280     }
281   } else {
282     if (!theShapeTool->IsReference(theLabel) ){
283       TopoDS_Shape aShape = theShapeTool->GetShape(theLabel);
284
285       std::shared_ptr<GeomAPI_Shape> aShapeGeom(new GeomAPI_Shape);
286       if (!theLoc.IsIdentity()){
287           aShape.Move(theLoc);
288       }
289       aShapeGeom->setImpl(new TopoDS_Shape(aShape));
290       aShapeName = theResultBody->addShapeName(aShapeGeom, aShapeName);
291     }
292     for(TDF_ChildIterator anIt(theLabel); anIt.More(); anIt.Next()) {
293
294       setShapeAttributes( theShapeTool, theColorTool, theMaterialTool,
295                          anIt.Value(), aPartLoc,theResultBody,theMaterialShape, theIsRef);
296     }
297   }
298 }
299
300 //=============================================================================
301 void storeMaterial( std::shared_ptr<ModelAPI_ResultBody>    theResultBody,
302                     const Handle(Standard_Transient)        &theEnti,
303                     const TopTools_IndexedMapOfShape        &theIndices,
304                     const Handle(Transfer_TransientProcess) &theTP,
305                     const TDF_Label                         &theShapeLabel,
306                     std::map< std::wstring, std::list<std::wstring>> &theMaterialShape )
307 {
308   // Treat Product Definition Shape only.
309   Handle(StepRepr_ProductDefinitionShape) aPDS =
310     Handle(StepRepr_ProductDefinitionShape)::DownCast(theEnti);
311   Handle(StepBasic_ProductDefinition)     aProdDef;
312
313   if (!aPDS.IsNull()) {
314     // Product Definition Shape ==> Product Definition
315     aProdDef = aPDS->Definition().ProductDefinition();
316   }
317
318   if (!aProdDef.IsNull()) {
319     // Product Definition ==> Property Definition
320     const Interface_Graph    &aGraph = theTP->Graph();
321     Interface_EntityIterator  aSubs  = aGraph.Sharings(aProdDef);
322     TopoDS_Shape              aShape;
323
324     for(aSubs.Start(); aSubs.More(); aSubs.Next()) {
325       Handle(StepRepr_PropertyDefinition) aPropD =
326         Handle(StepRepr_PropertyDefinition)::DownCast(aSubs.Value());
327
328       if (!aPropD.IsNull()) {
329         // Property Definition ==> Representation.
330         Interface_EntityIterator aSubs1 = aGraph.Sharings(aPropD);
331
332         for(aSubs1.Start(); aSubs1.More(); aSubs1.Next()) {
333           Handle(StepRepr_PropertyDefinitionRepresentation) aPDR =
334             Handle(StepRepr_PropertyDefinitionRepresentation)::
335             DownCast(aSubs1.Value());
336
337           if (!aPDR.IsNull()) {
338             // Property Definition ==> Material Name.
339             Handle(StepRepr_Representation) aRepr = aPDR->UsedRepresentation();
340
341             if (!aRepr.IsNull()) {
342               Standard_Integer anIr;
343
344               for(anIr = 1; anIr <= aRepr->NbItems(); anIr++) {
345                 Handle(StepRepr_RepresentationItem) aRI = aRepr->ItemsValue(anIr);
346                 Handle(StepRepr_DescriptiveRepresentationItem) aDRI =
347                   Handle(StepRepr_DescriptiveRepresentationItem)::DownCast(aRI);
348
349                 if (!aDRI.IsNull()) {
350                   // Get shape from Product Definition
351                   Handle(TCollection_HAsciiString) aMatName = aDRI->Name();
352                   if (!aMatName.IsNull()) {
353                     TCollection_ExtendedString
354                       aMatNameExt (aMatName->ToCString());
355
356                     if (aShape.IsNull()) {
357                       //Get the shape.
358                       aShape = getShape(aProdDef, theTP);
359                       if (aShape.IsNull()) {
360                         return;
361                       }
362                     }
363
364                     // as PRODUCT can be included in the main shape
365                     // several times, we look here for all iclusions.
366                     Standard_Integer anISub, aNbSubs = theIndices.Extent();
367
368                     for (anISub = 1; anISub <= aNbSubs; anISub++) {
369                       TopoDS_Shape aSub = theIndices.FindKey(anISub);
370
371                       if (aSub.IsPartner(aShape)) {
372                         std::shared_ptr<GeomAPI_Shape> aShapeGeom(new GeomAPI_Shape);
373                         aShapeGeom->setImpl(new TopoDS_Shape(aSub));
374                         std::wstring aNom = theResultBody->findShapeName(aShapeGeom);
375                         std::wstring aMName= Locale::Convert::toWString(aMatName->ToCString());
376                         theMaterialShape[aMName].push_back(aNom);
377
378                       }
379                     }
380                   }
381                 }
382               }
383             }
384           }
385         }
386       }
387     }
388   }
389 }
390