0023024: Update headers of OCCT files
[occt.git] / src / BRepMesh / BRepMesh_DiscretFactory.cxx
CommitLineData
b311480e 1// Created on: 2008-04-10
2// Created by: Peter KURNEV
3// Copyright (c) 2008-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21#include <BRepMesh_DiscretFactory.ixx>
22
23#include <OSD_SharedLibrary.hxx>
24#include <OSD_Function.hxx>
25#include <BRepMesh_IncrementalMesh.hxx>
0b97567d 26#include <BRepMesh_PDiscretRoot.hxx>
7fd59977 27
0b97567d
K
28namespace
29{
30 //! Embedded triangulation tool(s)
31 static TCollection_AsciiString THE_FAST_DISCRET_MESH ("FastDiscret");
32
33 //! Generate system-dependent name for dynamic library
34 //! (add standard prefixes and postfixes)
35 static void MakeLibName (const TCollection_AsciiString& theDefaultName,
36 TCollection_AsciiString& theLibName)
37 {
38 theLibName = "";
39 #ifndef WNT
40 theLibName += "lib";
41 #endif
42 theLibName += theDefaultName;
43 #ifdef WNT
44 theLibName += ".dll";
45 #elif __APPLE__
46 theLibName += ".dylib";
47 #elif defined (HPUX) || defined(_hpux)
48 theLibName += ".sl";
49 #else
50 theLibName += ".so";
51 #endif
52 }
53};
7fd59977 54
55//=======================================================================
56//function : BRepMesh_DiscretFactory
0b97567d 57//purpose :
7fd59977 58//=======================================================================
59BRepMesh_DiscretFactory::BRepMesh_DiscretFactory()
0b97567d
K
60: myPluginEntry (NULL),
61 myErrorStatus (BRepMesh_FE_NOERROR),
62 myDefaultName (THE_FAST_DISCRET_MESH),
63 myFunctionName ("DISCRETALGO")
7fd59977 64{
0b97567d
K
65 // register built-in meshing algorithms
66 myNames.Add (THE_FAST_DISCRET_MESH);
7fd59977 67}
0b97567d 68
7fd59977 69//=======================================================================
70//function : ~
0b97567d 71//purpose :
7fd59977 72//=======================================================================
73BRepMesh_DiscretFactory::~BRepMesh_DiscretFactory()
74{
75 Clear();
76}
0b97567d 77
7fd59977 78//=======================================================================
79//function : ~
0b97567d 80//purpose :
7fd59977 81//=======================================================================
82void BRepMesh_DiscretFactory::Clear()
83{
0b97567d 84 // what should we do here? Unload dynamic libraries and reset plugins list?
7fd59977 85}
0b97567d 86
7fd59977 87//=======================================================================
88//function : Get
0b97567d 89//purpose :
7fd59977 90//=======================================================================
0b97567d 91BRepMesh_DiscretFactory& BRepMesh_DiscretFactory::Get()
7fd59977 92{
0b97567d
K
93 //! global factory instance
94 static BRepMesh_DiscretFactory THE_GLOBAL_FACTORY;
95 return THE_GLOBAL_FACTORY;
7fd59977 96}
0b97567d 97
7fd59977 98//=======================================================================
99//function : ErrorStatus
0b97567d 100//purpose :
7fd59977 101//=======================================================================
0b97567d 102BRepMesh_FactoryError BRepMesh_DiscretFactory::ErrorStatus() const
7fd59977 103{
104 return myErrorStatus;
105}
0b97567d 106
7fd59977 107//=======================================================================
108//function : Names
0b97567d 109//purpose :
7fd59977 110//=======================================================================
0b97567d 111const TColStd_MapOfAsciiString& BRepMesh_DiscretFactory::Names() const
7fd59977 112{
113 return myNames;
114}
0b97567d 115
7fd59977 116//=======================================================================
117//function : SetDefaultName
0b97567d 118//purpose :
7fd59977 119//=======================================================================
0b97567d 120Standard_Boolean BRepMesh_DiscretFactory::SetDefaultName (const TCollection_AsciiString& theName)
7fd59977 121{
0b97567d 122 return SetDefault (theName, myFunctionName);
7fd59977 123}
0b97567d 124
7fd59977 125//=======================================================================
126//function : DefaultName
0b97567d 127//purpose :
7fd59977 128//=======================================================================
0b97567d 129const TCollection_AsciiString& BRepMesh_DiscretFactory::DefaultName() const
7fd59977 130{
131 return myDefaultName;
132}
0b97567d 133
7fd59977 134//=======================================================================
135//function : SetFunctionName
0b97567d 136//purpose :
7fd59977 137//=======================================================================
0b97567d 138Standard_Boolean BRepMesh_DiscretFactory::SetFunctionName (const TCollection_AsciiString& theFuncName)
7fd59977 139{
0b97567d 140 return SetDefault (myDefaultName, theFuncName);
7fd59977 141}
0b97567d 142
7fd59977 143//=======================================================================
144//function : FunctionName
0b97567d 145//purpose :
7fd59977 146//=======================================================================
0b97567d 147const TCollection_AsciiString& BRepMesh_DiscretFactory::FunctionName() const
7fd59977 148{
149 return myFunctionName;
150}
0b97567d 151
7fd59977 152//=======================================================================
0b97567d
K
153//function : SetDefault
154//purpose :
7fd59977 155//=======================================================================
0b97567d
K
156Standard_Boolean BRepMesh_DiscretFactory::SetDefault (const TCollection_AsciiString& theName,
157 const TCollection_AsciiString& theFuncName)
7fd59977 158{
0b97567d
K
159 myErrorStatus = BRepMesh_FE_NOERROR;
160 if (theName == THE_FAST_DISCRET_MESH)
161 {
162 // built-in, nothing to do
163 myPluginEntry = NULL;
164 myDefaultName = theName;
165 myFunctionName = theFuncName;
166 return Standard_True;
167 }
168 else if (theName == myDefaultName && theFuncName == myFunctionName)
169 {
170 // already active
171 return myPluginEntry != NULL;
7fd59977 172 }
0b97567d
K
173
174 TCollection_AsciiString aMeshAlgoId = theName + "_" + theFuncName;
175 BRepMesh_PluginEntryType aFunc = NULL;
176 if (myFactoryMethods.IsBound (aMeshAlgoId))
177 {
178 // retrieve from cache
179 aFunc = (BRepMesh_PluginEntryType )myFactoryMethods (aMeshAlgoId);
180 }
181 else
182 {
7fd59977 183 TCollection_AsciiString aLibName;
0b97567d
K
184 MakeLibName (theName, aLibName);
185 OSD_SharedLibrary aSL (aLibName.ToCString());
186 if (!aSL.DlOpen (OSD_RTLD_LAZY))
187 {
188 // library is not found
189 myErrorStatus = BRepMesh_FE_LIBRARYNOTFOUND;
190 return Standard_False;
7fd59977 191 }
0b97567d
K
192
193 // retrieve the function from plugin
194 aFunc = (BRepMesh_PluginEntryType )aSL.DlSymb (theFuncName.ToCString());
195 myFactoryMethods.Bind (aMeshAlgoId, (OSD_Function )aFunc);
196 }
197
198 if (aFunc == NULL)
199 {
200 // function is not found - invalid plugin?
201 myErrorStatus = BRepMesh_FE_FUNCTIONNOTFOUND;
202 return Standard_False;
203 }
204
205 // try to create dummy tool
206 BRepMesh_PDiscretRoot anInstancePtr = NULL;
207 Standard_Integer anErr = aFunc (TopoDS_Shape(), 0.001, 0.1, anInstancePtr);
208 if (anErr != 0 || anInstancePtr == NULL)
209 {
210 // can not create the algo specified
211 myErrorStatus = BRepMesh_FE_CANNOTCREATEALGO;
212 delete anInstancePtr;
213 return Standard_False;
7fd59977 214 }
0b97567d
K
215 delete anInstancePtr;
216
217 // if all checks done - switch to this tool
218 myPluginEntry = aFunc;
219 myDefaultName = theName;
220 myFunctionName = theFuncName;
221 myNames.Add (theName);
222 return Standard_True;
7fd59977 223}
0b97567d 224
7fd59977 225//=======================================================================
0b97567d
K
226//function : Discret
227//purpose :
7fd59977 228//=======================================================================
0b97567d
K
229Handle(BRepMesh_DiscretRoot) BRepMesh_DiscretFactory
230 ::Discret (const TopoDS_Shape& theShape,
231 const Standard_Real theDeflection,
232 const Standard_Real theAngle)
7fd59977 233{
0b97567d
K
234 Handle(BRepMesh_DiscretRoot) aDiscretRoot;
235 BRepMesh_PDiscretRoot anInstancePtr = NULL;
236 if (myPluginEntry != NULL)
237 {
238 // use plugin
239 Standard_Integer anErr = myPluginEntry (theShape, theDeflection, theAngle, anInstancePtr);
240 if (anErr != 0 || anInstancePtr == NULL)
241 {
242 // can not create the algo specified - should never happens here
243 myErrorStatus = BRepMesh_FE_CANNOTCREATEALGO;
244 return aDiscretRoot;
245 }
246 }
247 else //if (myDefaultName == THE_FAST_DISCRET_MESH)
248 {
249 // use built-in
250 BRepMesh_IncrementalMesh::Discret (theShape, theDeflection, theAngle, anInstancePtr);
251 }
252
253 // cover with handle
254 aDiscretRoot = anInstancePtr;
255
256 // return the handle
257 return aDiscretRoot;
7fd59977 258}