109 lines
4.1 KiB
Python
109 lines
4.1 KiB
Python
import os
|
|
import sys
|
|
import argparse
|
|
import win32com.client
|
|
import pythoncom
|
|
|
|
def export_assemblies_to_stl(input_dir, output_dir, filter_name=None, minimized=False):
|
|
pythoncom.CoInitialize()
|
|
|
|
try:
|
|
# Solid Edge starten oder verbinden
|
|
se_app = win32com.client.Dispatch("SolidEdge.Application")
|
|
se_app.Visible = True
|
|
se_app.WindowState = 2 if minimized else 1
|
|
|
|
documents = se_app.Documents
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
for filename in os.listdir(input_dir):
|
|
if filename.lower().endswith(".asm"):
|
|
if filter_name and filter_name.lower() not in filename.lower():
|
|
continue
|
|
asm_path = os.path.join(input_dir, filename)
|
|
stl_filename = os.path.splitext(filename)[0] + ".stl"
|
|
stl_path = os.path.join(output_dir, stl_filename)
|
|
|
|
print(f"Processing: {asm_path}")
|
|
|
|
try:
|
|
# Baugruppe öffnen
|
|
doc = documents.Open(asm_path)
|
|
# STL exportieren in den Unterordner
|
|
doc.SaveAs(stl_path)
|
|
print(f"-> Exported to: {stl_path}")
|
|
except Exception as e:
|
|
print(f"Error processing {filename}: {e}")
|
|
finally:
|
|
# Datei schließen
|
|
doc.Close(False)
|
|
|
|
# --- Aufräumen: .cfg und .log im Hauptordner ---
|
|
print("")
|
|
for file in os.listdir(input_dir):
|
|
if file.lower().endswith((".cfg", ".log")):
|
|
file_path = os.path.join(input_dir, file)
|
|
try:
|
|
os.remove(file_path)
|
|
print(f"Deleted (input): {file}")
|
|
except Exception as e:
|
|
print(f"Error deleting {file}: {e}")
|
|
|
|
# --- Aufräumen: .log im STL-Unterordner ---
|
|
for file in os.listdir(output_dir):
|
|
if file.lower().endswith(".log"):
|
|
file_path = os.path.join(output_dir, file)
|
|
try:
|
|
os.remove(file_path)
|
|
print(f"Deleted (output): {file}")
|
|
except Exception as e:
|
|
print(f"Error deleting {file}: {e}")
|
|
|
|
print("\nExport and cleanup completed.")
|
|
finally:
|
|
try:
|
|
if 'doc' in locals():
|
|
doc.Close(False)
|
|
except:
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description="Exportiert alle .asm-Dateien in einem Ordner nach STL.")
|
|
parser.add_argument("--in", dest="input_dir", required=False, help="Ordner mit .asm-Dateien (Standard: WORK oder ../work)")
|
|
parser.add_argument("--out", dest="output_dir", required=False, help="Zielordner für STL-Dateien (Standard: work/STL_Export)")
|
|
parser.add_argument("--filter", dest="filter_name", required=False, help="Nur Dateien verarbeiten, deren Name diesen String enthält")
|
|
parser.add_argument("--se-minimized", dest="minimized", action="store_true", default=False, help="Solid Edge minimiert starten")
|
|
|
|
if len(sys.argv) == 2 and sys.argv[1] in ("-h", "--help"):
|
|
parser.print_help()
|
|
sys.exit(0)
|
|
|
|
args = parser.parse_args()
|
|
|
|
work_dir = os.environ.get("PROJECT_WORK", "../work")
|
|
|
|
if args.input_dir:
|
|
input_dir = args.input_dir
|
|
print("Using provided input directory.\n")
|
|
else:
|
|
input_dir = os.environ.get("PROJECT_WORK", "../work")
|
|
print("No input directory provided. Using 'work' directory as input.\n")
|
|
|
|
if args.output_dir:
|
|
output_dir = args.output_dir
|
|
print(f"Saving exported STL files to provided output directory.\n")
|
|
else:
|
|
output_dir = os.path.join(work_dir, "STL_Export")
|
|
print(f"No output directory provided. Exporting STL files to 'work'.\n")
|
|
|
|
|
|
if not os.path.exists(input_dir):
|
|
print(f"Input folder not found: {input_dir}")
|
|
sys.exit(1)
|
|
|
|
if args.filter_name:
|
|
print(f"Filtering files containing: '{args.filter_name}'\n")
|
|
|
|
export_assemblies_to_stl(input_dir, output_dir, args.filter_name, args.minimized)
|