Adaptacion de los scripts de ObtainIOFromProjectTia a la logica de los directorios de trabajo

This commit is contained in:
Miguel 2025-05-02 23:27:36 +02:00
parent 239126bb96
commit b018e82848
9 changed files with 2267 additions and 162 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,9 @@
{
"level1": {
"api_key": "your-api-key-here",
"model": "gpt-3.5-turbo"
},
"level2": {},
"level3": {},
"working_directory": "C:\\Trabajo\\SIDEL\\06 - E5.007363 - Modifica O&U - SAE196 (cip integrato)\\Reporte\\IOExport"
}

View File

@ -0,0 +1,6 @@
{
"path": "C:\\Trabajo\\SIDEL\\06 - E5.007363 - Modifica O&U - SAE196 (cip integrato)\\Reporte\\IOExport",
"history": [
"C:\\Trabajo\\SIDEL\\06 - E5.007363 - Modifica O&U - SAE196 (cip integrato)\\Reporte\\IOExport"
]
}

View File

@ -2,6 +2,7 @@
export_logic_from_tia :
Script para exportar el software de un PLC desde TIA Portal en archivos XML y SCL.
"""
import tkinter as tk
from tkinter import filedialog
import os
@ -17,12 +18,14 @@ from backend.script_utils import load_configuration
# --- Configuration ---
TIA_PORTAL_VERSION = "18.0" # Target TIA Portal version (e.g., "18.0")
EXPORT_OPTIONS = None # Use default export options
KEEP_FOLDER_STRUCTURE = True # Replicate TIA project folder structure in export directory
KEEP_FOLDER_STRUCTURE = (
True # Replicate TIA project folder structure in export directory
)
# --- TIA Scripting Import Handling ---
# Check if the TIA_SCRIPTING environment variable is set
if os.getenv('TIA_SCRIPTING'):
sys.path.append(os.getenv('TIA_SCRIPTING'))
if os.getenv("TIA_SCRIPTING"):
sys.path.append(os.getenv("TIA_SCRIPTING"))
else:
# Optional: Define a fallback path if the environment variable isn't set
# fallback_path = "C:\\path\\to\\your\\TIA_Scripting_binaries"
@ -32,14 +35,23 @@ else:
try:
import siemens_tia_scripting as ts
EXPORT_OPTIONS = ts.Enums.ExportOptions.WithDefaults # Set default options now that 'ts' is imported
EXPORT_OPTIONS = (
ts.Enums.ExportOptions.WithDefaults
) # Set default options now that 'ts' is imported
except ImportError:
print("ERROR: Failed to import 'siemens_tia_scripting'.")
print("Ensure:")
print(f"1. TIA Portal Openness for V{TIA_PORTAL_VERSION} is installed.")
print("2. The 'siemens_tia_scripting' Python module is installed (pip install ...) or")
print(" the path to its binaries is set in the 'TIA_SCRIPTING' environment variable.")
print("3. You are using a compatible Python version (e.g., 3.12.X as per documentation).")
print(
"2. The 'siemens_tia_scripting' Python module is installed (pip install ...) or"
)
print(
" the path to its binaries is set in the 'TIA_SCRIPTING' environment variable."
)
print(
"3. You are using a compatible Python version (e.g., 3.12.X as per documentation)."
)
sys.exit(1)
except Exception as e:
print(f"An unexpected error occurred during import: {e}")
@ -48,13 +60,19 @@ except Exception as e:
# --- Functions ---
def select_project_file():
"""Opens a dialog to select a TIA Portal project file."""
root = tk.Tk()
root.withdraw() # Hide the main tkinter window
file_path = filedialog.askopenfilename(
title="Select TIA Portal Project File",
filetypes=[(f"TIA Portal V{TIA_PORTAL_VERSION} Projects", f"*.ap{TIA_PORTAL_VERSION.split('.')[0]}")] # e.g. *.ap18
filetypes=[
(
f"TIA Portal V{TIA_PORTAL_VERSION} Projects",
f"*.ap{TIA_PORTAL_VERSION.split('.')[0]}",
)
], # e.g. *.ap18
)
root.destroy()
if not file_path:
@ -62,19 +80,19 @@ def select_project_file():
sys.exit(0)
return file_path
def select_export_directory():
"""Opens a dialog to select the export directory."""
root = tk.Tk()
root.withdraw() # Hide the main tkinter window
dir_path = filedialog.askdirectory(
title="Select Export Directory"
)
dir_path = filedialog.askdirectory(title="Select Export Directory")
root.destroy()
if not dir_path:
print("No export directory selected. Exiting.")
sys.exit(0)
return dir_path
def export_plc_data(plc, export_base_dir):
"""Exports Blocks, UDTs, and Tag Tables from a given PLC."""
plc_name = plc.get_name()
@ -106,32 +124,42 @@ def export_plc_data(plc, export_base_dir):
print(f" Compiling block {block_name}...")
block.compile() #
if not block.is_consistent():
print(f" WARNING: Block {block_name} inconsistent after compile. Skipping.")
print(
f" WARNING: Block {block_name} inconsistent after compile. Skipping."
)
blocks_skipped += 1
continue
print(f" Exporting {block_name} as XML...")
block.export(target_directory_path=xml_blocks_path, #
block.export(
target_directory_path=xml_blocks_path, #
export_options=EXPORT_OPTIONS, #
export_format=ts.Enums.ExportFormats.SimaticML, #
keep_folder_structure=KEEP_FOLDER_STRUCTURE) #
keep_folder_structure=KEEP_FOLDER_STRUCTURE,
) #
try:
prog_language = block.get_property(name="ProgrammingLanguage")
if prog_language == "SCL":
print(f" Exporting {block_name} as SCL...")
block.export(target_directory_path=scl_blocks_path,
block.export(
target_directory_path=scl_blocks_path,
export_options=EXPORT_OPTIONS,
export_format=ts.Enums.ExportFormats.ExternalSource, #
keep_folder_structure=KEEP_FOLDER_STRUCTURE)
keep_folder_structure=KEEP_FOLDER_STRUCTURE,
)
except Exception as prop_ex:
print(f" Could not get ProgrammingLanguage for {block_name}. Skipping SCL. Error: {prop_ex}")
print(
f" Could not get ProgrammingLanguage for {block_name}. Skipping SCL. Error: {prop_ex}"
)
blocks_exported += 1
except Exception as block_ex:
print(f" ERROR exporting block {block_name}: {block_ex}")
blocks_skipped += 1
print(f" Program Blocks Export Summary: Exported={blocks_exported}, Skipped/Errors={blocks_skipped}")
print(
f" Program Blocks Export Summary: Exported={blocks_exported}, Skipped/Errors={blocks_skipped}"
)
except Exception as e:
print(f" ERROR processing Program Blocks: {e}")
traceback.print_exc()
@ -155,20 +183,26 @@ def export_plc_data(plc, export_base_dir):
print(f" Compiling UDT {udt_name}...")
udt.compile() #
if not udt.is_consistent():
print(f" WARNING: UDT {udt_name} inconsistent after compile. Skipping.")
print(
f" WARNING: UDT {udt_name} inconsistent after compile. Skipping."
)
udts_skipped += 1
continue
print(f" Exporting {udt_name}...")
udt.export(target_directory_path=udt_export_path, #
udt.export(
target_directory_path=udt_export_path, #
export_options=EXPORT_OPTIONS, #
# export_format defaults to SimaticML for UDTs
keep_folder_structure=KEEP_FOLDER_STRUCTURE) #
keep_folder_structure=KEEP_FOLDER_STRUCTURE,
) #
udts_exported += 1
except Exception as udt_ex:
print(f" ERROR exporting UDT {udt_name}: {udt_ex}")
udts_skipped += 1
print(f" UDT Export Summary: Exported={udts_exported}, Skipped/Errors={udts_skipped}")
print(
f" UDT Export Summary: Exported={udts_exported}, Skipped/Errors={udts_skipped}"
)
except Exception as e:
print(f" ERROR processing UDTs: {e}")
traceback.print_exc()
@ -190,15 +224,19 @@ def export_plc_data(plc, export_base_dir):
try:
# Note: Consistency check might not be available/needed for tag tables like blocks/UDTs
print(f" Exporting {table_name}...")
table.export(target_directory_path=tags_export_path, #
table.export(
target_directory_path=tags_export_path, #
export_options=EXPORT_OPTIONS, #
# export_format defaults to SimaticML for Tag Tables
keep_folder_structure=KEEP_FOLDER_STRUCTURE) #
keep_folder_structure=KEEP_FOLDER_STRUCTURE,
) #
tags_exported += 1
except Exception as table_ex:
print(f" ERROR exporting Tag Table {table_name}: {table_ex}")
tags_skipped += 1
print(f" Tag Table Export Summary: Exported={tags_exported}, Skipped/Errors={tags_skipped}")
print(
f" Tag Table Export Summary: Exported={tags_exported}, Skipped/Errors={tags_skipped}"
)
except Exception as e:
print(f" ERROR processing Tag Tables: {e}")
traceback.print_exc()
@ -208,17 +246,25 @@ def export_plc_data(plc, export_base_dir):
# --- Main Script ---
if __name__ == "__main__":
configs = load_configuration()
working_directory = configs.get("working_directory")
print("--- TIA Portal Data Exporter (Blocks, UDTs, Tags) ---")
# 1. Select Files/Folders
# Validate working directory
if not working_directory or not os.path.isdir(working_directory):
print("ERROR: Working directory not set or invalid in configuration.")
print("Please configure the working directory using the main application.")
sys.exit(1)
# 1. Select Project File, Export Directory comes from config
project_file = select_project_file()
export_dir = select_export_directory()
export_dir = working_directory # Use working directory from config
print(f"\nSelected Project: {project_file}")
print(f"Selected Export Directory: {export_dir}")
print(f"Using Export Directory (Working Directory): {export_dir}")
portal_instance = None
project_object = None
@ -228,7 +274,7 @@ if __name__ == "__main__":
print(f"\nConnecting to TIA Portal V{TIA_PORTAL_VERSION}...")
portal_instance = ts.open_portal(
version=TIA_PORTAL_VERSION,
portal_mode=ts.Enums.PortalMode.WithGraphicalUserInterface
portal_mode=ts.Enums.PortalMode.WithGraphicalUserInterface,
)
print("Connected to TIA Portal.")
print(f"Portal Process ID: {portal_instance.get_process_id()}") #
@ -252,7 +298,9 @@ if __name__ == "__main__":
# 5. Iterate and Export Data for each PLC
for plc_device in plcs:
export_plc_data(plc=plc_device, export_base_dir=export_dir)
export_plc_data(
plc=plc_device, export_base_dir=export_dir
) # Pass export_dir
print("\nExport process completed.")

View File

@ -2,12 +2,14 @@
export_CAx_from_tia :
Script que exporta los datos CAx de un proyecto de TIA Portal y genera un resumen en Markdown.
"""
import tkinter as tk
from tkinter import filedialog
import os
import sys
import traceback
import xml.etree.ElementTree as ET # Library to parse XML (AML)
from pathlib import Path # Import Path
script_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
@ -20,8 +22,8 @@ TIA_PORTAL_VERSION = "18.0" # Target TIA Portal version
# --- TIA Scripting Import Handling ---
# (Same import handling as the previous script)
if os.getenv('TIA_SCRIPTING'):
sys.path.append(os.getenv('TIA_SCRIPTING'))
if os.getenv("TIA_SCRIPTING"):
sys.path.append(os.getenv("TIA_SCRIPTING"))
else:
pass
@ -38,13 +40,19 @@ except Exception as e:
# --- Functions ---
def select_project_file():
"""Opens a dialog to select a TIA Portal project file."""
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="Select TIA Portal Project File",
filetypes=[(f"TIA Portal V{TIA_PORTAL_VERSION} Projects", f"*.ap{TIA_PORTAL_VERSION.split('.')[0]}")]
filetypes=[
(
f"TIA Portal V{TIA_PORTAL_VERSION} Projects",
f"*.ap{TIA_PORTAL_VERSION.split('.')[0]}",
)
],
)
root.destroy()
if not file_path:
@ -52,6 +60,7 @@ def select_project_file():
sys.exit(0)
return file_path
def select_output_directory():
"""Opens a dialog to select the output directory."""
root = tk.Tk()
@ -65,13 +74,16 @@ def select_output_directory():
sys.exit(0)
return dir_path
def find_elements(element, path):
"""Helper to find elements using namespaces commonly found in AML."""
# AutomationML namespaces often vary slightly or might be default
# This basic approach tries common prefixes or no prefix
namespaces = {
'': element.tag.split('}')[0][1:] if '}' in element.tag else '', # Default namespace if present
'caex': 'http://www.dke.de/CAEX', # Common CAEX namespace
"": (
element.tag.split("}")[0][1:] if "}" in element.tag else ""
), # Default namespace if present
"caex": "http://www.dke.de/CAEX", # Common CAEX namespace
# Add other potential namespaces if needed based on file inspection
}
# Try finding with common prefixes or the default namespace
@ -79,9 +91,11 @@ def find_elements(element, path):
# Construct path with namespace URI if prefix is defined
namespaced_path = path
if prefix:
parts = path.split('/')
namespaced_parts = [f"{{{uri}}}{part}" if part != '.' else part for part in parts]
namespaced_path = '/'.join(namespaced_parts)
parts = path.split("/")
namespaced_parts = [
f"{{{uri}}}{part}" if part != "." else part for part in parts
]
namespaced_path = "/".join(namespaced_parts)
# Try findall with the constructed path
found = element.findall(namespaced_path)
@ -93,7 +107,9 @@ def find_elements(element, path):
try:
# Simple attempt without namespace handling if the above fails
return element.findall(path)
except SyntaxError: # Handle potential errors if path isn't valid without namespaces
except (
SyntaxError
): # Handle potential errors if path isn't valid without namespaces
return []
@ -109,7 +125,9 @@ def parse_aml_to_markdown(aml_file_path, md_file_path):
# Find InstanceHierarchy - usually contains the project structure
# Note: Namespace handling in ElementTree can be tricky. Adjust '{...}' part if needed.
# We will use a helper function 'find_elements' to try common patterns
instance_hierarchies = find_elements(root, './/InstanceHierarchy') # Common CAEX tag
instance_hierarchies = find_elements(
root, ".//InstanceHierarchy"
) # Common CAEX tag
if not instance_hierarchies:
markdown_lines.append("Could not find InstanceHierarchy in the AML file.")
@ -121,32 +139,54 @@ def parse_aml_to_markdown(aml_file_path, md_file_path):
markdown_lines.append("")
# Look for InternalElements which represent devices/components
internal_elements = find_elements(ih, './/InternalElement') # Common CAEX tag
internal_elements = find_elements(
ih, ".//InternalElement"
) # Common CAEX tag
if not internal_elements:
markdown_lines.append("No devices (InternalElement) found in InstanceHierarchy.")
markdown_lines.append(
"No devices (InternalElement) found in InstanceHierarchy."
)
print("Info: No InternalElement tags found under InstanceHierarchy.")
else:
markdown_lines.append(f"Found {len(internal_elements)} device(s)/component(s):")
markdown_lines.append(
f"Found {len(internal_elements)} device(s)/component(s):"
)
markdown_lines.append("")
markdown_lines.append("| Name | SystemUnitClass | RefBaseSystemUnitPath | Attributes |")
markdown_lines.append(
"| Name | SystemUnitClass | RefBaseSystemUnitPath | Attributes |"
)
markdown_lines.append("|---|---|---|---|")
for elem in internal_elements:
name = elem.get('Name', 'N/A')
ref_path = elem.get('RefBaseSystemUnitPath', 'N/A') # Path to class definition
name = elem.get("Name", "N/A")
ref_path = elem.get(
"RefBaseSystemUnitPath", "N/A"
) # Path to class definition
# Try to get the class name from the RefBaseSystemUnitPath or SystemUnitClassLib
su_class_path = find_elements(elem, './/SystemUnitClass') # Check direct child first
su_class = su_class_path[0].get('Path', 'N/A') if su_class_path else ref_path.split('/')[-1] # Fallback to last part of path
su_class_path = find_elements(
elem, ".//SystemUnitClass"
) # Check direct child first
su_class = (
su_class_path[0].get("Path", "N/A")
if su_class_path
else ref_path.split("/")[-1]
) # Fallback to last part of path
attributes_md = ""
attributes = find_elements(elem, './/Attribute') # Find attributes
attributes = find_elements(elem, ".//Attribute") # Find attributes
attr_list = []
for attr in attributes:
attr_name = attr.get('Name', '')
attr_value_elem = find_elements(attr, './/Value') # Get Value element
attr_value = attr_value_elem[0].text if attr_value_elem and attr_value_elem[0].text else 'N/A'
attr_name = attr.get("Name", "")
attr_value_elem = find_elements(
attr, ".//Value"
) # Get Value element
attr_value = (
attr_value_elem[0].text
if attr_value_elem and attr_value_elem[0].text
else "N/A"
)
# Look for potential IP addresses (common attribute names)
if "Address" in attr_name or "IP" in attr_name:
@ -156,49 +196,66 @@ def parse_aml_to_markdown(aml_file_path, md_file_path):
attributes_md = "<br>".join(attr_list) if attr_list else "None"
markdown_lines.append(f"| {name} | {su_class} | `{ref_path}` | {attributes_md} |")
markdown_lines.append(
f"| {name} | {su_class} | `{ref_path}` | {attributes_md} |"
)
# Write to Markdown file
with open(md_file_path, 'w', encoding='utf-8') as f:
with open(md_file_path, "w", encoding="utf-8") as f:
f.write("\n".join(markdown_lines))
print(f"Markdown summary written to: {md_file_path}")
except ET.ParseError as xml_err:
print(f"ERROR parsing XML file {aml_file_path}: {xml_err}")
with open(md_file_path, 'w', encoding='utf-8') as f:
f.write(f"# Error\n\nFailed to parse AML file: {os.path.basename(aml_file_path)}\n\nError: {xml_err}")
with open(md_file_path, "w", encoding="utf-8") as f:
f.write(
f"# Error\n\nFailed to parse AML file: {os.path.basename(aml_file_path)}\n\nError: {xml_err}"
)
except Exception as e:
print(f"ERROR processing AML file {aml_file_path}: {e}")
traceback.print_exc()
with open(md_file_path, 'w', encoding='utf-8') as f:
f.write(f"# Error\n\nAn unexpected error occurred while processing AML file: {os.path.basename(aml_file_path)}\n\nError: {e}")
with open(md_file_path, "w", encoding="utf-8") as f:
f.write(
f"# Error\n\nAn unexpected error occurred while processing AML file: {os.path.basename(aml_file_path)}\n\nError: {e}"
)
# --- Main Script ---
if __name__ == "__main__":
configs = load_configuration()
working_directory = configs.get("working_directory")
print("--- TIA Portal Project CAx Exporter and Analyzer ---")
# 1. Select Files/Folders
# Validate working directory
if not working_directory or not os.path.isdir(working_directory):
print("ERROR: Working directory not set or invalid in configuration.")
print("Please configure the working directory using the main application.")
sys.exit(1)
# 1. Select Project File, Output Directory comes from config
project_file = select_project_file()
output_dir = select_output_directory()
output_dir = Path(
working_directory
) # Use working directory from config, ensure it's a Path object
print(f"\nSelected Project: {project_file}")
print(f"Selected Output Directory: {output_dir}")
print(f"Using Output Directory (Working Directory): {output_dir}")
# Define output file names
project_base_name = os.path.splitext(os.path.basename(project_file))[0]
aml_file = os.path.join(output_dir, f"{project_base_name}_CAx_Export.aml")
md_file = os.path.join(output_dir, f"{project_base_name}_CAx_Summary.md")
log_file = os.path.join(output_dir, f"{project_base_name}_CAx_Export.log") # Log file for the export process
# Define output file names using Path object
project_path = Path(project_file)
project_base_name = project_path.stem # Get filename without extension
aml_file = output_dir / f"{project_base_name}_CAx_Export.aml"
md_file = output_dir / f"{project_base_name}_CAx_Summary.md"
log_file = (
output_dir / f"{project_base_name}_CAx_Export.log"
) # Log file for the export process
print(f"Will export CAx data to: {aml_file}")
print(f"Will generate summary to: {md_file}")
print(f"Export log file: {log_file}")
portal_instance = None
project_object = None
cax_export_successful = False
@ -208,14 +265,19 @@ if __name__ == "__main__":
print(f"\nConnecting to TIA Portal V{TIA_PORTAL_VERSION}...")
portal_instance = ts.open_portal(
version=TIA_PORTAL_VERSION,
portal_mode=ts.Enums.PortalMode.WithGraphicalUserInterface
portal_mode=ts.Enums.PortalMode.WithGraphicalUserInterface,
)
print("Connected.")
# 3. Open Project
print(f"Opening project: {os.path.basename(project_file)}...")
project_object = portal_instance.open_project(project_file_path=project_file)
print(
f"Opening project: {project_path.name}..."
) # Use Path object's name attribute
project_object = portal_instance.open_project(
project_file_path=str(project_path)
) # Pass path as string
if project_object is None:
print("Project might already be open, attempting to get handle...")
project_object = portal_instance.get_project()
if project_object is None:
raise Exception("Failed to open or get the specified project.")
@ -223,10 +285,13 @@ if __name__ == "__main__":
# 4. Export CAx Data (Project Level)
print(f"Exporting CAx data for the project to {aml_file}...")
# Ensure output directory exists for the log file as well
os.makedirs(os.path.dirname(log_file), exist_ok=True)
# Ensure output directory exists (Path.mkdir handles this implicitly if needed later, but good practice)
output_dir.mkdir(parents=True, exist_ok=True)
export_result = project_object.export_cax_data(export_file_path=aml_file, log_file_path=log_file) # [cite: 361]
# Pass paths as strings to the TIA function
export_result = project_object.export_cax_data(
export_file_path=str(aml_file), log_file_path=str(log_file)
)
if export_result:
print("CAx data exported successfully.")
@ -235,9 +300,10 @@ if __name__ == "__main__":
print("CAx data export failed. Check the log file for details:")
print(f" Log file: {log_file}")
# Write basic error message to MD file if export fails
with open(md_file, 'w', encoding='utf-8') as f:
f.write(f"# Error\n\nCAx data export failed. Check log file: {log_file}")
with open(md_file, "w", encoding="utf-8") as f:
f.write(
f"# Error\n\nCAx data export failed. Check log file: {log_file}"
)
except ts.TiaException as tia_ex:
print(f"\nTIA Portal Openness Error: {tia_ex}")
@ -259,11 +325,15 @@ if __name__ == "__main__":
# 5. Parse AML and Generate Markdown (only if export was successful)
if cax_export_successful:
if os.path.exists(aml_file):
if aml_file.exists(): # Use Path object's exists() method
parse_aml_to_markdown(aml_file, md_file)
else:
print(f"ERROR: Export was reported successful, but AML file not found at {aml_file}")
with open(md_file, 'w', encoding='utf-8') as f:
f.write(f"# Error\n\nExport was reported successful, but AML file not found:\n{aml_file}")
print(
f"ERROR: Export was reported successful, but AML file not found at {aml_file}"
)
with open(md_file, "w", encoding="utf-8") as f:
f.write(
f"# Error\n\nExport was reported successful, but AML file not found:\n{aml_file}"
)
print("\nScript finished.")

View File

@ -3,6 +3,7 @@ export_io_from_CAx :
Script que sirve para exraer los IOs de un proyecto de TIA Portal y
generar un archivo Markdown con la información.
"""
import os
import sys
from tkinter import filedialog
@ -19,6 +20,7 @@ script_root = os.path.dirname(
sys.path.append(script_root)
from backend.script_utils import load_configuration
# --- extract_aml_data function (Unchanged from v15) ---
def extract_aml_data(root):
"""(v15 logic - Unchanged) Extracts data, correcting PLC network association lookup."""
@ -955,13 +957,15 @@ def process_aml_file(
print(f"ERROR processing AML file {aml_file_path}: {e}")
traceback.print_exc()
def select_cax_file():
"""Opens a dialog to select a CAx (XML) export file."""
def select_cax_file(initial_dir=None): # Add initial_dir parameter
"""Opens a dialog to select a CAx (XML) export file, starting in the specified directory."""
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="Select CAx Export File (XML)",
filetypes=[("XML Files", "*.xml"), ("All Files", "*.*")] # Changed filetypes
filetypes=[("XML Files", "*.xml"), ("AML Files", "*.aml"), ("All Files", "*.*")], # Added AML
initialdir=initial_dir # Set the initial directory
)
root.destroy()
if not file_path:
@ -969,6 +973,7 @@ def select_cax_file():
sys.exit(0)
return file_path
def select_output_directory():
"""Opens a dialog to select the output directory."""
root = tk.Tk()
@ -982,35 +987,53 @@ def select_output_directory():
sys.exit(0)
return dir_path
# --- Main Execution ---
if __name__ == "__main__":
# configs = load_configuration() # Keep if needed, otherwise remove
configs = load_configuration()
working_directory = configs.get("working_directory")
script_version = "v27 - User Input/Output Paths" # Updated version
script_version = "v28 - Working Directory Integration" # Updated version
print(
f"--- AML (CAx Export) to Hierarchical JSON and Obsidian MD Converter ({script_version}) ---"
)
# 1. Select Input CAx File and Output Directory
cax_file_path = select_cax_file()
output_dir = select_output_directory()
# Validate working directory
if not working_directory or not os.path.isdir(working_directory):
print("ERROR: Working directory not set or invalid in configuration.")
print("Attempting to use script's directory as fallback.")
# Fallback to script's directory or current directory if needed
working_directory = os.path.dirname(os.path.abspath(__file__))
if not os.path.isdir(working_directory):
working_directory = os.getcwd()
print(f"Using fallback directory: {working_directory}")
# Optionally, prompt user to select a working directory here if critical
# output_dir = select_output_directory() # Keep this if you want user selection on failure
# Use working_directory as the output directory
output_dir = working_directory
print(f"Using Working Directory for Output: {output_dir}")
# 1. Select Input CAx File, starting in the working directory
# Pass working_directory to the selection function
cax_file_path = select_cax_file(initial_dir=working_directory)
# Convert paths to Path objects
input_path = Path(cax_file_path)
output_path = Path(output_dir)
output_path = Path(output_dir) # Output path is the working directory
# Check if input file exists
if not input_path.is_file():
print(f"ERROR: Input file '{input_path}' not found or is not a file.")
sys.exit(1)
# Ensure output directory exists
# Ensure output directory exists (redundant if working_directory is valid, but safe)
output_path.mkdir(parents=True, exist_ok=True)
# Construct output file paths within the selected output directory
# Construct output file paths within the selected output directory (working_directory)
output_json_file = output_path / input_path.with_suffix(".hierarchical.json").name
output_md_file = output_path / input_path.with_name(f"{input_path.stem}_Hardware_Tree.md").name
output_md_upward_file = output_path / input_path.with_name(f"{input_path.stem}_IO_Upward_Debug.md").name
output_md_file = output_path / input_path.with_name(f"{input_path.stem}_Hardware_Tree.md") # Simplified name
output_md_upward_file = output_path / input_path.with_name(f"{input_path.stem}_IO_Upward_Debug.md") # Simplified name
print(f"Input AML: {input_path.resolve()}")
print(f"Output Directory: {output_path.resolve()}")

File diff suppressed because it is too large Load Diff

15
requirements.txt Normal file
View File

@ -0,0 +1,15 @@
flask
flask-sock
lxml
pandas
google-cloud-translate
openai
ollama
langid
openpyxl
beautifulsoup4
requests
mammoth
html2text
pypandoc
# siemens-tia-scripting # Requiere instalación especial de TIA Portal Openness