It is possible to enable Refine Shape and Make Solid with an option from the command line.
Code: Select all
#!/usr/bin/env python3
FREECAD_PATH = '/usr/lib/freecad-python3/lib'
#FREECAD_PATH = '/opt/freecad/lib'
import site
site.addsitedir(FREECAD_PATH)
import os
import sys
import argparse
import FreeCAD
import Mesh
import Part
PROG_NAME = os.path.basename(os.path.splitext(sys.argv[0])[0])
PROG_VERSION = '1.0'
DEFAULT_TOLERANCE = 0.05
def main():
parser = argparse.ArgumentParser(
description=PROG_NAME + ' V' + PROG_VERSION)
parser.add_argument(
'-V',
'--version',
action='version',
version='%(prog)s V' + PROG_VERSION,
help='display the version number')
parser.add_argument(
'-r',
'--refine-shape',
action='store_true',
default=False,
help='enable refine shape')
parser.add_argument(
'-s',
'--make-solid',
action='store_true',
default=False,
help='enable make solid')
parser.add_argument(
'-t',
'--tolerance',
type=float,
action='store',
default=DEFAULT_TOLERANCE,
metavar='VALUE',
help='the tolerance for sewing (default VALUE={})'
.format(DEFAULT_TOLERANCE))
parser.add_argument(
'mesh_file',
action='store',
metavar='mesh.ext',
default=None,
help='mesh input file')
parser.add_argument(
'shape_file',
action='store',
metavar='shape.ext',
default=None,
help='shape output file')
args = parser.parse_args()
refine_shape = args.refine_shape
make_solid = args.make_solid
tolerance = args.tolerance
mesh_file = args.mesh_file
shape_file = args.shape_file
(_, ext) = os.path.splitext(shape_file)
if not ext:
raise RuntimeError(
'filename\'s extension is missing, filename: "{}"'.format(
shape_file))
ext = ext.lower()
# import
mesh = Mesh.Mesh()
mesh.read(mesh_file)
# convert mesh to shape
shape = Part.Shape()
shape.makeShapeFromMesh(mesh.Topology, tolerance)
# Refine shape
if refine_shape:
shape = shape.removeSplitter()
# Make solid
if make_solid:
shape = Part.makeSolid(shape)
# exports to an STEP file
if ext in ['.step', '.stp']:
shape.exportStep(shape_file)
# export to an IGES file
elif ext in ['.iges', '.igs']:
shape.exportIges(shape_file)
# export to an STL file
elif ext == '.stl':
shape.exportStl(shape_file)
# export to an BREP file
elif ext in ['.brep', '.brp']:
shape.exportBrep(shape_file)
# unknown file extension
else:
raise RuntimeError('Unknown file extension: "{}"'.format(ext))
if __name__ == '__main__':
main()