Howto python fillet a track

Hi, I’m going to update my RF-tools plugins to be 5.99 compatible.

I would need some tips to ‘Fillet’ a track with python code…
I have two contiguous tracks and I would need to fillet those with python code.

import pcbnew
from pcbnew import *
pcb = pcbnew.GetBoard()
tks=[]
for item in pcb.GetTracks():
    if  item.IsSelected():
        tks.append(item)

now I have i.e. two selected tracks and I would need to Fillet those by python
I’ve found this function in pcbnew:

    def Fillet(self, aRadius: "int", aErrorMax: "int") -> "SHAPE_POLY_SET":
        r"""Fillet(SHAPE_POLY_SET self, int aRadius, int aErrorMax) -> SHAPE_POLY_SET"""
        return _pcbnew.SHAPE_POLY_SET_Fillet(self, aRadius, aErrorMax)

but I would need a 'SHAPE_POLY_SET'
Any tip would be appreciated.
M

Can you explain with a drawing/screenshot what you mean by fillet of tracks?

I would need to round a track corner using python code

You need to shorten ends of the tracks and insert a PCB_ARC between them.

Arcs are defined using start, end and mid points. Start and end would be the ends of the tracks and mid point needs to be calculated so that the circle of the arc is tangent to track lines.

PCB_ARC doesn’t work like a fillet track…
a fillet track can be dragged changing its radius, a PCB_ARC is a static shape

Try to play the attached video

That’s how kicad models it internally. If GUI doesn’t react the same way to your arc then it’s probably missing some other prerequisits. Try attaching tracks to it’s end and assigning it the same net.
Also I’m not sure what is shown on the video can be done after the fillet operation is complete. I don’t have 5.99 nearby to check at the moment.

I will post a sample code tomorrow… but it seem to me that the arc doesn’t behave the same… may be is the way in which I build the arc

I confirm that in 5.99 is possible to drag a curved track changing its fillet radius

Well just create an empty pcb with two tracks and a fillet. Open the file in text editor, you will see it’s just an arc.

so probably is related

new_shape = PCB_SHAPE() 
new_arc = PCB_ARC(new_shape)
new_arc.SetStart(p1)
new_arc.SetEnd(p2)
new_arc.SetMid(mp)

probably this one should work:

new_trk = PCB_TRACK()
new_arc = PCB_ARC(new_trk)
new_arc.SetStart(p1)
new_arc.SetEnd(p2)
new_arc.SetMid(mp)

I just tested on 5.99

board = pcbnew.GetBoard()
new_arc = pcbnew.PCB_ARC(board)
new_arc.SetStart(p1)
new_arc.SetEnd(p2)
new_arc.SetMid(mp)
new_arc.SetWidth(w)
board.Add(new_arc)

Works fine and is draggable. Didn’t show up until I reloaded the board though, not even with Refresh().

Thanks I confirm it!

that’s a known issue … it works only with action scripts, python console doesn’t allow ATM to show added items.

Still it would be nice to have a way to call the python Fillet function… this will automatic create an arc tangent to tracks.
‘Fillet’ function will allow the track to be dragged more than the dimension of the created arc.

I’m not sure I understand what you want to achieve that can’t be done natively in KiCad

Arc track interactive resizing is here:

That function has nothing to do with tracks, it’s for rounding edges of polygon shapes. You can’t use it for tracks because it won’t give you coordinates for arc which you ultimately have to create.

Yes, I know it. But I need to manipulate it to add some functionalities available ATM only with the plugins (i.e. Solder Mask Expander for tracks)

So what is the corresponding python function used by the GUI to fillet tracks?

You can lookup EDIT_TOOL::FilletTracks. It’s not usable in scripting, none of the tools are.

There is a SHAPE_ARC constructor that does fillet calculation that you want

/**
     * Build a SHAPE_ARC which is tangent to two segments and a given radius.
     *
     * @param aSegmentA is the first segment
     * @param aSegmentB is the second segment
     * @param aRadius is the arc radius
     * @param aWidth is the arc line thickness
     */
    SHAPE_ARC( const SEG& aSegmentA, const SEG& aSegmentB, int aRadius, int aWidth = 0 );

Then the code passes that shape to PCB_ARC. Unfortunately neither SHAPE_ARC nor SEG needed to create it that way are exposed in API.

thanks a lot, I will have a look at those to see if I can port the code to python.

This is the meat of it and has a cute diagram too

Be mindful of the license when porting GPL code directly.

1 Like

I will, but in this case I just adapted what I did for kicad 5 to calculate segments and simply got the middle point instead of all the discretized.
Thanks again for support :smiley:

2 Likes