GMSH exporter

GMSH exporter

Postby bomastudio » Mon Oct 15, 2012 11:26 am

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:

1. define a lineloop
2. define a face for each lineloop.

But the lineloop has to be defined in such way:

Code: Select all
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!
0
Last edited by bomastudio on Tue Jul 02, 2013 10:29 am, edited 1 time in total.
User avatar
bomastudio 
PluginStore Author
PluginStore Author
 

Re: GMSH exporter

Postby Dan Rathbun » Mon Oct 15, 2012 5:33 pm

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 the Geom::PolygonMesh and the Face.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 to exmesh:
exmesh = Geom::PolygonMesh.new()
.. and later (probably with in loop, iterated an Array of Sketchup::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.
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: GMSH exporter

    Postby Dan Rathbun » Mon Oct 15, 2012 7:15 pm

    (6) The Ruby += operator is very slow for strings.

      This is because the Ruby interpreter changes a += b to a = a + b, and causes Ruby to create temporary String 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 new String object's +() method, which always creates a new String object (the 2nd, again internally calling String.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 the str object, passing the internal ref for "};\n" as the argument. And as explained above, +() always creates a new String instance object, so this is the 3rd.
      Lastly, Ruby has to make a reference re-assignment, assigning str 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 new String objects and a reference re-assignment for str, can become ...
    str << "};" << newline
    ... which only creates 1 new String object (for the literal quoted expression,) and NO reference re-assignment for str

    Speed optimization is especially important in loops.
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: GMSH exporter

      Postby bomastudio » Fri Oct 19, 2012 9:39 am

      Hi Dan, thank you for all the suggestion!!! :D Very, very usefull!! I'll made all the corrections as soon as possibile.
      What about the loop? Any solution? :D
      0
      User avatar
      bomastudio 
      PluginStore Author
      PluginStore Author
       

      Re: GMSH exporter

      Postby Chris Fullmer » Fri Oct 19, 2012 5:16 pm

      I'm not sure which will be more helpful, but play around with Face.outer_loop and Face.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?
      0
      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written
      User avatar
      Chris Fullmer 
      SketchUp Team
      SketchUp Team
       

      Re: GMSH exporter

      Postby Chris Fullmer » Fri Oct 19, 2012 5:19 pm

      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.
      0
      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written
      User avatar
      Chris Fullmer 
      SketchUp Team
      SketchUp Team
       

      Re: GMSH exporter

      Postby bomastudio » Wed Oct 24, 2012 8:20 pm

      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? :oops: :oops:
      0
      User avatar
      bomastudio 
      PluginStore Author
      PluginStore Author
       

      Re: GMSH exporter

      Postby TIG » Wed Oct 24, 2012 10:53 pm

      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 and apt=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...
      0
      TIG
      User avatar
      TIG 
      Global Moderator
       

      Re: GMSH exporter

      Postby Chris Fullmer » Thu Oct 25, 2012 1:20 am

      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.

      Code: Select all
      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
      0
      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written
      User avatar
      Chris Fullmer 
      SketchUp Team
      SketchUp Team
       

      Re: GMSH exporter

      Postby bomastudio » Tue Jul 02, 2013 9:28 am

      Hi guys
      I'm returning on this plugin. I added the license (GLP2), so it's open source :ecstatic:

      Do 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?
      0
      User avatar
      bomastudio 
      PluginStore Author
      PluginStore Author
       

      Re: GMSH exporter

      Postby Djskippy » Tue Jul 02, 2013 8:11 pm

      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 Gmsh

      The 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.
      0

      Djskippy 
       

      Re: GMSH exporter

      Postby Jsgodwin » Tue Jul 02, 2013 8:29 pm

      Thanks for posting this! When opening the .geo file in gmsh, visually everything looks correct, but gmsh lists errors when reading the line loops.
      0

      Jsgodwin 
       

      Re: GMSH exporter

      Postby Djskippy » Tue Jul 02, 2013 10:21 pm

      Jsgodwin wrote: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.
      0

      Djskippy 
       

      Re: GMSH exporter

      Postby bomastudio » Fri Jul 05, 2013 2:48 pm

      Djskippy wrote: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 Gmsh

      The 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.



      :ecstatic: :ecstatic: Thanks!!!!! I was looking for this plugin for a long long time!!!!
      0
      User avatar
      bomastudio 
      PluginStore Author
      PluginStore Author
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: No registered users and 8 guests

      Visit our sponsors: