Select to view content in your preferred language

VBA programmatically use cursor to draw a rectangle on a topology

1024
5
12-09-2010 09:50 AM
sailiTang
Occasional Contributor
Hi,

   I would like to use cursor to draw a rectangle on a topology. And then all the nodes within the rectangle will change color automatically. Please see the attachment. Anybody knows how to write the code?


Thank you.
0 Kudos
5 Replies
Venkata_RaoTammineni
Regular Contributor
Hi,

You r looking for graphic rectangle or feature rectangle ?

Thanks and Regards,

Venkat
0 Kudos
sailiTang
Occasional Contributor
Hi Venkat,

    I think what I need should be graphic rectangle. Actually, the purpose of drawing a rectangle is selecting a topology area. And then I would like to make a different color for all the nodes within this area.

Thanks,

Saili
0 Kudos
Venkata_RaoTammineni
Regular Contributor
Hi,

You are looking for the same ?

  public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            try
            {
                // TODO:  Add SelectByShape.OnMouseDown implementation

                IMxDocument pMxDoc = m_application.Document as IMxDocument;

                IEnvelope pEnv;

                IRubberBand pRubberBand;

                IElement pElem;

                //creat new rubber envople

                pRubberBand = new RubberEnvelopeClass();

                pEnv = pRubberBand.TrackNew(pMxDoc.ActiveView.ScreenDisplay, null) as IEnvelope;

                //create new envolpe element

                pElem = new RectangleElementClass();

                pElem.Geometry = pEnv;

                IFillShapeElement pFillShapeElement;

                IFillSymbol pFillSymbol;

                IColor pColor;

                ILineSymbol pLineSymbol;

                IGraphicsContainer pGContainer;

                pFillShapeElement = pElem as IFillShapeElement;

                //set fill symbol

                pFillSymbol = pFillShapeElement.Symbol;

                pColor = pFillSymbol.Color;

                pColor.Transparency = 0;

                pFillSymbol.Color = pColor;

                //set the outline symbol;

                pLineSymbol = pFillSymbol.Outline;

                pColor.Transparency = 255;

                pColor.RGB = 255;

                pLineSymbol.Width = 12;

                pFillSymbol.Outline = pLineSymbol;

                //assing the symbol to teh rectangle

                pFillShapeElement.Symbol = pFillSymbol;

                //add rectange to the GC;

                pGContainer = pMxDoc.ActiveView as IGraphicsContainer;


                pGContainer.AddElement(pElem, 0);

                pMxDoc.ActiveView.Refresh();

                //use rectangle to search the top layer

                ISpatialFilter pSpatialFilter = new SpatialFilterClass();

                IFeatureCursor pFeatCursor;

                IFeature pFeature;

                IFeatureLayer pFeatLayer;

                int intCount = 0;

                pSpatialFilter.WhereClause = "FID >2";

                pSpatialFilter.Geometry = pEnv;

                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

                pFeatLayer = pMxDoc.SelectedLayer as IFeatureLayer;

                pFeatCursor = pFeatLayer.Search(pSpatialFilter, false);

                pFeature = pFeatCursor.NextFeature() as IFeature;

                while(pFeature!=null)
                {
                    intCount = intCount + 1;

                    pFeature = pFeatCursor.NextFeature();
                }

                MessageBox.Show("There are " + intCount + "Cities over 5000 Recangel Area");


                pGContainer.DeleteAllElements();
            }
            catch (Exception exp) { }


        }
0 Kudos
sailiTang
Occasional Contributor
Hi Venkat,

    Thank you for your code. My other problem is how to write the code for selecting a rectangle area on the topology.

Thanks

Saili
0 Kudos
sailiTang
Occasional Contributor
This is the code which can count the number of nodes and edges of a topology:
Dim pmXDoc As IMxDocument

Dim pTopologyLayer As ITopologyLayer

Dim pTopology As ITopology

Dim pTopoGraph As ITopologyGraph

Dim pEnumTopoNode As IEnumTopologyNode

Dim i As Long

Dim pTopoNode As ITopologyNode

Dim penumNodeEdge As IEnumNodeEdge

Dim pTopoEdge As ITopologyEdge

Dim batfrom As Boolean

Dim pPolyline As IPolyline

Set pmXDoc = ThisDocument

Set pTopologyLayer = pmXDoc.FocusMap.Layer(0) 'A topology has to be in first position in the view Table of content

Set pTopology = pTopologyLayer.Topology 'Get the topology

Set pTopoGraph = pTopology.Cache 'Get the topologygraph

pTopoGraph.Build pmXDoc.ActiveView.Extent, False 'Build the topology graph

Set pEnumTopoNode = pTopoGraph.Nodes 'Get all the nodes of the graph

'For each nodes get the adjacent edges and print the length of the edges

For i = 0 To pEnumTopoNode.Count - 1

   Set pTopoNode = pEnumTopoNode.Next

   Set penumNodeEdge = pTopoNode.Edges(True) 'Get all the adjacent edges

   MsgBox "Number of edges adjacent to that node : " & penumNodeEdge.Count

   MsgBox "Is the enumerator Clockwise : " & penumNodeEdge.IsClockwise

   penumNodeEdge.Next pTopoEdge, batfrom 'Get the next adjacent edge

   While Not pTopoEdge Is Nothing

      Set pPolyline = pTopoEdge.Geometry

      Debug.Print "Length of polyline : " & pPolyline.Length

      penumNodeEdge.Next pTopoEdge, batfrom 'Get the next edge

   Wend

Next

But this code counts the nodes and edges which should be in the whole view. What I want is that the user can use the cursor to select a rectangle area, and then the program can count the nodes and edges which are only in this rectangle area. Please see the attachment. Could you tell me how to write the code for selecting the topology area using cursor?

Thanks
Saili
0 Kudos