0024961: MeshVS: revision of DRAW commands
[occt.git] / src / BRepMesh / BRepMesh_DiscretFactory.cxx
CommitLineData
b311480e 1// Created on: 2008-04-10
2// Created by: Peter KURNEV
973c2be1 3// Copyright (c) 2008-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
fc9b36d6 16#include <BRepMesh_DiscretFactory.hxx>
7fd59977 17
18#include <OSD_SharedLibrary.hxx>
19#include <OSD_Function.hxx>
20#include <BRepMesh_IncrementalMesh.hxx>
0b97567d 21#include <BRepMesh_PDiscretRoot.hxx>
7fd59977 22
0b97567d
K
23namespace
24{
25 //! Embedded triangulation tool(s)
26 static TCollection_AsciiString THE_FAST_DISCRET_MESH ("FastDiscret");
27
28 //! Generate system-dependent name for dynamic library
29 //! (add standard prefixes and postfixes)
30 static void MakeLibName (const TCollection_AsciiString& theDefaultName,
31 TCollection_AsciiString& theLibName)
32 {
33 theLibName = "";
34 #ifndef WNT
35 theLibName += "lib";
36 #endif
37 theLibName += theDefaultName;
38 #ifdef WNT
39 theLibName += ".dll";
40 #elif __APPLE__
41 theLibName += ".dylib";
42 #elif defined (HPUX) || defined(_hpux)
43 theLibName += ".sl";
44 #else
45 theLibName += ".so";
46 #endif
47 }
48};
7fd59977 49
50//=======================================================================
51//function : BRepMesh_DiscretFactory
0b97567d 52//purpose :
7fd59977 53//=======================================================================
54BRepMesh_DiscretFactory::BRepMesh_DiscretFactory()
fc9b36d6 55: myPluginEntry (NULL),
56 myErrorStatus (BRepMesh_FE_NOERROR),
57 myDefaultName (THE_FAST_DISCRET_MESH),
0b97567d 58 myFunctionName ("DISCRETALGO")
7fd59977 59{
0b97567d 60 // register built-in meshing algorithms
fc9b36d6 61 myNames.Add(THE_FAST_DISCRET_MESH);
7fd59977 62}
0b97567d 63
7fd59977 64//=======================================================================
65//function : ~
0b97567d 66//purpose :
7fd59977 67//=======================================================================
68BRepMesh_DiscretFactory::~BRepMesh_DiscretFactory()
69{
fc9b36d6 70 clear();
7fd59977 71}
0b97567d 72
7fd59977 73//=======================================================================
fc9b36d6 74//function : clear
0b97567d 75//purpose :
7fd59977 76//=======================================================================
fc9b36d6 77void BRepMesh_DiscretFactory::clear()
7fd59977 78{
0b97567d 79 // what should we do here? Unload dynamic libraries and reset plugins list?
7fd59977 80}
0b97567d 81
7fd59977 82//=======================================================================
83//function : Get
0b97567d 84//purpose :
7fd59977 85//=======================================================================
0b97567d 86BRepMesh_DiscretFactory& BRepMesh_DiscretFactory::Get()
7fd59977 87{
0b97567d
K
88 //! global factory instance
89 static BRepMesh_DiscretFactory THE_GLOBAL_FACTORY;
90 return THE_GLOBAL_FACTORY;
7fd59977 91}
0b97567d 92
7fd59977 93//=======================================================================
0b97567d
K
94//function : SetDefault
95//purpose :
7fd59977 96//=======================================================================
fc9b36d6 97Standard_Boolean BRepMesh_DiscretFactory::SetDefault(
98 const TCollection_AsciiString& theName,
99 const TCollection_AsciiString& theFuncName)
7fd59977 100{
0b97567d
K
101 myErrorStatus = BRepMesh_FE_NOERROR;
102 if (theName == THE_FAST_DISCRET_MESH)
103 {
104 // built-in, nothing to do
105 myPluginEntry = NULL;
106 myDefaultName = theName;
107 myFunctionName = theFuncName;
108 return Standard_True;
109 }
110 else if (theName == myDefaultName && theFuncName == myFunctionName)
111 {
112 // already active
113 return myPluginEntry != NULL;
7fd59977 114 }
0b97567d
K
115
116 TCollection_AsciiString aMeshAlgoId = theName + "_" + theFuncName;
117 BRepMesh_PluginEntryType aFunc = NULL;
118 if (myFactoryMethods.IsBound (aMeshAlgoId))
119 {
120 // retrieve from cache
121 aFunc = (BRepMesh_PluginEntryType )myFactoryMethods (aMeshAlgoId);
122 }
123 else
124 {
7fd59977 125 TCollection_AsciiString aLibName;
0b97567d
K
126 MakeLibName (theName, aLibName);
127 OSD_SharedLibrary aSL (aLibName.ToCString());
128 if (!aSL.DlOpen (OSD_RTLD_LAZY))
129 {
130 // library is not found
131 myErrorStatus = BRepMesh_FE_LIBRARYNOTFOUND;
132 return Standard_False;
7fd59977 133 }
0b97567d
K
134
135 // retrieve the function from plugin
136 aFunc = (BRepMesh_PluginEntryType )aSL.DlSymb (theFuncName.ToCString());
137 myFactoryMethods.Bind (aMeshAlgoId, (OSD_Function )aFunc);
138 }
139
140 if (aFunc == NULL)
141 {
142 // function is not found - invalid plugin?
143 myErrorStatus = BRepMesh_FE_FUNCTIONNOTFOUND;
144 return Standard_False;
145 }
146
147 // try to create dummy tool
148 BRepMesh_PDiscretRoot anInstancePtr = NULL;
149 Standard_Integer anErr = aFunc (TopoDS_Shape(), 0.001, 0.1, anInstancePtr);
150 if (anErr != 0 || anInstancePtr == NULL)
151 {
152 // can not create the algo specified
153 myErrorStatus = BRepMesh_FE_CANNOTCREATEALGO;
154 delete anInstancePtr;
155 return Standard_False;
7fd59977 156 }
0b97567d
K
157 delete anInstancePtr;
158
159 // if all checks done - switch to this tool
160 myPluginEntry = aFunc;
161 myDefaultName = theName;
162 myFunctionName = theFuncName;
163 myNames.Add (theName);
164 return Standard_True;
7fd59977 165}
0b97567d 166
7fd59977 167//=======================================================================
0b97567d
K
168//function : Discret
169//purpose :
7fd59977 170//=======================================================================
fc9b36d6 171Handle(BRepMesh_DiscretRoot) BRepMesh_DiscretFactory::Discret(
172 const TopoDS_Shape& theShape,
173 const Standard_Real theDeflection,
174 const Standard_Real theAngle)
7fd59977 175{
0b97567d
K
176 Handle(BRepMesh_DiscretRoot) aDiscretRoot;
177 BRepMesh_PDiscretRoot anInstancePtr = NULL;
178 if (myPluginEntry != NULL)
179 {
180 // use plugin
fc9b36d6 181 Standard_Integer anErr = myPluginEntry (theShape,
182 theDeflection, theAngle, anInstancePtr);
183
0b97567d
K
184 if (anErr != 0 || anInstancePtr == NULL)
185 {
186 // can not create the algo specified - should never happens here
187 myErrorStatus = BRepMesh_FE_CANNOTCREATEALGO;
188 return aDiscretRoot;
189 }
190 }
191 else //if (myDefaultName == THE_FAST_DISCRET_MESH)
192 {
193 // use built-in
fc9b36d6 194 BRepMesh_IncrementalMesh::Discret (theShape,
195 theDeflection, theAngle, anInstancePtr);
0b97567d
K
196 }
197
198 // cover with handle
199 aDiscretRoot = anInstancePtr;
200
201 // return the handle
202 return aDiscretRoot;
7fd59977 203}