./features/recursively visit files
Download Features Source code Forum Ask for support

Recursively visit (convert) files

The code below recursively finds all files with the "SLDPRT" extension in the directory $workdir. It then converts all the collected files to STEP format and saves them in the $targetdir output directory. It should be emphasized that Analysis Situs is unable to read native CAD formats, therefore this example is only demonstrative. However, using commercial data exchange bridges (such as Datakit), you can override the load-part command to make it work with more file formats.

  set workdir   ...
  set targetdir ...
  set targetExt SLDPRT
  
  set filenames []
  
  # Callback on visiting a certain file.
  proc on_visit {path} {
    global filenames
    lappend filenames $path
  }
  
  # Recursive visiting procedure.
  proc visit {base glob func} {
    foreach f [glob -nocomplain -types f -directory $base $glob] {
      if {[catch {eval $func [list [file join $base $f]]} err]} {
        puts stderr "error: $err"
      }
    }
    foreach d [glob -nocomplain -types d -directory $base *] {
      visit [file join $base $d] $glob $func
    }
  }
  
  # Procedure to find files of a certain type.
  proc find_files {base ext} {
    visit $base *.$ext [list on_visit]
  }
  
  # Find files with a certain extension.
  find_files [lindex $workdir 0] $targetExt
  
  puts $filenames
  
  notifier -init
  
  foreach inFilename $filenames {
    puts "Next file: $inFilename"
    set basename [lindex [file split $inFilename] end]
    set basename [file rootname $basename]
  
    puts "Target output file: $targetdir/$basename.stp"
    load-part $inFilename
    save-step "$targetdir/$basename.stp"
  
    notifier -step
  }
  
  notifier -finish