阅读:2454回复:2
[求助]怎样用vb在arcmap里根据坐标生成polygon
<P> 下面是我在vba里读取cad数据在arcmap里生成图形的代码,我想将他转换到vb里该怎么弄呀?<BR>求高手指点!谢谢</P>
<P>Dim pWorkspaceFactory As IWorkspaceFactory<BR> Dim pFeatureWorkspace As IFeatureWorkspace<BR> Dim pfeaturelayer As IFeatureLayer<BR> Dim pMxDocument As IMxDocument<BR> Dim pmap As IMap<BR> Dim plin As IPolyline<BR> Dim ppolygon As IPolygon<BR> Dim pPointCollection As IPointCollection<BR> Dim ppoint As IPoint<BR> Dim sDir As String<BR> Dim pFeatureTarget As IFeature<BR> Dim pFeatureClassTarget As IFeatureClass<BR> Dim acadApp As AcadApplication ' AutoCAD应用程序对象<BR> Dim acadDoc As AcadDocument<BR> 'Dim pfeaturelayer As IFeatureLayer<BR> 'Dim pmxdocument As IMxDocument<BR>' Dim pmap As IMap<BR>Sub hh()<BR> ' Dim acadApp As AcadApplication ' AutoCAD应用程序对象<BR> ' Dim acadDoc As AcadDocument<BR> Dim cn As New ADODB.Connection<BR> Dim rst As New ADODB.RecordSet<BR> Dim gdp As New ADODB.RecordSet<BR> Dim gdv3 As New ADODB.RecordSet<BR> Dim cadtc As String '图层变量<BR> Dim tcsz() As Variant '图层数组<BR> Dim xh As Integer<BR> Dim sfilepath As String, sfilename As String<BR> Dim pfields As IFields<BR> Dim pfield As IField<BR> Dim shpname As String, nr As String<BR> Dim js As Integer<BR> Dim zdh As String, syz As String, jfh As String, fjqh As String, tdzl As String '宗地号、使用者、</P> <P>街坊号、土地坐落<BR> 'Dim ftype(0 To 1) As Integer<BR> ' Dim fdata(0 To 1) As Variant<BR> Dim ftype(0) As Integer<BR> Dim fdata(0) As Variant<BR> Set acadApp = GetObject(, "AutoCAD.Application.16")<BR> If Err Then<BR> MsgBox "AUTOCAD图形软件未打开!"<BR> End<BR> End If<BR> Dim entry As AcadLayer</P> <P> If Err Then<BR> Err.Clear<BR> ' 创建一个新的AutoCAD应用程序对象<BR> Set acadApp = CreateObject("AutoCAD.Application.16")<BR> <BR> If Err Then<BR> MsgBox Err.Description<BR> Exit Sub<BR> End If<BR> End If<BR> On Error GoTo ErrorHandler:<BR> ' 显示AutoCAD应用程序<BR> acadApp.Visible = True<BR> Set acadDoc = acadApp.ActiveDocument<BR> acadDoc.SendCommand "zoom" ; vbCr ; "e" ; vbCr<BR> acadDoc.SendCommand "-purge" ; vbCr ; "a" ; vbCr ; "" ; vbCr ; "n" ; vbCr '清楚多余的图层<BR> '下面进行图层循环加载不同的空表<BR> p = 0<BR> For Each entry In acadDoc.Layers<BR> If entry.Name <> "0" Then<BR> cadtc = entry.Name<BR> shpname = jztc(cadtc)<BR> ' End If<BR> If shpname <> "" Then<BR> ' ftype(0) = 0: fdata(0) = "LWPOLYLINE"<BR> ftype(0) = 8: fdata(0) = cadtc<BR> ' ftype(1) = 8: fdata(1) = cadtc<BR> On Error Resume Next<BR> Dim xzj As AcadSelectionSet<BR> Dim zb As Variant<BR> If Not IsNull(acadDoc.SelectionSets.Item("st")) Then<BR> Set xzj = acadDoc.SelectionSets.Item("st")<BR> xzj.Delete<BR> End If<BR> Set xzj = acadDoc.SelectionSets.Add("st") '新建选择集<BR> 'MsgBox xzj.Name<BR> xzj.Select acSelectionSetAll, , , ftype, fdata '选择宗地<BR> js = 1<BR> For Each ty In xzj<BR> If Right(cadtc, 1) = "屋" Then<BR> If ty.Closed = True Then<BR> If ty.Area > 0 Then<BR> dds = (UBound(ty.Coordinates) + 1) / 2<BR> zb = ty.Coordinates<BR> scfw zb, dds, js, cadtc<BR> End If<BR> End If<BR> Else<BR> a = ty.ObjectName<BR> ' MsgBox ty.ObjectName<BR> If ty.ObjectName = "AcDbText" Then<BR> zb = ty.InsertionPoint<BR> nr = ty.TextString<BR> zj zb, cadtc, nr<BR> Else<BR> <BR> dds = (UBound(ty.Coordinates) + 1) / 2<BR> zb = ty.Coordinates<BR> xzdw zb, dds, js, cadtc<BR> End If<BR> End If<BR> js = js + 1<BR> Next ' For Each ty In xzj<BR> End If '' If shpname <> "" Then<BR> p = p + 1<BR> End If 'If entry.Name <> "0" Then<BR> Next ' For Each entry In acadDoc.Layers</P> <P> '//结束编辑<BR> ' pWorkspaceEdit.StopEditOperation<BR> ' pWorkspaceEdit.StopEditing True<BR> Exit Sub<BR>ErrorHandler:<BR> MsgBox Err.Description<BR>End Sub</P> <P><BR>Function zbtq(pid, cn, zdh, syz, zdmj, jj, h, tdzl) As Variant '生成宗地<BR>'On Error Resume Next<BR> 'Dim pfeaturelayer As IFeatureLayer<BR>' Dim pmxdocument As IMxDocument<BR>' Dim pmap As IMap<BR> Dim jdh As String, jfh As String, djh As String<BR> Dim xh As Integer<BR> xh = h + 1<BR> jdh = Left(jj, 3)<BR> jfh = Right(jj, 3)<BR> djh = "320506" + Trim(jj) + Trim(zdh)<BR> Set pfeaturelayer = New FeatureLayer<BR> Set pMxDocument = Application.Document<BR> Set pmap = pMxDocument.FocusMap<BR> Set pfeaturelayer = pmap.Layer(0)<BR> Dim pFeatureClassTarget As IFeatureClass<BR> Dim pfclass As IFeature<BR> Dim pFeatureTarget As IFeature<BR> Set pFeatureClassTarget = pfeaturelayer.FeatureClass<BR> Set pFeatureTarget = pFeatureClassTarget.CreateFeature<BR>Dim zb() As Double<BR>Dim gdv3 As New ADODB.RecordSet<BR>Dim gdpv As New ADODB.RecordSet<BR>Dim ppoint As IPoint<BR>Set ppoint = New Point<BR> Dim ppolygon As IPolygon<BR> Dim pPointCollection As IPointCollection<BR> Set pPointCollection = New Polygon<BR>Dim sm As Integer<BR> Set pfields = pFeatureClassTarget.Fields<BR> ' k = pfields.FindField("code")<BR> pFeatureTarget.Value(2) = Str(xh)<BR> pFeatureTarget.Value(3) = "DJL507"<BR> pFeatureTarget.Value(4) = "2006010100"<BR> pFeatureTarget.Value(5) = "320506"<BR> pFeatureTarget.Value(6) = jdh<BR> pFeatureTarget.Value(7) = jfh<BR> pFeatureTarget.Value(8) = zdh<BR> pFeatureTarget.Value(9) = djh<BR> pFeatureTarget.Value(12) = zdmj<BR> pFeatureTarget.Value(15) = tdzl<BR> pFeatureTarget.Value(16) = "国有"<BR> gdv3.Open "select sm=count(vid) from gdv3 where vid in (select pvid from gdpv where pid= " ; pid ; " </P> <P>)", cn, adOpenDynamic, adLockPessimistic<BR> If Not gdv3.EOF Then<BR> sm = gdv3.Fields("sm")<BR> End If<BR> gdv3.Close<BR> ReDim zb(2 * sm - 1)<BR> i = 0<BR> gdpv.Open "select * from gdpv where pid=" ; pid ; " order by pvo asc", cn, adOpenDynamic, </P> <P>adLockBatchOptimistic<BR>Do While Not gdpv.EOF<BR> gdv3.Open "select * from gdv3 where vid = " ; gdpv.Fields("pvid") ; " ", cn, adOpenDynamic, </P> <P>adLockPessimistic<BR> If Not gdv3.EOF Then<BR> X = gdv3.Fields("y")<BR> zb(i * 2) = gdv3.Fields("y")<BR> zb(i * 2 + 1) = gdv3.Fields("x")<BR> i = i + 1<BR> End If<BR> gdpv.MoveNext<BR> gdv3.Close<BR> Loop<BR> gdpv.Close<BR> For i = 0 To sm - 1<BR> ' Set pPointCollection = New polygon<BR> ppoint.PutCoords zb(2 * i), zb(2 * i + 1)<BR> pPointCollection.AddPoint ppoint<BR> Next<BR> Set ppolygon = pPointCollection<BR> ppolygon.Close<BR> ' Set pFeatureTarget.shape = pPointCollection<BR> Set pFeatureTarget.Shape = ppolygon<BR> ' Set pFeatureTarget.Shape = pPointCollection<BR> pFeatureTarget.Store<BR> zbtq = zb<BR>End Function<BR>'Private Sub jztc(tc) '根据cad图层信息加载相应shape文件<BR>Function jztc(tc) As String<BR>Dim sfilepath As String, shpname As String<BR>sfilepath = "d:\sy"<BR>Select Case tc<BR>Case "一般房屋"<BR>shpname = "dlgkb01a"<BR>Case "简单房屋"<BR>shpname = "dlgkb01a"<BR>Case "围墙"<BR>shpname = "dlgkb01l"<BR>Case "栏杆"<BR>shpname = "dlgkb01l"<BR>Case "活树篱笆"<BR>shpname = "dlgkb01l"<BR>Case "铁丝网"<BR>shpname = "dlgkb01l"<BR>'Case "界址线"<BR>'shpname = "djlkc14a"<BR>Case "内部道路"<BR>shpname = "dlgkd01l"<BR>Case "标注"<BR>shpname = "dlgkb03p"<BR>Case "居民地注记"<BR>shpname = "dlgkb03p"<BR>Case "交通及附属设施注记"<BR>shpname = "dlgkd03p"<BR>End Select</P> <P>If shpname <> "" Then<BR>'If shpname = "dlgkb01a" Then<BR> Set pWorkspaceFactory = New ShapefileWorkspaceFactory<BR> Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(sfilepath, 0)<BR> Set pfeaturelayer = New FeatureLayer<BR> Set pfeaturelayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(shpname)<BR> pfeaturelayer.Name = pfeaturelayer.FeatureClass.AliasName<BR> 'Add the FeatureLayer to the focus map<BR> Set pMxDocument = Application.Document<BR> Set pmap = pMxDocument.FocusMap<BR> pmap.AddLayer pfeaturelayer '打开图层<BR> End If<BR> jztc = shpname<BR>End Function<BR>Private Sub scfw(zb, sm, xh, tc) '生成房屋等面状地物<BR>Dim dldm As String<BR>Select Case tc<BR> Case "一般房屋"<BR> dldm = "211100"<BR> Case "简单房屋"<BR> dldm = "212000"<BR> Case "建筑中房屋"<BR> dldm = "213000"<BR> Case "破坏房屋"<BR> dldm = "214100"<BR> Case "棚房"<BR> dldm = 215100<BR> End Select<BR> <BR>'Dim zbz() As Double<BR>'ReDim zbz(2 * sm - 1) As Double<BR>'For j = 0 To sm * 2 - 1<BR>'zbz(j) = zb(j)<BR>'Next j<BR>'Dim na As String<BR> Set pfeaturelayer = pmap.Layer(0)<BR> ' Dim pFeatureClassTarget As IFeatureClass<BR> ' Dim pfclass As IFeature<BR> 'Dim pFeatureTarget As IFeature<BR> Set pFeatureClassTarget = pfeaturelayer.FeatureClass<BR> Set pFeatureTarget = pFeatureClassTarget.CreateFeature<BR> ' Set pfield = pFeatureClassTarget.Fields<BR> ' s = pfield.Field("cord")<BR> pFeatureTarget.Value(2) = Str(xh)<BR> pFeatureTarget.Value(3) = dldm<BR> 'pFeatureTarget.Value(3) = "243100"<BR> ' Dim ppoint As IPoint<BR> Set ppoint = New Point<BR> ' Dim ppolygon As IPolygon<BR> ' Dim pPointCollection As IPointCollection<BR> Set pPointCollection = New Polygon<BR> For i = 0 To sm - 1<BR> ' Set pPointCollection = New Polygon<BR> ppoint.PutCoords zb(2 * i), zb(2 * i + 1)<BR> pPointCollection.AddPoint ppoint<BR> Next<BR> Set ppolygon = pPointCollection<BR> ppolygon.Close<BR> ' Set pFeatureTarget.shape = pPointCollection<BR> Set pFeatureTarget.Shape = ppolygon<BR> ' Set pFeatureTarget.Shape = pPointCollection<BR> pFeatureTarget.Store</P> <P>End Sub<BR>Private Sub xzdw(zb, sm, xh, tc) '生成围墙、栏杆、道路等线状地物<BR>Dim dldm As String</P> <P>Select Case tc<BR>Case "围墙"<BR>dldm = "243100"<BR>Case "栏杆"<BR>dldm = "245100"<BR>Case "等外公路"<BR>dldm = "433000"<BR>Case "内部道路"<BR>dldm = "444000"<BR>End Select<BR> Set pfeaturelayer = pmap.Layer(0)<BR> Set pFeatureClassTarget = pfeaturelayer.FeatureClass<BR> Set pFeatureTarget = pFeatureClassTarget.CreateFeature<BR> pFeatureTarget.Value(2) = Str(xh)<BR> pFeatureTarget.Value(3) = dldm<BR> Set ppoint = New Point<BR> Set pPointCollection = New Polyline<BR> For i = 0 To sm - 1<BR> ' Set pPointCollection = New Polygon<BR> ppoint.PutCoords zb(2 * i), zb(2 * i + 1)<BR> pPointCollection.AddPoint ppoint<BR> Next<BR> ' Set ppolygon = pPointCollection<BR> ' ppolygon.Close<BR> ' Set pFeatureTarget.shape = pPointCollection<BR> Set pFeatureTarget.Shape = pPointCollection<BR> ' Set pFeatureTarget.Shape = pPointCollection<BR> pFeatureTarget.Store<BR> <BR>End Sub<BR>Private Sub zj(zb, tc, nr) '生成各种注记<BR>Dim zjnr As String<BR>Dim dldm As String<BR>Select Case tc<BR> Case "居民地注记"<BR> dldm = "200000"<BR> Case "工矿建(构)筑物及其它设施注记"<BR> dldm = "300000"<BR> Case "交通及附属设施注记"<BR> dldm = "400000"<BR> Case "管线及附属设施注记"<BR> dldm = "500000"<BR> Case "水系及附属设施注记"<BR> dldm = "600000"<BR> Case "地貌和土质注记“"<BR> dldm = "800000"<BR> Case "境界注记"<BR> dldm = "700000"<BR> Case "植被注记"<BR> dldm = "900000"<BR> <BR>End Select</P> <P><BR> Set pfeaturelayer = pmap.Layer(0)<BR> Set pFeatureClassTarget = pfeaturelayer.FeatureClass<BR> Set pFeatureTarget = pFeatureClassTarget.CreateFeature<BR> pFeatureTarget.Value(3) = dldm<BR> pFeatureTarget.Value(4) = nr<BR> Set ppoint = New Point<BR> ppoint.PutCoords zb(0), zb(1)<BR> Set pFeatureTarget.Shape = ppoint<BR> pFeatureTarget.Store<BR> ' pGraphicsContainer.AddElement pTextElement, 0 '向Map中添加元素<BR> <BR> <BR> <BR>End Sub<BR></P> |
|
1楼#
发布于:2008-10-12 19:16
<P>要根据你最后的运行环境,如果是仍然是在ArcMap 环境下运行,就要做一个Command ,将VBA 代码直接拷贝到click 事件响应函数中调用,在Create 函数中获取Application ,和mxDocumanet ,然后编译成dll 。 再启动Arcmap ,customize ,加入你的command ,即可。</P>
<P> 如果在Engine 环境下运行,就要将其中 Dim pMxDocument As IMxDocument,去掉,直接用 Imap 即可,其余代码基本不变。</P> |
|
2楼#
发布于:2008-10-16 19:02
谢谢!指点!已经弄好了!
|
|