Page MenuHomec4science

layer_part.py
No OneTemporary

File Metadata

Created
Tue, Jul 16, 19:11

layer_part.py

import numpy as np
import argparse
import sys
import os
from meshing import *
def parse_args():
"""
Function to handle arguments
"""
parser = argparse.ArgumentParser(description='Slice a Geometry and Mesh')
parser.add_argument("freecad_dir", type=str, help='location of FreeCAD library directory')
parser.add_argument("geom_file", type=str, help='filename of the geometry data')
parser.add_argument("layer_thickness", type=float, help='layer thickness')
parser.add_argument("geom_dir", type=str, help='name of the directory to save layered geometry files')
parser.add_argument("mesh_dir", type=str, help='name of the directory to save layered mesh files')
args = parser.parse_args()
return args
def main():
#get the arguments
args = parse_args()
#add FreeCAD directory to path and import FreeCAD modules
sys.path.append(args.freecad_dir)
import FreeCAD
import Part
from FreeCAD import Base
#create directory to save layered files
try:
os.mkdir(args.geom_dir)
except FileExistsError:
pass
#read step file
part = Part.Shape()
part.read(args.geom_file)
#get geometric data of the part
del_x = part.BoundBox.XMax - part.BoundBox.XMin
del_y = part.BoundBox.YMax - part.BoundBox.YMin
z_min = part.BoundBox.ZMin
z_max = part.BoundBox.ZMax
center = Base.Vector(part.BoundBox.XMin, part.BoundBox.YMin, part.BoundBox.ZMin)
extension = '.step'
#initialize first box to intersect the part
z = z_min + args.layer_thickness
count = 1
#loop until the part is intersected completely, save files in geom_dir, call meshing script for the file
while z < z_max:
#make box with same size in x and y directions, incrementing length in z direction
box = Part.makeBox(del_x, del_y, z-z_min, center)
#get the intersection
common = box.common(part)
#save the intersection
name = '/layer_{}'.format(str(count).zfill(3))
common.exportStep(args.geom_dir + name + extension)
#increment loop variables
z += args.layer_thickness
count += 1
#call meshing script on the saved step file
meshing(args.geom_dir, name, extension, args.mesh_dir)
if __name__=='__main__':
main()

Event Timeline