summaryrefslogtreecommitdiff
path: root/src/BRepMesh/BRepMesh_DiscretFactory.cxx
blob: 6d4ce30abdd8eb0ce68ee83b85220af22dbc2ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// File:    BRepMesh_DiscretFactory.cxx
// Created: Thu Apr 10 13:32:00 2008
// Author:  Peter KURNEV <pkv@irinox>

#include <BRepMesh_DiscretFactory.ixx>

#include <OSD_SharedLibrary.hxx>
#include <OSD_Function.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <BRepMesh_PDiscretRoot.hxx>

namespace
{
  //! Embedded triangulation tool(s)
  static TCollection_AsciiString THE_FAST_DISCRET_MESH ("FastDiscret");

  //! Generate system-dependent name for dynamic library
  //! (add standard prefixes and postfixes)
  static void MakeLibName (const TCollection_AsciiString& theDefaultName,
                                 TCollection_AsciiString& theLibName)
  {
    theLibName = "";
  #ifndef WNT
    theLibName += "lib";
  #endif
    theLibName += theDefaultName;
  #ifdef WNT
    theLibName += ".dll";
  #elif __APPLE__
    theLibName += ".dylib";
  #elif defined (HPUX) || defined(_hpux)
    theLibName += ".sl";
  #else
    theLibName += ".so";
  #endif
  }
};

//=======================================================================
//function : BRepMesh_DiscretFactory
//purpose  :
//=======================================================================
BRepMesh_DiscretFactory::BRepMesh_DiscretFactory()
: myPluginEntry (NULL),
  myErrorStatus (BRepMesh_FE_NOERROR),
  myDefaultName (THE_FAST_DISCRET_MESH),
  myFunctionName ("DISCRETALGO")
{
  // register built-in meshing algorithms
  myNames.Add (THE_FAST_DISCRET_MESH);
}

//=======================================================================
//function : ~
//purpose  :
//=======================================================================
BRepMesh_DiscretFactory::~BRepMesh_DiscretFactory()
{
  Clear();
}

//=======================================================================
//function : ~
//purpose  :
//=======================================================================
void BRepMesh_DiscretFactory::Clear()
{
  // what should we do here? Unload dynamic libraries and reset plugins list?
}

//=======================================================================
//function : Get
//purpose  :
//=======================================================================
BRepMesh_DiscretFactory& BRepMesh_DiscretFactory::Get()
{
#ifdef __BORLANDC__
  static BRepMesh_DiscretFactory* sFactory = new BRepMesh_DiscretFactory;
  return *sFactory;
#else
  //! global factory instance
  static BRepMesh_DiscretFactory THE_GLOBAL_FACTORY;
  return THE_GLOBAL_FACTORY;
#endif
}

//=======================================================================
//function : ErrorStatus
//purpose  :
//=======================================================================
BRepMesh_FactoryError BRepMesh_DiscretFactory::ErrorStatus() const
{
  return myErrorStatus;
}

//=======================================================================
//function : Names
//purpose  :
//=======================================================================
const TColStd_MapOfAsciiString& BRepMesh_DiscretFactory::Names() const
{
  return myNames;
}

//=======================================================================
//function : SetDefaultName
//purpose  :
//=======================================================================
Standard_Boolean BRepMesh_DiscretFactory::SetDefaultName (const TCollection_AsciiString& theName)
{
  return SetDefault (theName, myFunctionName);
}

//=======================================================================
//function : DefaultName
//purpose  :
//=======================================================================
const TCollection_AsciiString& BRepMesh_DiscretFactory::DefaultName() const
{
  return myDefaultName;
}

//=======================================================================
//function : SetFunctionName
//purpose  :
//=======================================================================
Standard_Boolean BRepMesh_DiscretFactory::SetFunctionName (const TCollection_AsciiString& theFuncName)
{
  return SetDefault (myDefaultName, theFuncName);
}

//=======================================================================
//function : FunctionName
//purpose  :
//=======================================================================
const TCollection_AsciiString& BRepMesh_DiscretFactory::FunctionName() const
{
  return myFunctionName;
}

//=======================================================================
//function : SetDefault
//purpose  :
//=======================================================================
Standard_Boolean BRepMesh_DiscretFactory::SetDefault (const TCollection_AsciiString& theName,
                                                      const TCollection_AsciiString& theFuncName)
{
  myErrorStatus = BRepMesh_FE_NOERROR;
  if (theName == THE_FAST_DISCRET_MESH)
  {
    // built-in, nothing to do
    myPluginEntry  = NULL;
    myDefaultName  = theName;
    myFunctionName = theFuncName;
    return Standard_True;
  }
  else if (theName == myDefaultName && theFuncName == myFunctionName)
  {
    // already active
    return myPluginEntry != NULL;
  }

  TCollection_AsciiString aMeshAlgoId = theName + "_" + theFuncName;
  BRepMesh_PluginEntryType aFunc = NULL;
  if (myFactoryMethods.IsBound (aMeshAlgoId))
  {
    // retrieve from cache
    aFunc = (BRepMesh_PluginEntryType )myFactoryMethods (aMeshAlgoId);
  }
  else
  {
    TCollection_AsciiString aLibName;
    MakeLibName (theName, aLibName);
    OSD_SharedLibrary aSL (aLibName.ToCString());
    if (!aSL.DlOpen (OSD_RTLD_LAZY))
    {
      // library is not found
      myErrorStatus = BRepMesh_FE_LIBRARYNOTFOUND;
      return Standard_False;
    }

    // retrieve the function from plugin
    aFunc = (BRepMesh_PluginEntryType )aSL.DlSymb (theFuncName.ToCString());
    myFactoryMethods.Bind (aMeshAlgoId, (OSD_Function )aFunc);
  }

  if (aFunc == NULL)
  {
    // function is not found - invalid plugin?
    myErrorStatus = BRepMesh_FE_FUNCTIONNOTFOUND;
    return Standard_False;
  }

  // try to create dummy tool
  BRepMesh_PDiscretRoot anInstancePtr = NULL;
  Standard_Integer anErr = aFunc (TopoDS_Shape(), 0.001, 0.1, anInstancePtr);
  if (anErr != 0 || anInstancePtr == NULL)
  {
    // can not create the algo specified  
    myErrorStatus = BRepMesh_FE_CANNOTCREATEALGO;
    delete anInstancePtr;
    return Standard_False;
  }
  delete anInstancePtr;

  // if all checks done - switch to this tool
  myPluginEntry  = aFunc;
  myDefaultName  = theName;
  myFunctionName = theFuncName;
  myNames.Add (theName);
  return Standard_True;
}

//=======================================================================
//function : Discret
//purpose  :
//=======================================================================
Handle(BRepMesh_DiscretRoot) BRepMesh_DiscretFactory
  ::Discret (const TopoDS_Shape& theShape,
             const Standard_Real theDeflection,
             const Standard_Real theAngle)
{
  Handle(BRepMesh_DiscretRoot) aDiscretRoot;
  BRepMesh_PDiscretRoot anInstancePtr = NULL;
  if (myPluginEntry != NULL)
  {
    // use plugin
    Standard_Integer anErr = myPluginEntry (theShape, theDeflection, theAngle, anInstancePtr);
    if (anErr != 0 || anInstancePtr == NULL)
    {
      // can not create the algo specified - should never happens here
      myErrorStatus = BRepMesh_FE_CANNOTCREATEALGO;
      return aDiscretRoot;
    }
  }
  else //if (myDefaultName == THE_FAST_DISCRET_MESH)
  {
    // use built-in
    BRepMesh_IncrementalMesh::Discret (theShape, theDeflection, theAngle, anInstancePtr);
  }

  // cover with handle
  aDiscretRoot = anInstancePtr;

  // return the handle
  return aDiscretRoot;
}