test-direct-export.jsonā¢5.44 kB
{
"method": "tools/call",
"params": {
"name": "project_execute",
"arguments": {
"projectId": "S000009result_1278407893",
"description": "Export visible tables from layout XML to CSV",
"code": "import os\nimport csv\nimport xml.etree.ElementTree as ET\n\nresult = {\n 'status': 'PENDING',\n 'project_name': '',\n 'layout_file': '',\n 'output_dir': '',\n 'tables': [],\n 'exported_files': [],\n 'total_rows': 0,\n 'errors': []\n}\n\ntry:\n # Get project path and name\n ver_path = visum.GetPath(1)\n project_dir = os.path.dirname(ver_path)\n project_name = os.path.splitext(os.path.basename(ver_path))[0]\n \n result['project_name'] = project_name\n result['output_dir'] = project_dir\n \n # Find layout file\n layout_path = os.path.join(project_dir, 'tabelle_report.lay')\n if not os.path.exists(layout_path):\n raise Exception(f\"Layout file non trovato: {layout_path}\")\n \n result['layout_file'] = layout_path\n \n # Parse XML\n tree = ET.parse(layout_path)\n root = tree.getroot()\n \n # Extract tables\n for list_item in root.iter('listLayoutItem'):\n try:\n # Get table metadata\n common = list_item.find('.//listLayoutCommonEntries')\n if common is None:\n continue\n \n list_title = common.get('listTitle', 'Unknown')\n \n graphic_params = list_item.find('.//listGraphicParameterLayoutItems')\n if graphic_params is None:\n continue\n \n net_object_type = graphic_params.get('netObjectType', '')\n if not net_object_type:\n continue\n \n # Extract columns\n columns = []\n for attr_def in list_item.iter('attributeDefinition'):\n attr_id = attr_def.get('attributeID')\n if attr_id:\n columns.append(attr_id)\n \n if not columns:\n continue\n \n result['tables'].append({\n 'title': list_title,\n 'type': net_object_type,\n 'columns': columns,\n 'column_count': len(columns)\n })\n \n except Exception as e:\n result['errors'].append(f\"Error parsing table: {str(e)}\")\n \n # Map netObjectType to Visum collections\n collection_mapping = {\n 'LINK': ('Links', 'No'),\n 'NODE': ('Nodes', 'No'),\n 'ZONE': ('Zones', 'No'),\n 'ODPAIR': ('ODPairs', 'No'),\n 'LINE': ('Lines', 'Name'),\n 'LINEROUTE': ('LineRoutes', 'Name'),\n 'STOP': ('StopPoints', 'No'),\n 'TURN': ('Turns', 'No')\n }\n \n # Export each table to CSV\n for table_info in result['tables']:\n net_type = table_info['type']\n \n if net_type not in collection_mapping:\n result['errors'].append(f\"Collection mapping not found for {net_type}\")\n continue\n \n collection_name, key_attr = collection_mapping[net_type]\n \n try:\n # Get collection\n collection = getattr(visum.Net, collection_name)\n count = collection.Count\n \n if count == 0:\n result['errors'].append(f\"{collection_name}: no data\")\n continue\n \n # Create CSV file\n safe_title = table_info['title'].replace(' ', '_').replace('(', '').replace(')', '')\n csv_file = os.path.join(project_dir, f\"{project_name}_{safe_title}.csv\")\n \n # Write CSV\n with open(csv_file, 'w', newline='', encoding='utf-8') as f:\n writer = csv.writer(f, delimiter=';')\n \n # Header\n writer.writerow(table_info['columns'])\n \n # Data rows\n rows_written = 0\n for i in range(count):\n try:\n item = collection.ItemByKey(i+1) if key_attr == 'No' else collection.Item(i)\n row = []\n for col in table_info['columns']:\n try:\n value = item.AttValue(col)\n row.append(value)\n except:\n row.append('') # Missing attribute\n writer.writerow(row)\n rows_written += 1\n except:\n continue\n \n file_size = os.path.getsize(csv_file)\n result['exported_files'].append({\n 'table': table_info['title'],\n 'file': os.path.basename(csv_file),\n 'rows': rows_written,\n 'columns': table_info['column_count'],\n 'size_kb': round(file_size / 1024, 2)\n })\n result['total_rows'] += rows_written\n \n except Exception as e:\n result['errors'].append(f\"{net_type}: {str(e)}\")\n \n result['status'] = 'SUCCESS'\n \nexcept Exception as e:\n result['status'] = 'FAILED'\n result['error'] = str(e)\n\nresult"
}
},
"jsonrpc": "2.0",
"id": 1
}