MapInfo Pro

 View Only

MapInfo Monday: Exporting Labels as Text Objects

  • 1.  MapInfo Monday: Exporting Labels as Text Objects

    Employee
    Posted 05-19-2025 03:53
    Edited by Peter Møller 05-19-2025 04:48
      |   view attached

    Last week, I helped @Joerg Huebner from our Technical Support team with an inquiry from a customer.

    The customer was looking for a way to export labels to DXF. The labels were dimensions on measurement lines. Below, you see an example of dimension lines with the dimension shown as labels for each line. I used the Dimension Line tool from MapCAD to create these.

    They had looked into using the Labeler tool, which can convert labels in the active map to text objects. 
    Unfortunately, MapInfo Pro only generates labels for the map extent. This means that the Labeler tool will only generate the labels for a small subset of the data.
    The obvious thought here would be to zoom out to the full extent of the table. However, this doesn't work because the labels often look their best in a limited zoom range. If you zoom out too far, the labels will become too big.
    So the question was: How can we convert labels for all our dimension lines to text so that we can export these to DXF?

    Thought Process

    My first idea was related to the Labeler tool that they had tried out.
    You can find the source code for most of the tools that come with MapInfo Pro in the Samples folder in the MapBasic installation folder. This does require that you install MapBasic, of course. With this source code, you can modify the existing tool to meet a requirement that the tool doesn't support by default.
    My idea was to modify the Labeler tool to pan through the extent of the data in the table. I would start in the top left corner and pan to the right. For each map, I would export the labels. When I got to the right data extent, I would move the map back to the left data extent and move it down.
    In this way, you could create all the labels for the table at the current zoom of your map. You would need to keep track of the ROWIDs that you create labels for. This is to ensure you only create one label for each object.
    This idea led me to look into the MapBasic functions that are available around labels. Maybe I could do this without panning the map?
    You can extract the labels for a specific layer in a map using the LabelInfo() function. You must initialize the label pointer by calling the LabelFindFirst() function, and then loop over all labels using the LabelFindNext() function. Find more about this process in the LabelInfo() function help.
    That's the way the Labeler tool extracts the labels. So again, this is limited to the labels in the current map extent.
    I then investigated the way you can create text using MapBasic.
    MapBasic has a statement that can create text: Create Text. It also has a function that can create text: CreateText().
    The statement creates a single text per statement. The function CreateText(), on the other hand, can be used in an Update statement.
    It also includes parameters similar to labels, and it uses a map to control the size of the text in combination with the current Font style.
    The syntax looks like this:
    CreateText( window_id, x, y, text, angle, anchor, offset )
    Most of these parameters can be read directly from the table and the label settings for the layer.
    The angle parameter could be a bit tricky, but I knew that the DrawTools tool could return an angle from a (poly)line.
    That final option was worth exploring further. You can find the help section for the CreateText() function here.

    Final Solution

    Here are the steps to reproduce this method of converting labels to text objects.

    1. Create a copy of your measurement table, as you will replace the line objects with text objects.

    2. Add new columns to hold the rotation and the text you use as your labels. I named these DimensionText and Rotation. The first is of type Char(10) and the other is of type Float.
    3. Use these statements to update the Rotation column and the DiensionText column:
    Print DTSetCoordsysUsingTable("DimensioningText")
    Update DimensioningText
      Set Rotation = DTLineDirection(OBJ),
          DimensionText = Format$(CartesianObjectLen(Object, "m"), "#.00")+" m"

    Note that you need to have DrawTools running when executing these statements. The functions DTSetCoordsysUsingTable() and DTLineDirection() are published by DrawTools into the MapInfo Pro interface. You also need to be running at least DrawTools v3.1.4, see below under DrawTools.

    4. To control the orientation of the text, we will make sure it always appears correct. We do this by changing some of the rotation values. If the rotation is between 90 and 270, we add 180 to it. And finally, we make sure the Rotation is smaller than 360.

    Update DimensioningText
      Set Rotation = IIf(Rotation >= 90 And Rotation <= 270, Rotation + 180, Rotation) 
    Update DimensioningText
      Set Rotation = IIf(Rotation > 360, Rotation - 360, Rotation) 
    Commit Table DimensioningText

    5. Finally, it is time to convert the lines to text objects. Make sure your active window is at the correct zoom level and that the current font is the font you want to use for your text objects.

    Set CoordSys Table DimensioningText
    Update DimensioningText 
      Set OBJ = CreateText(FrontWindow(), CentroidX(OBJ), CentroidY(OBJ), DimensionText, Rotation, 2, 2) 
    

    The final two values configure the anchor of the text object (2 = LAYER_INFO_LBL_POS_TC = Top Center) and the offset from the anchor position in points. You get these values from the label settings of the layer.

    You can run the entire script from the MapBasic window or the SQL Window. You could also include the first two steps: Create a copy of the table and add the additional two columns to the script.

    The final text objects will look like the labels. In the map below, I have turned off the display of the original layer.
    Now with a table of text objects, I can export this to, for example, DXF.
    In this example, we created text rotated with the lines. That's why we needed to calculate the rotation.

    DrawTools

    I was struggling a bit getting DrawTools to return the correct direction for the lines. The all seems a bit off. Not much, but enough to be noticed.

    Finally, I realized that the function in DrawTools used to calculate the direction wasn't using the same projection as my table in my MapInfo Pro session.

    I needed a way to set the projection inside DrawTools from my script.

    That required a new version of DrawTools where I added two new procedures that were also registered and so published for MapInfo Pro to see and use them.

    This means that you need a later version of DrawTools to make this solution work. I have attached v3.1.4 to this post, and I will get the MapInfo Marketplace updated with this or a newer version very soon.



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

    Attachment(s)

    zip
    DrawTools x64.zip   2.08 MB 1 version