Commit 6c1f01f9 by Christophe Geuzaine

basic gmsh::graphics and gmsh::fltk API

parent b0fb4375
......@@ -45,6 +45,10 @@
#include "PluginManager.h"
#endif
#if defined(HAVE_OPENGL)
#include "drawContext.h"
#endif
#if defined(HAVE_FLTK)
#include "FlGui.h"
#endif
......@@ -1909,6 +1913,24 @@ void gmsh::model::occ::importShapes(const std::string &fileName,
}
}
// void gmsh::model::occ::importShapesNativePointer(const void *shape,
// vector_pair &outDimTags,
// const bool highestDimOnly)
// {
// if(!_isInitialized()){ throw -1; }
// _createOcc();
// outDimTags.clear();
// #if defined(HAVE_OCC)
// if(!GModel::current()->getOCCInternals()->importShapes
// (static_cast<TopoDS_Shape*>(shape), outDimTags, highestDimOnly)){
// throw 1;
// }
// #else
// Msg::Error("Gmsh requires OpenCASCADE to import native shape");
// throw -1;
// #endif
// }
void gmsh::model::occ::setMeshSize(const vector_pair &dimTags, const double size)
{
if(!_isInitialized()){ throw -1; }
......@@ -2217,18 +2239,52 @@ void gmsh::plugin::run(const std::string &name)
// gmsh::graphics
void gmsh::graphics::runFltkGui()
void gmsh::graphics::draw()
{
#if defined(HAVE_OPENGL)
drawContext::global()->draw();
#endif
}
// gmsh::fltk
void gmsh::fltk::initialize()
{
if(!_isInitialized()){ throw -1; }
#if defined(HAVE_FLTK)
FlGui::instance(_argc, _argv);
if(GModel::current()->getFileName().empty())
GModel::current()->setFileName("untitled.geo");
if(!FlGui::instance()->run()){
throw 1;
}
FlGui::check();
#else
Msg::Error("Fltk not available");
throw -1;
#endif
}
void gmsh::fltk::wait(const double time)
{
if(!_isInitialized()){ throw -1; }
#if defined(HAVE_FLTK)
if(!FlGui::available())
FlGui::instance(_argc, _argv);
if(time >= 0)
FlGui::wait(time);
else
FlGui::wait();
#else
Msg::Error("Fltk not available");
throw -1;
#endif
}
void gmsh::fltk::run()
{
if(!_isInitialized()){ throw -1; }
#if defined(HAVE_FLTK)
if(!FlGui::available())
FlGui::instance(_argc, _argv);
FlGui::instance()->run(); // this calls draw() once
#else
Msg::Error("Fltk GUI not available");
Msg::Error("Fltk not available");
throw -1;
#endif
}
......@@ -515,7 +515,6 @@ FlGui *FlGui::instance(int argc, char **argv)
_instance = new FlGui(argc, argv);
// set all options in the new GUI
InitOptionsGUI(0);
// say welcome!
Msg::StatusBar(false, "Gmsh %s", GetGmshVersion());
// log the following for bug reports
......@@ -531,17 +530,15 @@ FlGui *FlGui::instance(int argc, char **argv)
Msg::Info("Launch date : %s", Msg::GetLaunchDate().c_str());
Msg::Info("Command line : %s", Msg::GetCommandLineArgs().c_str());
Msg::Info("-------------------------------------------------------");
// set global bounding box in CTX (necessary if we run the gui without any
// model/post-processing data)
SetBoundingBox();
}
return _instance;
}
int FlGui::run()
{
// bounding box computation necessary if we run the gui without merging any
// files (e.g. if we build the geometry with python and create the gui from
// the python script)
SetBoundingBox();
// draw the scene
drawContext::global()->draw();
......@@ -955,7 +952,9 @@ void FlGui::setGraphicTitle(std::string title)
{
for(unsigned int i = 0; i < graph.size(); i++){
std::ostringstream sstream;
if(!i)
if(title.empty())
sstream << "Gmsh";
else if(!i)
sstream << "Gmsh - " << title;
else
sstream << "Gmsh - " << title << " [" << i << "]";
......
......@@ -1225,6 +1225,9 @@ void GEO_Internals::synchronize(GModel *model)
}
}
// recompute global boundind box in CTX
SetBoundingBox();
Msg::Debug("GModel imported:");
Msg::Debug("%d vertices", model->getNumVertices());
Msg::Debug("%d edges", model->getNumEdges());
......
......@@ -2926,6 +2926,9 @@ void OCC_Internals::synchronize(GModel *model)
_copyExtrudedMeshAttributes(region, occr);
}
// recompute global boundind box in CTX
SetBoundingBox();
Msg::Debug("GModel imported:");
Msg::Debug("%d vertices", model->getNumVertices());
Msg::Debug("%d edges", model->getNumEdges());
......@@ -3740,7 +3743,6 @@ int GModel::importOCCShape(const void *shape)
#endif
_occ_internals->synchronize(this);
snapVertices();
SetBoundingBox();
return 1;
}
......
......@@ -408,6 +408,9 @@ occ.add('removeAllDuplicates',doc,None)
doc = '''Imports BREP, STEP or IGES shapes from the file `fileName'. The imported entities are returned in `outDimTags'. If the optional argument `highestDimOnly' is set, only import the highest dimensional entities in the file. The optional argument `format' can be used to force the format of the file (currently "brep", "step" or "iges").'''
occ.add('importShapes',doc,None,istring('fileName'),ovectorpair('outDimTags'),ibool('highestDimOnly','true','True'),istring('format','""'))
#doc = '''Imports native OpenCASCADE shapes by providing a raw pointer to a TopoDS_Shape. The imported entities are returned in `outDimTags'. If the optional argument `highestDimOnly' is set, only import the highest dimensional entities in the file.'''
#occ.add('importShapesNativePointer',doc,None,voidstar('shape'),ovectorpair('outDimTags'),ibool('highestDimOnly','true','True'))
doc = '''Sets a mesh size constraint on the geometrical entities `dimTags'. Currently only entities of dimension 0 (points) are handled.'''
occ.add('setMeshSize',doc,None,ivectorpair('dimTags'),idouble('size'))
......@@ -459,8 +462,21 @@ plugin.add('run',doc,None,istring('name'))
graphics = gmsh.add_module('graphics','Graphics functions')
doc = '''Launch the FLTK graphical user interface.'''
graphics.add('runFltkGui',doc,None)
doc = '''Draws all the OpenGL scenes.'''
graphics.add('draw',doc,None)
################################################################################
fltk = gmsh.add_module('fltk','Fltk graphical user interface functions')
doc = '''Creates the Fltk graphical user interface.'''
fltk.add('initialize',doc,None)
doc = '''Waits at most `time' seconds for user interface events and returns. If `time' < 0, wait indefinitely.'''
fltk.add('wait',doc,None,idouble('time', '-1.'))
doc = '''Runs the event loop of the Fltk graphical user interface, i.e. repeatedly calls `wait()`. First automatically creates the user interface if it has not yet been initialized.'''
fltk.add('run',doc,None)
################################################################################
......
......@@ -1184,11 +1184,27 @@ namespace gmsh { // Top-level functions
namespace graphics { // Graphics functions
// Launch the FLTK graphical user interface.
GMSH_API void runFltkGui();
// Draws all the OpenGL scenes.
GMSH_API void draw();
} // namespace graphics
namespace fltk { // Fltk graphical user interface functions
// Creates the Fltk graphical user interface.
GMSH_API void initialize();
// Waits at most `time' seconds for user interface events and returns. If
// `time' < 0, wait indefinitely.
GMSH_API void wait(const double time = -1.);
// Runs the event loop of the Fltk graphical user interface, i.e. repeatedly
// calls `wait()`. First automatically creates the user interface if it has not
// yet been initialized.
GMSH_API void run();
} // namespace fltk
} // namespace gmsh
#undef GMSH_API
......
......@@ -3094,14 +3094,63 @@ class graphics:
"""
@staticmethod
def runFltkGui():
def draw():
"""
Launch the FLTK graphical user interface.
Draws all the OpenGL scenes.
"""
ierr = c_int()
lib.gmshGraphicsRunFltkGui(
lib.gmshGraphicsDraw(
byref(ierr))
if ierr.value != 0 :
raise ValueError(
"gmshGraphicsRunFltkGui returned non-zero error code : ",
"gmshGraphicsDraw returned non-zero error code : ",
ierr.value)
class fltk:
"""
Fltk graphical user interface functions
"""
@staticmethod
def initialize():
"""
Creates the Fltk graphical user interface.
"""
ierr = c_int()
lib.gmshFltkInitialize(
byref(ierr))
if ierr.value != 0 :
raise ValueError(
"gmshFltkInitialize returned non-zero error code : ",
ierr.value)
@staticmethod
def wait(time=-1.):
"""
Waits at most `time' seconds for user interface events and returns. If
`time' < 0, wait indefinitely.
"""
ierr = c_int()
lib.gmshFltkWait(
c_double(time),
byref(ierr))
if ierr.value != 0 :
raise ValueError(
"gmshFltkWait returned non-zero error code : ",
ierr.value)
@staticmethod
def run():
"""
Runs the event loop of the Fltk graphical user interface, i.e. repeatedly
calls `wait()`. First automatically creates the user interface if it has
not yet been initialized.
"""
ierr = c_int()
lib.gmshFltkRun(
byref(ierr))
if ierr.value != 0 :
raise ValueError(
"gmshFltkRun returned non-zero error code : ",
ierr.value)
......@@ -1161,10 +1161,31 @@ void gmshPluginRun(const char * name,int * ierr){
} catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}
}
void gmshGraphicsRunFltkGui(int * ierr){
void gmshGraphicsDraw(int * ierr){
if(ierr) *ierr = 0;
try {
gmsh::graphics::runFltkGui();
gmsh::graphics::draw();
} catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}
}
void gmshFltkInitialize(int * ierr){
if(ierr) *ierr = 0;
try {
gmsh::fltk::initialize();
} catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}
}
void gmshFltkWait(const double time,int * ierr){
if(ierr) *ierr = 0;
try {
gmsh::fltk::wait(time);
} catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}
}
void gmshFltkRun(int * ierr){
if(ierr) *ierr = 0;
try {
gmsh::fltk::run();
} catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}
}
......@@ -1257,8 +1257,21 @@ GMSH_API void gmshPluginSetString(const char * name,
GMSH_API void gmshPluginRun(const char * name,
int * ierr);
/* Launch the FLTK graphical user interface. */
GMSH_API void gmshGraphicsRunFltkGui(int * ierr);
/* Draws all the OpenGL scenes. */
GMSH_API void gmshGraphicsDraw(int * ierr);
/* Creates the Fltk graphical user interface. */
GMSH_API void gmshFltkInitialize(int * ierr);
/* Waits at most `time' seconds for user interface events and returns. If
* `time' < 0, wait indefinitely. */
GMSH_API void gmshFltkWait(const double time,
int * ierr);
/* Runs the event loop of the Fltk graphical user interface, i.e. repeatedly
* calls `wait()`. First automatically creates the user interface if it has
* not yet been initialized. */
GMSH_API void gmshFltkRun(int * ierr);
#undef GMSH_API
#endif
#include <cstdio>
#include <gmsh.h>
int main(int argc, char **argv)
{
gmsh::initialize(argc, argv);
// creates the FLTK user interface; this could also be called after the
// geometry is created (or not at all - fltk.run() will do it automatically)
gmsh::fltk::initialize();
// Copied from boolean.cpp...
gmsh::model::add("boolean");
gmsh::option::setNumber("Mesh.Algorithm", 6);
gmsh::option::setNumber("Mesh.CharacteristicLengthMin", 0.4);
gmsh::option::setNumber("Mesh.CharacteristicLengthMax", 0.4);
double R = 1.4, Rs = R*.7, Rt = R*1.25;
std::vector<std::pair<int, int> > ov;
std::vector<std::vector<std::pair<int, int> > > ovv;
gmsh::model::occ::addBox(-R,-R,-R, 2*R,2*R,2*R, 1);
gmsh::model::occ::addSphere(0,0,0,Rt, 2);
gmsh::model::occ::intersect({{3, 1}}, {{3, 2}}, ov, ovv, 3);
gmsh::model::occ::addCylinder(-2*R,0,0, 4*R,0,0, Rs, 4);
gmsh::model::occ::addCylinder(0,-2*R,0, 0,4*R,0, Rs, 5);
gmsh::model::occ::addCylinder(0,0,-2*R, 0,0,4*R, Rs, 6);
gmsh::model::occ::fuse({{3, 4}, {3, 5}}, {{3, 6}}, ov, ovv, 7);
gmsh::model::occ::cut({{3, 3}}, {{3, 7}}, ov, ovv, 8);
gmsh::model::occ::synchronize();
// ...end of copy
// this would be equivalent to gmsh.fltk.run():
//
// gmsh::graphics::draw();
// while(1){
// gmsh::fltk::wait();
// std::printf("just treated an event in the interface\n");
// }
gmsh::fltk::run();
gmsh::finalize();
return 0;
}
import gmsh
import sys
model = gmsh.model
factory = model.occ
gmsh.initialize(sys.argv)
# creates the FLTK user interface; this could also be called after the geometry
# is created (or not at all - fltk.run() will do it automatically)
gmsh.fltk.initialize()
# Copied from boolean.py...
model.add("boolean")
gmsh.option.setNumber("Mesh.Algorithm", 6);
gmsh.option.setNumber("Mesh.CharacteristicLengthMin", 0.4);
gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 0.4);
R = 1.4; Rs = R*.7; Rt = R*1.25
factory.addBox(-R,-R,-R, 2*R,2*R,2*R, 1)
factory.addSphere(0,0,0,Rt, 2)
factory.intersect([(3, 1)], [(3, 2)], 3)
factory.addCylinder(-2*R,0,0, 4*R,0,0, Rs, 4)
factory.addCylinder(0,-2*R,0, 0,4*R,0, Rs, 5)
factory.addCylinder(0,0,-2*R, 0,0,4*R, Rs, 6)
factory.fuse([(3, 4), (3, 5)], [(3, 6)], 7)
factory.cut([(3, 3)], [(3, 7)], 8)
factory.synchronize();
# ...end of copy
# this would be equivalent to gmsh.fltk.run():
#
# gmsh.graphics.draw();
# while 1:
# gmsh.fltk.wait()
# print "just treated an event in the interface"
gmsh.fltk.run()
gmsh.finalize()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment