Skip to content

Instantly share code, notes, and snippets.

@pshriwise
Created November 18, 2017 17:51
Show Gist options
  • Save pshriwise/e2abbbc6269e8595e3f6e47c58a49f5c to your computer and use it in GitHub Desktop.
Save pshriwise/e2abbbc6269e8595e3f6e47c58a49f5c to your computer and use it in GitHub Desktop.
Script for separating a STEP CAD file into different volumes using Cubit/Trelis
import cubit
import argparse
# dictionary containing the appropriate
# (export_type, file_extension)
# for each allowed export type in the script
EXPORT_TYPES = {
"stl" : ("stl", ".stl"),
"sat" : ("acis", ".sat"),
"acis" : ("acis", ".sat")
}
def parse_filename(input_file):
elements = input_file.split("/")
# if this is not a relative/absolute path
# then assume current directory
if elements[0] == input_file:
directory = "./"
else:
directory = "/".join(elements[:-1]) + "/"
basename = elements[-1][:-5]
return directory, basename
def main(input_file, export_type, output_dir, dry_run = False):
# sort out where we're goint to put
# the volume files
out_dir, basename = parse_filename(input_file)
if (len(output_dir)):
out_dir = output_dir
if out_dir[-1] != "/":
out_dir += "/"
# start up cubit
cubit.init("-nojournal")
# import the file
import_cmd = "import step" + "'" + input_file + "'"
cubit.silent_cmd(import_cmd)
# get all of the volumes in the model
volumes = cubit.get_entities("Volume")
print "Found " + str(len(volumes)) + " volumes in the CAD model."
# retrieve the export value and file extension
try:
export_key, file_ext = EXPORT_TYPES[export_type]
except:
raise NotImplementedError("Unsupported export type was specified")
# now export each volume
for vol in volumes:
# construct file name using dir, basename, volume index, and file extension
filename = "'" + out_dir + basename + "_" + "volume_" + str(vol) + file_ext + "'"
# insert export key and filename into command
export_cmd = "export " + export_key + " " + filename + "volume " + str(vol)
# export the file
if dry_run:
print filename
else:
cubit.silent_cmd(export_cmd)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = "Simple script to separate a STEP file into its individual volumes using Cubit or Trelis "
"Please ensure the Trlis 'bin' directory has been added to your LD_LIBRARY_PATH and PYTHONPATH" )
parser.add_argument("step_file", type = str, help = "Name of the step file you'd like produce volume files for")
parser.add_argument("-e, --export-type", dest = "export_type", type = str, default = "stl", help = "Export type for the output volume files")
parser.add_argument("-o", dest = "output_dir", type = str, default = "", help = "Optionally provided location of output files")
parser.add_argument("-n", dest = "dry_run", action = 'store_true', default = False, help = "Option for printing potential output filenames wit\
hout executing anything.")
args = parser.parse_args()
main(args.step_file, args.export_type, args.output_dir, args.dry_run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment