Convert mesh to shape

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
xyz3D
Posts: 4
Joined: Sat Aug 31, 2019 2:39 pm

Convert mesh to shape

Post by xyz3D »

This simple script convert a mesh from a command line into a shape. The output formats are those of FreeCAD STEP, IGES, STL and BREP.

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()
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Convert mesh to shape

Post by wmayer »

The Shape class has the method removeSplitter() which does the refinement.
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: Convert mesh to shape

Post by chrisb »

Thanks for sharing. Usually we get every now and then an invalid mesh, but now I have none at hand. So I don't know what happens with such a mesh. Is it possible to apply with another commandline switch a CheckGeometry on the result?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Convert mesh to shape

Post by openBrain »

wmayer wrote: Wed Nov 13, 2019 6:14 pm The Shape class has the method removeSplitter() which does the refinement.
I think you believe that was a question but that actually was a statement from OP. It is already implemented. ;)
Post Reply