Select to view content in your preferred language

IWorkspaceEdit and mousedown

2669
10
04-02-2010 08:20 AM
TonyAlmeida
MVP Regular Contributor
I have tool that creates points based on clicks. Tool works great as a shapefile, but i can't seem to get to work on an SDE feature class.

I get the following error:
One or more layers failed to draw:

FDO error: 0
The requested operation is invalid on a closed state[TonyAddressPoints.DBO.Shared_Address_Points]

from what i have read i need to implement the StartEditing but i don't know how. I need some help on how to get this to work in a geodatabase.

Thanks.

  

Dim pFeature1 As IFeature
Dim tmpPoint1 As IPoint


Private Sub UIToolControl2_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)


        'Adds a point to a shapefile
    Dim pMap As IMap
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap

    'Get the first layer in the map
    Dim pFeatLyr As IGeoFeatureLayer
    Set pFeatLyr = pMap.Layer(0)

     'Create a point from the mouse down click
    Dim tmpPoint As IPoint
    Set tmpPoint = New Point
    Set tmpPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

    'Get the feature class
    Dim pFClass As IFeatureClass
    Set pFClass = pFeatLyr.FeatureClass

    'Create the new point feature
    Dim pfeature As IFeature
    Dim dblDist As Double
    If button = 2 Then
        Set pFeature1 = Nothing
        Set tmpPoint1 = Nothing
        Exit Sub
    End If
    If pFeature1 Is Nothing Then
        If tmpPoint1 Is Nothing Then
            Set tmpPoint1 = tmpPoint
        Else
            Set pfeature = pFClass.CreateFeature
            Set pfeature.Shape = tmpPoint
            If Not tmpPoint1 Is Nothing Then
                dblDist = 0
                dblDist = GetDistance1(pfeature)
                pfeature.Value(pfeature.Class.Fields.FindField("Distance")) = dblDist
                pfeature.Value(pfeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""
                pfeature.Store
            End If
            Set pFeature1 = pfeature
        End If
    Else
        Set pfeature = pFClass.CreateFeature
        Set pfeature.Shape = tmpPoint
        If Not pFeature1 Is Nothing Then
            dblDist = 0
            dblDist = GetDistance(pFeature1, pfeature)
            pfeature.Value(pfeature.Class.Fields.FindField("Distance")) = dblDist
            pfeature.Value(pfeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""

            pfeature.Store
        End If
        Set pFeature1 = pfeature
    End If
    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
End Sub
Function GetDistance1(pFeature1 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = tmpPoint1
    Set pGeom2 = pFeature1.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance1 = dblDist
End Function
Function GetDistance(pFeature1 As IFeature, pFeature2 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = pFeature1.Shape
    Set pGeom2 = pFeature2.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance = dblDist
End Function
0 Kudos
10 Replies
TonyAlmeida
MVP Regular Contributor
I am not sure how to make all work with my current code. I have looked at examples but i can't get to work. Can you help me put it together please.
0 Kudos
TonyAlmeida
MVP Regular Contributor
I am getting a compile error: user-defined type not defined on
Dim pDataset As ESRI.ArcGIS.Geodatabase.IDataset


Statements is in red "pEditWorkspace.StartEditOperation()" & "pEditWorkspace.StopEditOperation()"
0 Kudos
G__Venkata_VijayaKumar
Occasional Contributor
Try removing the qualifying library names...

Dim pDataset As IDataset
0 Kudos
TonyAlmeida
MVP Regular Contributor
I am not getting a an Compile error on this line, and i don't know why.

I am getting a compile error: user-defined type not defined on
Dim pDataset As ESRI.ArcGIS.Geodatabase.IDataset

Here is my current code, please look.

Private Sub UIToolControl2_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)


        'Adds a point to a shapefile
    Dim pMap As IMap
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Dim pDataset As IDataset

    'Get the first layer in the map
    Dim pFeatLyr As IGeoFeatureLayer
    Set pFeatLyr = pMap.Layer(0)
   


     'Create a point from the mouse down click
    Dim tmpPoint As IPoint
    Set tmpPoint = New Point
    Set tmpPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

    'Get the feature class
    Dim pFClass As IFeatureClass
    Set pFClass = pFeatLyr.FeatureClass

'QI from IFeatureclass to IDataset

Dim pDataset As ESRI.ArcGIS.Geodatabase.IDataset
Set pDataset = pFClass

'Get the Workspace from the IDataset
Dim pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace
Set pWorkspace = pDataset.Workspace

'QI from IWorkspace to IWorkspaceEdit
Dim pWorkspaceEdit As ESRI.ArcGIS.Geodatabase.IWorkspaceEdit
Set pWorkspaceEdit = pWorkspace

'Start editing if needed
If Not pEditWorkSpace.IsBeingEdited Then
pEditWorkSpace.StartEditing (True)
End If



'Start an edit operation
pEditWorkSpace.StartEditOperation (True)

    'Create the new point feature
    Dim pFeature As IFeature
    Dim dblDist As Double
    If button = 2 Then
        Set pFeature1 = Nothing
        Set tmpPoint1 = Nothing
        Exit Sub
    End If
    If pFeature1 Is Nothing Then
        If tmpPoint1 Is Nothing Then
            Set tmpPoint1 = tmpPoint
        Else
            Set pFeature = pFClass.CreateFeature
            Set pFeature.Shape = tmpPoint
            If Not tmpPoint1 Is Nothing Then
                dblDist = 0
                dblDist = GetDistance1(pFeature)
                pFeature.Value(pFeature.Class.Fields.FindField("Distance")) = dblDist
                pFeature.Value(pFeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""
                pFeature.Store
            End If
            Set pFeature1 = pFeature
        End If
    Else
        Set pFeature = pFClass.CreateFeature
        Set pFeature.Shape = tmpPoint
        If Not pFeature1 Is Nothing Then
            dblDist = 0
            dblDist = GetDistance(pFeature1, pFeature)
            pFeature.Value(pFeature.Class.Fields.FindField("Distance")) = dblDist
            pFeature.Value(pFeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""

            pFeature.Store
            'Complete the edit operation
    pEditWorkSpace.StopEditOperation (True)
        End If
        Set pFeature1 = pFeature
    End If

    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
End Sub
Function GetDistance1(pFeature1 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = tmpPoint1
    Set pGeom2 = pFeature1.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance1 = dblDist
End Function
Function GetDistance(pFeature1 As IFeature, pFeature2 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = pFeature1.Shape
    Set pGeom2 = pFeature2.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance = dblDist
End Function
0 Kudos
G__Venkata_VijayaKumar
Occasional Contributor
1. Remove the "ESRI.ArcGIS.Geodatabase." in every declaration it is present.
2. Check variable usage in your code, pWorkspaceEdit and pEditWorkspace are not the same.
    Replace "pEditWorkspace" in your code with "pWorkspaceEdit"
3. There is no declaration for pFeature1, you need to declare it as a variable of type IFeature.

Just a few observations.
0 Kudos
TonyAlmeida
MVP Regular Contributor
Oops code was a little wacked cuz i've been working on it and can't quit get. I don't understand why this code work great on a shapefile doesn't work in a geodatabase?

I set the declaration here Set pFeature1 = pFeature.
0 Kudos
G__Venkata_VijayaKumar
Occasional Contributor
hmmm...

By declaration i meant...

Dim pFeature1 As IFeature
0 Kudos
TonyAlmeida
MVP Regular Contributor
I am getting a run-time error '424': Object required on line "If temPoint 1 is Nothing then".

My current code which i needs help with please....



Private Sub UIToolControl2_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)


        'Adds a point to a shapefile
    Dim pMap As IMap
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Dim pDataset As IDataset
    Dim pFeature1 As IFeature

    'Get the first layer in the map
    Dim pFeatLyr As IGeoFeatureLayer
    Set pFeatLyr = pMap.Layer(0)
   


     'Create a point from the mouse down click
    Dim tmpPoint As IPoint
    Set tmpPoint = New Point
    Set tmpPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

    'Get the feature class
    Dim pFClass As IFeatureClass
    Set pFClass = pFeatLyr.FeatureClass

'QI from IFeatureclass to IDataset

'Dim pDataset As IDataset
Set pDataset = pFClass

'Get the Workspace from the IDataset
Dim pWorkspace As IWorkspace
Set pWorkspace = pDataset.Workspace

'QI from IWorkspace to IWorkspaceEdit
Dim pWorkspaceEdit As IWorkspaceEdit
Set pWorkspaceEdit = pWorkspace

'Start editing if needed
If Not pWorkspaceEdit.IsBeingEdited Then
pWorkspaceEdit.StartEditing (True)
End If



'Start an edit operation
pWorkspaceEdit.StartEditOperation

    'Create the new point feature
    Dim pFeature As IFeature
    Dim dblDist As Double
    If button = 2 Then
        Set pFeature1 = Nothing
        Set tmpPoint1 = Nothing
        Exit Sub
    End If
    If pFeature1 Is Nothing Then
        If tmpPoint1 Is Nothing Then
            Set tmpPoint1 = tmpPoint
        Else
            Set pFeature = pFClass.CreateFeature
            Set pFeature.Shape = tmpPoint
            If Not tmpPoint1 Is Nothing Then
                dblDist = 0
                dblDist = GetDistance1(pFeature)
                pFeature.Value(pFeature.Class.Fields.FindField("Distance")) = dblDist
                pFeature.Value(pFeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""
                pFeature.Store
            End If
            Set pFeature1 = pFeature
        End If
    Else
        Set pFeature = pFClass.CreateFeature
        Set pFeature.Shape = tmpPoint
        If Not pFeature1 Is Nothing Then
            dblDist = 0
            dblDist = GetDistance(pFeature1, pFeature)
            pFeature.Value(pFeature.Class.Fields.FindField("Distance")) = dblDist
            pFeature.Value(pFeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""

            pFeature.Store
            'Complete the edit operation
    pWorkspaceEdit.StopEditOperation
        End If
        Set pFeature1 = pFeature
    End If

    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
End Sub
Function GetDistance1(pFeature1 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = tmpPoint1
    Set pGeom2 = pFeature1.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance1 = dblDist
End Function
Function GetDistance(pFeature1 As IFeature, pFeature2 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = pFeature1.Shape
    Set pGeom2 = pFeature2.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance = dblDist
End Function
0 Kudos
TonyAlmeida
MVP Regular Contributor
ok, i have got it working some what. When i click i get an "ArcMap Drawing Errors" dsd."CC\TALMEIDA".TonyDSD_DBO_CC_Address_Points:  Invalid column value[SiteNum]. When i check the layers attributes there all gone. I checked the table in ArcCatalog and the table is fine, i tired adding the layer from ArcCatalog to my arcmap and it gives me that error. I am totally lost.

Here is the code...

Dim pFeature1 As IFeature
Dim tmpPoint1 As IPoint

Private Sub UIToolControl2_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)


        'Adds a point to a shapefile
    Dim pMap As IMap
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Dim pDataset As IDataset
    Dim pFeature1 As IFeature
    'Dim tmpPoint1 As IPoint

    'Get the first layer in the map
    Dim pFeatLyr As IGeoFeatureLayer
    Set pFeatLyr = pMap.Layer(0)
   


     'Create a point from the mouse down click
    Dim tmpPoint As IPoint
    Set tmpPoint = New Point
    Set tmpPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

    'Get the feature class
    Dim pFClass As IFeatureClass
    Set pFClass = pFeatLyr.FeatureClass

'QI from IFeatureclass to IDataset

'Dim pDataset As IDataset
Set pDataset = pFClass

'Get the Workspace from the IDataset
Dim pWorkspace As IWorkspace
Set pWorkspace = pDataset.Workspace

'QI from IWorkspace to IWorkspaceEdit
Dim pWorkspaceEdit As IWorkspaceEdit
Set pWorkspaceEdit = pWorkspace

'Start editing if needed
If Not pWorkspaceEdit.IsBeingEdited Then
pWorkspaceEdit.StartEditing (True)
End If

'Start an edit operation
pWorkspaceEdit.StartEditOperation

    'Create the new point feature
    Dim pFeature As IFeature
    Dim dblDist As Double
    If button = 2 Then
        Set pFeature1 = Nothing
        Set tmpPoint1 = Nothing
        Exit Sub
    End If
    If pFeature1 Is Nothing Then
        If tmpPoint1 Is Nothing Then
            Set tmpPoint1 = tmpPoint
        Else
            Set pFeature = pFClass.CreateFeature
            Set pFeature.Shape = tmpPoint
            If Not tmpPoint1 Is Nothing Then
                dblDist = 0
                dblDist = GetDistance1(pFeature)
                pFeature.Value(pFeature.Class.Fields.FindField("Distance")) = dblDist
                pFeature.Value(pFeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""
                pFeature.Store
            End If
            Set pFeature1 = pFeature
        End If
    Else
        Set pFeature = pFClass.CreateFeature
        Set pFeature.Shape = tmpPoint
        If Not pFeature1 Is Nothing Then
            dblDist = 0
            dblDist = GetDistance(pFeature1, pFeature)
            pFeature.Value(pFeature.Class.Fields.FindField("Distance")) = dblDist
            pFeature.Value(pFeature.Class.Fields.FindField("SiteNum")) = (dblDist / 5.28) + InputBox("Enter Address Range") & ""

            pFeature.Store
            'Complete the edit operation
    pWorkspaceEdit.StopEditOperation
        End If
        Set pFeature1 = pFeature
    End If

    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
End Sub
Function GetDistance1(pFeature1 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = tmpPoint1
    Set pGeom2 = pFeature1.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance1 = dblDist
End Function
Function GetDistance(pFeature1 As IFeature, pFeature2 As IFeature) As Double
    Dim pGeom1 As IGeometry
    Dim pGeom2 As IGeometry
    Dim pProx As IProximityOperator
    Dim dblDist As Double
    Set pGeom1 = pFeature1.Shape
    Set pGeom2 = pFeature2.Shape
    Set pProx = pGeom2
    dblDist = pProx.ReturnDistance(pGeom1)
    'MsgBox dblDist
    GetDistance = dblDist
End Function
0 Kudos