VB CAD Tips

www.vbcad.com

Convert a 2-D array of points to a 3-D array of points
 
AutoCAD stores point arrays in two different formats.  2-D and 3-D arrays are grouped with (X, Y) and (X, Y, Z) values respectively.  Getting an array in a 2-D format to a 3-D array is a simple process.   We do this by creating a new variable array, provide the X, and Y values from the old array, and plug in the new Z value before entering the next X, Y pair.  The code below shows how this is done.  At the end of the procedure, the variable 'NewPts' contains a 3-D array of points.

Sub TwoD_to_ThreeD()
   '********************************
   '* Variable Declarations *
   '********************************

   Dim Pts(0 To 7) As Double 'Array for LWPolyline
   Dim NewPts() As Double 'A dynamic array
   Dim NewZ As Double 'Z value for new points
   Dim TopArray As Long 'New Upper-bound for dynamic array
   Dim OldIndex As Long
   Dim I As Long 'For Next Loop
   TopArray = UBound(Pts) + (UBound(Pts) + 1) / 2
   ReDim NewPts(0 To TopArray)
   '********************************
   '* Set values for 2-D Points. *
   '* These values could be pulled *
   '* from a LWPolyline's coords *
   '********************************

   Pts(0) = 0: Pts(1) = 0
   Pts(2) = 100: Pts(3) = 0
   Pts(4) = 100: Pts(5) = 100
   Pts(6) = 0: Pts(7) = 100
   '********************************
   '* Set values for 3-D Points. *
   '********************************

   NewZ = 50 'Z Value for 3-D Points
   OldIndex = 0 'Counter for Old Point Array
   For I = LBound(NewPts) To UBound(NewPts) Step 3
      NewPts(I) = Pts(OldIndex)
      NewPts(I + 1) = Pts(OldIndex + 1)
      NewPts(I + 2) = NewZ
      OldIndex = OldIndex + 2
   Next I
   'Now the variable "NewPts" contains the 3-D array
End Sub



Back to Tips ©2000 by VB CAD  All rights reserved.         www.vbcad.com