A Short Course in MapBasic
MapBasic is a real programming language that you can use to automate MapInfo tasks, create extensions to MapInfo commands or even write turnkey applications. It is sold as a separate package as an addition to MapInfo, but it can only run on machines that have MapInfo installed.
It is based on the ancient programming language BASIC (which stands for Beginners All-purpose Symbolic Instruction Code) and is an easy language to learn.
Tools for Learning MapBasic - Workspaces
There are a number of tools and resources you should keep in mind when you first start learning MapBasic. The first is the Workspace. As you open maps and arrange layers, set styles and themes, MapInfo keeps track of all this and if you save your session as a workspace, everything you did to get the map(s) and browsers gets saved in the workspace. What's good about this is that workspaces are simply a recording of all the MapBasic commands that you need to recreate the session. So a workspace actually writes MapBasic code for you. If you're in a hurry, you can create a workspace and then copy out of it, the relevant commands you need to do what you want to do in your own program.
Check this out before you read on. Open MapInfo, and open one or more tables, place them in a map window, zoom and center the map to someplace interesting to you; maybe set a few labels, change the style for a layer, etc. Then save the session in a workpsace.
Now open that workspace file with a text editor (like notepad, wordpad, UltraEdit, etc.) and take a look at it. The first three lines (starting with an exclamation mark) are simply control information to allow MapInfo to recognize the file as a workspace. But everything after that is pure MapBadic. Keep this trick in mind; it's a quick way to get MapInfo to write your code for you!
The MapBasic Window
Another tool you can use is the MapBasic Window. You can execute MapBasic commands directly in this window, or you can perform actions via the menus and see the MapBasic code generated for each (or most) of these commands. To see this in action, open the MapBasic Window by choosing "Show MapBasic Window" option on the Options menu. Now, with this window open, open a table from the menu and display it on the map and then close the table. Notice that the Open Table, Map From and Close Table commands have been written for you.
Now, highlight the Open Table and Map From commands with the mouse, and press Enter. This executes the commands and you should see your table displayed as a map again. Now supposing you opened the table STATES, type in the command "Browse * from STATES" (without the quotes) and press Enter. If you opened a different table substitute that name for "STATES". This should open a browser of your table's data. Now place the cursor on the end of the Close Table command and press Enter. This also executes the command where the cursor is.
The MapBasic Window can execute all MapBasic code except for looping and branching statements which is quite a lot. You could use this technique to execute repetitious tasks where you generate the code the first time using the menus, then all you have do is change one or two things (such a table name) and then select the code, press Enter and this runs all the steps again with your changes. And of course, you can generate code easily this way and then paste it into your MapBasic program.
Next - Anatomy of a MapBasic Program
| subtopics: |