home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2002 March / PCWMAR02.iso / software / turbocad / v8trial / TurboCADv8ProfessionalNoReg.exe / Data.Cab / F41127_modContour.bas < prev    next >
Encoding:
BASIC Source File  |  2001-10-16  |  9.5 KB  |  314 lines

  1. Attribute VB_Name = "modContour"
  2. '******************************************************************'
  3. '*                                                                *'
  4. '*                      TurboCAD for Windows                      *'
  5. '*                   Copyright (c) 1993 - 2001                    *'
  6. '*             International Microcomputer Software, Inc.         *'
  7. '*                            (IMSI)                              *'
  8. '*                      All rights reserved.                      *'
  9. '*                                                                *'
  10. '******************************************************************'
  11.  
  12. Option Explicit
  13.  
  14. 'USGS DLG files have unix newline characters: Chr(10)
  15. 'VB's Line Input statement can only deal with Chr(13)-Chr(10) combinations
  16. Function UnixLineInput(result As String) As Boolean
  17.     On Error Resume Next
  18.     
  19.     result = ""
  20.     If EOF(1) Then
  21.         UnixLineInput = False
  22.         Exit Function
  23.     End If
  24.     
  25.     Dim c As String
  26.     Do While True
  27.         If EOF(1) Then Exit Do
  28.         c = Input(1, #1)
  29.         If c = Chr(10) Then Exit Do
  30.         result = result & c
  31.     Loop
  32.     UnixLineInput = True
  33. End Function
  34.  
  35.  
  36. 'Read contour line info from a USGS DLG file (optional format)
  37. Sub ConvertContourDlgFile(ByVal fname As String, ByVal sheet As Excel.Worksheet)
  38.  
  39.     'For specifications, see: http://rockyweb.cr.usgs.gov/nmpstds/dlgstds.html
  40.     'Part 2, Appendix 2-B lists file format records
  41.     'Part 3 lists attribute codes (we use 21-25 to get contour elevation data)
  42.     
  43.     On Error Resume Next
  44.     Dim i, nnodes, nareas, maxline As Integer
  45.     Dim row, col As Long
  46.     Dim line As String
  47.     
  48.     row = 1
  49.     Open fname For Input As #1
  50.     
  51.     'Skip to first category line
  52.     For i = 1 To 15
  53.         If Not UnixLineInput(line) Then GoTo Finished
  54.     Next
  55.     
  56.     'Get category parameters
  57.     nnodes = CInt(Mid(line, 31, 6))
  58.     nareas = CInt(Mid(line, 47, 6))
  59.     maxline = CInt(Mid(line, 57, 6))
  60.     
  61.     'Skip nodes
  62.     For i = 1 To nnodes
  63.         Do While Left(line, 1) <> "N"
  64.             If Not UnixLineInput(line) Then GoTo Finished
  65.         Loop
  66.         
  67.         'Consume N record
  68.         If Not UnixLineInput(line) Then GoTo Finished
  69.     Next
  70.     
  71.     'Skip areas
  72.     For i = 1 To nareas
  73.         Do While Left(line, 1) <> "A"
  74.             If Not UnixLineInput(line) Then GoTo Finished
  75.         Loop
  76.         
  77.         'Consume A record
  78.         If Not UnixLineInput(line) Then GoTo Finished
  79.     Next
  80.     
  81.     'Read lines
  82.     Dim idline, ncoords, maxcoords, natts, nchars, nrecs, j, k, l, m As Integer
  83.     Dim col1, col2 As Integer
  84.     Dim xarr() As Single
  85.     Dim yarr() As Single
  86.     Dim att1, att2 As Integer
  87.     Dim elev As Single
  88.     Dim hasElev As Boolean
  89.     
  90.     'Get a bunch of memory for our coordinate values
  91.     maxcoords = 500
  92.     ReDim xarr(maxcoords)
  93.     ReDim yarr(maxcoords)
  94.     
  95.     For i = 1 To maxline
  96.         Do While Left(line, 1) <> "L"
  97.             If Not UnixLineInput(line) Then GoTo Finished
  98.         Loop
  99.         
  100.         'Get line record parameters
  101.         idline = CInt(Mid(line, 2, 5))
  102.         ncoords = CInt(Mid(line, 43, 6))
  103.         natts = CInt(Mid(line, 49, 6))
  104.         nchars = CInt(Mid(line, 55, 6))
  105.         
  106.         'Consume L record
  107.         If Not UnixLineInput(line) Then GoTo Finished
  108.         
  109.         'Get coordinates
  110.         If ncoords > maxcoords Then
  111.             maxcoords = ncoords
  112.             ReDim xarr(maxcoords)
  113.             ReDim yarr(maxcoords)
  114.         End If
  115.         k = 1 'coordinate number
  116.         nrecs = (ncoords + 2) \ 3
  117.         For j = 1 To nrecs
  118.             col1 = 1
  119.             col2 = 13
  120.             l = 1 'coordinate index in record
  121.             Do While k <= ncoords And l <= 3
  122.                 xarr(k) = CSng(Mid(line, col1, 12))
  123.                 yarr(k) = CSng(Mid(line, col2, 12))
  124.                 k = k + 1
  125.                 l = l + 1
  126.                 col1 = col1 + 24
  127.                 col2 = col2 + 24
  128.             Loop
  129.             UnixLineInput line
  130.         Next
  131.         
  132.         'Get contour elevation attributes
  133.         elev = 0
  134.         hasElev = False
  135.         k = 1 'attribute number
  136.         nrecs = (natts + 5) \ 6
  137.         For j = 1 To nrecs
  138.             col1 = 1
  139.             col2 = 7
  140.             l = 1 'attribute index in record
  141.             Do While k <= natts And l <= 6
  142.                 att1 = CInt(Mid(line, col1, 6))
  143.                 att2 = CInt(Mid(line, col2, 6))
  144.                 
  145.                 'Look for contour elevation attribute
  146.                 If att1 >= 21 And att1 <= 25 Then
  147.                     Select Case att1
  148.                     Case 21:
  149.                         '10000 feet or more
  150.                         elev = CSng(att2) + 10000
  151.                     Case 22:
  152.                         '0 to 9999 feet
  153.                         elev = CSng(att2)
  154.                     Case 23:
  155.                         'below datum
  156.                         elev = -CSng(att2)
  157.                     Case 24:
  158.                         'meters
  159.                         elev = CSng(att2) / 0.3048
  160.                     Case 25:
  161.                         'meters below datum
  162.                         elev = -CSng(att2) / 0.3048
  163.                     End Select
  164.                     hasElev = True
  165.                 End If
  166.                 k = k + 1
  167.                 l = l + 1
  168.                 col1 = col1 + 12
  169.                 col2 = col2 + 12
  170.             Loop
  171.             UnixLineInput line
  172.         Next
  173.         
  174.         'Put contour into worksheet
  175.         If hasElev Then
  176.             nrecs = (ncoords + 99) \ 100
  177.             k = 0
  178.             For j = 1 To nrecs
  179.             
  180.                 'How many coordinates in this row?
  181.                 m = ncoords - k
  182.                 If m > 100 Then m = 100
  183.                 
  184.                 With sheet.Cells
  185.                     .Item(row, 1) = idline
  186.                     .Item(row, 2) = k
  187.                     .Item(row, 3) = m
  188.                     .Item(row, 4) = elev
  189.                     For l = 1 To m
  190.                         .Item(row, l * 2 + 3) = xarr(k)
  191.                         .Item(row, l * 2 + 4) = yarr(k)
  192.                         k = k + 1
  193.                     Next
  194.                 End With
  195.             Next
  196.         End If
  197.     Next
  198.  
  199. Finished:
  200.     Close #1
  201. End Sub
  202.  
  203. Sub ConvertContours()
  204.     'Data we're using is part of USGS's "Lake Tahoe Data Clearinghouse" model project
  205.     'Sample file emer_hypso.dlg contains hypsography (contour) data for Emerald Bay 7.5' series
  206.     'This and other DLG files are available for download at: http://tahoe.usgs.gov/tahoe/DLG.html
  207.  
  208.     On Error GoTo ConvertError
  209.     Dim xlapp As New Excel.Application
  210.     Dim xlbook As Excel.Workbook
  211.     
  212.     Set xlbook = xlapp.Workbooks.Add()
  213.     ConvertContourDlgFile "J:\Samples\New\Excel\emer_hypso.dlg", xlbook.ActiveSheet
  214.     xlbook.SaveAs "J:\Samples\New\Excel\contours.xls"
  215.     
  216. Cleanup:
  217.     'Release Excel stuff
  218.     Set xlbook = Nothing
  219.     Set xlapp = Nothing
  220.     Exit Sub
  221.     
  222. ConvertError:
  223.     MsgBox "Error " & Err.Description
  224.     GoTo Cleanup
  225. End Sub
  226.  
  227. Sub ImportContours()
  228.     On Error GoTo ImportError
  229.     
  230.     Dim xlapp As New Excel.Application
  231.     
  232.     'Test to see if Excel was initialized
  233.     If xlapp Is Nothing Then
  234.         MsgBox "Could not create Excel application object"
  235.         Exit Sub
  236.     End If
  237.     
  238.     Dim grs As IMSIGX.Graphics
  239.     Dim gr As IMSIGX.Graphic
  240.     Dim xlbook As Excel.Workbook
  241.     
  242.     'Open a workbook
  243.     Set xlbook = xlapp.Workbooks.Open("J:\Samples\New\Excel\Contours.xls")
  244.     If Not xlbook Is Nothing Then
  245.     
  246.         'Target to add graphics to
  247.         Set grs = ActiveDrawing.Graphics
  248.         
  249.         Dim r As Long
  250.         Dim coord As Long
  251.         Dim id As Long
  252.         Dim ncoords As Long
  253.         Dim lastId As Long
  254.         Dim x As Double
  255.         Dim y As Double
  256.         Dim z As Double
  257.         
  258.         'Format of worksheet
  259.         'In each row:
  260.         'Col 1:  id of contour line
  261.         'Col 2:  starting coordinate index for this row
  262.         'Col 3:  number of x-y coordinate pairs in this row
  263.         'Col 4:  contour elevation (in feet)
  264.         'Col 5, 7, etc.:  x coordinate (UTM, meters)
  265.         'Col 6, 8, etc.:  y coordinate
  266.         
  267.         lastId = -1
  268.         With xlbook.ActiveSheet.Cells
  269.             For r = 1 To 65536
  270.             
  271.                 'If no id, we're done
  272.                 If .Item(r, 1) = "" Then
  273.                     Exit For
  274.                 End If
  275.                 
  276.                 id = .Item(r, 1)
  277.                 Debug.Print "id: " & id
  278.                 
  279.                 'New id: generate new polyline
  280.                 If id <> lastId Then
  281.                     Set gr = grs.Add
  282.                     lastId = id
  283.                 End If
  284.                 
  285.                 'Get number of coordinates and elevation
  286.                 ncoords = .Item(r, 3)
  287.                 z = .Item(r, 4)
  288.                 
  289.                 For coord = 1 To ncoords
  290.                 
  291.                     'Get x-y coordinates and create polyline vertex
  292.                     x = .Item(r, 2 * coord + 3)
  293.                     y = .Item(r, 2 * coord + 4)
  294.                     gr.Vertices.Add x, y, z
  295.                 Next
  296.             Next
  297.         End With
  298.     End If
  299.     
  300. Cleanup:
  301.     'Release TurboCAD stuff
  302.     Set gr = Nothing
  303.     Set grs = Nothing
  304.  
  305.     'Release Excel stuff
  306.     Set xlbook = Nothing
  307.     Set xlapp = Nothing
  308.     Exit Sub
  309.     
  310. ImportError:
  311.     MsgBox "Error " & Err.Description
  312.     GoTo Cleanup
  313. End Sub
  314.