VBScript- (TIP) add new textfile export functionality to PolyWorks

Started by prehistory, April 07, 2009, 11:10:35 AM

Previous topic - Next topic

prehistory

Hi,

I periodically run into problems with PolyWorks, where I am trying to write a text file that is not supported.  For instance, I might want to write a text file from IMInspect, that is the typical File > Export Objects > Selected Data Points & Errors to Text File.  But my only options are Data + Error Vector, or Reference + Error Vector, and I want Data + Reference + Error Vector. 

Or I have a set of scan data that has position, normals, and intensity, and want to export it as a text file, but my only options are Points Only, Points + Vectors, Points + Intensity, etc. 

You CAN write out several files and then use the PW macro language to read each line from each file, then write out a new file line by line.  However, read/write is notoriously slow in the macro language.  This is something Visual Basic can do a million times faster!

I use the SYSTEM command to fire off my VB scripts from within the macro if I want to.  I can also actually write the VB command inside the macro, and make the macro write the file, then fire it off, if I want things to be self-contained.

Anyway, here is a sample VB Script I used to take two point cloud exports (Points + Intensities, Points + Vectors) and combine them into the single one I wanted.  Since I had 4.5 million points, I timed a small sample and calculated that PW would take 37 hours to do this with a macro.  With VB, it took about 4 minutes.

' this script will take two text files, and add the specified fields from one to the end of the line on the other, making a text file with additional fields.
' in this particular application, I couldn't export my point cloud in the format x y z i j k intensity, even though I can IMPORT that format.  I could only export
' in the format x y z i j k, or x y z intensity.  So this macro takes the intensity field from the second file and writes it to the end of the line after the previous fields.
' in this case, with about 4.5 million points, it would have taken PW 37 hours to edit the file using the macro language.
' this macro took only 4 minutes

Const ForReading = 1
Const ForAppending = 2

' specify the files you want to combine here.  In this example, I'm combining a point cloud with vector info with a point cloud with intensity info, and making an output file
VectorFile = "D:\Data\SPAR2009\points_vectors.txt"
IntensityFile = "D:\Data\SPAR2009\points_intensities.txt"
CombineFile = "D:\Data\SPAR2009\points_output.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")

set objInFile1 = objFSO.OpenTextFile(VectorFile)
set objInFile2 = objFSO.OpenTextFile(IntensityFile)

Set objOutFile = objFSO.OpenTextFile(CombineFile, ForAppending, True)

' while we have not reached the end of the file, we read in line number N from the VectorFile (objInFile1) and save it as arrStr
' then we read in line number N from IntensityFile (objInFile2), split the line at [space] using the the split command as arrStr2
' and locate the 4th field in the line (arrStr2(3)), which we save as arrint
' finally, we write out the resulting combined line (arrNewStr) to the output file

Do while NOT objInFile1.AtEndOfStream
' get the x y z i j k entire line from the first file
  arrStr = objInFile1.ReadLine
 
  ' get intensity from other file
    arrStr2 = split(objInFile2.ReadLine," ")
arrint = CInt(arrStr2(3))

  arrNewStr = CStr(arrStr & " " & arrint)
  objOutFile.WriteLine arrNewStr
Loop

EFV

This is an excellent idea.  Silly me, I never even considered doing extensive read/writes outside of polyworks.

I'm going to have to use this.