1 // Author: Kirill Gavrilov
2 // Copyright (c) 2019 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <RWObj_CafReader.hxx>
17 #include <Message_ProgressSentry.hxx>
19 IMPLEMENT_STANDARD_RTTIEXT(RWObj_CafReader, RWMesh_CafReader)
21 //================================================================
22 // Function : Constructor
24 //================================================================
25 RWObj_CafReader::RWObj_CafReader()
26 : myIsSinglePrecision (Standard_False)
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);
34 //================================================================
35 // Function : BindNamedShape
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)
43 if (theShape.IsNull())
48 RWMesh_NodeAttributes aShapeAttribs;
49 aShapeAttribs.Name = theName;
50 if (theMaterial != NULL)
52 // assign material and not color
53 //aShapeAttribs.Style.SetColorSurf (Quantity_ColorRGBA (theMaterial->DiffuseColor, 1.0f - theMaterial->Transparency));
55 Handle(XCAFDoc_VisMaterial) aMat = new XCAFDoc_VisMaterial();
56 if (!myObjMaterialMap.Find (theMaterial->Name, aMat)) // material names are used as unique keys in OBJ
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())
67 aMatXde.DiffuseTexture = new Image_Texture (theMaterial->DiffuseTexture);
70 aMat = new XCAFDoc_VisMaterial();
71 aMat->SetCommonMaterial (aMatXde);
72 aMat->SetRawName (new TCollection_HAsciiString (theMaterial->Name));
74 aShapeAttribs.Style.SetMaterial (aMat);
76 myAttribMap.Bind (theShape, aShapeAttribs);
80 myRootShapes.Append (theShape);
84 //================================================================
85 // Function : createReaderContext
87 //================================================================
88 Handle(RWObj_TriangulationReader) RWObj_CafReader::createReaderContext()
90 Handle(RWObj_TriangulationReader) aReader = new RWObj_TriangulationReader();
94 //================================================================
95 // Function : performMesh
97 //================================================================
98 Standard_Boolean RWObj_CafReader::performMesh (const TCollection_AsciiString& theFile,
99 const Handle(Message_ProgressIndicator)& theProgress,
100 const Standard_Boolean theToProbe)
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;
111 isDone = aCtx->Probe (theFile.ToCString(), theProgress);
115 isDone = aCtx->Read (theFile.ToCString(), theProgress);
117 if (!aCtx->FileComments().IsEmpty())
119 myMetadata.Add ("Comments", aCtx->FileComments());
121 for (NCollection_IndexedMap<TCollection_AsciiString>::Iterator aFileIter (aCtx->ExternalFiles()); aFileIter.More(); aFileIter.Next())
123 myExternalFiles.Add (aFileIter.Value());