2

I'm trying to write a tool that moves a selected point from its old location to wherever I click, but can't seem to find the code in the MapBasic user guide.

Ideally, I would like to select an object, click the button for the tool, then click the new location and the point would be moved.

I have attempted to build the tool using the following code but when I click the new point where the feature should be located, it does not move.

enter image description here

2

My suggestion is very similar to @T_Bacon's solution.

I have implemented the recommendations that I gave in a comment to his answer.

Sub Main
   '// Adding a new Toolbutton to the Drawing buttonpad
   Alter ButtonPad ID 2 'Drawing   
      ToolButton
         Calling MoveSelectedPoint
         Icon MI_ICON_ARROW_1
         Cursor MI_CURSOR_CROSSHAIR
         DrawMode DM_CUSTOM_POINT
         HelpMsg "Click to move selected point\nClick to move selected point"

End Sub

Sub MoveSelectedPoint

Dim selectionTable as String
Dim myPoint as Object
Dim fX, fY As Float

   '// set your coordinate system before returning coordinates
   '// Can be referring to the map window
   Set Coordsys Window FrontWindow()

   '// Or you can set a specific hardcoded coordsys
   '//Set Coordsys ...    

   fX = CommandInfo(CMD_INFO_X)
   fY = CommandInfo(CMD_INFO_Y)

   If SelectionInfo(SEL_INFO_NROWS) > 1 then
      Note "Please select only one object!"
      Exit Sub
   ElseIf SelectionInfo(SEL_INFO_NROWS) < 1 then
      Note "No objects selected!"
      Exit Sub
   End If

   Fetch First From Selection
   myPoint = Selection.obj

   Alter Object myPoint Geography OBJ_GEO_POINTX, fX
   Alter Object myPoint Geography OBJ_GEO_POINTY, fY

   selectionTable = SelectionInfo(SEL_INFO_SELNAME)
   Update selectionTable 
      Set obj = myPoint

End Sub

If you are looking for some sample code, you can have a look at the DrawTools application on Github or the DrawTools-x64 application if you are on one of the 64 bit releases of MapInfo Pro.

1

This answer is for 32-bit versions of MapInfo Professional (without the ribbon menu). I'm not sure whether the new versions will be different but I haven't got one installed to check.

What you're looking for is the Alter Object statement. For a selected point you could do the following:

Dim myPoint as Object

myPoint = Selection.obj

Alter Object myPoint Geography OBJ_GEO_POINTX, newXCoord
Alter Object myPoint Geography OBJ_GEO_POINTY, newYCoord

If you're looking to get the new X and Y coordinates from a mouse click using the ToolButton functionality then you'll need to use the CommandInfo function to pull out that information in the reserved ToolHandler procedure.

Sub ToolHandler

    Dim selectionTable as String
    Dim myPoint as Object

    If SelectionInfo(SEL_INFO_NROWS) > 1 then
        Note "Please select only one object!"
        Exit Sub
    ElseIf SelectionInfo(SEL_INFO_NROWS) < 1 then
        Note "No objects selected!"
        Exit Sub
    End If

    myPoint = Selection.obj

    Set Coordsys ... '// set your coordinate system before returning coordinates   

    Alter Object myPoint Geography OBJ_GEO_POINTX, CommandInfo(CMD_INFO_X)
    Alter Object myPoint Geography OBJ_GEO_POINTY, CommandInfo(CMD_INFO_Y)

    selectionTable = SelectionInfo(SEL_INFO_SELNAME)
    Update selectionTable set obj = myPoint

End Sub

The above ToolHandler procedure is a special reserved procedure name in MapBasic and will automatically add the ToolButton ToolButton to the menu toolbar. I would recommend looking up ToolHandler, and CommandInfo in the MapBasic User Guide for a bit more information.

  • 1
    Neat, @T_Bacon! I would recommend building your own tool instead of using the ToolHandler but the code would be the exact same. And I would also recommend always to get the coordinate from the tool before doing other things in the code. – Peter Horsbøll Møller Apr 4 '17 at 8:39
  • Indeed you could use a custom ToolButton with it's own handler procedure which would give you a bit more flexibility in terms of button icons and drawing modes. – T_Bacon Apr 4 '17 at 9:35
  • I've tried both methods you guys suggested and in both cases, when I click the new point where the feature should be located, the point does not move. The tool doesn't crash or throw an error, and there are no issues when compiling. I'm using two subs to move features in two different .TAB files (as a check so that users don't intend to move one feature type but inadvertently move another). I've also slightly adjusted some variable names, otherwise have used the code verbatim. – ta harp Apr 4 '17 at 18:23
  • First thing to look at is that your Set Coordsys statement looks incomplete, you haven't specified a projection. You should probably use the method @peter-horsbøll-møller gave in his answer which takes the coordinates system of the front window (assuming this is a map window if you are selecting an object first?). Also, how is this procedure being called? Have you set up a custom ToolButton as in Peter's answer? – T_Bacon Apr 5 '17 at 14:16
  • I initially tried his method and it didn't work either. I just changed the code to Set Map CoordSys Earth Projection which has worked for me in the past. Could it be that I need to include a CreatePoint statement? fX = CommandInfo(CMD_INFO_X) fY = CommandInfo(CMD_INFO_Y) oObj = CreatePoint (fX,fY) – ta harp Apr 5 '17 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.