Placing a component using KiCad's Python API - python

I have a problem with KiCad's python API (5.1), I cannot find a way to add a component onto the PCB.
I found some examples but they no longer work with the version of KiCad I am using. The next step is going through the C++ code to understand how a component is placed on the board, but before that, I thought was worth asking for help here.

Adding a component is quite easy, just instantiate a module and place it on the board.
m = pcbnew.FootprintLoad("D:/path/to/lib.pretty","footprint_name")
board.Add(m)
But be aware that you cannot add the same module multiple times, you have to create multiple modules. I found this out with hours of debugging.
footprint = pcbnew.FootprintLoad("/usr/share/kicad/modules/MountingHole.pretty", "MountingHole_3.2mm_M3")
board.Add(pcbnew.MODULE(footprint))
board.Add(pcbnew.MODULE(footprint))
board.Save("board.kicad_pcb")

Related

Is it possible to execute indented lines from another file in Python?

Say, I have two files demo.py
# demo.py
from pathlib import Path
for i in range(5):
exec(Path('another_file.txt').read_text())
and another_file.txt (note the indent)
print(i)
Is it possible to make python demo.py run?
N.B. This is useful when using Page (or wxformbuilder or pyqt's designer) to generate a GUI layout where skeletons of callback functioins are automatically generated. The skeletons will have to be modified, at the same time, each iteration overwrites the skeletons -- code snippets will have to be copied back. Anyway, you know what I am talking about if you have used any of Page or wxformbuilder or pyqt's designer.
You can solve the basic problem by removing the indent:
from pathlib import Path
import textwrap
for i in range(5):
exec(textwrap.dedent(Path('another_file.txt').read_text()))
There are still two rather major problems with this:
There are serious security implications here. You're running code without including it in your project. The idea that you can "worry about security and other issues later" will cause you pain at a later point. You'll see similar advice on this site with avoiding SQL injection. That later date may never come, and even if it does, there's a very real chance you won't remember or correctly identify all of the problems. It's absolutely better to avoid the problems in the first place.
Also, with dynamic code like this, you run the very real risk of running into a syntax error with a call stack that shows you nothing about where the code is from. It's not bad for simple cases like this, but as you add more and more complexity to a project like this, you'll likely find you're adding more support to help you debug issues you run into, rather than spending your time adding features.
And, combining these two problems can be fun. It's contrived, but if you changed the for loop to a while loop like this:
i = 0
while i < 5:
exec(textwrap.dedent(Path('another_file.txt').read_text()))
i += 1
And then modified the text file to be this:
print(i)
i += 1
It's trivial to understand why it's no longer operating the 5 times you expect, but as both "sides" of this project get more complex, figuring out the complex interplay between the elements will get much harder.
In short, don't use eval. Future you will thank past you for making your life easier.

Keyboard Control Python

I want python to click Enter. However, I don't want to install any outside extensions. I want it to click enter without using them. I found many resources like win32 but I want to do it without using external resources. I don't mind which version of python it is on.
Is this possible?
If so, how?
I have looked on the web but couldn't find anything. Please, can someone help?
Thank you in advance.
Yes.
It should definitely be possible using the ctypes library to talk directly to the Windows dlls
If you don't want to install a helper module that has done all this for you, then you pretty much have to do it old school by going after the really low level code.
In short, call the SendInput function of user32.dll by using ctypes.windll.user32.SendInput. Good luck with getting the parameters correct - I don't have the patience to figure it out for you.
Here are the docs for the user32.dll SendInput API
Here is a helpful resource for figuring out the data types. This is actually part of ctypes and can be imported by import ctypes.wintypes but I find it instructional to read the actual code.
So for example, to create a WORD with the value of the Virtual Key Code VK_RETURN, I think it would be
>>> ctypes.wintypes.WORD(0x0D)
c_ushort(13)
But there is a lot more you have to piece together from there. Just build up your parameters and call the function.
Last hint, use the examples in the second link for how to build "C" type structures. Then build one yourself using ctypes.Structure

Python: Create a polyhedron and get its section

I want to create a polyhedron by giving its coordinates, and then get a specific section. Maybe something like:
Points = [A(0,0,0),B(1,0,0),C(1,1,0),D(0,1,0),A"(0,0,1),B"(1,0,1),C"(1,1,1),D"(0,1,1)]
Body = BodyGenerate(Points)
section1 = Body.section(z = 0.5)
section2 = Body.section(x+y= 1)
#And then I can get properties like `area`, `width` of these sections.
How can I do that? Is there any lib for that?
Here is what I find so far:
Vpython is pretty what I need, but it seems too basic, and cannot create a polyhedron
Blender and Rhino are good tools. But they focus more on modelling rather than programming. Python is more like a interactive commandline rather than a program itself.
Three.js seems what I need, but js cannot perform scientific computation.
As a blender user my opinion is biased that way. Yes it focuses on being a gui tool for end users, but it deeply integrates python for defining the user interface all the way to adding features through python addons. It also includes a python console that lets you type in commands that alter data directly, allowing you to see changes in the 3dview. It is also possible to add extra python modules that can then be used within blender.
Blender's python api can be read and searched online.
You should find some examples to help create a polyhedron in the extra objects addon. It is included with blender so you can also find the scripts in your blender install.
Maybe look into how the intersect tool works, you can call the intersect operator yourself, there is also a boolean modifier that may be better to get a closed mesh to calculate the volume.
For calculating the volume there is BMesh.calc_volume, it is used by the 3d printing toolbox.
You might want to add blender.stackexchange.com to your list to get blender specific python help.

PJSUA --null-audio for python bindings

How does one specify --null-audio option working with python API (http://trac.pjsip.org/repos/wiki/Python_SIP_Tutorial)?
Thanks.
Somehow you're wasting hours of time searching and then, desperate, you post a question to Stackoverflow, and the exact moment after it you find an answer by yourself. This place is the most useful problem solver ever.
After you've created your lib, call lib.set_null_snd_dev(). That must be done after making lib.init (here's my example:)
lib = pj.Lib()
lib.init(log_cfg=pj.LogConfig(level=3, callback=log_cb))
lib.set_null_snd_dev()

Help with PyEPL logging

I have never used Python before, most of my programming has been in MATLAB and Unix. However, recently I have been given a new assignment that involves fixing an old PyEPL program written by a former employee (I've tried contacting him directly but he won't respond to my e-mails). I know essentially nothing about Python, and though I am picking it up, I thought I'd just quickly ask for some advice here.
Anyway, there are two issues at hand here, really. The first is this segment of the code:
exp = Experiment()
exp.setBreak()
vt = VideoTrack("video")
at = AudioTrack("audio")
kt = KeyTrack("key")
log = LogTrack("session")
clk = PresentationClock()
I understand what this is doing; it is creating a series of tracking files in the directory after the program is run. However, I have searched a bunch of online tutorials and can't find a reference to any of these commands in them. Maybe I'm not searching the right places or something, but I cannot find ANYTHING about this.
What I need to do is modify the
log = LogTrack("session")
segment of the code, so that all of the session.log files go into a new directory, separate from the other log files. But I also need to find a way to not only concatenate them into a single session.log file, but add a new column to that file that will add the subject number (the program is meant to be run by multiple subjects to collect data).
I am not asking anyone to do my work for me, but if anyone could give me some pointers, or any sort of advice, I would greatly appreciate it.
Thanks
I would first check if there is a line in the code
from some_module_name import *
This could easily explain why you can call these functions (classes?). It will also tell you what file to look in to modify the code for LogTrack.
Edit:
So, a little digging seems to find that LogTrack is part of PyEPL's textlog module. These other classes are from other modules. Somewhere in this person's code should be a line something like:
from PyEPL.display import VideoTrack
from PyEPL.sound import AudioTrack
from PyEPL.textlog import LogTrack
...
This means that these are classes specific to PyEPL. There are a few ways you could go about modifying how they work. You can modify the source of the LogTrack class so that it operates differently. Perhaps easier would be to simply subclass LogTrack and change some of its methods.
Either of these will require a fairly thorough understanding of how this class operates.
In any case, I would download the source from here, open up the code/textlog.py file, and start reading how LogTrack works.

Categories

Resources