0029902: Data Exchange, XCAF - provide extended Material definition for visualization...
[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     }
74     aShapeAttribs.Style.SetMaterial (aMat);
75   }
76   myAttribMap.Bind (theShape, aShapeAttribs);
77
78   if (theIsRootShape)
79   {
80     myRootShapes.Append (theShape);
81   }
82 }
83
84 //================================================================
85 // Function : createReaderContext
86 // Purpose  :
87 //================================================================
88 Handle(RWObj_TriangulationReader) RWObj_CafReader::createReaderContext()
89 {
90   Handle(RWObj_TriangulationReader) aReader = new RWObj_TriangulationReader();
91   return aReader;
92 }
93
94 //================================================================
95 // Function : performMesh
96 // Purpose  :
97 //================================================================
98 Standard_Boolean RWObj_CafReader::performMesh (const TCollection_AsciiString& theFile,
99                                                const Handle(Message_ProgressIndicator)& theProgress,
100                                                const Standard_Boolean theToProbe)
101 {
102   Handle(RWObj_TriangulationReader) aCtx = createReaderContext();
103   aCtx->SetSinglePrecision (myIsSinglePrecision);
104   aCtx->SetCreateShapes (Standard_True);
105   aCtx->SetShapeReceiver (this);
106   aCtx->SetTransformation (myCoordSysConverter);
107   aCtx->SetMemoryLimit (myMemoryLimitMiB == -1 ? Standard_Size(-1) : Standard_Size(myMemoryLimitMiB * 1024 * 1024));
108   Standard_Boolean isDone = Standard_False;
109   if (theToProbe)
110   {
111     isDone = aCtx->Probe (theFile.ToCString(), theProgress);
112   }
113   else
114   {
115     isDone = aCtx->Read (theFile.ToCString(), theProgress);
116   }
117   if (!aCtx->FileComments().IsEmpty())
118   {
119     myMetadata.Add ("Comments", aCtx->FileComments());
120   }
121   for (NCollection_IndexedMap<TCollection_AsciiString>::Iterator aFileIter (aCtx->ExternalFiles()); aFileIter.More(); aFileIter.Next())
122   {
123     myExternalFiles.Add (aFileIter.Value());
124   }
125   return isDone;
126 }