old_contents: #!/usr/bin/env python
  """
  .. module:: lcad_to_ldraw
     :synopsis: Generates a ldraw format file from a lcad model.
  
  .. moduleauthor:: Hazen Babcock
  """
  
  import os
  import sys
  
  import lcad_language.interpreter as interpreter
  
  if (len(sys.argv) < 2):
      print "usage: <lcad file> <ldraw file (optional)> <time points (optional)>"
      print "       If you want to specify time points you also have to specify the ldraw file."
      exit()
  
  # Parse arguments.
  output_fname = sys.argv[1][:-4] + "dat"
  time_points = 1
  if (len(sys.argv) == 3):
      output_fname = sys.argv[2]
  elif (len(sys.argv) == 4):
      output_fname = sys.argv[2]
      try:
          time_points = int(sys.argv[3])
      except ValueError as e:
          print "Third argument (time points) is not an integer."
          raise
  
  # Read input file.
  with open(sys.argv[1]) as fp:
      ldraw_file_contents = fp.read()
  
  # Generate output files.
  cur_dir = os.getcwd()    
  index = 0
  while (index < time_points):
  
      # Change current working directory to the location of the lcad file (so that imported files will be found correctly).
      if not (os.path.dirname(sys.argv[1]) == ""):
          os.chdir(os.path.dirname(sys.argv[1]))
  
      # Generate parts.
      parts = interpreter.execute(ldraw_file_contents, filename = sys.argv[1], time_index = index).getParts()
  
      if (index == 0):
          print "Model has", len(parts), "parts."
      elif ((index % 10) == 0):
          print " time step", index
  
      # Reset working directory.
      os.chdir(cur_dir)
  
      # Save.
      dat_fname = output_fname
      if (time_points > 1):
          [name, ext] = os.path.splitext(output_fname)
          dat_fname = name + "_" + "{0:05d}".format(index) + ext
  
      if (len(parts) > 0):
          with open(dat_fname, "w") as fp_out:
              fp_out.write("0 // Do not edit, automatically generated by openldraw from " + os.path.basename(sys.argv[1]) + "\n")
              for part in parts:
                  fp_out.write(part.toLDraw() + "\n")
  
      index += 1
  
  print "Done."
  
new_contents: #!/usr/bin/env python
  """
  .. module:: lcad_to_ldraw
     :synopsis: Generates a ldraw format file from a lcad model.
  
  .. moduleauthor:: Hazen Babcock
  """
  
  import os
  import sys
  
  import lcad_language.interpreter as interpreter
  
  if (len(sys.argv) < 2):
      print "usage: <lcad file> <ldraw file (optional)> <time points (optional)>"
      print "       If you want to specify time points you also have to specify the ldraw file."
      exit()
  
  # Parse arguments.
  output_fname = sys.argv[1][:-4] + "dat"
  time_points = 1
  if (len(sys.argv) == 3):
      output_fname = sys.argv[2]
  elif (len(sys.argv) == 4):
      output_fname = sys.argv[2]
      try:
          time_points = int(sys.argv[3])
      except ValueError as e:
          print "Third argument (time points) is not an integer."
          raise
  
  # Read input file.
  with open(sys.argv[1]) as fp:
      ldraw_file_contents = fp.read()
  
  # Generate output files.
  cur_dir = os.getcwd()    
  index = 0
  while (index < time_points):
  
      # Change current working directory to the location of the lcad file (so that imported files will be found correctly).
      if not (os.path.dirname(sys.argv[1]) == ""):
          os.chdir(os.path.dirname(sys.argv[1]))
  
      # Generate sorted parts list.
      parts = interpreter.execute(ldraw_file_contents, filename = sys.argv[1], time_index = index).getSortedParts()
  
      if (index == 0):
          print "Model has", len(parts), "parts."
      elif ((index % 10) == 0):
          print " time step", index
  
      # Reset working directory.
      os.chdir(cur_dir)
  
      # Save.
      dat_fname = output_fname
      if (time_points > 1):
          [name, ext] = os.path.splitext(output_fname)
          dat_fname = name + "_" + "{0:05d}".format(index) + ext
  
      if (len(parts) > 0):
          with open(dat_fname, "w") as fp_out:
              fp_out.write("0 // Do not edit, automatically generated by openldraw from " + os.path.basename(sys.argv[1]) + "\n")
              for i in range(len(parts)):
  
                  # Add part.
                  fp_out.write(parts[i].toLDraw() + "\n")
  
                  # Check if we need to add a step.
                  if (i < (len(parts)-1)):
                      if (parts[i].step != parts[i+1].step):
                          fp_out.write("0 STEP\n")
  
      index += 1
  
  print "Done."
  
subject: Add step meta command to .dat file if needed.