1 // Copyright (C) 2014-2021 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 <GeomAlgoAPI_STEPImportXCAF.h>
22 #include <BRep_Builder.hxx>
24 #include <Interface_EntityIterator.hxx>
25 #include <Interface_Graph.hxx>
26 #include <Interface_InterfaceModel.hxx>
28 #include <Quantity_Color.hxx>
30 #include <StepRepr_DescriptiveRepresentationItem.hxx>
31 #include <StepRepr_ProductDefinitionShape.hxx>
32 #include <StepRepr_PropertyDefinitionRepresentation.hxx>
33 #include <StepRepr_Representation.hxx>
35 #include <TDataStd_Name.hxx>
36 #include <TDF_ChildIDIterator.hxx>
37 #include <TDocStd_Document.hxx>
39 #include <TopExp_Explorer.hxx>
41 #include <Transfer_TransientProcess.hxx>
42 #include <TransferBRep.hxx>
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>
51 #include <Locale_Convert.h>
53 //=============================================================================
54 TopoDS_Shape getShape(const Handle(Standard_Transient) &theEnti,
55 const Handle(Transfer_TransientProcess) &theTP)
58 Handle(Transfer_Binder) aBinder = theTP->Find(theEnti);
60 if (aBinder.IsNull()) {
64 aResult = TransferBRep::ShapeResult(aBinder);
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)
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;
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);
92 std::shared_ptr<GeomAPI_Shape> ageom = setgeom(shapeTool,mainLabel,theError);
94 STEPControl_Reader aReader = theReader.ChangeReader();
96 // BEGIN: reading materials of sub-shapes from file
98 TopTools_IndexedMapOfShape anIndices;
99 TopExp::MapShapes(ageom->impl<TopoDS_Shape>(), anIndices);
101 Handle(Interface_InterfaceModel) Model = aReader.WS()->Model();
102 Handle(XSControl_TransferReader) TR = aReader.WS()->TransferReader();
104 Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
106 Standard_Integer nb = Model->NbEntities();
108 for (Standard_Integer ie = 1; ie <= nb; ie++) {
109 Handle(Standard_Transient) enti = Model->Value(ie);
112 storeMaterial(theResultBody,enti, anIndices, TP, mainLabel,theMaterialShape);
116 if (adoc->CanClose() == CDM_CCS_OK)
121 //=============================================================================
122 std::shared_ptr<GeomAPI_Shape> setgeom(const Handle(XCAFDoc_ShapeTool) &theShapeTool,
123 const TDF_Label &theLabel,
124 std::string& theError)
127 TopoDS_Compound aCompound;
128 aB.MakeCompound(aCompound);
130 TDF_LabelSequence aFrshapes;
131 theShapeTool->GetShapes(aFrshapes);
133 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
135 if (aFrshapes.Length() == 0) {
136 aGeomShape->setImpl(new TopoDS_Shape());
138 } else if (aFrshapes.Length() == 1) {
139 TopoDS_Shape shape = theShapeTool->GetShape(aFrshapes.Value(1));
140 aGeomShape->setImpl(new TopoDS_Shape(shape));
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) ) {
151 if (!theShapeTool->IsReference(aLabel) ){
152 for(TDF_ChildIterator anIt(aLabel); anIt.More(); anIt.Next()) {
153 aB.Add(aCompound, theShapeTool->GetShape(anIt.Value()) );
156 aB.Add(aCompound, aS);
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());
172 aGeomShape->setImpl(new TopoDS_Shape(aShape));
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,
186 std::wstring aShapeName;
187 Handle(TDataStd_Name) aN;
189 if (theLabel.FindAttribute(TDataStd_Name::GetID(), aN)) {
190 TCollection_ExtendedString aName = aN->Get();
192 aShapeName = Locale::Convert::toWString(TCollection_AsciiString(aName).ToCString()) ;
195 TopLoc_Location aPartLoc = theLoc;
196 Handle(XCAFDoc_Location) al;
197 if (theLabel.FindAttribute(XCAFDoc_Location::GetID(), al)) {
199 aPartLoc = aPartLoc * al->Get();
201 aPartLoc = al->Get();
205 if (theShapeTool->IsReference(theLabel) && theShapeTool->GetReferredShape(theLabel, aRef)) {
207 setShapeAttributes( theShapeTool, theColorTool, theMaterialTool, aRef,
208 aPartLoc,theResultBody,theMaterialShape,true);
211 if (theShapeTool->IsSimpleShape(theLabel) && (theIsRef || theShapeTool->IsFree(theLabel))) {
213 TopoDS_Shape aShape = theShapeTool->GetShape(theLabel);
215 std::shared_ptr<GeomAPI_Shape> aShapeGeom(new GeomAPI_Shape);
216 if (!theLoc.IsIdentity()) {
219 aShapeGeom->setImpl(new TopoDS_Shape(aShape));
220 aShapeName = theResultBody->addShapeName(aShapeGeom, aShapeName);
223 aShape.Location(theIsRef ? theLoc : aPartLoc);
225 (aShape.ShapeType() == TopAbs_VERTEX) ?
227 (aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) ?
229 (aShape.ShapeType() == TopAbs_FACE ||
230 aShape.ShapeType() == TopAbs_SHELL) ? 2 :3;
232 Handle(TCollection_HAsciiString) aMatName;
233 Handle(TCollection_HAsciiString) aMatDescription;
234 Standard_Real aMatDensity;
235 Handle(TCollection_HAsciiString) aMatDensName;
236 Handle(TCollection_HAsciiString) aMatDensValType;
238 if (theMaterialTool->GetMaterial(theLabel, aMatName, aMatDescription, aMatDensity,
239 aMatDensName, aMatDensValType)) {
240 std::wstring aNameMaterial = Locale::Convert::toWString(aMatName->ToCString());
242 theMaterialShape[aNameMaterial].push_back(aShapeName);
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);
259 // check explicit coloring of boundary entities
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);
274 aShapeGeom->setImpl(new TopoDS_Shape(aShapeface));
275 theResultBody->addShapeColor(
276 theResultBody->addShapeName(aShapeGeom , aNameFace.str()), aColRGB);
282 if (!theShapeTool->IsReference(theLabel) ){
283 TopoDS_Shape aShape = theShapeTool->GetShape(theLabel);
285 std::shared_ptr<GeomAPI_Shape> aShapeGeom(new GeomAPI_Shape);
286 if (!theLoc.IsIdentity()){
289 aShapeGeom->setImpl(new TopoDS_Shape(aShape));
290 aShapeName = theResultBody->addShapeName(aShapeGeom, aShapeName);
292 for(TDF_ChildIterator anIt(theLabel); anIt.More(); anIt.Next()) {
294 setShapeAttributes( theShapeTool, theColorTool, theMaterialTool,
295 anIt.Value(), aPartLoc,theResultBody,theMaterialShape, theIsRef);
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 )
308 // Treat Product Definition Shape only.
309 Handle(StepRepr_ProductDefinitionShape) aPDS =
310 Handle(StepRepr_ProductDefinitionShape)::DownCast(theEnti);
311 Handle(StepBasic_ProductDefinition) aProdDef;
313 if (!aPDS.IsNull()) {
314 // Product Definition Shape ==> Product Definition
315 aProdDef = aPDS->Definition().ProductDefinition();
318 if (!aProdDef.IsNull()) {
319 // Product Definition ==> Property Definition
320 const Interface_Graph &aGraph = theTP->Graph();
321 Interface_EntityIterator aSubs = aGraph.Sharings(aProdDef);
324 for(aSubs.Start(); aSubs.More(); aSubs.Next()) {
325 Handle(StepRepr_PropertyDefinition) aPropD =
326 Handle(StepRepr_PropertyDefinition)::DownCast(aSubs.Value());
328 if (!aPropD.IsNull()) {
329 // Property Definition ==> Representation.
330 Interface_EntityIterator aSubs1 = aGraph.Sharings(aPropD);
332 for(aSubs1.Start(); aSubs1.More(); aSubs1.Next()) {
333 Handle(StepRepr_PropertyDefinitionRepresentation) aPDR =
334 Handle(StepRepr_PropertyDefinitionRepresentation)::
335 DownCast(aSubs1.Value());
337 if (!aPDR.IsNull()) {
338 // Property Definition ==> Material Name.
339 Handle(StepRepr_Representation) aRepr = aPDR->UsedRepresentation();
341 if (!aRepr.IsNull()) {
342 Standard_Integer anIr;
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);
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());
356 if (aShape.IsNull()) {
358 aShape = getShape(aProdDef, theTP);
359 if (aShape.IsNull()) {
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();
368 for (anISub = 1; anISub <= aNbSubs; anISub++) {
369 TopoDS_Shape aSub = theIndices.FindKey(anISub);
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);