RD_CONF für SVG zu bearbeiten
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Python Debugger: Current File",
|
||||
"type": "debugpy",
|
||||
"request": "launch",
|
||||
"program": "${file}",
|
||||
"args": ["1_TEF_Boegen_input.json"],
|
||||
"console": "integratedTerminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
def convert_to_float(value):
|
||||
"""Safely convert value to float, return original if conversion fails"""
|
||||
try:
|
||||
return float(value)
|
||||
except (ValueError, TypeError):
|
||||
return value
|
||||
def process_json_file(input_file):
|
||||
# Validate and prepare file paths
|
||||
input_path = os.path.abspath(input_file)
|
||||
if not os.path.isfile(input_path):
|
||||
print(f"Error: Input file not found - {input_path}")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Reading file: {input_path}")
|
||||
|
||||
# Read JSON file
|
||||
try:
|
||||
with open(input_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error: Invalid JSON format in {input_path}")
|
||||
print(f"Details: {str(e)}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error reading file {input_path}: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
# Process each item in the JSON array
|
||||
for i, item in enumerate(data):
|
||||
print(f"\nProcessing item {i+1}: {item.get('Sivasnr', 'Unknown')}")
|
||||
|
||||
# Convert numerical values to float
|
||||
for key in item:
|
||||
if isinstance(item[key], (int, float, str)) and any(x in key.lower() for x in ['mm', 'width', 'height', 'radius', 'winkel']):
|
||||
item[key] = convert_to_float(item[key])
|
||||
|
||||
# 1. Add four new attributes and calculate their values
|
||||
print("\nStep 1: Adding four new attributes with calculations")
|
||||
|
||||
# OFBogen_CP2_x_mm
|
||||
of_cp2_x = float(item["OFBogen_CP1_x_mm"]) + float(item["OFBogen_center_line_width_mm"])
|
||||
item["OFBogen_CP2_x_mm"] = round(of_cp2_x, 3)
|
||||
print(f"Added OFBogen_CP2_x_mm: {item['OFBogen_CP2_x_mm']}")
|
||||
|
||||
# TEFBogen_CP2_x_mm
|
||||
tef_cp2_x = float(item["TEFBogen_CP1_x_mm"]) + float(item["TEFBogen_center_line_width_mm"])
|
||||
item["TEFBogen_CP2_x_mm"] = round(tef_cp2_x, 3)
|
||||
print(f"Added TEFBogen_CP2_x_mm: {item['TEFBogen_CP2_x_mm']}")
|
||||
|
||||
# OFBogen_CP2_y_mm
|
||||
of_cp2_y = float(item["OFBogen_CP1_y_mm"]) + float(item["OFBogen_center_line_height_mm"])
|
||||
item["OFBogen_CP2_y_mm"] = round(of_cp2_y, 3)
|
||||
print(f"Added OFBogen_CP2_y_mm: {item['OFBogen_CP2_y_mm']}")
|
||||
|
||||
# TEFBogen_CP2_y_mm
|
||||
tef_cp2_y = float(item["TEFBogen_CP1_y_mm"]) + float(item["TEFBogen_center_line_height_mm"])
|
||||
item["TEFBogen_CP2_y_mm"] = round(tef_cp2_y, 3)
|
||||
print(f"Added TEFBogen_CP2_y_mm: {item['TEFBogen_CP2_y_mm']}")
|
||||
|
||||
# 2. Calculate pixel dimensions and SVG dimensions
|
||||
print("\nStep 2: Calculating pixel and SVG dimensions")
|
||||
|
||||
# Calculate Gruppe_width_px and Gruppe_height_px
|
||||
item["Gruppe_width_px"] = round(float(item["Gruppe_width_mm"]) * 3.7795, 3)
|
||||
item["Gruppe_height_px"] = round(float(item["Gruppe_height_mm"]) * 3.7795, 3)
|
||||
print(f"Calculated Gruppe_width_px: {item['Gruppe_width_px']}")
|
||||
print(f"Calculated Gruppe_height_px: {item['Gruppe_height_px']}")
|
||||
|
||||
# Compare width and height to determine scaling
|
||||
if float(item["Gruppe_width_mm"]) > float(item["Gruppe_height_mm"]):
|
||||
print("Width is larger, setting calculated_SVG_width to 1000px")
|
||||
scale = round(1000 / float(item["Gruppe_width_mm"]), 6)
|
||||
item["calculated_SVG_width_px"] = 1000
|
||||
item["calculated_SVG_height_px"] = round(float(item["Gruppe_height_mm"]) * scale+3.7795/2, 3)
|
||||
scale_RD_H = round(1000 / float(item["calculated_SVG_height_px"]), 6)
|
||||
scale_RD_W = 1
|
||||
elif float(item["Gruppe_width_mm"]) == float(item["Gruppe_height_mm"]):
|
||||
print("Width =Height")
|
||||
scale = round(1000 / float(item["Gruppe_width_mm"]), 6)
|
||||
item["calculated_SVG_width_px"] = 1000
|
||||
item["calculated_SVG_height_px"] = round(float(item["Gruppe_height_mm"]) * scale, 3)
|
||||
scale_RD_H = round(1000 / float(item["calculated_SVG_height_px"]), 6)
|
||||
scale_RD_W = 1
|
||||
else:
|
||||
print("Height is larger, setting calculated_SVG_height to 1000px")
|
||||
scale = round(1000 / float(item["Gruppe_height_mm"]), 6)
|
||||
item["calculated_SVG_height_px"] = 1000
|
||||
item["calculated_SVG_width_px"] = round(float(item["Gruppe_width_mm"]) * scale+3.7795/2, 3)
|
||||
scale_RD_W = round(1000 / float(item["calculated_SVG_width_px"]), 6)
|
||||
scale_RD_H = 1
|
||||
|
||||
print(f"Calculated scale: {scale}")
|
||||
print(f"Calculated calculated_SVG_width_px: {item['calculated_SVG_width_px']}")
|
||||
print(f"Calculated calculated_SVG_height_px: {item['calculated_SVG_height_px']}")
|
||||
print(f"Calculated scale_RD_W: {scale_RD_W}")
|
||||
print(f"Calculated scale_RD_H: {scale_RD_H}")
|
||||
|
||||
# 3. Create connectionPoints array
|
||||
print("\nStep 3: Creating connectionPoints array")
|
||||
|
||||
connection_points = []
|
||||
|
||||
# CP1
|
||||
cp1_x = round(float(item["OFBogen_CP1_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp1_y = round(float(item["OFBogen_CP1_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp1 = {
|
||||
"id": "cp1",
|
||||
"x": cp1_x,
|
||||
"y": cp1_y,
|
||||
"direction": 270.0,
|
||||
"linkClass": "Omniflo"
|
||||
}
|
||||
connection_points.append(cp1)
|
||||
print(f"Created CP1: x={cp1_x}, y={cp1_y}, direction=270.0, linkClass=Omniflo")
|
||||
|
||||
# CP2
|
||||
cp2_x = round(float(item["OFBogen_CP2_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp2_y = round(float(item["OFBogen_CP2_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp2_direction = round(90 + float(item["KurvenWinkel"]), 1)
|
||||
cp2 = {
|
||||
"id": "cp2",
|
||||
"x": cp2_x,
|
||||
"y": cp2_y,
|
||||
"direction": cp2_direction,
|
||||
"linkClass": "Omniflo"
|
||||
}
|
||||
connection_points.append(cp2)
|
||||
print(f"Created CP2: x={cp2_x}, y={cp2_y}, direction={cp2_direction}, linkClass=Omniflo")
|
||||
|
||||
# CP3
|
||||
cp3_x = round(float(item["TEFBogen_CP1_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp3_y = round(float(item["TEFBogen_CP1_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp3 = {
|
||||
"id": "cp3",
|
||||
"x": cp3_x,
|
||||
"y": cp3_y,
|
||||
"direction": 270.0,
|
||||
"linkClass": "OmnifloTEF"
|
||||
}
|
||||
connection_points.append(cp3)
|
||||
print(f"Created CP3: x={cp3_x}, y={cp3_y}, direction=270.0, linkClass=OmnifloTEF")
|
||||
|
||||
# CP4
|
||||
cp4_x = round(float(item["TEFBogen_CP2_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp4_y = round(float(item["TEFBogen_CP2_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp4_direction = round(90 + float(item["KurvenWinkel"]), 1)
|
||||
cp4 = {
|
||||
"id": "cp4",
|
||||
"x": cp4_x,
|
||||
"y": cp4_y,
|
||||
"direction": cp4_direction,
|
||||
"linkClass": "OmnifloTEF"
|
||||
}
|
||||
connection_points.append(cp4)
|
||||
print(f"Created CP4: x={cp4_x}, y={cp4_y}, direction={cp4_direction}, linkClass=OmnifloTEF")
|
||||
|
||||
item["connectionPoints"] = connection_points
|
||||
|
||||
# Prepare output path
|
||||
output_dir = os.path.dirname(input_path) or '.' # Handle case when no directory in path
|
||||
base_name = os.path.splitext(os.path.basename(input_path))[0]
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
output_path = os.path.join(output_dir, f"{base_name}_processed_output.json")
|
||||
|
||||
# Save processed JSON file
|
||||
try:
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
print(f"\nProcessing complete! Results saved to new file: {output_path}")
|
||||
print(f"Original file remains unchanged: {input_path}")
|
||||
except Exception as e:
|
||||
print(f"\nError saving processed file: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
if len(sys.argv) != 2:
|
||||
raise ValueError("请提供输入文件路径")
|
||||
input_file = sys.argv[1]
|
||||
process_json_file(input_file)
|
||||
except Exception as e:
|
||||
print(f"错误详情: {str(e)}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,259 @@
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
def convert_to_float(value):
|
||||
"""Convert value to float safely, return original if conversion fails"""
|
||||
try:
|
||||
return float(value)
|
||||
except (ValueError, TypeError):
|
||||
return value
|
||||
|
||||
def process_json_file(input_filename, output_filename=None):
|
||||
"""
|
||||
Process JSON file and generate output
|
||||
|
||||
Args:
|
||||
input_filename: Input file path (can be relative or absolute)
|
||||
output_filename: Optional output file path. Auto-generated if not provided
|
||||
"""
|
||||
# Get base path from environment variable
|
||||
json_base_path = os.environ.get("JSON_PATH","JSON")
|
||||
if not json_base_path:
|
||||
print("Error: JSON_PATH environment variable not set")
|
||||
print("Please set JSON_PATH in your .bat file")
|
||||
sys.exit(1)
|
||||
|
||||
# Normalize path (handle path separator issues)
|
||||
json_base_path = os.path.normpath(json_base_path)
|
||||
|
||||
# Build full input path
|
||||
if os.path.isabs(input_filename):
|
||||
input_path = input_filename
|
||||
else:
|
||||
input_path = os.path.join(json_base_path, input_filename)
|
||||
|
||||
# Convert to absolute path and normalize
|
||||
input_path = os.path.abspath(os.path.normpath(input_path))
|
||||
|
||||
# Validate input file
|
||||
if not os.path.isfile(input_path):
|
||||
print(f"Error: Input file not found - {input_path}")
|
||||
print(f"Current working directory: {os.getcwd()}")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"\nReading file: {input_path}")
|
||||
|
||||
# Read JSON file
|
||||
try:
|
||||
with open(input_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error: Invalid JSON format in {input_path}")
|
||||
print(f"Details: {str(e)}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error reading file {input_path}: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
# Print found item count
|
||||
total_items = len(data)
|
||||
processed_items = 0
|
||||
print(f"\nFound {total_items} JSON records")
|
||||
print("="*50) # Separator line
|
||||
|
||||
|
||||
# 处理JSON数据 (保持原有处理逻辑不变)
|
||||
for i, item in enumerate(data):
|
||||
print(f"\nProcessing item {i+1}: {item.get('Sivasnr', 'Unknown')}")
|
||||
|
||||
try:
|
||||
# 转换数值为float
|
||||
for key in item:
|
||||
if isinstance(item[key], (int, float, str)) and any(x in key.lower() for x in ['mm', 'width', 'height', 'radius', 'winkel']):
|
||||
item[key] = convert_to_float(item[key])
|
||||
|
||||
# 1. 添加四个新属性并计算它们的值
|
||||
print("\nStep 1: Adding four new attributes with calculations")
|
||||
|
||||
# OFBogen_CP2_x_mm
|
||||
of_cp2_x = float(item["OFBogen_CP1_x_mm"]) + float(item["OFBogen_center_line_width_mm"])
|
||||
item["OFBogen_CP2_x_mm"] = round(of_cp2_x, 3)
|
||||
print(f"Added OFBogen_CP2_x_mm: {item['OFBogen_CP2_x_mm']}")
|
||||
|
||||
# TEFBogen_CP2_x_mm
|
||||
tef_cp2_x = float(item["TEFBogen_CP1_x_mm"]) + float(item["TEFBogen_center_line_width_mm"])
|
||||
item["TEFBogen_CP2_x_mm"] = round(tef_cp2_x, 3)
|
||||
print(f"Added TEFBogen_CP2_x_mm: {item['TEFBogen_CP2_x_mm']}")
|
||||
|
||||
# OFBogen_CP2_y_mm
|
||||
of_cp2_y = float(item["OFBogen_CP1_y_mm"]) + float(item["OFBogen_center_line_height_mm"])
|
||||
item["OFBogen_CP2_y_mm"] = round(of_cp2_y, 3)
|
||||
print(f"Added OFBogen_CP2_y_mm: {item['OFBogen_CP2_y_mm']}")
|
||||
|
||||
# TEFBogen_CP2_y_mm
|
||||
tef_cp2_y = float(item["TEFBogen_CP1_y_mm"]) + float(item["TEFBogen_center_line_height_mm"])
|
||||
item["TEFBogen_CP2_y_mm"] = round(tef_cp2_y, 3)
|
||||
print(f"Added TEFBogen_CP2_y_mm: {item['TEFBogen_CP2_y_mm']}")
|
||||
|
||||
# 2. 计算像素尺寸和SVG尺寸
|
||||
print("\nStep 2: Calculating pixel and SVG dimensions")
|
||||
|
||||
# 计算 Gruppe_width_px 和 Gruppe_height_px
|
||||
item["Gruppe_width_px"] = round(float(item["Gruppe_width_mm"]) * 3.7795, 3)
|
||||
item["Gruppe_height_px"] = round(float(item["Gruppe_height_mm"]) * 3.7795, 3)
|
||||
print(f"Calculated Gruppe_width_px: {item['Gruppe_width_px']}")
|
||||
print(f"Calculated Gruppe_height_px: {item['Gruppe_height_px']}")
|
||||
|
||||
# 比较宽度和高度以确定缩放比例
|
||||
if float(item["Gruppe_width_mm"]) > float(item["Gruppe_height_mm"]):
|
||||
print("Width is larger, setting calculated_SVG_width to 1000px")
|
||||
scale = round(1000 / float(item["Gruppe_width_mm"]), 6)
|
||||
item["calculated_SVG_width_px"] = 1000
|
||||
item["calculated_SVG_height_px"] = round(float(item["Gruppe_height_mm"]) * scale+3.7795/2, 3)
|
||||
scale_RD_H = round(1000 / float(item["calculated_SVG_height_px"]), 6)
|
||||
scale_RD_W = 1
|
||||
elif float(item["Gruppe_width_mm"]) == float(item["Gruppe_height_mm"]):
|
||||
print("Width = Height")
|
||||
scale = round(1000 / float(item["Gruppe_width_mm"]), 6)
|
||||
item["calculated_SVG_width_px"] = 1000
|
||||
item["calculated_SVG_height_px"] = round(float(item["Gruppe_height_mm"]) * scale, 3)
|
||||
scale_RD_H = round(1000 / float(item["calculated_SVG_height_px"]), 6)
|
||||
scale_RD_W = 1
|
||||
else:
|
||||
print("Height is larger, setting calculated_SVG_height to 1000px")
|
||||
scale = round(1000 / float(item["Gruppe_height_mm"]), 6)
|
||||
item["calculated_SVG_height_px"] = 1000
|
||||
item["calculated_SVG_width_px"] = round(float(item["Gruppe_width_mm"]) * scale+3.7795/2, 3)
|
||||
scale_RD_W = round(1000 / float(item["calculated_SVG_width_px"]), 6)
|
||||
scale_RD_H = 1
|
||||
|
||||
print(f"Calculated scale: {scale}")
|
||||
print(f"Calculated calculated_SVG_width_px: {item['calculated_SVG_width_px']}")
|
||||
print(f"Calculated calculated_SVG_height_px: {item['calculated_SVG_height_px']}")
|
||||
print(f"Calculated scale_RD_W: {scale_RD_W}")
|
||||
print(f"Calculated scale_RD_H: {scale_RD_H}")
|
||||
|
||||
# 3. 创建connectionPoints数组
|
||||
print("\nStep 3: Creating connectionPoints array")
|
||||
|
||||
connection_points = []
|
||||
|
||||
# CP1
|
||||
cp1_x = round(float(item["OFBogen_CP1_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp1_y = round(float(item["OFBogen_CP1_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp1 = {
|
||||
"id": "cp1",
|
||||
"x": cp1_x,
|
||||
"y": cp1_y,
|
||||
"direction": 270.0,
|
||||
"linkClass": "Omniflo"
|
||||
}
|
||||
connection_points.append(cp1)
|
||||
print(f"Created CP1: x={cp1_x}, y={cp1_y}, direction=270.0, linkClass=Omniflo")
|
||||
|
||||
# CP2
|
||||
cp2_x = round(float(item["OFBogen_CP2_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp2_y = round(float(item["OFBogen_CP2_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp2_direction = round(90 + float(item["KurvenWinkel"]), 1)
|
||||
cp2 = {
|
||||
"id": "cp2",
|
||||
"x": cp2_x,
|
||||
"y": cp2_y,
|
||||
"direction": cp2_direction,
|
||||
"linkClass": "Omniflo"
|
||||
}
|
||||
connection_points.append(cp2)
|
||||
print(f"Created CP2: x={cp2_x}, y={cp2_y}, direction={cp2_direction}, linkClass=Omniflo")
|
||||
|
||||
# CP3
|
||||
cp3_x = round(float(item["TEFBogen_CP1_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp3_y = round(float(item["TEFBogen_CP1_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp3 = {
|
||||
"id": "cp3",
|
||||
"x": cp3_x,
|
||||
"y": cp3_y,
|
||||
"direction": 270.0,
|
||||
"linkClass": "OmnifloTEF"
|
||||
}
|
||||
connection_points.append(cp3)
|
||||
print(f"Created CP3: x={cp3_x}, y={cp3_y}, direction=270.0, linkClass=OmnifloTEF")
|
||||
|
||||
# CP4
|
||||
cp4_x = round(float(item["TEFBogen_CP2_x_mm"]) * scale * scale_RD_W, 3)
|
||||
cp4_y = round(float(item["TEFBogen_CP2_y_mm"]) * scale * scale_RD_H, 3)
|
||||
cp4_direction = round(90 + float(item["KurvenWinkel"]), 1)
|
||||
cp4 = {
|
||||
"id": "cp4",
|
||||
"x": cp4_x,
|
||||
"y": cp4_y,
|
||||
"direction": cp4_direction,
|
||||
"linkClass": "OmnifloTEF"
|
||||
}
|
||||
connection_points.append(cp4)
|
||||
print(f"Created CP4: x={cp4_x}, y={cp4_y}, direction={cp4_direction}, linkClass=OmnifloTEF")
|
||||
|
||||
item["connectionPoints"] = connection_points
|
||||
# Increment success counter
|
||||
processed_items += 1
|
||||
print(f"Item {i} processed successfully")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error processing item {i}: {str(e)}")
|
||||
continue
|
||||
|
||||
# Print processing statistics
|
||||
print("\n" + "="*50)
|
||||
print("Processing Statistics:")
|
||||
print(f"Total records found: {total_items}")
|
||||
print(f"Successfully processed: {processed_items}")
|
||||
if total_items > 0:
|
||||
success_rate = (processed_items / total_items) * 100
|
||||
print(f"Success rate: {success_rate:.2f}%")
|
||||
print("="*50 + "\n")
|
||||
|
||||
# Handle output file path
|
||||
if output_filename is None:
|
||||
base_name = os.path.splitext(os.path.basename(input_path))[0]
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
output_filename = f"{base_name}_processed_{timestamp}.json"
|
||||
|
||||
# Build output path
|
||||
if os.path.isabs(output_filename):
|
||||
output_path = output_filename
|
||||
else:
|
||||
output_path = os.path.join(json_base_path, output_filename)
|
||||
|
||||
# Ensure output directory exists
|
||||
os.makedirs(os.path.dirname(output_path) or '.', exist_ok=True)
|
||||
|
||||
# Save processed JSON file
|
||||
try:
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, indent=2, ensure_ascii=False)
|
||||
print(f"Processing complete! Results saved to: {output_path}")
|
||||
print(f"Original file remains unchanged: {input_path}")
|
||||
except Exception as e:
|
||||
print(f"\nError saving processed file: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# Get JSON_PATH from environment
|
||||
json_path = os.environ.get("JSON_PATH","JSON")
|
||||
|
||||
# Default filenames
|
||||
input_filename = "1_TEF_Boegen_input.json"
|
||||
output_filename = "1_TEF_Boegen_output.json"
|
||||
|
||||
# Use command line arguments if provided
|
||||
if len(sys.argv) > 1:
|
||||
input_filename = sys.argv[1]
|
||||
if len(sys.argv) > 2:
|
||||
output_filename = sys.argv[2]
|
||||
|
||||
process_json_file(input_filename, output_filename)
|
||||
except Exception as e:
|
||||
print(f"Error: {str(e)}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
+45
-28
@@ -1,18 +1,3 @@
|
||||
''' Script Analysis
|
||||
|
||||
This Python script processes JSON and TXT files to update dimensions and connection points in SVG-related data. Here's the main logic:
|
||||
Input Handling:
|
||||
Reads a JSON file containing reference data
|
||||
Processes all TXT files in a specified directory
|
||||
Data Processing:
|
||||
Creates a mapping between Sivasnr (from filenames) and JSON data
|
||||
For each matching TXT file:
|
||||
Updates width and height based on JSON data (converting mm to px)
|
||||
Updates connection points (x, y, direction) from JSON data
|
||||
Preserves the original file structure while updating specific values
|
||||
Reporting:
|
||||
Prints detailed change reports to console
|
||||
Skips files without matching JSON data '''
|
||||
import json
|
||||
import os
|
||||
import glob
|
||||
@@ -30,13 +15,21 @@ def process_files(json_file_path, txt_files_dir):
|
||||
# Create Sivasnr to JSON data mapping
|
||||
sivasnr_mapping = {item["Sivasnr"]: item for item in json_data}
|
||||
|
||||
# Initialize counters
|
||||
total_files = 0
|
||||
processed_files = 0
|
||||
skipped_files = 0
|
||||
|
||||
# Prepare report content
|
||||
report_content = []
|
||||
report_content.append(f"Modification Report - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
report_content.append(f"JSON Reference File: {json_file_path}")
|
||||
report_content.append(f"TXT Files Directory: {txt_files_dir}")
|
||||
report_content.append("="*50 + "\n")
|
||||
|
||||
# Process all TXT files
|
||||
for txt_file_path in glob.glob(os.path.join(txt_files_dir, '*.txt')):
|
||||
total_files += 1
|
||||
# Extract Sivasnr from filename
|
||||
sivasnr = os.path.splitext(os.path.basename(txt_file_path))[0]
|
||||
|
||||
@@ -50,7 +43,7 @@ def process_files(json_file_path, txt_files_dir):
|
||||
|
||||
# Prepare entry for this file
|
||||
file_entry = []
|
||||
file_entry.append(f"\nProcessing file: {txt_file_path}")
|
||||
file_entry.append(f"\nProcessing file: {os.path.basename(txt_file_path)}")
|
||||
file_entry.append("="*50)
|
||||
|
||||
# Record old values
|
||||
@@ -108,7 +101,9 @@ def process_files(json_file_path, txt_files_dir):
|
||||
file_entry.append(f" y: {change['y'][0]} → {change['y'][1]}")
|
||||
file_entry.append(f" direction: {change['direction'][0]} → {change['direction'][1]}")
|
||||
file_entry.append(f" linkClass: {change['linkClass'][0]} → {change['linkClass'][1]}")
|
||||
file_entry.append(f"\nFile {txt_file_path} processed successfully")
|
||||
|
||||
processed_files += 1
|
||||
file_entry.append(f"\nFile processed successfully")
|
||||
file_entry.append("="*50)
|
||||
|
||||
# Add this file's entry to main report
|
||||
@@ -117,19 +112,41 @@ def process_files(json_file_path, txt_files_dir):
|
||||
# Also print to console
|
||||
print("\n" + "\n".join(file_entry))
|
||||
else:
|
||||
print(f"\nSkipping file {txt_file_path} (no matching JSON data found)")
|
||||
skipped_files += 1
|
||||
|
||||
# Write the report file if any changes were made
|
||||
if len(report_content) > 2: # More than just the header
|
||||
with open(log_file_path, 'w', encoding='utf-8') as f:
|
||||
f.write("\n".join(report_content))
|
||||
print(f"\nModification report saved to: {log_file_path}")
|
||||
else:
|
||||
print("\nNo files were modified - no report generated")
|
||||
# Add processing statistics to report
|
||||
report_content.append("\n" + "="*50)
|
||||
report_content.append("Processing Statistics:")
|
||||
report_content.append(f"Total TXT files found: {total_files}")
|
||||
report_content.append(f"Total JSON records available: {len(json_data)}")
|
||||
report_content.append(f"Successfully processed: {processed_files}")
|
||||
report_content.append(f"Skipped files: {skipped_files}")
|
||||
if total_files > 0:
|
||||
success_rate = (processed_files / len(json_data)) * 100
|
||||
report_content.append(f"Success rate: {success_rate:.2f}%")
|
||||
report_content.append("="*50)
|
||||
|
||||
# Print statistics to console
|
||||
print("\n" + "="*50)
|
||||
print("Processing Statistics:")
|
||||
print(f"Total TXT files found: {total_files}")
|
||||
print(f"Total JSON records available: {len(json_data)}")
|
||||
print(f"Successfully processed: {processed_files}")
|
||||
print(f"Skipped files: {skipped_files}")
|
||||
if total_files > 0:
|
||||
success_rate = (processed_files / len(json_data)) * 100
|
||||
print(f"Success rate: {success_rate:.2f}%")
|
||||
print("="*50 + "\n")
|
||||
|
||||
# Write the report file
|
||||
with open(log_file_path, 'w', encoding='utf-8') as f:
|
||||
f.write("\n".join(report_content))
|
||||
print(f"Modification report saved to: {log_file_path}")
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
json_file_path = "1_TEF_Boegen_input_processed_output.json"
|
||||
txt_files_dir = "C:/Program Files/RuleDesigner/RDConfigurator Fusion/WebApi/Editor2D/SSG/shapes/props"
|
||||
json_path = os.environ.get("JSON_PATH", "JSON")
|
||||
json_file_path = os.path.join(json_path, "1_TEF_Boegen_output.json")
|
||||
txt_files_dir = os.environ.get("PROPS_PATH", "props")
|
||||
process_files(json_file_path, txt_files_dir)
|
||||
print("\nAll files processed!")
|
||||
print("\nProcessing complete!")
|
||||
@@ -107,8 +107,10 @@ def main():
|
||||
Main processing function
|
||||
"""
|
||||
# Configure paths
|
||||
json_file_path = "1_TEF_Boegen_input.json"
|
||||
svg_folder_path = r"C:\Program Files\RuleDesigner\RDConfigurator Fusion\WebApi\Editor2D\SSG\shapes\svg"
|
||||
json_path=os.environ.get("JSON_PATH","JSON")
|
||||
json_file_path = os.path.join(json_path,"1_TEF_Boegen_input.json")
|
||||
svg_folder_path = os.environ.get("XML_PATH","svg")
|
||||
|
||||
|
||||
try:
|
||||
# Read and parse JSON file
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
@echo off
|
||||
|
||||
set TEFBogen_PATH=%~dp0
|
||||
|
||||
set XML_PATH=C:\Program Files\RuleDesigner\RDConfigurator Fusion\WebApi\Editor2D\SSG\shapes\svg
|
||||
|
||||
set JSON_PATH=%TEFBogen_PATH%JSON
|
||||
|
||||
python 4_TEFBogen_SVG_XML_Modifier_Script.py
|
||||
|
||||
|
||||
pause
|
||||
@@ -0,0 +1,12 @@
|
||||
@echo off
|
||||
|
||||
set TEFBogen_PATH=%~dp0
|
||||
set PROPS_PATH=C:\Program Files\RuleDesigner\RDConfigurator Fusion\WebApi\Editor2D\SSG\shapes\props
|
||||
|
||||
set JSON_PATH=%TEFBogen_PATH%JSON
|
||||
|
||||
python 1_process_TEFBogen_json_1.py
|
||||
python 2_update_props_TEF_Boegen_from_json_1.py
|
||||
|
||||
|
||||
pause
|
||||
Reference in New Issue
Block a user