MapInfo Pro

 View Only

MapBasic Monday: An introduction to MapBasic

  • 1.  MapBasic Monday: An introduction to MapBasic

    Employee
    Posted 01-09-2023 10:31
    Edited by Peter Møller 02-03-2023 02:32
    Happy #MapInfoMonday!

    I'm again lending out the #MapInfoMonday space to its techie cousin, #MapBasicMonday​​​. During our latest MapInfo Pro Customer Support Forum, we got a request for some getting-started resources for MapBasic. This is the first of more posts on this topic.

    What is MapBasic?

    MapBasic is the scripting language for MapInfo Pro. It is based on the famous Basic language that was popular in the 80s. I used Basic to write simple games in school, such as "Shoot Down the Rocket". Basic games allow you to control your gun's position using the left and right keys on the keyboard.

    MapBasic of course has been extended to include all sorts of spatial functions allowing you to automate MapInfo Pro.

    You can find MapBasic in various places. MapBasic is used in workspaces to store the settings for a session in MapInfo Pro allowing you to restore your settings.

    MapBasic statements are in many cases executed when you click a control on the ribbon interface. You can see and capture these statements by opening the MapBasic window.

    MapBasic is also used to write and compile scripts into MapBasic executables, MBXs, that can be loaded into MapInfo Pro allowing you to either extend the capability of MapInfo Pro or automate repetitive tasks. 

    The MapBasic Window

    When you want to learn MapBasic I always recommend opening the MapBasic window in MapInfo Pro and seeing the statements capture in the window. These statements are put into the MapBasic window when you use the interface to perform a given task.

    This really is an easy way to start automating a task in MapInfo Pro: Open the MapBasic window, perform your task using the MapInfo Pro interface, and then see the MapBasic statement needed in the MapBasic window.

    You can open the MapBasic window from the Tool Windows dropdown on the Home tab.

    You can place and dock the MapBasic window where you'd like. I prefer to stick it to the bottom of the MapInfo Pro interface like this. In fact, I have set the MapBasic window to always open when I launch MapInfo Pro. Normally it only takes up a small part of my screen but when I need it, I'll make it higher like below to better be able to work with it.

    Let's try to capture some statements. I'll open a table in a new map window and change the settings of the layer with regard to override style and labels.

    As you can see, the MapBasic window captures the statements as I use the interface to open the table and change the layer settings too.

    The Open Table statement opens a table in MapInfo Pro. Map From create a new map window using one or more tables. In our example, just one table. The Set Map statements are used to change the map itself or the layers in the map. In the example above, the statements set a global style for the layer and set the layer to use style override, the second statement turns on automatically labeling and the third statement changes the label expression to use the column Contract.

    Now the great thing about the MapBasic window is that it doesn't only allow you to capture statements. You can also run statements from the MapBasic window. Place the cursor in the line you want to execute and hit the Enter key on the keyboard. Or select multiple statements if you want to execute multiple statements at a time.

    Note that you may have to use Ctrl+Enter to execute the statements. This depends on the preferences for the MapBasic & SQL window. If your preferences look like this, you can execute statements with just the Enter key. If Enter Key executes command is unchecked, you need to use Ctrl+Enter.

    Let us close all tables and try to execute the commands that opened the tables and configured the layer settings.

    Notice that I don't select the Close All Interactive statement in the MapBasic window. If I did, I wouldn't be able to see if it worked as it would close all after performing the actions.

    When I hit the Enter key to execute the selected commands, I get this error:

    The problem is that many of my commands refer to a specific window using a fixed ID, 3010. That is the normal way MapInfo Pro refers to windows as you can have multiple windows open. The problem here is that the ID is static. When MapInfo Pro created a new window that window will be assigned an ID. You need to use this ID when referring to the window afterward. In our example, we are referring to the active window, also known as the Front Window. MapBasic comes with a function that can give you the ID of the active window: FrontWindow(). All we have to do is to change 3010 to FrontWindow().

    Let's make this adjustment and try again. Also, remember to close the table before running the statements.

    And now it works. You have made your first automation! Congratulations!

    The MapBasic Editor

    Let's take this to an even higher level. Let's convert this small tool into a MapBasic application. It's not really that hard. I promise.

    First, you need to download and install the MapBasic compiler and editor. You can download it from the MapBasic downloads on Precisely.com. Make sure to download a version of MapBasic that matches your version of MapInfo Pro. Then install it and launch MapBasic. You will now see the MapBasic Editor.

    For this little example, copy the text from the MapBasic window inside MapInfo Pro. In MapBasic open a new file and paste the statements copied from the MapBasic window into this new file. Save the file with a saying name into a folder of your choice.

    From the Project menu, select Compile Current File. You can also use Ctrl + K to compile the file into an MBX, a MapBasic Executable.

    If the compilation was successful, you will see a dialog saying so. Otherwise, you can see potential errors listed at the bottom of the file. Click on each error to go to it. We will get into details about this at a later stage.

    If the compilation worked, you can now run the MBX from the Project menu using Run, or just use Ctrl + U.

    This was the first part of the introduction to MapBasic. We will extend this in a later article going into more detail about the structure of writing MapBasic applications. Stay tuned!

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------