04/30/10

Code Page 01

 

If you code VB, .NET and VBA to AutoCAD, and especially AutoCAD to Excel...

you're going to enjoy this page...it's being developed and will be uploaded shortly.

 
Home
Code Page 01
Code Page 02
Code Page 03
Program Updates
Introducing AutoLISP

 

 



 
  • CAD Clinic: AutoCAD Commands in VB .NET

     

    Use .NET Editors to Create Simple AutoCAD commands  (18DEC2008)

     

  • Exception Handling in VB.NET

    TRY • Catch • Finally • End Try  (17DEC2008)

     

    • Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.

      VB.NET provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process.

 

  • Sorting an array (a directory list)

     

    • Dim comparer1 As New CompareNames

      Dim Folder As New IO.DirectoryInfo(strDwgPrefix & "Current PQM Data")

       

      files = Folder.GetFiles("*.DXF", SearchOption.AllDirectories)

      Array.Sort(files, comparer1)

       

      If files.Length > 0 Then 'if there is even 1 DXF then go do this...

      Dim DXFMsgStr As String = "The following files are included in the DXF file list;" & vbCr & vbCr

      FileCounter = 0

       

      For Each FileyWiley In files

      FileCounter = FileCounter + 1

      DXFMsgStr = DXFMsgStr & FileyWiley.FullName & vbCr

      Next FileyWiley

       

      MsgBox(DXFMsgStr & vbCr & vbCr & _

      "This list contains " & FileCounter & " separate DXF files.")

      Next

       

      Public Class CompareNames

      Implements IComparer

      Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare

      Dim file1 As FileInfo = CType(x, FileInfo)

      Dim file2 As FileInfo = CType(y, FileInfo)

      Return file1.FullName.CompareTo(file2.FullName)

      End Function

       

      End Class

 

 

  • List Files in a Directory in VB.NET

     

    • fileArray = Directory.GetFiles(yourPath)                     'you can get an array of files (their path)
      'this example shows how to loop through a directory and show all of the GIF images in  it.
      Dim pics as ArrayList = new ArrayList()                     'an ArrayList to Bind to a control
      Dim myFile as String, code as String                         'file and html code strings
      Const YOUR_DIRECTORY as String = "/images/"      'constant for your image directory


      'add the image to your ArrayList
    • For Each myFile in Directory.GetFiles(Server.MapPath(YOUR_DIRECTORY), "*.gif")
          image =  YOUR_DIRECTORY & Path.GetFileName(myFile)
          code = "<img src=""" & image & """><br>"
          pics.Add(code)                                                   
      Next

     

    • 'Bind the pics to a DataList
      dlImages.DataSource = pics
      dlImages.DataBind

     

  • List all files in a folder with VB.NET

     

    In .NET you can use the DirectoryInfo class of the System.IO namespace to get a list of files in a particular folder. DirectoryInfo has a GetFiles method that returns a file list, as FileInfo structures, from the specified directory. Optionally, GetFiles takes a pattern as a parameter that can limit the list of files returned.

    • Imports System.IO
         
      Dim strFileSize As String = ""
      Dim di As New IO.DirectoryInfo("C:\temp")
      Dim aryFi As IO.FileInfo() = di.GetFiles("*.txt")
      Dim fi As IO.FileInfo

     

    • For Each fi In aryFi
         strFileSize = (Math.Round(fi.Length / 1024)).ToString()
         Console.WriteLine("File Name: {0}", fi.Name)
         Console.WriteLine("File Full Name: {0}", fi.FullName)
         Console.WriteLine("File Size (KB): {0}", strFileSize )
         Console.WriteLine("File Extension: {0}", fi.Extension)
         Console.WriteLine("Last Accessed: {0}", fi.LastAccessTime)
         Console.WriteLine("Read Only: {0}", (fi.Attributes.ReadOnly = True).ToString)
      Next

     

  • Verifying if a File Exists...Creating an Intro File if it Does Not Exist

     

    • strDwgPrefix = ThisDrawing.GetVariable("DWGPREFIX")
      strDwgName = ThisDrawing.GetVariable("DWGNAME")
      strTempPreFix = ThisDrawing.GetVariable("TEMPPREFIX")

      x = Dir(strTempPreFix & "PellaVisionDocNoData.dat")

      If x <> "" Then
      FileExists = True
      Open strTempPreFix & "PellaVisionDocNoData.dat" For Input As #1
      Input #1, strDocNoLong
      Close #1
      ReadPellaVisionDataFile
      Else:
      FileExists = False
      Open strTempPreFix & "PellaVisionDocNoData.dat" For Output As #1
      Write #1, "No DocNo Exists"
      Close #1
      Open strTempPreFix & "PellaVisionData.dat" For Output As #1
      For x2 = 1 To 200
      Write #1, "Line #" & x2 & ", no PellaVisionData.dat file"
      Next x2
      Close #1
      End If

     

  • VBA - verifying a Listbox Entity is Highlighted, and then setting a program variable equal to the highlighted value 01NOV2008
    • For i = 0 To lbModel.ListCount - 1
          If lbModel.Selected(i) = True Then strModel = lbModel.List(i)
      Next i

     

  • VB.NET - verifying a Listbox Entity is Highlighted, and then setting a program variable equal to the highlighted value 06JAN2009
    • Dim Item As String

      For Each Item In lbDXFFileList.SelectedItems

           DXFfileNames.Add(Item)

      Next

       

  ...and about a zillion more. Email me if you have an immediate need.

 

Home | Code Page 01 | Code Page 02 | Code Page 03 | Program Updates | Introducing AutoLISP

 

© MMIX PellaCAD, Inc. All rights reserved. all MSNBC Web Components used with permission.

Site Established: 01/01/97 by AMOS Systems Development Company Last Updated: 12/06/09