gsl1982
路人甲
路人甲
  • 注册日期2004-05-08
  • 发帖数135
  • QQ
  • 铜币543枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:1675回复:1

切割问题

楼主#
更多 发布于:2004-10-21 09:52
<P>请问,在屏幕上画个矩形或多边形,如何得到矩形或多边形内的要素,要素和矩形相交的要切割。但不切割原图层要素,只是临时得到切割后的要素!</P>
<P>如何实现阿,用什么库,什么接口?</P>
喜欢0 评分0
gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
1楼#
发布于:2004-10-21 16:09
<P>在ao的帮助里有个Cut polygons sample,你可以看看</P><P><b>Description:

<TEXT></b>This sample duplicates the 'Cut Polygon Features' task but uses a polygon edit sketch instead of a polyline edit sketch. This task makes it easier to create doughnut polygons for example. The illustration below shows an edit sketch being drawn over a single selected feature. Finishing the sketch creates a new feature by cutting it out from the selected feature. </P><P><FONT color=#008000>'This task sets the edit sketch geometry to polygon type and uses this</FONT>
<CODE><FONT color=#008000>'geometry as a cookie cutter on selected features</FONT></CODE>

<CODE><FONT color=#0000ff>Option Explicit

Implements</FONT></CODE> IEditTask

<CODE><FONT color=#0000ff>Private</FONT></CODE> m_pEditor <CODE><FONT color=#0000ff>As</FONT></CODE> IEditor
<CODE><FONT color=#0000ff>Private</FONT></CODE> m_pEdSketch <CODE><FONT color=#0000ff>As</FONT></CODE> IEditSketch
<CODE><FONT color=#0000ff>Private</FONT></CODE> m_pMxDoc <CODE><FONT color=#0000ff>As</FONT></CODE> IMxDocument
<CODE><FONT color=#0000ff>Private WithEvents</FONT></CODE> EditorEvents <CODE><FONT color=#0000ff>As</FONT></CODE> Editor

<CODE><FONT color=#0000ff>Private Sub</FONT></CODE> IEditTask_Activate(<CODE><FONT color=#0000ff>ByVal</FONT></CODE> Editor <CODE><FONT color=#0000ff>As</FONT></CODE> esriEditor.IEditor, <CODE><FONT color=#0000ff>ByVal</FONT></CODE> oldTask <CODE><FONT color=#0000ff>As</FONT></CODE> esriEditor.IEditTask)
  <CODE><FONT color=#0000ff>Set</FONT></CODE> m_pEditor = Editor
  <CODE><FONT color=#0000ff>Set</FONT></CODE> EditorEvents = Editor
  <CODE><FONT color=#0000ff>Set</FONT></CODE> m_pEdSketch = Editor
  <CODE><FONT color=#0000ff>Set</FONT></CODE> m_pMxDoc = Editor.Parent.Document
  <CODE><FONT color=#008000>'Call Selection changed to see if the sketch tools should be enabled</FONT></CODE>
  EditorEvents_OnSelectionChanged
<CODE><FONT color=#0000ff>End Sub

Private Sub</FONT></CODE> IEditTask_Deactivate()
  <CODE><FONT color=#008000>'When the task is deactivated, stop listening for events</FONT></CODE>
  <CODE><FONT color=#0000ff>Set</FONT></CODE> EditorEvents = <CODE><FONT color=#0000ff>Nothing
End Sub

Private Property Get</FONT></CODE> IEditTask_Name() <CODE><FONT color=#0000ff>As String</FONT></CODE>
  IEditTask_Name = "Cut Polygons by Area" <CODE><FONT color=#008000>'Give our task a name</FONT></CODE>
<CODE><FONT color=#0000ff>End Property

Private Sub</FONT></CODE> IEditTask_OnDeleteSketch()

<CODE><FONT color=#0000ff>End Sub

Private Sub</FONT></CODE> IEditTask_OnFinishSketch()
  <CODE><FONT color=#0000ff>On Error GoTo</FONT></CODE> handler:

  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pRefreshArea <CODE><FONT color=#0000ff>As</FONT></CODE> IInvalidArea   <CODE><FONT color=#008000>'Used to specify the area of the display to refresh</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pOrigFeature <CODE><FONT color=#0000ff>As</FONT></CODE> IFeature       <CODE><FONT color=#008000>'The selected feature</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pNewFeature <CODE><FONT color=#0000ff>As</FONT></CODE> IFeature        <CODE><FONT color=#008000>'New feature created during operation</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pEnumFeat <CODE><FONT color=#0000ff>As</FONT></CODE> IEnumFeature
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pCutGeom <CODE><FONT color=#0000ff>As</FONT></CODE> IGeometry          <CODE><FONT color=#008000>'The geometry that performs the cut - edit sketch</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pRemainder <CODE><FONT color=#0000ff>As</FONT></CODE> IGeometry        <CODE><FONT color=#008000>'Difference between edit sketch and orig feature (modified orig feature)</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pSubDivision <CODE><FONT color=#0000ff>As</FONT></CODE> IGeometry      <CODE><FONT color=#008000>'Intersection between edit sketch and orig feaure (new feature)</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pFeatureClass <CODE><FONT color=#0000ff>As</FONT></CODE> IFeatureClass <CODE><FONT color=#008000>'The feature class in which to place new features</FONT></CODE>
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pTopoOperator <CODE><FONT color=#0000ff>As</FONT></CODE> ITopologicalOperator
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pFields <CODE><FONT color=#0000ff>As</FONT></CODE> IFields
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> pField <CODE><FONT color=#0000ff>As</FONT></CODE> IField
  <CODE><FONT color=#0000ff>Dim</FONT></CODE> FieldCount <CODE><FONT color=#0000ff>As Integer</FONT></CODE>
    
  <CODE><FONT color=#008000>'Start an edit operation so we can have undo/redo</FONT></CODE>
  m_pEditor.StartOperation
  
  <CODE><FONT color=#008000>'Get ready to invalidate the features</FONT></CODE>
  <CODE><FONT color=#0000ff>Set</FONT></CODE> pRefreshArea = <CODE><FONT color=#0000ff>New</FONT></CODE> InvalidArea
  
  <CODE><FONT color=#0000ff>Set</FONT></CODE> pCutGeom = m_pEdSketch.Geometry <CODE><FONT color=#008000>'Get the cutting geometry from the edit sketch</FONT></CODE>
  <CODE><FONT color=#0000ff>Set</FONT></CODE> pTopoOperator = pCutGeom
  pTopoOperator.Simplify <CODE><FONT color=#008000>' make sure the edit sketch geometry is simple</FONT></CODE>
  
  <CODE><FONT color=#0000ff>Set</FONT></CODE> pEnumFeat = m_pEditor.EditSelection <CODE><FONT color=#008000>'Get the selected set of editable features</FONT></CODE>
  pEnumFeat.Reset
  <CODE><FONT color=#0000ff>Set</FONT></CODE> pOrigFeature = pEnumFeat.Next  <CODE><FONT color=#008000>'Get the next feature in the enumeration (the first one in this case)</FONT></CODE>
  <CODE><FONT color=#0000ff>If</FONT></CODE> pOrigFeature <CODE><FONT color=#0000ff>Is Nothing Then GoTo</FONT></CODE> handler <CODE><FONT color=#008000>'abort the operation if nothing is selected</FONT></CODE>
 
  <CODE><FONT color=#008000>'Start a loop for each feature in the selection</FONT></CODE>
  <CODE><FONT color=#0000ff>Do While Not</FONT></CODE> pOrigFeature <CODE><FONT color=#0000ff>Is Nothing</FONT></CODE>

    <CODE><FONT color=#008000>'Only process polygon features</FONT></CODE>
    <CODE><FONT color=#0000ff>If</FONT></CODE> pOrigFeature.Shape.GeometryType = esriGeometryPolygon <CODE><FONT color=#0000ff>Then</FONT></CODE>
    
      pRefreshArea.Add pOrigFeature      <CODE><FONT color=#008000>'Add the feature to the invalidate object</FONT></CODE>
  
      <CODE><FONT color=#0000ff>Set</FONT></CODE> pTopoOperator = pOrigFeature.Shape
      <CODE><FONT color=#0000ff>Set</FONT></CODE> pSubDivision = pTopoOperator.Intersect(pCutGeom, esriGeometry2Dimension)
          
      <CODE><FONT color=#0000ff>If Not</FONT></CODE> pSubDivision.IsEmpty <CODE><FONT color=#0000ff>Then
        Set</FONT></CODE> pFeatureClass = pOrigFeature.Class
        <CODE><FONT color=#0000ff>Set</FONT></CODE> pNewFeature = pFeatureClass.CreateFeature  <CODE><FONT color=#008000>'Create a new Feature in the same feature class</FONT></CODE>
        <CODE><FONT color=#0000ff>Set</FONT></CODE> pNewFeature.Shape = pSubDivision  <CODE><FONT color=#008000>'Put the subdivided portion into this feature</FONT></CODE>
        
        <CODE><FONT color=#008000>'Set the original featuer's shape to the results returned from the difference</FONT></CODE>
        <CODE><FONT color=#0000ff>Set</FONT></CODE> pRemainder = pTopoOperator.Difference(pCutGeom)
        <CODE><FONT color=#0000ff>Set</FONT></CODE> pOrigFeature.Shape = pRemainder
  
        <CODE><FONT color=#008000>'copy the attributes of the orig feature the new feature</FONT></CODE>
        <CODE><FONT color=#0000ff>Set</FONT></CODE> pFields = pOrigFeature.Fields
        <CODE><FONT color=#0000ff>For</FONT></CODE> FieldCount = 0 <CODE><FONT color=#0000ff>To</FONT></CODE> pFields.FieldCount - 1 <CODE><FONT color=#008000>'skip OID and geometry</FONT></CODE>
          <CODE><FONT color=#0000ff>Set</FONT></CODE> pField = pOrigFeature.Fields.Field(FieldCount)
          <CODE><FONT color=#0000ff>If Not</FONT></CODE> pField.Type = esriFieldTypeGeometry <CODE><FONT color=#0000ff>And Not</FONT></CODE> pField.Type = esriFieldTypeOID _
            <CODE><FONT color=#0000ff>And</FONT></CODE> pField.Editable <CODE><FONT color=#0000ff>Then</FONT></CODE>
              pNewFeature.Value(FieldCount) = pOrigFeature.Value(FieldCount)
            <CODE><FONT color=#0000ff>End If
        Next</FONT></CODE> FieldCount
  
        pOrigFeature.Store  <CODE><FONT color=#008000>'Now store the original feature with the remainder geometry</FONT></CODE>
        pNewFeature.Store   <CODE><FONT color=#008000>'Store the subdivided feature</FONT></CODE>
      <CODE><FONT color=#0000ff>End If
    
    End If</FONT></CODE> <CODE><FONT color=#008000>'End check for polygons</FONT></CODE>

    <CODE><FONT color=#0000ff>Set</FONT></CODE> pOrigFeature = pEnumFeat.Next  <CODE><FONT color=#008000>'Get the next feature in the selection</FONT></CODE>
  <CODE><FONT color=#0000ff>Loop</FONT></CODE>

  m_pMxDoc.FocusMap.ClearSelection <CODE><FONT color=#008000>'Clear the map's selection</FONT></CODE>

  <CODE><FONT color=#008000>'Refresh the display</FONT></CODE>
  <CODE><FONT color=#0000ff>Set</FONT></CODE> pRefreshArea.Display = m_pEditor.Display
  pRefreshArea.Invalidate esriAllScreenCaches <CODE><FONT color=#008000>'Update the display</FONT></CODE>

  m_pEditor.StopOperation ("Cut Polygon Features")  <CODE><FONT color=#008000>'Complete the edit operation</FONT></CODE>
  
  <CODE><FONT color=#0000ff>Exit Sub</FONT></CODE>
  
handler:
  MsgBox "Unable to perform the cut task."
  m_pEditor.AbortOperation <CODE><FONT color=#008000>'In the event of an error, abort the operation</FONT></CODE>
<CODE><FONT color=#0000ff>End Sub

Private Sub</FONT></CODE> EditorEvents_OnSelectionChanged()
  <CODE><FONT color=#008000>'Only enabled the sketch tools if there is something selected</FONT></CODE>
  <CODE><FONT color=#0000ff>If</FONT></CODE> m_pEditor.SelectionCount = 0 <CODE><FONT color=#0000ff>Then</FONT></CODE>
    m_pEdSketch.GeometryType = esriGeometryNull
  <CODE><FONT color=#0000ff>Else</FONT></CODE>
    m_pEdSketch.GeometryType = esriGeometryPolygon    <CODE><FONT color=#008000>'Set the edit sketch geometry type</FONT></CODE>
  <CODE><FONT color=#0000ff>End If
End Sub</FONT></CODE>
</P>
GIS麦田守望者,期待与您交流。
举报 回复(0) 喜欢(0)     评分
游客

返回顶部