0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / RWObj / RWObj_CafReader.cxx
1 // Author: Kirill Gavrilov
2 // Copyright (c) 2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <RWObj_CafReader.hxx>
16
17 #include <Message_ProgressSentry.hxx>
18
19 IMPLEMENT_STANDARD_RTTIEXT(RWObj_CafReader, RWMesh_CafReader)
20
21 //================================================================
22 // Function : Constructor
23 // Purpose  :
24 //================================================================
25 RWObj_CafReader::RWObj_CafReader()
26 : myIsSinglePrecision (Standard_False)
27 {
28   //myCoordSysConverter.SetInputLengthUnit (-1.0); // length units are undefined within OBJ file
29   // OBJ format does not define coordinate system (apart from mentioning that it is right-handed),
30   // however most files are stored Y-up
31   myCoordSysConverter.SetInputCoordinateSystem (RWMesh_CoordinateSystem_glTF);
32 }
33
34 //================================================================
35 // Function : BindNamedShape
36 // Purpose  :
37 //================================================================
38 void RWObj_CafReader::BindNamedShape (const TopoDS_Shape& theShape,
39                                       const TCollection_AsciiString& theName,
40                                       const RWObj_Material* theMaterial,
41                                       const Standard_Boolean theIsRootShape)
42 {
43   if (theShape.IsNull())
44   {
45     return;
46   }
47
48   RWMesh_NodeAttributes aShapeAttribs;
49   aShapeAttribs.Name = theName;
50   if (theMaterial != NULL)
51   {
52     // assign material and not color
53     //aShapeAttribs.Style.SetColorSurf (Quantity_ColorRGBA (theMaterial->DiffuseColor, 1.0f - theMaterial->Transparency));
54
55     Handle(XCAFDoc_VisMaterial) aMat = new XCAFDoc_VisMaterial();
56     if (!myObjMaterialMap.Find (theMaterial->Name, aMat)) // material names are used as unique keys in OBJ
57     {
58       XCAFDoc_VisMaterialCommon aMatXde;
59       aMatXde.IsDefined = true;
60       aMatXde.AmbientColor    = theMaterial->AmbientColor;
61       aMatXde.DiffuseColor    = theMaterial->DiffuseColor;
62       aMatXde.SpecularColor   = theMaterial->SpecularColor;
63       aMatXde.Shininess       = theMaterial->Shininess;
64       aMatXde.Transparency    = theMaterial->Transparency;
65       if (!theMaterial->DiffuseTexture.IsEmpty())
66       {
67         aMatXde.DiffuseTexture  = new Image_Texture (theMaterial->DiffuseTexture);
68       }
69
70       aMat = new XCAFDoc_VisMaterial();
71       aMat->SetCommonMaterial (aMatXde);
72       aMat->SetRawName (new TCollection_HAsciiString (theMaterial->Name));
73       myObjMaterialMap.Bind (theMaterial->Name, aMat);
74     }
75     aShapeAttribs.Style.SetMaterial (aMat);
76   }
77   myAttribMap.Bind (theShape, aShapeAttribs);
78
79   if (theIsRootShape)
80   {
81     myRootShapes.Append (theShape);
82   }
83 }
84
85 //================================================================
86 // Function : createReaderContext
87 // Purpose  :
88 //================================================================
89 Handle(RWObj_TriangulationReader) RWObj_CafReader::createReaderContext()
90 {
91   Handle(RWObj_TriangulationReader) aReader = new RWObj_TriangulationReader();
92   return aReader;
93 }
94
95 //================================================================
96 // Function : performMesh
97 // Purpose  :
98 //================================================================
99 Standard_Boolean RWObj_CafReader::performMesh (const TCollection_AsciiString& theFile,
100                                                const Handle(Message_ProgressIndicator)& theProgress,
101                                                const Standard_Boolean theToProbe)
102 {
103   Handle(RWObj_TriangulationReader) aCtx = createReaderContext();
104   aCtx->SetSinglePrecision (myIsSinglePrecision);
105   aCtx->SetCreateShapes (Standard_True);
106   aCtx->SetShapeReceiver (this);
107   aCtx->SetTransformation (myCoordSysConverter);
108   aCtx->SetMemoryLimit (myMemoryLimitMiB == -1 ? Standard_Size(-1) : Standard_Size(myMemoryLimitMiB * 1024 * 1024));
109   Standard_Boolean isDone = Standard_False;
110   if (theToProbe)
111   {
112     isDone = aCtx->Probe (theFile.ToCString(), theProgress);
113   }
114   else
115   {
116     isDone = aCtx->Read (theFile.ToCString(), theProgress);
117   }
118   if (!aCtx->FileComments().IsEmpty())
119   {
120     myMetadata.Add ("Comments", aCtx->FileComments());
121   }
122   for (NCollection_IndexedMap<TCollection_AsciiString>::Iterator aFileIter (aCtx->ExternalFiles()); aFileIter.More(); aFileIter.Next())
123   {
124     myExternalFiles.Add (aFileIter.Value());
125   }
126   return isDone;
127 }