I'm trying to create pline using MapBasic. My idea was to create simple tool with dialog box containing couple of EditBox, PopupMenu's to retrieve pline attributes from user. So far I have only making tools with same logic for point object and my code would look something like:


Sub Main()

    Create ButtonPad AppName 
        As toolbutton
        Icon 16 
        DrawMode DM_CUSTOM_POINT 
        Calling SomePointSUB 

End Sub

Sub SomePointSUB

    click_x = CommandInfo(CMD_INFO_X)
    click_y = CommandInfo(CMD_INFO_Y)

    Insert Into "POINT_TAB" ("attribute1", "attribute2"...., obj)
    values ((value1), (value2)....., CreatePoint(click_x, click_y))

End Sub


So, this was straight forward, but creating polyline is not so (at least for me). All of the examples I found online had fixed number of nodes, like this (from MapBasic user guide):


    Include "mapbasic.def"
        Type Point
        x As Float
        y As Float
        End Type

    Dim objcoord(5) As Point        
    Dim numnodes, i As Integer, myobj As Object        
        numnodes = 3        
        set CoordSys Earth        
    objcoord(1).x = -89.213 objcoord(1).y = 32.017        
    objcoord(2).x = -89.204 objcoord(2).y = 32.112        
    objcoord(3).x = -89.187 objcoord(3).y = 32.096

        Create Pline Into Variable myobj 0

For i = 1 to numnodes    
Alter Object myobj Node Add (objcoord(i).x,objcoord(i).y)    

Insert Into cables (obj) Values (myobj)


MapBasic Reference says: "The Create Pline statement creates a polyline object. If you need to create a polyline object, but do not know until run-time how many nodes the object should contain, create the object in two steps: First, use Create Pline to create an object with no nodes, and then use the Alter Object statement to add detail to the polyline object."

I'm quite new to MapBasic, so I do understand logic of this (probably some loop will be required to add node for every click), but is simply out of my current MapBasic knowledge.


I don't think you need to create the PLine object explicitly, you'll be pleased to hear it's much simpler than that.

Use the ToolButton as you have set up in your first code example and set the DrawMode to DM_CUSTOM_POLYLINE. When the user finishes drawing the line the handler procedure will be called where you can then retrieve the drawn object using CommandInfo(CUSTOM_INFO_CUSTOM_OBJ).


Include "MapBasic.def"
Include "Icons.def"

Declare Sub Main
Declare Sub PLineToolButtonHandler

Sub Main()

    '// create buttonpad and with custom toolbutton
    Create ButtonPad "Test"
        As toolbutton
        Icon 16 
        Calling PLineToolButtonHandler 

End Sub

Sub PLineToolButtonHandler

Dim oPLine as Object

    '// retrieve drawn object
    oPLine = CommandInfo(CMD_INFO_CUSTOM_OBJ)

    '// insert object into cosmetic layer
    Insert into WindowInfo(FrontWindow(), WIN_INFO_TABLE) (OBJ) Values (oPLine)

End Sub

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.