How to modify multiple objects at once?

480 views
Skip to first unread message

sparsons

unread,
Dec 5, 2006, 2:16:05 PM12/5/06
to MapInfo-L
Hello,

My name is Shane and I'm new to the group. I've been working with
MapBasic for a little time now, and have a question:

How would you go about modifying many objects at once in code?

ie: you have more than one object selected and you want to change the
attributes of all the objects to brush style solid and yellow.

I have this now, which works fine for changing one object, however does
not work for many objects selected:

Dim myobj As Object
myobj = selection.obj
Dim b As Brush
b=MakeBrush (2,YELLOW,YELLOW)

Alter Object myobj
Info OBJ_INFO_BRUSH,b

Update Selection Set obj = myobj

Any help would be much appreciated! Thank you in advance!


-Shane

Uffe Kousgaard

unread,
Dec 5, 2006, 2:37:12 PM12/5/06
to mapi...@googlegroups.com
You would have to define a function for changing the brush of an object and
then use the update command:

Update Selection
Set obj = MyFunction(obj)

See the mapbasic documentation on how to define functions.

Regards
Uffe

Bill Thoen

unread,
Dec 5, 2006, 2:43:03 PM12/5/06
to mapi...@googlegroups.com
On Tue, Dec 05, 2006 at 11:16:05AM -0800, sparsons wrote:
> How would you go about modifying many objects at once in code?
>
> ie: you have more than one object selected and you want to change the
> attributes of all the objects to brush style solid and yellow.
>
> I have this now, which works fine for changing one object, however does
> not work for many objects selected:
>
> Dim myobj As Object
> myobj = selection.obj
> Dim b As Brush
> b=MakeBrush (2,YELLOW,YELLOW)
>
> Alter Object myobj
> Info OBJ_INFO_BRUSH,b
>
> Update Selection Set obj = myobj

The general logic to use when doing this in code is to loop through each
object record by record in the selection set and update them individually.
It goes something like this:

dim sSel as string
dim i, j as integer
dim aObj as alias
dim objSel as object

j = SelectionInfo (SEL_INFO_NROWS)
if j > 0 then
sSel = SelectionInfo (SEL_INFO_SELNAME)
aObj = sSel + ".obj"
for i = 1 to j
fetch rec i from sSel
objSel = aObj
alter object objSel info OBJ_INFO_BRUSH, MakeBrush(2, YELLOW, YELLOW)
update sSel set obj=objSel where rowid=i
next
end if

The alias variable allows you to refernece a table.field name when you
only have the table and/or file name as variables. The rowid refernce in
the update statement lets you reference a particular row in a MapInfo
table.

Also, you've got to make sure that the selection is from the table you want
and that all selected objects can accept a brush attribute. Thene there's
the issues of saving the changes and cleaning up the selection so you don't
get a ton of temporary files like Query1, Query2, ..., QueryN floating around
your user's environment.

A much faster way to update objects which avoids a slow MapBasic loop is to
write a function that accepts an object and returns the updated object and
then you call it in an SQL update statement in your code, like so:
update sSel set obj = AlterObj(obj)

HTH,

- Bill Thoen

Peter Horsbøll Møller

unread,
Dec 5, 2006, 2:43:45 PM12/5/06
to mapi...@googlegroups.com

Shane,

You just need to add a loop to the code and you are almost there:

Dim myobj As Object
Dim b As Brush
Dim nRow As Integer

b=MakeBrush (2,YELLOW,YELLOW)

Fetch First From Selection
Do Until EOT(Selection)
        myobj = selection.obj
        nRow    = selection.ROWID

        Alter Object myobj
                Info OBJ_INFO_BRUSH, b

        Update Selection
                Set obj = myobj

                Where ROWID = nRow

        Fetch Next From Selection
Loop

or you could move the alter object into a function that returns the changed object:

'*****************************
Sub Main

Dim b As Brush
b=MakeBrush (2,YELLOW,YELLOW)

Update Selection
        Set obj = STYLChangeBrush(OBJ, b)

End Sub

'*****************************
Function STYLChangeBrush(ByVal oRegion As Object, ByVal bNew As Brush)

        STYLChangeBrush = oRegion

        Alter Object oRegion
                Info OBJ_INFO_BRUSH, bNew

        STYLChangeBrush = oRegion

End Function


HTH,

Peter Horsbøll Møller
GIS Developer, MTM
Geographical Information & IT
 
COWI A/S
Odensevej 95
DK-5260 Odense S.
Denmark
 
Tel     +45 6311 4900
Direct  +45 6311 4908
Mob     +45 5156 1045
Fax     +45 6311 4949
E-mail  p...@cowi.dk
http://www.cowi.dk/gis

-----Original Message-----
From: mapi...@googlegroups.com [mailto:mapi...@googlegroups.com] On Behalf Of sparsons
Sent: Tuesday, December 05, 2006 8:16 PM
To: MapInfo-L
Subject: [MI-L] How to modify multiple objects at once?


Hello,

My name is Shane and I'm new to the group. I've been working with MapBasic for a little time now, and have a question:

How would you go about modifying many objects at once in code?

ie: you have more than one object selected and you want to change the attributes of all the objects to brush style solid and yellow.

I have this now, which works fine for changing one object, however does not work for many objects selected:

Dim myobj As Object
  myobj = selection.obj
Dim b As Brush
  b=MakeBrush (2,YELLOW,YELLOW)

Alter Object myobj
  Info OBJ_INFO_BRUSH,b

Update Selection Set obj = myobj

Any help would be much appreciated! Thank you in advance!


-Shane




sparsons

unread,
Dec 5, 2006, 3:06:31 PM12/5/06
to MapInfo-L
Wow,

Thank all of you so much! Quick replies and some great advice! I'll be
busy implimenting these ideas, again thank you so much!

Reply all
Reply to author
Forward
0 new messages