FE 'tkGooie' Utilities
3D Model File Generator
FE Home Page >
FE Downloads Page >
FE 'tkGooies' Description Page >
FE 'tkGooies' '3Dtools' Page >
This 3D Model File Generator (for polyhedra) Page
INTRODUCTION to a
Around April 2012, I found a couple of 3D model viewer Tk scripts on the web site wiki.tcl.tk.
I was inspired by the authors of those 2 scripts --- Mark Stucky and Gerard Sookahet --- to create some 3D model viewing utilities --- complete with fairly robust 3D model data importers (for a variety of 3D data formats --- such as Wavefront OBJ files, Cyberware PLY files, STL stereolithography files, CAD/CAE/FEA-like data files, etc.).
In 2012, I was getting started on some 3D viewing projects, but to aid in that endeavor, I found that it would be nice to have a variety of test files at hand --- boxes, tetrahedrons, octahedrons, icosahedrons, spheres, cones, cylinders, tori --- even buckyballs --- of various dimensions and in various formats.
And what better way to generate such files (with parameters such as widths, heights, depths, radii, etc.) than with Tcl.
And what better way to make an easy-and-quick-and-consolidated-and-self-documenting way of running the code that generates those files than Tk.
Some of the goals that I had in mind for the GUI included:
Here are a couple of screenshots that sum up in a few inches what the resulting GUI looks like. The 2 images indicate the current extent of the capabilities --- and some capabilities (model-types indicated by a hash-sign at the beginning of the line in the listbox) that may be implemented in the future.
The first image shows that I am dealing with a 'BOX-6-QUADS' model-type.
And the first image shows the parameter prompts for that model-type --- parameters: width,height,depth.
The image also indicates the (ASCII) 3D model file types that I plan to use in the future: OBJ format, PLY format, OFF format, and STL format.
The 2nd image indicates that I have clicked on the 'WriteFile' button and the label at the upper right of the GUI indicates that an OBJ file was written --- and the number vertex (point) and face (polygon) records written. (A text editor popped up showing the output file. I dismissed that editor window and took this screenshot.)
The listbox on the left of these images indicates that, so far, I have implemented at least one 'writer' for several model-types:
DESCRIPTION OF THE CODE
I provide the code for this 3D model generator GUI below.
I follow my usual 'canonical' structure for Tk code. So for this Tk script:
0) Set general window & widget parms (win-name, win-position, win-color-scheme, fonts, widget-geometry-parms, win-size-control, text-array-for-labels-etc). 1a) Define ALL frames (and sub-frames, if any). 1b) Pack ALL frames and sub-frames. 2) Define & pack all widgets in the frames, frame by frame. 3) Define keyboard and mouse/touchpad/touch-sensitive-screen action BINDINGS, if needed. 4) Define PROCS, if needed. 5) Additional GUI initialization (typically with one or more of the procs), if needed.
This Tk coding structure is discussed in more detail on the page A Canonical Structure for Tk Code --- and variations.
This makes it easy for me to find code sections --- while generating and testing this script, and when looking for code snippets to include in other scripts (code re-use).
One new thing that I have started doing in 2013 is using a text-array for text in labels, buttons, and other widgets in the GUI. This can make it easier for people to internationalize my scripts. I will be using a text-array like this in most of my scripts in the future.
Experimenting with the GUI
As in all my scripts that use the 'pack' geometry manager (which is all of my 100-plus scripts, so far), I provide the four main pack parameters --- '-side', '-anchor', '-fill', '-expand' --- on all of the 'pack' commands for the frames and widgets.
That helps me when I am initially testing the behavior of a GUI (the various widgets within it) as I resize the main window.
I think I have found a nice combination of the pack parameters. The listbox and its scrollbars expand/contract when the window is resized --- while the label and button widgets stay fixed in size and relative location.
You can experiment with the '-side', '-anchor', '-fill', and '-expand' parameters on the 'pack' commands for the various frames and widgets --- to get the widget behavior that you want.
Additional experimentation: You might want to change the fonts used for the various GUI widgets. For example, you could change '-weight' from 'bold' to 'normal' --- or '-slant' from 'roman' to 'italic'. Or change font families.
In fact, you may NEED to change the font families, because the families I used may not be available on your computer --- and the default font that the 'wish' interpreter chooses may not be very pleasing.
I use variables to set geometry parameters of widgets --- parameters such as border-widths and padding. And I have included the '-relief' parameter on the definitions of frames and widgets. Feel free to experiment with those 'appearance' parameters as well.
Some features of the code
That said, here's the code --- with plenty of comments to describe what most of the code-sections are doing.
One of the rather unusual features of this script is the fact that the 'parameters' frame is changed according to the 'model-type' selected.
You can look at the procs
to see how the pack forget command is used to change the widgets in the 'parameters' frame.
Those 'load_parameters_frame_XXX' procs also enable or disable the 'output-type' radiobuttons on the GUI to correspond to the currently available 'write' procs for any selected 'model-type'.
You can use the 'Help' button on the GUI to see information on how to add a 'model-type' to this utility. It involves adding a 'load_parameters_frame_XXX' proc and at least one 'write_model_XXX_YYY' proc to the script.
The bottom of the 'Help' also describes the formats of OBJ, PLY, OFF, and STL files --- and the help describes which parts of the format specifications are being supported by these 'writers'.
It is my hope that the copious comments in the code will help Tcl-Tk coding 'newbies' get started in making GUI's like this --- and perhaps have some fun with the 'pack forget' command.
Without the comments, potential young Tcler's might be tempted to return to their iPhones and iPads and iPods --- to watch Earth's/Jupiter's/Uranus's Funniest Home Videos.
Here is a link to CODE for the script '3DmodelGenerator.tk'.
Setting the Text Editor
Another not-so-common feature of this Tk script --- besides the 'pack forget' technique that is used to 'dynamically' change the parameters frame in the GUI --- is that, after the model file is written, it is displayed to the user in a GUI text editor of the user's choice.
You can see in the text of the 'HELPtext' variable, set at the bottom of the script above, that the user can edit the 'set' statement for variable 'EDITOR_text', near the bottom of the script, to set the text-editor to be the user's preferred editor.
The editor is called up from within the Tk script by using the Tcl 'exec' command.
The 'writers' are so fast that IMMEDIATELY after clicking on the 'WriteFile' button, the text-editor pops up displaying the model file.
Some Possible Enhancements
As I work on some 3D model and 3D surface viewing utilities in coming months/years, I may add several 'model types' to this GUI. So, if you find this utility of use, you may want to check back here in coming months/years to see if an updated version of the script is available.
Another possible enhancement/addition:
It took several days to get this code in shape. But now I am in a position to generate model files (of various dimensions and shapes and numbers of polygons/ponts) at about 30 microseconds per model file, instead of over 30 minutes per model file. :-) :-) :-) :-) :-)
Thank you, Tcl-Tk developers, for making it possible for me to make nice quality images of geometry in 3-space.
I changed the GUI rather significantly by changing the model-type selection from using a couple of rows of radiobuttons to a using a listbox on the left of the GUI. This facilitates adding more 'model-types'. (I updated the text and images above accordingly.)
I can also add more description of each model-type, by taking advantage of the x-scroll-bar of the listbox.
The y-scroll-bar allows for an unlimited number of added 'model-types'.
I have replaced the two GUI images above with the new images, and I have replaced the code above with the new code.
You can see the 'set' statement for the 'HELPtext' variable, near the bottom of the script, for a description of how to add new 'model-types' to the listbox.
Some 'model-types' that I have not implemented yet can be seen in the listbox of the images above. They are indicated by a hash-sign at the beginning of those listbox lines.
Over the coming months/years, I may implement more of those 'model-types'.
However, to accomodate a large number of types of models --- and to avoid putting a lot of model geometry data in the script, I may change the script to read from a library of model files --- and apply transformations to those 'unit' models --- such as magnification, translation, and rotation of coordinates --- to create a new model file --- say, in OBJ format.
Here is an image of the sphere model that was written out as an OBJ file,
and then read into my 3D model file viewer utility --- and shown with
random coloring of the model facets.
Bottom of this page for a
To return to a previously visited web page location, click on the
Back button of your web browser a sufficient number of times.
OR, use the History-list option of your web browser.
The first releasable version of this code was posted in 2012 Nov at http://wiki.tcl.tk/37286.
This FE web page was created 2014 May 14 ---