Retrieving Ratsnests using python scripting

Hi,
I am writing an autorouter algorithm through python scripting that sets up track based on ratsnests. So I want to retrieve the ratsnest lines(start & end points) given on a pcb board. I read the documentation of pcbnew as given in KiCad Pcbnew Python Scripting: pcbnew.py Source File ,but some of the APIs aren’t available in the version I have(Version 4.1.1).Besides that the version I have still contains some of the API calls related to ratsnests lines, however these API calls return objects that have less properties than in the documentation (like RN_NET & RN_DYNAMIC_LINE classes).
I would appreciate any help or guidance to how may I retrieve the ratsnests lines through python scripting without having to recalculate these from the netlist since they are already drawn in the pcbdesign I have.

I’m pretty sure you can’t access the ratsnest through python.

I’m also sceptical about trying to use it for aoutorouting at all. The lines are completely arbitrary, or are based on some algorithm which in many cases doesn’t tell anything about optimal start/end points of routing.

Have you already read Autorouting, autorouter, autoplacing and the linked threads?

1 Like

Alright so I will have to compute distances based on pads.
Yes,I read other threads they all seem not optimistic towards a good autorouter for Kicad. But I am dedicated to it since it’s my fyp. However I am still not sure if writing a whole new autorouting program(c/c++ based) to test my algorithm would be easier than to use kicad.

Rather, you can leave out “for KiCad” and say “not optimistic towards a good autorouter”. (Just to make clear to readers that KiCad isn’t a special problem for autorouting – autorouting itself is.)

2 Likes

How serious are you about this?

Designing a good autorouter is not a trivial task. Think about a multi year project by a team of programmers. It’s also computing intensive, I have doubts whether python would be a good choice for this.

And even if you have a good autorouter, apparently they need a lot of specific setting to work properly. Some long threads have been written on this forum concerning autorouters.

Personally I don’t like autorouters very much. I prefer to do manual routing because it tends to deliver much better output then a (simple) autorouter.

If you want to combine simplicity with effectiveness, you get something like the Interactive router in KiCad. Currently it already has a bunch of neat features that speed up laying tracks:

  1. Drawing multiple segments in one go.
  2. The f for finish hotkey, which routes a track to the nearest pin.
  3. Push 'n Shove for tracks & via’s.
  4. Dragging a footprint while keeping tracks attached.
  5. Optimization to smooth / merge track segments.

If you want to spend some programming time on KiCad to make an improvement in some limited amount of time. Improving the interactive router is probably a better way to go.

1 Like

I am very serious about this, I already said that this is my final year project. I already designed a very complex algorithm, I just need to test it on any PCB design program. I came across Kicad scripting tool and found that it may do the work. The algorithm I designed also uses multithreads. Now the only issue I have is that I am not sure if I shall use Kicad or just start a program by myself(maybe in c/c++) as they did in the freerouting extension, and implement my algorithm through it.

My first thought would be that this is a too big of a bite for a “final year project”. But I don’t know you, nor your background, and maybe your thoughts are better worked out and more organized then only “youth enthusiasm”. I certainly hope so, but do keep in check what is realistic.

I think the best outcome would be to implement it in KiCad itself as an extention / additoin of the interactive router itself. This would mean programming in C++, and probably a month or more just getting to know the KiCad codebase.

What you get back for it, is access to everything that is already in KiCad, and experience with managing and modifying code of a big software project. Even if the end result is not usable, this experience alone can be worth the voyage. If the result is usable and good enough to be integrated in KiCad (even if it’s mostly the groundwork, and some minimum viable functions) then developing it inside KiCad gives your code a much better chance for long term use then an addon such as Freerouting.

But overall, the way I understand it “industry” has mostly given up on full autorouters. Interactive routers, which combine automated functions with human input (Even a “full” autorouter needs a lot of setup from a human) is apparently the way to go. Things like breaking out a bus from a big BGA chip, or autorouting partial designs, such as address lines to DDR memory IC’s can be powerful tools.

There would be a lot of mundane programming before you can even start on the autorouter itself. You need to read input files, figure out how to render footprints, outline of the PCB and such on the PCB, have functions to write the result back to HDD / SSD. You will need visual capabilities at least for testing and verifying your autorouter algorithms. When it is an external program, it’s also much less likely to be used by other people and just fade away like almost all end year projects do. (Including my own).

If you consider the way to go with integration into KiCad itself, then I suggest you start with making a proposal and to communicate with some of the KiCad developers. At the moment they have no plans for an autorouter, but that is mainly because they simply lack the amount of time they can spend on such a topic. There are too many other topics (around 1500 open issues on gitlab) which are competing and waiting for implementation.

I do not know what their reaction would be. Maybe they disregard it, maybe they endorse it and want to help. Last February for example, there was a day reserved just before Fosdem, to help people wanting to start coding in KiCad to help them with getting to know the KiCad code base.

1 Like

Why not use the SPECCTRA export and import file-menu items? Online you can find a document titled “SPECCTRA Design language Reference” for a description of the fileformats. It is a similar route as used by Freerouting, TopoR, etc.

Now you also have other PCB software that you can use to check your file parser and file generator. Maybe you can reuse sourcecode of an other project for your file parser, to save time and focus on the routing algorithms.

I also found a “SPECCTRA Tutorial”, which is focused on a Cadence autorouter. It might generate ideas for your autorouter project.

2 Likes

I suppose you could train one of those LLM, AI thingamajiggers to do the board layout. Those of us that actually find this one the more fun parts of a project wonder why you would give a machine that pleasure instead. (posting for a friend :wink: )

Something like this:

The first example on their website, looks acceptable for a 4 layer PCB, but it would be completely useless for a 2 layer PCB. That is one of the problems of autorouters. Cleaning up to be able to make a proper GND plane on a size constrained PCB. That example also very much room available for just a few tracks. Routing a space constrained PCB is a whole different beast.

And then, a quick search on youtube for deeppcb finds:

I don’t like Davey Jones much. because he talks faster then his brain can think, but it does give you some idea of why many people do not like autorouter much. From what I remember from his video years ago (DeepPCB may have improved too) is that at least half his complaints are groundless. (no pun intended) but just esthetic.

Another problem is that footprint placement is more complicated / important then the routing itself. An interesting use of an autorouter would be to not use it mainly for the routing itself, but just to verify whether footprint placement is correct, or to localize where problems with footprints are. Although the ratsnest is a pretty good indication of this too.

I would be curious to see in how far a simple python script would be, that simulates combinations of x (Start routing) and f (finish to nearest pad) would be if it’s done for all tracks up to a certain pad distance (say 20mm). Simple things like this could be time saving functions.

1 Like

I know this project is too big of a bite for a fyp. But this fyp is in collaboration with a big company, and I’ve been working on research for ~4months. And now, after they approved the algorithm I proposed, I need to test it.
Now concerning integrating into Kicad, I would really like to join this community and help in the development whether in the project I am doing or other issues, however the amount of time given is not enough for me to enter this path, that’s why for now, if this project shall be done as an extension through Kicad I will definitely try my best to do it, or else I will have to maybe write my own ‘beta’ program(very basic features)just to test this algorithm.
In the mean time, do you think its possible to find Kicad developers that may help with some guidance for such extension?

I think even if you could get the ratsnest lines they will be useless to you. As eelik says, they are arbitrarily drawn to join up copper areas belonging to a net. All the information you need about the areas is in the DSN file. Besides working from the DSN file allows you to compare your work with existing autorouters. Surely there are graphics libraries that can work out which are the disjoint areas for you.

TL;DR: You are stumbling over a cavil.

1 Like

I already read the same feedbacks about DeepPCB, not only DeepPCB, but currently there’s no autorouter that may achieve 100% routability for complex boards.The algorithm I proposed is able to place vias in valid positions to switch between layers and do maze routing, it also always tends to give the shortest valid routes. It’s also able to avoid blockages, and is able to connect to tracks belonging to same net while maze routing (i.e. not always connect to the destination pad).While most of the algorithms I read use congestion based routing with multiple iterations or rip-up&reroute until a threshold congestion limit is met.

Basically you have 3 options and you have to choose one because they are wildly different and will not be easy to switch from one to another mid way.

  1. Study kicad code and implement your autorouter directly in the app. You’ll need strong c++ skills to have hope to get any results. You will be able to do anything, only limited by your coding ability and time here.
  2. Implement the router as a python plugin using existing api to read board objects and nets and generate tracks as output. Convenient for users but python will limit you in performance and you will not be able to effectively use multithreading. Still, can do a lot with these limitations and provide value.
  3. Go the Spectra export/ DSN import route like freerouting does. You are free to use any language but you will likely have to reimplement some stuff like parsing the formats and probably will have to build some kind of UI on your own. If you are good with java you may even look into replacing the routing core of Freerouting and reuse everything else: import/export, GUI, configs. May save you significant time.
1 Like

I just answer forum questions and sometimes report issues on gitlab. I do not know about KiCad’s internals.

Simply forking the KiCad code, compiling it and looking if you can make some sense of it is one way to start. I have looked into the source code on some occasions (for example to find the fomula’s used for transmission lines in the calculator) and such things were quite easy to find.

If you want to get in touch with KiCad developers, then either creating an issue on gitlab, or writing a mail to Contact Us – KiPro are options. You don’t have to do both, as there is a big overlap in the people who use those websites.

But do keep in mind that developer hours for KiCad are a limited resource, so don’t start with a long essay. I guess it also helps if you add some info about your own background (how much programming experience do you have?) How much (programing) time / hours can you spend on your fyp? Ask yourself which information they need to evaluate your request for help, and use that to write your initial introduction.

1 Like

Actually @Seth_h and @craftyjon read and write here occasionally, maybe that’s the easiest way to reach the team. Issue database isn’t for questions or general communication. The developers mailing list is meant for that.

Alright any recommendations on where to start if I want to learn more about Spectra/DSN?

I think there are documents floating around (or officially freely available) that describe the format. (It is a data exchange format, implemented in many programs).

KiCad can write the export files, and import them, so you can have a look at the KiCad source code for that. Same with the Freerouter sourcecode. (I think it’s javascript).

I agree that using the DSN format will be the way to go here. It will mean your project can interoperate with various different CAD tools rather than just KiCad, and it also means you don’t need to learn about KiCad internals.

On the other hand, if you are interested in KiCad in other ways than just for this project, getting familiar with the codebase pays back. I don’t know why Jon as a core developer tries to steer you away from that :grin:

1 Like