thomasa

CreateFeatureClass error for lines but not for polygons or points

Discussion created by thomasa on Feb 9, 2012
Latest reply on Feb 12, 2012 by thomasa
Hi all,
I am creating shape files with simple code that works fine for polygon or point shape files. For line shape files the code however fails at the FeatureWorkspace.CreateFeatureClass command (highlighted in code below). I guess it's just a pretty dumb mistake...
Any help is appreciated!

Axel

Sub CreateShape(sDBPath As String, sOutShapeName As String, lBID As Long)

  Dim pWkSp As IWorkspace, pShWkSpF As IWorkspaceFactory, pFWkSp As IFeatureWorkspace
  Dim pOutShapeFCls As IFeatureClass
  Dim pPolygon As IPolygon, pF As IFeature
  Dim pField As IField, pFieldEdit As IFieldEdit
  Dim pFields As IFields, pFieldsEdit As IFieldsEdit
  Dim pGeomDefEdit As IGeometryDefEdit, cGeomTyp As esriGeometryType
  Dim pCLSID As UID
  Dim sFeldname As String
  Dim iNFields As Integer, iFI As Integer
 
  Set pShWkSpF = New ShapefileWorkspaceFactory
  Set pWkSp = pShWkSpF.OpenFromFile(sDBPath, 0)
  Set pFWkSp = pWkSp
  If pFWkSp Is Nothing Then Exit Sub
 
  If sOutShapeName Like "*poly*" Then
    cGeomTyp = esriGeometryPolygon
    iNFields = 13
    sFeldname = "POLY-ID"
  ElseIf sOutShapeName Like "*line*" Then
    cGeomTyp = esriGeometryPolyline
    iNFields = 17
    sFeldname = "ARC-ID"
  ElseIf sOutShapeName Like "*dot*" Then
    cGeomTyp = esriGeometryPoint
    iNFields = 14
    sFeldname = "POINT-ID"
  Else
    MsgBox "File name not accepted", vbCritical
    Exit Sub
  End If
 
  Set pFields = New Fields
  Set pFieldsEdit = pFields
  Set pField = New Field
  pFieldsEdit.FieldCount = iNFields
  iFI = 0
 
  Set pGeomDefEdit = New GeometryDef                           ' Geometry field
  With pGeomDefEdit
    .GeometryType = cGeomTyp
    .HasM = False
    .HasZ = False
    Set .SpatialReference = SetProjection       ' set in a separate function, not included in this post
  End With
       
  Set pFieldEdit = New Field
  With pFieldEdit
    .AliasName = "FID"
    .Name = "FID"
    .Type = esriFieldTypeOID
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "Shape"
    .AliasName = "Geometry"
    .Type = esriFieldTypeGeometry
    Set .GeometryDef = pGeomDefEdit
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  If Not cGeomTyp = esriGeometryLine Then
    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "AREA"
      .AliasName = "Area"
      .Precision = 20
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "PERIMETER"
      .AliasName = "Perimeter"
      .Precision = 20
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1
  End If
 
  If cGeomTyp = esriGeometryLine Then
    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "FNODE"
      .AliasName = "FNODE"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "TNODE"
      .AliasName = "TNODE"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "LPOLY"
      .AliasName = "LPOLY"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "RPOLY"
      .AliasName = "RPOLY"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1

    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "LENGTH"
      .AliasName = "LENGTH"
      .Precision = 11
      .Scale = 0
      .Type = esriFieldTypeDouble
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1
  End If
 
  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "SYS_ID"
    .AliasName = "SYS_ID"
    .Precision = 2
    .Scale = 0
    .Type = esriFieldTypeSingle
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = sFeldname
    .AliasName = sFeldname
    .Precision = 2
    .Scale = 0
    .Type = esriFieldTypeSingle
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "BLATT_ID"
    .AliasName = "BLATT_ID"
    .Precision = 6
    .Scale = 6
    .Type = esriFieldTypeInteger
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  If Not cGeomTyp = esriGeometryPolygon Then
    Set pFieldEdit = New Field
    With pFieldEdit
      .Name = "SCHNITT_ID"
      .AliasName = "SCHNITT_ID"
      .Precision = 6
      .Scale = 6
      .Type = esriFieldTypeInteger
    End With
    Set pFieldsEdit.Field(iFI) = pFieldEdit
    iFI = iFI + 1
  End If
     
  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "BETRIEB"
    .AliasName = "Betrieb"
    .Length = 254
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "BLATTNAME"
    .AliasName = "Blattname"
    .Length = 65
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

  Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "RASTERBILD"
    .AliasName = "Rasterbild"
    .Length = 25
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

   Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "CD_NAME"
    .AliasName = "CD_Name"
    .Length = 20
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

   Set pFieldEdit = New Field
  With pFieldEdit
    .Name = "HOTLINK"
    .AliasName = "Hotlink"
    .Length = 80
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
  iFI = iFI + 1

   Set pFieldEdit = New Field
    With pFieldEdit
    .Name = "BEMERKUNG"
    .AliasName = "Bemerkung"
    .Length = 60
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(iFI) = pFieldEdit
 
  Set pCLSID = New UID
  pCLSID.Value = "esriGeodatabase.Feature"
 
  Set pOutShapeFCls = pFWkSp.CreateFeatureClass(sOutShapeName, pFields, pCLSID, Nothing, esriFTSimple, "Shape", "")  ' code fails here
  If pOutShapeFCls Is Nothing Then Exit Sub
  Set pF = pOutShapeFCls.CreateFeature
  If pF Is Nothing Then Exit Sub
  lFld = pF.Fields.FindField("BLATT_ID")
  If lFld > -1 Then pF.Value(lFld) = lBID
  pF.Store

End Sub

Outcomes