How to show wx dialog message in KiCAD pcbnew plugin

When using KiCAD’s Python API for pcbnew, you can show a dialog by using the following snippet

# Show info dialog
dlg = wx.MessageDialog(None, "Please select one or multiple footprints!\n...or use Ctrl+A to select everything.", "No footprints selected", wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()

Note that you need to

import wx

at the top of your plugin.

This code will show the following dialog:

 

Complete plugin example:

#!/usr/bin/env python
import pcbnew
import wx

class DialogExamplePlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Show dialog example"
        self.category = "A descriptive category name"
        self.description = "A description of the plugin and what it does"
        self.show_toolbar_button = False # Optional, defaults to False

    def Run(self):
        dlg = wx.MessageDialog(None, "Please select one or multiple footprints!\n...or use Ctrl+A to select everything.", "No footprints selected", wx.OK | wx.ICON_ERROR)
        dlg.ShowModal()
        dlg.Destroy()

DialogExamplePlugin().register() # Instantiate and register to Pcbnew

You can place this plugin, for example, in

~/.local/share/kicad/7.0/scripting/plugins/DialogExamplePlugin.py

Don’t forget to refresh the plugins from the pcbnew menu.

Posted by Uli Köhler in KiCAD, Python

Where to place custom KiCAD plugins on Linux?

In order to create a custom simple KiCAD plugin, save it as NAME.py in the following folder (where NAME is any filename such as MyTestPlugin):

~/.local/share/kicad/7.0/scripting/plugins

For example, you could place a plugin in

~/.local/share/kicad/7.0/scripting/plugins/MyPlugin.py

or in

~/.local/share/kicad/7.0/scripting/plugins/AddMyStuff.py

Note that you don’t need to restat pcbnew to load the plugins, you can just reload all plugins from the menu!

Posted by Uli Köhler in KiCAD

How to read KiCAD pick&place position file using pandas in Python

If you’ve exported a KiCAD pick & place position file using the GUI or the command line:

kicad-cli pcb export pos MyPCB.kicad_pcb --units mm -o MyPCB.pos

you can read it from within your Python script using pandas.read_table(...) like this:

import pandas as pd

pos = pd.read_table('KKS-Microcontroller-Board-R2.2.pos', delim_whitespace=True, names=["Ref", "Val", "Package", "PosX", "PosY", "Rot","Side"], comment="#")

Optionally, you can also index pos by the Ref column (which contains values such as C12D1 or U5):

pos.set_index("Ref", inplace=True)

You can also pack all that into a function:

def read_kicad_pos_file(filename):
    pos = pd.read_table(filename, delim_whitespace=True, names=["Ref", "Val", "Package", "PosX", "PosY", "Rot","Side"], comment="#")
    pos.set_index("Ref", inplace=True)
    return pos

If you’ve used .set_index(), you can access a component such as C13 using

pos.loc["C13"]

Example output:

Val                100nF_25V
Package    C_0603_1608Metric
PosX                187.1472
PosY               -101.8243
Rot                    180.0
Side                     top
Name: C1, dtype: object

 

Posted by Uli Köhler in KiCAD, pandas, Python

How to export KiCAD pick&place position file using kicad-cli

Since KiCAD 7.0, you can use the kicad-cli command line tool to export a pick & place position TSV file from a KiCAD project.

First, find out what the filename of the PCB file is. Typically, if your project is named MyPCB.kicad_pro, your main schematic file is named MyPCB.kicad_pcb.

Now you can export the position file using

kicad-cli pcb export pos MyPCB.kicad_pcb --units mm

This will output, for example

Loading board

If the PCB file is named MyPCB.kicad_pcb, the position file will be called MyPCB.pos.

You can also set a custom output file name such as out.pos using the -o option.

kicad-cli pcb export pos MyPCB.kicad_pcb -o out.pos --units mm

 

 

Posted by Uli Köhler in KiCAD

How to export KiCAD schematic PDF using kicad-cli

Since KiCAD 7.0, you can use the kicad-cli command line tool to export a colored schematic PDF from a KiCAD project.

First, find out what the filename of the main schematic file is. Typically, if your project is named MyPCB.kicad_pro, your main schematic file is named MyPCB.kicad_sch.

Now you can export the schematic PDF using

kicad-cli sch export pdf MyPCB.kicad_sch

This will output, for example

Plotted to '/home/uli/MyPCB/MyPCB.pdf'.

Done.

The PDF file from the output contains your schematic PDF.

Posted by Uli Köhler in KiCAD

How to iterate JSON array in C++ using Boost.JSON

You can use a standard for iteration loon on j.as_array() in order to iterate all values in the given JSON array:

for (auto& value : json.as_array()) {
    std::cout << value.as_int64() << std::endl;
}

Full example:

#include <boost/json.hpp>
#include <iostream>

int main()
{
    const char* json_str = R"([1, 2, 3, 4, 5])";
    boost::json::value j = boost::json::parse(json_str);

    if (j.is_array()) {
        for (auto& value : j.as_array()) {
            std::cout << value.as_int64() << std::endl;
        }
    }

    return 0;
}

Compile using

g++ -o json json.cpp -lboost_json

This will print

1
2
3
4
5

 

Posted by Uli Köhler in Boost, C/C++

How to copy boost::urls::url in C++ (Boost.Url)

You can copy a boost::urls::url by using it copy constructor:

boost::urls::url url1 = boost::urls::url::parse("https://example.com/path1");
boost::urls::url url2(url1); // Copy url1 to url2 using the copy constructor

 

Posted by Uli Köhler in Boost, C/C++

How to access JSON nested value in C++ using Boost.JSON

#include <boost/json.hpp>

boost::json::value j = /* ... */;
// Access nested value with key "value"
j.at("value").as_string();

Full example:

#include <boost/json.hpp>
#include <iostream>

int main() {
    // Hardcoded JSON string
    const std::string json_str = R"(
        {
            "key": "1234",
            "value": "Hello, world!"
        }
    )";

    // Parse the JSON string
    boost::json::value j = boost::json::parse(json_str);

    // Access the "value" property and print it to stdout
    std::string value = j.at("value").as_string().c_str();
    std::cout << value << std::endl; // Prints "Hello, world!"

    return 0;
}

Compile using

g++ -o json json.cpp -lboost_json
Posted by Uli Köhler in Boost, C/C++

How to parse JSON from std::string in C++ using Boost.JSON

#include <boost/json.hpp>

boost::json::value j = boost::json::parse(json_str);

Full example:

#include <boost/json.hpp>
#include <iostream>

int main() {
    // Hardcoded JSON string
    const std::string json_str = R"(
        {
            "key": "1234",
            "value": "Hello, world!"
        }
    )";

    // Parse the JSON string
    boost::json::value j = boost::json::parse(json_str);

    // Access the "value" property and print it to stdout
    std::string value = j.at("value").as_string().c_str();
    std::cout << value << std::endl; // Prints "Hello, world!"

    return 0;
}

Compile using

g++ -o json json.cpp -lboost_json
Posted by Uli Köhler in Boost, C/C++

How to encode URL query parameters in C++ using Boost.URL

#include <iostream>
#include <boost/url.hpp>

using namespace boost::urls;

int main() {
    std::string username = "myusername";
    std::string password = "mypassword";
    url_view base_url = "https://example.com/api/login";

    url encoded_url(base_url);
    encoded_url.params().set("user name", username); // With space to test proper encoding
    encoded_url.params().set("password", password);

    // Prints "https://example.com/api/login?username=my%20user%20name&password=mypassword"
    std::cout << "Encoded URL: " << encoded_url << std::endl;

    return 0;
}

Compile using

g++ -o urlhost urlhost.cpp -lboost_url

 

Posted by Uli Köhler in Boost, C/C++

How to parse host from URL in C++ using Boost::URL

If you have an URL such as:

std::string myURL = "https://subdomain.example.com/api/test";

you can parse the hostname from it using Boost::URL (you need at least Boost 1.81.0, previous versions of boost don’t have Boost.URL) using

boost::urls::url_view url(myURL);
std::string host = url.host();

std::cout << host << std::endl; // Prints "subdomain.example.com"

Full example:

#include <string>
#include <iostream>

#include <boost/url.hpp>

int main() {
    std::string myURL = "https://subdomain.example.com/api/test";

    boost::urls::url_view url(myURL);
    std::string host = url.host();

    std::cout << host << std::endl; // Prints "subdomain.example.com"
}

Compile using

g++ -o urlhost urlhost.cpp -lboost_url

 

Posted by Uli Köhler in Boost, C/C++

How to fix GCC fatal error: parquet/arrow/writer.h: No such file or directory

Problem:

While compiling your C++ application, you see an error message like

src/main.cpp:6:10: fatal error: parquet/arrow/writer.h: No such file or directory
    6 | #include <parquet/arrow/writer.h>

Solution:

You need to install the Parquet C++ libraries which are shipped together with the Arrow libraries.

On Ubuntu, you can do that using

sudo apt update
sudo apt install -y -V ca-certificates lsb-release wget
wget https://apache.jfrog.io/artifactory/arrow/$(lsb_release --id --short | tr 'A-Z' 'a-z')/apache-arrow-apt-source-latest-$(lsb_release --codename --short).deb
sudo apt install -y -V ./apache-arrow-apt-source-latest-$(lsb_release --codename --short).deb
sudo apt update -y
sudo apt -y install libparquet-dev

For other operating systems, see the official Arrow installation guide,

Posted by Uli Köhler in C/C++, GCC errors

How to fix GCC fatal error: arrow/api.h: No such file or directory on Ubuntu

Problem:

While compiling your C++ application, you see an error message like

src/main.cpp:3:10: fatal error: arrow/api.h: No such file or directory
    3 | #include <arrow/api.h>

Solution:

You need to install the Arrow C++ libraries.

On Ubuntu, you can do that using

sudo apt update
sudo apt install -y -V ca-certificates lsb-release wget
wget https://apache.jfrog.io/artifactory/arrow/$(lsb_release --id --short | tr 'A-Z' 'a-z')/apache-arrow-apt-source-latest-$(lsb_release --codename --short).deb
sudo apt install -y -V ./apache-arrow-apt-source-latest-$(lsb_release --codename --short).deb
sudo apt update -y
sudo apt -y install libarrow-dev

For other operating systems, see the official Arrow installation guide,

Posted by Uli Köhler in C/C++, GCC errors

Where to find cheap PCB-mount SMA connectors?

As for most parts, if you order more than just a few pcs, LCSC is the cheapest source of these components (however, you need to consider the rather expensive shipping costs).

Here’s some SMA connectors I’ve used:

Note that I don’t use them in typical RF applications, so I don’t have any information on impedance matching or accuracy etc.

 

Posted by Uli Köhler in Components, Electronics

What current rating do JST VH connectors have?

JST VH connectors such as the 6-pin B6P-VH male header have a current rating of 10 A per pin.

Source: JST VH datasheet

Posted by Uli Köhler in Electronics

What pin pitch do JST VH connectors have?

JST VH connectors such as the 6-pin B6P-VH male header have a pin pitch of 3.96mm.

Source: JST VH datasheet

Posted by Uli Köhler in Electronics

What is the correct tariff code/HTSN for MELF resistors?

For SMD MELF resistors with less than 20 Watts rated power, the correct tariff code is

8533.21.0040

with the following description:

Fixed Resistors, Power Handling Capacity Not Exceeding 20w,smd, Having Two Terminals, Cylindrical Leadless

Source: DigiKey shipment with such MELF resistors to me, confirmed by this Avnet page

 

Posted by Uli Köhler in Electronics

How to install VirtualBox extension pack (.vbox-extpack) on the command line (headless)

Use the following command to install a VirtualBox extension pack on a server or other headless machine:

sudo vboxmanage extpack install [filename]

for example

sudo vboxmanage extpack install Oracle_VM_VirtualBox_Extension_Pack-7.0.8.vbox-extpack

 

 

Posted by Uli Köhler in Virtualization
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPTPrivacy &amp; Cookies Policy