You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

converting polygon graphic to polygon shape...   Aaron Wisher Dec 30, 2002
Re: converting polygon graphic to polygon s...   Olivier Damanet Dec 30, 2002
Re: converting polygon graphic to polygon s...   Aaron Wisher Dec 31, 2002
Re: converting polygon graphic to polygon s...   Olivier Damanet Dec 31, 2002
Re: converting polygon graphic to polygon s...   Olivier Damanet Dec 31, 2002
Re: converting polygon graphic to polygon s...   Aaron Wisher Dec 31, 2002
Re: converting polygon graphic to polygon s...   Olivier Damanet Dec 31, 2002
Re: converting polygon graphic to polygon s...   Aaron Wisher Dec 31, 2002
Re: converting polygon graphic to polygon s...   Dan McCoy Jan 08, 2003
Re: converting polygon graphic to polygon s...   Aaron Wisher Jan 09, 2003
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject converting polygon graphic to polygon shapefile 
Author Aaron Wisher 
Date Dec 30, 2002 
Message I am trying to add a polygon graphic to an edit session already active using the following code, but it kicks out and doesn't add the graphic to the shapefile. any help is appreciated.

in the GetElements function it kicks out of the function at: For l = 0 To pGraphicsContainerSelect.ElementSelectionCount - 1

and I cannot figure out why. I have the polygon edit shapefile layer selected in the TOC and the graphic polygon selected in the layout view. 
 
Private Sub ConvertIndexPolygonGraphicToShape()

    ' adds all the selected Polygon Elements to the current edit layer

    Dim pFeatureLayer As IFeatureLayer
    Dim pMxDocument As IMxDocument

        Set pMxDocument = ThisDocument
        Set pFeatureLayer = pMxDocument.FocusMap.Layer(0)

    StartEditingMapExtentPolygon
    
    Dim pElementCollection As IElementCollection
        Set pElementCollection = GetElements(pMxDocument.FocusMap)
            
        If pElementCollection.Count > 0 Then
                pEditor.StartOperation
            Dim pEditLayers As IEditLayers
                Set pEditLayers = pEditor
                WriteElements pEditLayers.CurrentLayer.FeatureClass, pElementCollection
                pEditor.StopOperation "AddGraphics2FeatClass"
                DeleteElements pMxDocument.FocusMap, pElementCollection
        End If
' Stop the Editing session
        'StopEditingMapExtentPolygon
End Sub

Function GetElements(pGraphicsContainerSelect As IGraphicsContainerSelect) As IElementCollection

    Set GetElements = New ElementCollection
    Dim l As Long
    For l = 0 To pGraphicsContainerSelect.ElementSelectionCount - 1
        If TypeOf pGraphicsContainerSelect.SelectedElement(l) Is IPolygonElement Then
            GetElements.Add pGraphicsContainerSelect.SelectedElement(l)
        End If
    Next l
End Function

Sub DeleteElements(pGraphicsContainer As IGraphicsContainer, pElementCollection As IElementCollection)

    Dim pActiveView As IActiveView
    Set pActiveView = pGraphicsContainer
    
    ' for refresh envelope
    Dim d As Double
    d = pActiveView.ScreenDisplay.DisplayTransformation.FittedBounds.Width / 20#
    
    Dim l As Long
    Dim pElement As IElement
    Dim pEnvelope As IEnvelope
    
    For l = 0 To pElementCollection.Count - 1
        pElementCollection.QueryItem l, pElement
        pGraphicsContainer.DeleteElement pElement
        Set pEnvelope = pElement.Geometry.Envelope
        pEnvelope.Expand d, d, False
        pActiveView.PartialRefresh esriViewGraphics + esriViewGeography, Nothing, pEnvelope
    Next l
End Sub

Sub WriteElements(pFeatureClass As IFeatureClass, pElementCollection As IElementCollection)

    Dim pFeatureCursor As IFeatureCursor
        Set pFeatureCursor = pFeatureClass.Insert(True)
    Dim pFeatureBuffer As IFeatureBuffer
        Set pFeatureBuffer = pFeatureClass.CreateFeatureBuffer
    Dim l As Long
    Dim pElement As IElement
        
        If pFeatureClass.ShapeType = esriGeometryPolygon Then
                For l = 0 To pElementCollection.Count - 1
                    pElementCollection.QueryItem l, pElement
                Set pFeatureBuffer.Shape = pElement.Geometry
                    pFeatureCursor.InsertFeature pFeatureBuffer
                Next l
            pFeatureCursor.Flush
        End If
End Sub
 
  Aaron Wisher 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Olivier Damanet 
Date Dec 30, 2002 
Message When calling GetElements, you pass the focus map as IGraphicsContainerSelect, so it considers the graphic elements from the map view. Pass IMxDocument::PageLayout instead. 
  Olivier. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Aaron Wisher 
Date Dec 31, 2002 
Message it still does the same thing with the following code:

it fails at: If TypeOf pGraphicsContainerSelect.SelectedElement(l) Is IPolygonElement Then

it is not seeing the selected graphic for some reason. 
 
Private Sub ConvertIndexPolygonGraphicToShape(pFeatureLayer As IFeatureLayer)

    ' adds all the selected Polygon Elements to the current edit layer

    'Dim pFeatureLayer As IFeatureLayer
    Dim pMxDocument As IMxDocument

        Set pMxDocument = ThisDocument
        'Set pFeatureLayer = pMxDocument.FocusMap.Layer(0)
        'Set pFeatureLayer = pMxDocument.PageLayout

    StartEditingMapExtentPolygon
    
    Dim pElementementCollection As IElementCollection
        Set pElementementCollection = GetElements(pMxDocument.PageLayout)
            
        If pElementementCollection.Count > 0 Then
                pEditor.StartOperation
            Dim pEditLayers As IEditLayers
                Set pEditLayers = pEditor
                WriteElements pEditLayers.CurrentLayer.FeatureClass, pElementementCollection
                pEditor.StopOperation "AddGraphics2FeatClass"
                DeleteElements pMxDocument.FocusMap, pElementementCollection
        End If
' Stop the Editing session
        StopEditingMapExtentPolygon
End Sub

Function GetElements(pGraphicsContainerSelect As IGraphicsContainerSelect) As IElementCollection

    Set GetElements = New ElementCollection
    Dim l As Long
    For l = 0 To pGraphicsContainerSelect.ElementSelectionCount - 1
        If TypeOf pGraphicsContainerSelect.SelectedElement(l) Is IPolygonElement Then
            GetElements.Add pGraphicsContainerSelect.SelectedElement(l)
        End If
    Next l
End Function
 
  Aaron Wisher 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Olivier Damanet 
Date Dec 31, 2002 
Message Try the code below. Actually, elements like CircleElement and RectangleElement do not implement IPolygonElement. Therefore, such elements cannot be recognized as 'polygonElements' 
 
Function GetElements(pGraphicsContainerSelect As IGraphicsContainerSelect) As IElementCollection

    Set GetElements = New ElementCollection
    Dim l As Long
    For l = 0 To pGraphicsContainerSelect.ElementSelectionCount - 1
        If TypeOf pGraphicsContainerSelect.SelectedElement(l).Geometry Is IPolygon Then
            GetElements.Add pGraphicsContainerSelect.SelectedElement(l)
        End If
    Next l
End Function
 
  Olivier. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Olivier Damanet 
Date Dec 31, 2002 
Message IFillShapeElement is implemented by Circle, Ellipse, Multipatch, Polygon and Rectangle elements: 
 
Function GetElements(pGraphicsContainerSelect As IGraphicsContainerSelect) As IElementCollection

    Set GetElements = New ElementCollection
    Dim l As Long
    For l = 0 To pGraphicsContainerSelect.ElementSelectionCount - 1
        If TypeOf pGraphicsContainerSelect.SelectedElement(l) Is IFillShapeElement Then
            GetElements.Add pGraphicsContainerSelect.SelectedElement(l)
        End If
    Next l
End Function
 
  Olivier. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Aaron Wisher 
Date Dec 31, 2002 
Message that works well. Thanks. However, the polygon that is written to the shapefile uses the pagelayout coordinates instead of the map coordinates, so the polygon is located at the coordinate origin and is only 8 inches high. Any thoughts? 
  Aaron Wisher 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Olivier Damanet 
Date Dec 31, 2002 
Message Elements which have been created on the page layout are with 'page' coordinates.
If the element had been created on the map view, and next been selected inside the map frame on the layout view, then you could get it in map coordinates, if passing the ::FocusMap as GraphicContainerSelect.
But I guess the elements are digitized on the layout view, aren't they?
A thought: Maybe you could use IDisplayTransformation: Get a reference to both ScreenDisplays: PageLayout and Map (IDisplay). Use the ::DisplayTransformation of the page layout to convert 'page' units to device units and then use the ::DisplayTrandformation of the map to convert device coordinates to map coordinates... :-/ 
  Olivier. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Aaron Wisher 
Date Dec 31, 2002 
Message I was hoping you wouldn't say that.:(
I guess I need to re-think this project.
Thanks for the help. 
  Aaron Wisher 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Dan McCoy 
Date Jan 08, 2003 
Message Aaron or Oliver,

If you can figure out how to apply the transformation to get pagelayout polys into map units, could you share the code?


Dan 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: converting polygon graphic to polygon shapefile 
Author Aaron Wisher 
Date Jan 09, 2003 
Message like this. 
 
Public Sub CreateIndexElement()
    
    Dim pMxDocument As IMxDocument
        Set pMxDocument = ThisDocument
    
    Dim pRectangleElement As IElement
    
    Dim pGraphicsContainer As IGraphicsContainer
        Set pGraphicsContainer = pMxDocument.PageLayout

        pGraphicsContainer.Reset
    
    Dim pElement As IElement
        Set pElement = pGraphicsContainer.Next
        
    
    Dim OffSwitch As Boolean
        OffSwitch = False
            
            Do While OffSwitch = False
                If TypeOf pElement Is IMapFrame Then
                    Set pRectangleElement = New RectangleElement
                    pRectangleElement.Geometry = pElement.Geometry
                    OffSwitch = True
                End If
            Set pElement = pGraphicsContainer.Next
            Loop
      
    Dim pLayoutView As IActiveView
    Dim pMapView As IActiveView
    Dim pMap As IMap
        Set pMap = pMxDocument.FocusMap
        Set pLayoutView = pMxDocument.PageLayout

        Set pMapView = pMap
        Set pMxDocument.ActiveView = pLayoutView
  
  'Get the display transformations
    Dim pPageTransformation As IDisplayTransformation
    Dim pMapTransformation As IDisplayTransformation
        Set pPageTransformation = pLayoutView.ScreenDisplay.DisplayTransformation
        Set pMapTransformation = pMapView.ScreenDisplay.DisplayTransformation
        
  'Transform envelope from page to map coordinates
    Dim Rect As tagRECT
    Dim pEnv As IEnvelope
        Set pEnv = pRectangleElement.Geometry.Envelope
    'Transform from Page units to Device units
        pPageTransformation.TransformRect pEnv, Rect, esriTransformToDevice + esriTransformPosition + esriTransformSize
    'Transform from Device units to Map units
        pMapTransformation.TransformRect pEnv, Rect, esriTransformToMap + esriTransformPosition

    Dim pPointCollection As IPointCollection
        Set pPointCollection = New Polygon
            With pPointCollection
                .AddPoint pEnv.LowerLeft
                .AddPoint pEnv.UpperLeft
                .AddPoint pEnv.UpperRight
                .AddPoint pEnv.LowerRight
            End With
    
    'Query interface to IPolygon to close the geometry
    Dim pPolygon As IPolygon
        Set pPolygon = pPointCollection
    Dim pNewElement As IElement
        Set pNewElement = New PolygonElement
        pNewElement.Geometry = pPolygon

     
    Dim pGraphicsContainer2 As IGraphicsContainer
        Set pGraphicsContainer2 = pMxDocument.FocusMap
        pGraphicsContainer2.AddElement pNewElement, 0

        'Select the Polygon element
    Dim pGraphicsContainerSelect As IGraphicsContainerSelect
        Set pGraphicsContainerSelect = pGraphicsContainer2
        pGraphicsContainerSelect.SelectElement pNewElement
        
        Set pMxDocument.ActiveView = pMapView
        pMxDocument.ActiveView.Refresh

End Sub
 
  Aaron Wisher