0027427: Exception on loading a DE package to Draw
[occt.git] / src / Interface / Interface_Category.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Interface_Category.hxx>
16 #include <Interface_GeneralModule.hxx>
17 #include <Interface_InterfaceModel.hxx>
18 #include <Interface_Protocol.hxx>
19 #include <Interface_ShareTool.hxx>
20 #include <Standard_Transient.hxx>
21 #include <Standard_Mutex.hxx>
22 #include <TCollection_AsciiString.hxx>
23 #include <TColStd_SequenceOfAsciiString.hxx>
24 #include <NCollection_Vector.hxx>
25
26 static int init = 0;
27 static Standard_CString unspec = "unspecified";
28
29 static Standard_Mutex gMapTypesMutex;
30 static volatile Standard_Boolean gMapTypesInit = Standard_False;
31
32 static NCollection_Vector<TCollection_AsciiString>& theCats()
33 {
34   static NCollection_Vector<TCollection_AsciiString> aCat;
35   return aCat;
36 }
37
38 Standard_Integer Interface_Category::CatNum
39   (const Handle(Standard_Transient)& theEnt,
40    const Interface_ShareTool& theShares)
41 {
42   if (theEnt.IsNull()) return 0;
43   Standard_Integer CN;
44   Handle(Interface_GeneralModule) aModule;
45   if (!myGTool->Select (theEnt,aModule,CN)) return 0;
46   return aModule->CategoryNumber (CN,theEnt,theShares);
47 }
48
49 void Interface_Category::Compute
50   (const Handle(Interface_InterfaceModel)& theModel,
51    const Interface_ShareTool& theShares)
52 {
53   ClearNums();
54   if (theModel.IsNull()) return;
55   Standard_Integer CN, i, nb = theModel->NbEntities();
56   myGTool->Reservate (nb);
57   if (nb == 0) return;
58   myNum = new TColStd_HArray1OfInteger (1,nb);  myNum->Init(0);
59   for (i = 1; i <= nb; i ++) {
60     Handle(Standard_Transient) anEnt = theModel->Value(i);
61     if (anEnt.IsNull()) continue;
62     Handle(Interface_GeneralModule) aModule;
63     if (!myGTool->Select (anEnt,aModule,CN)) continue;
64     myNum->SetValue (i,aModule->CategoryNumber (CN,anEnt,theShares));
65   }
66 }
67
68 Standard_Integer Interface_Category::Num (const Standard_Integer theNumEnt) const
69 {
70   if (myNum.IsNull()) return 0;
71   if (theNumEnt < 1 || theNumEnt > myNum->Length()) return 0;
72   return myNum->Value(theNumEnt);
73 }
74
75 // List of Categories
76
77 Standard_Integer Interface_Category::AddCategory (const Standard_CString theName)
78 {
79   Standard_Integer aNum = Interface_Category::Number (theName);
80   if (aNum > 0) return aNum;
81   theCats().Append (TCollection_AsciiString(theName));
82   return theCats().Length()+1;
83 }
84
85 Standard_Integer Interface_Category::NbCategories()
86 {
87   return theCats().Length();
88 }
89
90 Standard_CString Interface_Category::Name (const Standard_Integer theNum)
91 {
92   if (theNum < 0) return "";
93   if (theNum < theCats().Lower() || theNum > theCats().Upper()) return unspec;
94   return theCats().ChangeValue(theNum).ToCString();
95 }
96
97 Standard_Integer Interface_Category::Number (const Standard_CString theName)
98 {
99   Standard_Integer i;
100   for (i = theCats().Lower(); i <= theCats().Upper(); i ++) {
101     if (theCats().ChangeValue(i).IsEqual(theName)) return i;
102   }
103   return 0;
104 }
105
106 void Interface_Category::Init ()
107 {
108   // On first call, initialize static map
109   if ( !gMapTypesInit )
110   {
111     gMapTypesMutex.Lock();
112     if ( !gMapTypesInit )
113     {
114       if (init) return;  init = 1;
115       init = Interface_Category::AddCategory ("Shape");
116       init = Interface_Category::AddCategory ("Drawing");
117       init = Interface_Category::AddCategory ("Structure");
118       init = Interface_Category::AddCategory ("Description");
119       init = Interface_Category::AddCategory ("Auxiliary");
120       init = Interface_Category::AddCategory ("Professional");
121       init = Interface_Category::AddCategory ("FEA");
122       init = Interface_Category::AddCategory ("Kinematics");
123       init = Interface_Category::AddCategory ("Piping");
124
125       gMapTypesInit = Standard_True;
126     }
127     gMapTypesMutex.Unlock();
128   }
129 }