4151c94d |
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 | |
4151c94d |
17 | IMPLEMENT_STANDARD_RTTIEXT(RWObj_CafReader, RWMesh_CafReader) |
18 | |
19 | //================================================================ |
20 | // Function : Constructor |
21 | // Purpose : |
22 | //================================================================ |
23 | RWObj_CafReader::RWObj_CafReader() |
24 | : myIsSinglePrecision (Standard_False) |
25 | { |
26 | //myCoordSysConverter.SetInputLengthUnit (-1.0); // length units are undefined within OBJ file |
27 | // OBJ format does not define coordinate system (apart from mentioning that it is right-handed), |
28 | // however most files are stored Y-up |
29 | myCoordSysConverter.SetInputCoordinateSystem (RWMesh_CoordinateSystem_glTF); |
30 | } |
31 | |
32 | //================================================================ |
33 | // Function : BindNamedShape |
34 | // Purpose : |
35 | //================================================================ |
36 | void RWObj_CafReader::BindNamedShape (const TopoDS_Shape& theShape, |
37 | const TCollection_AsciiString& theName, |
38 | const RWObj_Material* theMaterial, |
39 | const Standard_Boolean theIsRootShape) |
40 | { |
41 | if (theShape.IsNull()) |
42 | { |
43 | return; |
44 | } |
45 | |
46 | RWMesh_NodeAttributes aShapeAttribs; |
47 | aShapeAttribs.Name = theName; |
48 | if (theMaterial != NULL) |
49 | { |
a4815d55 |
50 | // assign material and not color |
51 | //aShapeAttribs.Style.SetColorSurf (Quantity_ColorRGBA (theMaterial->DiffuseColor, 1.0f - theMaterial->Transparency)); |
52 | |
53 | Handle(XCAFDoc_VisMaterial) aMat = new XCAFDoc_VisMaterial(); |
54 | if (!myObjMaterialMap.Find (theMaterial->Name, aMat)) // material names are used as unique keys in OBJ |
55 | { |
56 | XCAFDoc_VisMaterialCommon aMatXde; |
57 | aMatXde.IsDefined = true; |
58 | aMatXde.AmbientColor = theMaterial->AmbientColor; |
59 | aMatXde.DiffuseColor = theMaterial->DiffuseColor; |
60 | aMatXde.SpecularColor = theMaterial->SpecularColor; |
61 | aMatXde.Shininess = theMaterial->Shininess; |
62 | aMatXde.Transparency = theMaterial->Transparency; |
63 | if (!theMaterial->DiffuseTexture.IsEmpty()) |
64 | { |
65 | aMatXde.DiffuseTexture = new Image_Texture (theMaterial->DiffuseTexture); |
66 | } |
67 | |
68 | aMat = new XCAFDoc_VisMaterial(); |
69 | aMat->SetCommonMaterial (aMatXde); |
70 | aMat->SetRawName (new TCollection_HAsciiString (theMaterial->Name)); |
a14f2b47 |
71 | myObjMaterialMap.Bind (theMaterial->Name, aMat); |
a4815d55 |
72 | } |
73 | aShapeAttribs.Style.SetMaterial (aMat); |
4151c94d |
74 | } |
75 | myAttribMap.Bind (theShape, aShapeAttribs); |
76 | |
77 | if (theIsRootShape) |
78 | { |
79 | myRootShapes.Append (theShape); |
80 | } |
81 | } |
82 | |
83 | //================================================================ |
84 | // Function : createReaderContext |
85 | // Purpose : |
86 | //================================================================ |
87 | Handle(RWObj_TriangulationReader) RWObj_CafReader::createReaderContext() |
88 | { |
89 | Handle(RWObj_TriangulationReader) aReader = new RWObj_TriangulationReader(); |
90 | return aReader; |
91 | } |
92 | |
93 | //================================================================ |
94 | // Function : performMesh |
95 | // Purpose : |
96 | //================================================================ |
97 | Standard_Boolean RWObj_CafReader::performMesh (const TCollection_AsciiString& theFile, |
7e785937 |
98 | const Message_ProgressRange& theProgress, |
4151c94d |
99 | const Standard_Boolean theToProbe) |
100 | { |
101 | Handle(RWObj_TriangulationReader) aCtx = createReaderContext(); |
102 | aCtx->SetSinglePrecision (myIsSinglePrecision); |
103 | aCtx->SetCreateShapes (Standard_True); |
104 | aCtx->SetShapeReceiver (this); |
105 | aCtx->SetTransformation (myCoordSysConverter); |
106 | aCtx->SetMemoryLimit (myMemoryLimitMiB == -1 ? Standard_Size(-1) : Standard_Size(myMemoryLimitMiB * 1024 * 1024)); |
107 | Standard_Boolean isDone = Standard_False; |
108 | if (theToProbe) |
109 | { |
110 | isDone = aCtx->Probe (theFile.ToCString(), theProgress); |
111 | } |
112 | else |
113 | { |
114 | isDone = aCtx->Read (theFile.ToCString(), theProgress); |
115 | } |
116 | if (!aCtx->FileComments().IsEmpty()) |
117 | { |
118 | myMetadata.Add ("Comments", aCtx->FileComments()); |
119 | } |
120 | for (NCollection_IndexedMap<TCollection_AsciiString>::Iterator aFileIter (aCtx->ExternalFiles()); aFileIter.More(); aFileIter.Next()) |
121 | { |
122 | myExternalFiles.Add (aFileIter.Value()); |
123 | } |
124 | return isDone; |
125 | } |