2

I wrote a piece of code a while ago for colouring lines depending on what was in a certain column. I struggled through it, but managed to get it to work. Now, I want to write a piece of code that will do the same for points, rather than lines. I'm presuming I can alter my existing code slightly to do this but I can't for the life of me figure it out! It doesn't help that I'm not that experienced with MapBasic, and have forgotten most of what I learned the first time round!

Here is my existing code. It looks at what each entry in the table has in column called 'symbology', and changes the colour and line style as such.

My new code will need to make all points a size 5 circle (from Map Symbols font) with a border. The only thing that will change will be the colour.


Include "MapBasic.def"


Declare Sub Main

Declare Function ChangeLineColour (ByVal objTarget as Object, ByVal stringAttribute as String) as Object


Sub Main()


Update ww_gravity_pipe_location set Obj = ChangeLineColour(obj, symbology)


End Sub

Function ChangeLineColour (ByVal objTarget as Object, ByVal stringAttribute as String) as Object


Dim curPen, newPen as Pen

Dim penWidth, penPattern, penColour as Integer


'// check that the object is a line object, if not just return the original object

If Not ObjectInfo(objTarget, OBJ_INFO_TYPE) = Any(OBJ_TYPE_LINE, OBJ_TYPE_PLINE) then

       ChangeLineColour = objTarget

       Exit Function

End if

 '// get the current pen style

curPen = ObjectInfo(objTarget, OBJ_INFO_PEN)


'// get the individual style attributes of the current pen


'// set the new pen colour based on the numerical attribute

Do Case stringAttribute

       Case "Surface Water  (S)" 

              penColour = BLUE
          penWidth = 2

       Case "Combined  (C)" 

              penColour = RED
          penWidth = 2

       Case "Foul  (F)"

          penColour = 11239175
          penWidth = 2

    Case "Proposed"
          penColour = GREEN
          penWidth = 2
              penPattern = 12

    Case "Abandoned"

          penColour = 9079434
          penWidth = 2
          penPattern = 112



End Case

'// - Colour codes from http://www.tydac.ch/color/
'// create a new pen with the new colour

newPen = MakePen(penWidth, penPattern, penColour)



'// alter the object we passed into the function to use the new pen

Alter Object objTarget Info OBJ_INFO_PEN, newPen



'// return the altered object to the update statement

ChangeLineColour = objTarget



End Function

All the cases will remain the same, so they don't need to be changed.

I'm guessing I need to change all the 'pens' to 'symbols' or something along those lines.

3

all you need to do really is use a Symbol object instead of Pen

dim newSymb, currSymb as Symbol, symColour,symShape,symSize as Integer

set the values for each integer variable in a Do-Case block as you have done and use newSymb = MakeSymbol(symShape,SymColour,symSize)

If you need any more advice here are all the documentation links and you can probably get all the information you need from ObjectInfo(), Symbol clause and MakeSymbol

1

Something along these lines:

Function ChangeSymbolColour (ByVal objTarget as Object, ByVal stringAttribute as String) as Object

Dim newSymbol as Symbol
Dim nColour as Integer

'// check that the object is a point object, if not just return the original object
If Not ObjectInfo(objTarget, OBJ_INFO_TYPE) = Any(OBJ_TYPE_POINT) then
   ChangeLineColour = objTarget
   Exit Function
End if

'// get the current pen style
'//You don't really need to do this
'curPen = ObjectInfo(objTarget, OBJ_INFO_PEN)

'// get the individual style attributes of the current pen

'// set the new pen colour based on the numerical attribute

Do Case stringAttribute
   Case "Surface Water  (S)" 
      nColour = BLUE

   Case "Combined  (C)" 
      nColour = RED

   Case "Foul  (F)"
      nColour = 11239175

   Case "Proposed"
       nColour = GREEN

   Case "Abandoned"
      nColour = 9079434
End Case

'// - Colour codes from http://www.tydac.ch/color/
'// create a new pen with the new colour

newSymbol = MakeFontSymbol(41, nColour, 5, "Map Symbols", 0, 0) 

'// alter the object we passed into the function to use the new pen
Alter Object objTarget Info OBJ_INFO_SYMBOL, newSymbol

'// return the altered object to the update statement
ChangeLineColour = objTarget

End Function

You don't have to read the original symbol from the object as you don't really use it here. Also note that to create a symbol using a True Type font like the "Map Symbols" you need to use the MakeFontSymbol() function.

In the function above only the colour changes - all the other parts of the symbol is hard coded but you can of course also make these variable.

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.