Skip to content

Commit 3596bae

Browse files
committed
fix find_component_connections() in netlist_tools.py
1 parent a3613f2 commit 3596bae

File tree

1 file changed

+108
-3
lines changed

1 file changed

+108
-3
lines changed

kicad_mcp/tools/netlist_tools.py

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,112 @@ async def find_component_connections(project_path: str, component_ref: str, ctx:
299299
netlist_data = extract_netlist(schematic_path)
300300

301301
if "error" in netlist_data:
302-
logger.error("Failed to extract netlist")
303-
302+
logger.error(f"Failed to extract netlist: {netlist_data['error']}")
303+
ctx.info(f"Failed to extract netlist: {netlist_data['error']}")
304+
return {"success": False, "error": netlist_data['error']}
305+
306+
# Check if component exists in the netlist
307+
components = netlist_data.get("components", {})
308+
if component_ref not in components:
309+
logger.error(f"Component {component_ref} not found in schematic")
310+
ctx.info(f"Component {component_ref} not found in schematic")
311+
return {
312+
"success": False,
313+
"error": f"Component {component_ref} not found in schematic",
314+
"available_components": list(components.keys())
315+
}
316+
317+
# Get component information
318+
component_info = components[component_ref]
319+
320+
# Find connections
321+
await ctx.report_progress(50, 100)
322+
ctx.info("Finding connections...")
323+
324+
nets = netlist_data.get("nets", {})
325+
connections = []
326+
connected_nets = []
327+
328+
for net_name, pins in nets.items():
329+
# Check if any pin belongs to our component
330+
component_pins = []
331+
for pin in pins:
332+
if pin.get('component') == component_ref:
333+
component_pins.append(pin)
334+
335+
if component_pins:
336+
# This net has connections to our component
337+
net_connections = []
338+
339+
for pin in component_pins:
340+
pin_num = pin.get('pin', 'Unknown')
341+
# Find other components connected to this pin
342+
connected_components = []
343+
344+
for other_pin in pins:
345+
other_comp = other_pin.get('component')
346+
if other_comp and other_comp != component_ref:
347+
connected_components.append({
348+
"component": other_comp,
349+
"pin": other_pin.get('pin', 'Unknown')
350+
})
351+
352+
net_connections.append({
353+
"pin": pin_num,
354+
"net": net_name,
355+
"connected_to": connected_components
356+
})
357+
358+
connections.extend(net_connections)
359+
connected_nets.append(net_name)
360+
361+
# Analyze the connections
362+
await ctx.report_progress(70, 100)
363+
ctx.info("Analyzing connections...")
364+
365+
# Categorize connections by pin function (if possible)
366+
pin_functions = {}
367+
if "pins" in component_info:
368+
for pin in component_info["pins"]:
369+
pin_num = pin.get('num')
370+
pin_name = pin.get('name', '')
371+
372+
# Try to categorize based on pin name
373+
pin_type = "unknown"
374+
375+
if any(power_term in pin_name.upper() for power_term in ["VCC", "VDD", "VEE", "VSS", "GND", "PWR", "POWER"]):
376+
pin_type = "power"
377+
elif any(io_term in pin_name.upper() for io_term in ["IO", "I/O", "GPIO"]):
378+
pin_type = "io"
379+
elif any(input_term in pin_name.upper() for input_term in ["IN", "INPUT"]):
380+
pin_type = "input"
381+
elif any(output_term in pin_name.upper() for output_term in ["OUT", "OUTPUT"]):
382+
pin_type = "output"
383+
384+
pin_functions[pin_num] = {
385+
"name": pin_name,
386+
"type": pin_type
387+
}
388+
389+
# Build result
390+
result = {
391+
"success": True,
392+
"project_path": project_path,
393+
"schematic_path": schematic_path,
394+
"component": component_ref,
395+
"component_info": component_info,
396+
"connections": connections,
397+
"connected_nets": connected_nets,
398+
"pin_functions": pin_functions,
399+
"total_connections": len(connections)
400+
}
401+
402+
await ctx.report_progress(100, 100)
403+
ctx.info(f"Found {len(connections)} connections for component {component_ref}")
404+
405+
return result
406+
304407
except Exception as e:
305-
raise e
408+
logger.error(f"Error finding component connections: {str(e)}", exc_info=True)
409+
ctx.info(f"Error finding component connections: {str(e)}")
410+
return {"success": False, "error": str(e)}

0 commit comments

Comments
 (0)