06be34fe01b700169fc86c4bfec4791a0749b23d
[occt.git] / src / Graphic3d / Graphic3d_Texture1D.cxx
1 // Created on: 1997-07-28
2 // Created by: Pierre CHALAMET
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 // Modified     22-12-97 : FMN ; Ajout Modulate
22
23 // Modified :   GG 10/01/2000 IMP 
24 //              Adds Name(),NumberOfTextures() and TextureName() methods
25
26 #include <Graphic3d_Texture1D.ixx>
27 #include <stdlib.h>
28
29 #include <TCollection_AsciiString.hxx>
30 #include <OSD_Directory.hxx>
31 #include <OSD_File.hxx>
32 #include <OSD_Path.hxx>
33
34 static const char *NameOfTexture_to_FileName[] = 
35 {
36   "1d_elevation.rgb"
37 };
38
39
40 static TCollection_AsciiString GetEnvir ( ) {
41
42   static Standard_Boolean IsDefined=Standard_False ;
43   static TCollection_AsciiString VarName;
44   if ( !IsDefined ) {
45     char *envir, *casroot ;
46     envir = getenv("CSF_MDTVTexturesDirectory") ;
47     
48     Standard_Boolean HasDefinition = Standard_False ;
49     if ( !envir ) { 
50       casroot  = getenv("CASROOT");
51       if ( casroot ) {
52         VarName = TCollection_AsciiString  (casroot);
53         VarName += "/src/Textures" ;
54         HasDefinition = Standard_True ;
55       }
56     } else {
57       VarName = TCollection_AsciiString  (envir);
58       HasDefinition = Standard_True ;
59     }
60     if ( HasDefinition ) {
61       OSD_Path aPath ( VarName );
62       OSD_Directory aDir(aPath);
63       if ( aDir.Exists () ) {
64         TCollection_AsciiString aTexture = VarName + "/2d_MatraDatavision.rgb" ;
65         OSD_File TextureFile ( aTexture );
66         if ( !TextureFile.Exists() ) {
67           cout << " CSF_MDTVTexturesDirectory or CASROOT not correctly setted " << endl;
68           cout << " not all files are found in : "<<VarName.ToCString() << endl;
69           Standard_Failure::Raise ( "CSF_MDTVTexturesDirectory or CASROOT not correctly setted " );
70         }
71       } else {
72         cout << " CSF_MDTVTexturesDirectory or CASROOT not correctly setted " << endl;
73         cout << " Directory : "<< VarName.ToCString() << " not exist " << endl;
74         Standard_Failure::Raise ( "CSF_MDTVTexturesDirectory or CASROOT not correctly setted " );
75       }      
76       return VarName ;
77     } else {
78       cout << " CSF_MDTVTexturesDirectory and CASROOT not setted " << endl;
79       cout << " one of these variable are mandatory to use this fonctionnality" << endl;
80       Standard_Failure::Raise ( "CSF_MDTVTexturesDirectory and CASROOT not setted " );
81
82     }
83     IsDefined = Standard_True ; 
84   }
85   return VarName ;
86 }
87
88 Graphic3d_Texture1D::Graphic3d_Texture1D(const Handle(Graphic3d_StructureManager)& SM,const Standard_CString FileName,const Graphic3d_TypeOfTexture Type)
89 : Graphic3d_TextureMap(SM,"", FileName, Type),
90   myName(Graphic3d_NOT_1D_UNKNOWN)
91 {
92 }
93
94
95 Graphic3d_Texture1D::Graphic3d_Texture1D(const Handle(Graphic3d_StructureManager)& SM,const Graphic3d_NameOfTexture1D NOT, const Graphic3d_TypeOfTexture Type)
96 : Graphic3d_TextureMap(SM, GetEnvir().ToCString(), NameOfTexture_to_FileName[NOT], Type),
97   myName(NOT)
98 {
99 }
100
101 Graphic3d_NameOfTexture1D Graphic3d_Texture1D::Name() const {
102
103   return myName;
104 }
105
106 Standard_Integer Graphic3d_Texture1D::NumberOfTextures() {
107
108   return sizeof(NameOfTexture_to_FileName)/sizeof(char*);
109 }
110
111 Standard_CString Graphic3d_Texture1D::TextureName(const Standard_Integer aRank)
112  {
113
114   if( aRank < 1 || aRank > NumberOfTextures() )
115         Standard_OutOfRange::Raise(" BAD index of texture");
116   TCollection_AsciiString filename(NameOfTexture_to_FileName[aRank-1]);
117   Standard_Integer i = filename.SearchFromEnd(".");
118
119   static TCollection_AsciiString name;
120   name = filename.SubString(4,i-1);
121   return name.ToCString();
122 }
123