Select to view content in your preferred language

Drawing rectangles and markers with vba

933
4
06-07-2011 06:07 AM
FlorianSchulze
Emerging Contributor
Hi All,
I hat little contact to VBA in ArcGis during my study, that`s why I am wrinting here right now.

At the moment I try to plan some flightlines for a project of mine. Therefore, I use the "draw rectangle" tool to generate the flightlines (stipes for a sensor). Then I use the properties for exact locating them.

Because I have to do this for a lot of flightlines, I thought of using little VBA to automate parts of this process. But beginning seems to be very hard. I even dont really know where to start. So my question - what is the object i have to use for drawing rectangles and markers? Is there an example code available using these objects?

I hope, you understand my question, as my english is very poor...

Yours
Geo
0 Kudos
4 Replies
FlorianSchulze
Emerging Contributor
Hi all,
as there are still no answers to my questions, I hope to get some help with a code-fragment I already found. This code "draws" a rectangle at a position where I click with my mouse. I think it might just be a little editing that this code does what I need. Instead of clicking with the mouse, the rectangle has to be drawn with given width, Length and Position. Maybe someone can show me, what has to be changed. Here is the code:

 Dim pMXDoc As IMxDocument
  Dim pEnv As IEnvelope
  Dim pGraCont As IGraphicsContainer
  Dim pRubberEnv As IRubberBand
  Dim pElem As IElement
  
  ' QI for the MXDocument interface
  Set pMXDoc = ThisDocument
  
  ' QI for the IGraphicsContainerSelect interface on the document's activeview
  Set pGraCont = pMXDoc.ActiveView
  
  ' Create a new RubberEnvelope
  Set pRubberEnv = New RubberEnvelope
  ' Return a new Envelope from the tracker object using TrackNew
  Set pEnv = pRubberEnv.TrackNew(pMXDoc.ActiveView.screenDisplay, Nothing)
  ' Create a new EnvelopeElement and set its Geometry
  Set pElem = New RectangleElement
  pElem.geometry = pEnv
  
  'Add the new element at Z order zero
  pGraCont.AddElement pElem, 0

    ' Refresh the activeview
  pMXDoc.ActiveView.Refresh



Hope you can help me (please)

Yours Geo
0 Kudos
AlexanderGray
Honored Contributor
It is difficult to provide any direction because it is not clear what you mean by drawing rectangles.  The code sample provided shows how to add rectangle elements to the graphics layer of a map or page layout.  This only persist in the mxd.  Usually people want to store the data in a database or something.  You can also draw directly to the activeview, it all depends on what you want the end result to be.  Perhaps if you described how you go about doing in through the interface, it would provide more information.
0 Kudos
FlorianSchulze
Emerging Contributor
Hi Agray1,
thank you for your answer. At the moment it is enough, when I am able to draw the rectangles to the grafic layer, because I have to rotate and adjust them in a following step - and I know how to do this.
Therefore the missing information for me is, how to make fixed values for width, length and position of my rectangle. I dont want to use my mouse.

As you already mentioned, I can also draw at the active view, but I have to avow, that I don`t know, how to rotate and move such rectangles in a following step. I would be very thankful if you or someone else could help me to solve my problem.

Yours Geo
0 Kudos
FlorianSchulze
Emerging Contributor
Hi All,
I could already finish a lot of my code. At the moment it does exactly what I want. Just need to find out, how to edit fill-color and frame-color of my new rectangles.

  
// getting Width, Height and Number of rectangles from form

Dim pMXDoc As IMxDocument
  Dim pEnv As IEnvelope
  Dim pGraCont As IGraphicsContainer
  Dim pRubberEnv As IRubberBand
  Dim pElem As IElement

  ' DIM Coordinates
    Dim EnvXMin As Double
    Dim EnvYMin As Double

    EnvXMin = 425419
    EnvYMin = 6011500

  ' QI for the MXDocument interface
  Set pMXDoc = ThisDocument
  
  ' QI for the IGraphicsContainerSelect interface on the document's activeview
  Set pGraCont = pMXDoc.ActiveView
  'create new Envelope
  Set pEnv = New Envelope
  pEnv.YMin = EnvYMin
  pEnv.XMin = EnvXMin
  pEnv.Height = EnvHeight
  pEnv.Width = EnvWidth
  
    For i = 1 To Numbers
        
        ' Create a new EnvelopeElement and set its Geometry
        Set pElem = New RectangleElement
        pElem.geometry = pEnv
    
        'Add the new element at Z order zero
        pGraCont.AddElement pElem, 0
     
        ' Generate Offset for next rectangle
        pEnv.Offset (EnvWidth / 2), 0

    Next i
    
  ' Refresh the activeview
  pMXDoc.ActiveView.Refresh
0 Kudos