Created
January 1, 2026 17:26
-
-
Save stephensmitchell/1ec24ec24bb3adb1963d27d72f204557 to your computer and use it in GitHub Desktop.
STEP to IFC Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| STEP to IFC Converter | |
| Uses FreeCAD to convert STEP files to IFC format. | |
| Usage: | |
| step_to_ifc.py <input_step_file> [output_ifc_file] | |
| If output_ifc_file is not specified, the IFC file will be created | |
| in the same directory as the input STEP file with the same name. | |
| """ | |
| import sys | |
| import os | |
| import argparse | |
| # FreeCAD imports | |
| import FreeCAD as App | |
| import Part | |
| import importIFC | |
| def print_progress(message, percent=None): | |
| """Print progress message to stdout for the calling application to parse.""" | |
| if percent is not None: | |
| print(f"PROGRESS:{percent}:{message}", flush=True) | |
| else: | |
| print(f"STATUS:{message}", flush=True) | |
| def convert_step_to_ifc(input_path, output_path=None): | |
| """ | |
| Convert a STEP file to IFC format. | |
| Args: | |
| input_path: Path to the input STEP file | |
| output_path: Path for the output IFC file (optional) | |
| Returns: | |
| str: Path to the created IFC file | |
| """ | |
| # Validate input file | |
| if not os.path.exists(input_path): | |
| raise FileNotFoundError(f"Input file not found: {input_path}") | |
| # Determine output path | |
| if output_path is None: | |
| base_name = os.path.splitext(input_path)[0] | |
| output_path = base_name + ".ifc" | |
| print_progress("Initializing FreeCAD...", 0) | |
| # Create a new document | |
| doc = App.newDocument("StepToIfc") | |
| try: | |
| print_progress("Loading STEP file...", 10) | |
| # Import the STEP file | |
| Part.insert(input_path, doc.Name) | |
| print_progress("Processing geometry...", 30) | |
| # Recompute the document | |
| doc.recompute() | |
| # Check if any objects were imported | |
| if len(doc.Objects) == 0: | |
| raise ValueError("No geometry found in STEP file") | |
| print_progress(f"Found {len(doc.Objects)} object(s)", 50) | |
| print_progress("Exporting to IFC format...", 60) | |
| # Export to IFC | |
| # Get all objects to export | |
| objects_to_export = doc.Objects | |
| # Use importIFC to export | |
| importIFC.export(objects_to_export, output_path) | |
| print_progress("Verifying output...", 90) | |
| # Verify the output file was created | |
| if not os.path.exists(output_path): | |
| raise RuntimeError("IFC export failed - output file not created") | |
| print_progress(f"Conversion complete: {output_path}", 100) | |
| return output_path | |
| finally: | |
| # Clean up - close the document | |
| App.closeDocument(doc.Name) | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Convert STEP files to IFC format using FreeCAD", | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=""" | |
| Examples: | |
| step_to_ifc.py model.step | |
| step_to_ifc.py model.step output.ifc | |
| step_to_ifc.py "C:\\Models\\part.step" "C:\\Output\\part.ifc" | |
| """ | |
| ) | |
| parser.add_argument( | |
| "input", | |
| help="Path to the input STEP file" | |
| ) | |
| parser.add_argument( | |
| "output", | |
| nargs="?", | |
| default=None, | |
| help="Path for the output IFC file (optional, defaults to same location as input)" | |
| ) | |
| args = parser.parse_args() | |
| try: | |
| result_path = convert_step_to_ifc(args.input, args.output) | |
| print(f"SUCCESS:{result_path}") | |
| return 0 | |
| except FileNotFoundError as e: | |
| print(f"ERROR:File not found: {e}", file=sys.stderr) | |
| return 1 | |
| except ValueError as e: | |
| print(f"ERROR:Invalid input: {e}", file=sys.stderr) | |
| return 2 | |
| except Exception as e: | |
| print(f"ERROR:Conversion failed: {e}", file=sys.stderr) | |
| return 3 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment