#!/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)>"
    exit()

# Generate parts.
with open(sys.argv[1]) as fp:

    # Change current working directory to the location of the lcad file.
    cur_dir = os.getcwd()    
    os.chdir(os.path.dirname(sys.argv[1]))
    parts = interpreter.execute(fp.read()).getParts()
    os.chdir(cur_dir)

print "Model has", len(parts), "parts."

# Save.
if (len(sys.argv) == 3):
    ldraw_fname = sys.argv[2]
else:
    ldraw_fname = sys.argv[1][:-4] + "dat"

with open(ldraw_fname, "w") as fp:
    fp.write("0 // Do not edit, automatically generated by openlcad from " + os.path.basename(sys.argv[1]) + "\n")
    for part in parts:
        fp.write(part.toLDraw() + "\n")

print "Done."

#!/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)>"
    exit()

# Generate parts.
with open(sys.argv[1]) as fp:

    # Change current working directory to the location of the lcad file.
    cur_dir = os.getcwd()    
    if not (os.path.dirname(sys.argv[1]) == ""):
        os.chdir(os.path.dirname(sys.argv[1]))
    parts = interpreter.execute(fp.read()).getParts()
    os.chdir(cur_dir)

print "Model has", len(parts), "parts."

# Save.
if (len(sys.argv) == 3):
    ldraw_fname = sys.argv[2]
else:
    ldraw_fname = sys.argv[1][:-4] + "dat"

with open(ldraw_fname, "w") as fp:
    fp.write("0 // Do not edit, automatically generated by openlcad from " + os.path.basename(sys.argv[1]) + "\n")
    for part in parts:
        fp.write(part.toLDraw() + "\n")

print "Done."

Fix to work correctly if we are already in the directory of the lcad file.
Fix to work correctly if we are already in the directory of the lcad file.
