Accessing Group2D contained in Group2D

  1. 6 days ago

    Robert L

    May 21 XDC Speakers Federal Way, WA (Seattle Area)

    Group2D inherits from Object2D. It is a container for Object2D's. It is somewhat like an array. You can iterate through all the contents of a Group2D.
    An example would be:
    in this example, g2D_Draw is a Group2D.

    Var lastIndex As Integer
    lastIndex = g2D_Draw.Count - 1
    For nIndex As Integer = 0 To lastIndex
       g2D_Draw.Item(nIndex).BorderColor = Color.Red
    Next nIndex

    If g2D_Draw contains a bunch of RectShapes and OvalShapes (which are Objects2D's) then they are changed to Red. It is all great.

    It is legal for Group2Ds to contain Group2D's. What happens if g2D_Draw contains a bunch of Group2Ds?

    If you run the code above, the compiler accepts it but nothing happens. It is legal to apply a BorderColor to a Group2D , but it has no effect.

    A real-world example is one in which you have curved lines that are actually made up of many short straight line segments (CurveShapes). These are all brought into a single Group2D. This is legal and sensible. A line created in this way will draw just fine into a canvas. If you have several of these lines, they can be brought into a "parent" Group2D. Lines stored in this way will draw just fine into a canvas, What if you wanted to make all the lines Red?

    If you iterate through the "parent" Group2D, each element can have it BorderColor made Red. It is legal as mentioned above, but nothing happens. So somehow, one imagines, you have to iterate through the "parent" Group2D and then iterate through each element in that Group2D which themselves are Group2D's to change their BorderColor to Red. But the compiler objects.

    Var lastIndex As Integer
    lastIndex = g2D_Draw.Count - 1
    For nIndex As Integer = 0 To lastIndex
      Var innerLastIndex As Integer
      innerLastIndex = g2D_Draw.Item(nIndex).Count
    Next nIndex

    Now g2D_Draw.Item(nIndex) is, in fact a Group2D. That individual element of g2D_Draw is, itself a Group2D. But the compiler objects:

    Type "Object2D" has no member named "Count"

    Well, many Objects2D's have no member named "Count" but this Object2D, being a Group2D, does have a member named Count.

    I tried

    Var lastIndex As Integer
    lastIndex = g2D_Draw.Count - 1
    For nIndex As Integer = 0 To lastIndex
      Var someGroup2D As New Group2D
      someGroup2D = g2D_Draw.Item(nIndex)
    Next nIndex

    That also will not work. You will get an error message that Object2D's are not Group2D's even though in this situation that particular Object2D is a Group2D.

    My question is: How do I accomplish what I want. That is to change several curved lines, each individually created as a bunch of CurveShapes living in a Group2D to Red?

    It feels like you need to be doing some casting to get the type you want. Check the type of what is returned by Item() and then cast it so you can access the properties you need. Something like this:

    If g2D_Draw.Item(nIndex) IsA Group2D Then
       innerLastIndex = Group2D(g2D_Draw.Item(nIndex)).Count
    End If
  2. Rudolf J

    May 21 Germany Frankfurt

    Hello Robert,
    forget Group2D and take pixmapshape
    draw them one by one in your picture
    A small example for creating curve shapes.
    is much easier and each element can be accessed when spoken in an array.

    https://www.dropbox.com/s/rleo8rgniq5kcwe/test-graphicsPath-engl.xojo_binary_project?dl=1

  3. Paul L

    May 21 Xojo Inc, Third Party Store Answer

    It feels like you need to be doing some casting to get the type you want. Check the type of what is returned by Item() and then cast it so you can access the properties you need. Something like this:

    If g2D_Draw.Item(nIndex) IsA Group2D Then
       innerLastIndex = Group2D(g2D_Draw.Item(nIndex)).Count
    End If
  4. 5 days ago

    Robert L

    May 21 XDC Speakers Federal Way, WA (Seattle Area)

    Rudolf links a project with LOTs of graphic examples/techniques which are going to be very helpful for me as I move forward on my own current project. There is a lot to digest here.

    Paul's suggestion worked for me. I included the snippet of code below to illustrate it.

    Const THE_COLOR As Color = Color.Red
    Var lastIndex As Integer
    lastIndex = cn.g2D_AllLines.Count -1 // minus one because zero-based
    
    For iIndex As Integer = 0 To lastIndex
      
      Var innerLastIndex As Integer
      innerLastIndex = Group2D(cn.g2D_AllLines.Item(iIndex)).Count - 1
      
      For jInnerIndex As Integer = 0 To innerLastIndex
        
        Group2D(cn.g2D_AllLines.Item(iIndex)).Item(jInnerIndex).BorderColor = THE_COLOR
        
      Next jInnerIndex
      
    Next iIndex

or Sign Up to reply!