I’m totally new in the field of scripting and I’m trying to draw a rectangular shape for boards.
What I need is a script. It will take as arguments the length and the width of the rectangular shape and draw this shape on the board automatically. Is that possible in your opinion ?
Take a look at the KiCommand code and the draw_segment function:
def draw_segment(x1,y1,x2,y2,layer=pcbnew.Dwgs_User,thickness=0.15*pcbnew.IU_PER_MM):
"""Draws the line segment indicated by the x,y values
on the given layer and with the given thickness."""
board = pcbnew.GetBoard()
ds=pcbnew.DRAWSEGMENT(board)
board.Add(ds)
ds.SetStart(pcbnew.wxPoint(x1,y1))
ds.SetEnd(pcbnew.wxPoint(x2,y2))
ds.SetLayer(layer)
ds.SetWidth(max(1,int(thickness)))
return ds
You can use functions in wxpointutil.py to help manipulate points if you need to.
A lot of functions in KiCommand deal with a variety of input types like strings, comma separated strings, lists and lists of lists: turning all of those to integers, so don’t get confused by that while looking at that code. There’s lots of useful functions in there.
If you’re running the nightly, you might consider installing KiCommand and using that to draw segments. If you’re trying to learn how to make significant scripted functionality, straight Python is the way to go.
I found your Gihub this morning and tried these lines of code. However, the segment didn’t want to appear.
But, by luck, when I zoomed on the board, a found a little round circle. And its parameters showed me that the coordinates entered in the python code were in nanometers and not in millimeters. And so, I need to multiplicate all coordinates by one million to have the correct segment lenght. Is that normal or did I miss something?
You’re exactly right! See the default value of thickness, I use pcbnew.IU_PER_MM (there’s also MIL). IU refers to "Internal Units’.
Also, width and height (aka EDA_RECT) can be converted to points using one of the convenience functions in KiCommand. Then you can call draw_segment() with those points. Look for functions dealing with EDA_RECT or BoundingBox (which is a function on most object that return the orthogonal EDA_RECT surrounding the object.
There are other functions in KiCommand that convert an object into a “tight rectangle” with appropriate orientation (often non orthogonal).
Good luck, and ask any questions you need. I’ll be unavailable for a week or so beginning tomorrow.