GMSH exporter
-
Hi all, I'm writing a SketchUp to GMSH exporter. GMSH, if someone doesn't know what is, is an open source Finite Element Mesh generator http://geuz.org/gmsh/. I would like to "embed" GMSH inside SU (opaque to the user, of course) and draw/mesh/visualize the model all inside SU.In order to do this I wnat to use the *.geo file format. So now the task is to "convert" the SU geometry in the *.geo format. I'm able to export vertex, edges, but not the faces one as GMSH has a "strange" way of defining these:
- define a lineloop
- define a face for each lineloop.
But the lineloop has to be defined in such way:
Line Loop ( expression ) = { expression-list }; Creates an oriented line loop. The expression inside the parentheses is the line loop's identification number; the expression-list on the right hand side should contain the identification numbers of all the elementary lines that constitute the line loop. A line loop must be a closed loop, and the elementary lines should be ordered and oriented (using negative identification numbers to specify reverse orientation). If the orientation is correct, but the ordering is wrong, Gmsh will actually reorder the list internally to create a consistent loop. Although Gmsh supports it, it is not recommended to specify multiple line loops (or subloops) in a single Line Loop command. (Line loops are used to create surfaces; see Surfaces.)
I've attached the plugin if someone wants to read it. Thank to all can help!
-
OK...
(1) It is a no-no to change the Ruby base classes! (Yes we know that the round method was added in later versions of Ruby, but every author cannot be changing base classes. Either users should use ONE Ruby backport extension, ... or you should be using a custom Float subclass WITHIN your OWN namespace.)
(2) Your code is not wrapped within your OWN namespace modules(s). (The instance variables in your code belong to
Object
so are really global instance variables, and is also not nice.)- Choose a toplevel namespace such as:
module Boma
* Choose a sub-namespace such as:module Boma::Gmsh
(3) You are creating an undo operation, BUT:
- You are not making any changes to the model, so there will be nothing to actually "Undo."* File write operations cannot be undone with SketchUp's undo command. You would need to write a command that erases the file using Ruby's
File.delete()
method.
(4) I did a search through your code.. and did not find "
Geom::PolygonMesh
" nor ".mesh
", which means you are not leveraging the power and speed of a virtual memory mesh. Read up on theGeom::PolygonMesh
and theFace.mesh()
method.Example... you have a mesh object in memory
exmesh
that will be the mesh to export. You have a face in the model, whose mesh you wish to add toexmesh
:
exmesh = Geom::PolygonMesh.new()
.. and later (probably with in loop, iterated anArray
ofSketchup::Face
objects.):
exmesh.add_polygon( face.mesh.polygons )
(5) Ruby is an English programming language, it is easier to help with your code, if you publish using English var and method names. (A search and replace macro run in your editor.)
Just a few ideas.
- Choose a toplevel namespace such as:
-
(6) The Ruby
+=
operator is very slow for strings.This is because the Ruby interpreter changes
a += b
toa = a + b
, and causes Ruby to create temporaryString
instance objects.Example:
str =+ "};" + newline
Ruby first has to call (internally,)String.new()
for the literal value"};"
(the 1st,) so it can then access that newString
object's+()
method, which always creates a newString
object (the 2nd, again internally callingString.new
,) containing the value ="};\n"
AT this point (internally,) the expression is now:
str = str + "};\n"
Now Ruby has to call the instance method+()
for thestr
object, passing the internal ref for"};\n"
as the argument. And as explained above,+()
always creates a newString
instance object, so this is the 3rd.
Lastly, Ruby has to make a reference re-assignment, assigningstr
to point at the 3rd new string object's internal reference, which now becomes non-internal.Instead: use the
String
classes'<<()
"append" method, which can be chained, as it always returns the reference to it's receiver (ie, the original object.)
So:
str =+ "};" + newline
... which forces the creation of 3 newString
objects and a reference re-assignment forstr
, can become ...
str << "};" << newline
... which only creates 1 newString
object (for the literal quoted expression,) and NO reference re-assignment forstr
Speed optimization is especially important in loops.
-
Hi Dan, thank you for all the suggestion!!! Very, very usefull!! I'll made all the corrections as soon as possibile.
What about the loop? Any solution? -
I'm not sure which will be more helpful, but play around with
Face.outer_loop
andFace.loops
.https://developers.google.com/sketchup/docs/ourdoc/face
Outer look should return just the outer loop around a face. Loops should return an array of loops that bound the face, meaning if there are holes in the face, it will return the loop around those holes. It looked like gmsh didn't want faves with holes though. So you might triangulate your model first to get rid of holes in faces. Or I suppose you could just triangulate faces with holes in them?
-
Though I guess if you're using the quicker polygon mesh then the standard sketchup face loop might not help? I'm not too sure how that would all interact.
-
Ok. Now I'm working with the mesh. But the problem of the ordering, as GMSH needs, the edges of the mesh is still pending..... can you suggest how to solve?
-
Face.outer_loop.edges
returns the edges in ccw order: edges have .start and .end vertices, .length and .line [start+vector] etc...
Face.outer_loop.vertices
is similar for vertices -pt=vertex.position
gives a Point3d andapt=pt.to_a
returns an[x,y,z]
array.
If your file format needs YZ flipping adjust the array into[x,-z,y]
to solve this...
Point coordinates are in always inches [irrespective of the model's units] BUT these can be converted to other unit formats - e.g..to_m
. To make the 'number' into a string use.to_s
on it... -
If he's already got his mesh put together, would it make sense to use that object to then write all the vertices? Something like this will display them to your console for testing. the
mesh
I use in the first line is your mesh object.mesh.polygons.each_with_index do |e,index| unless e==nil puts "" puts "Face #{(index+1).to_s}" e.each_with_index do |pt, index2| puts "Vert# #{pt.to_s} - " + mesh.polygon_points_at(index+1)[index2].to_s end end end
-
Hi guys
I'm returning on this plugin. I added the license (GLP2), so it's open sourceDo you think it is a good idea to map the nodes with a Dictionary? As in IGES_EXPORT plugin http://sketchucation.com/forums/viewtopic.php?f=323&t=43307&hilit=iges?
-
Hi Bomastudio,
Some time ago I've written an export script from Sketchup to Gmsh.
Sketchup Groups are exported as Volumes in Gmsh
Sketchup Faces are exported as plane surfaces in Gmsh
Sketchup Edges are exported as lines in Gmsh
Sketchup Vertices are exported as points in GmshThe script handles nested groups and applies all transformations to the points in Gmsh
Inner faces are exported correctly.
It even exports text entered in Sketchup (not 3D text).The only thing this script doesn't do is: Export circles and arc's as Gmsh circles, it now just exports them as lines and points (that's how Sketchup see's them).
To correct this you'll need to take a look a IGES_EXPORT and how it exports circles, cylinders and cones.
-
Thanks for posting this! When opening the .geo file in gmsh, visually everything looks correct, but gmsh lists errors when reading the line loops.
-
@jsgodwin said:
Thanks for posting this! When opening the .geo file in gmsh, visually everything looks correct, but gmsh lists errors when reading the line loops.
You could give my exporter a try, the line loops are exported correctly.
-
@djskippy said:
Hi Bomastudio,
Some time ago I've written an export script from Sketchup to Gmsh.
Sketchup Groups are exported as Volumes in Gmsh
Sketchup Faces are exported as plane surfaces in Gmsh
Sketchup Edges are exported as lines in Gmsh
Sketchup Vertices are exported as points in GmshThe script handles nested groups and applies all transformations to the points in Gmsh
Inner faces are exported correctly.
It even exports text entered in Sketchup (not 3D text).The only thing this script doesn't do is: Export circles and arc's as Gmsh circles, it now just exports them as lines and points (that's how Sketchup see's them).
To correct this you'll need to take a look a IGES_EXPORT and how it exports circles, cylinders and cones.Thanks!!!!! I was looking for this plugin for a long long time!!!!
Advertisement