Giter VIP home page Giter VIP logo

pcbmodelgen's Introduction

pcbmodelgen

Convert KiCAD PCB files to models for import in openEMS

Description

You can use this software to convert KiCAD PCB files to Octave function files. Then call from Octave script to import model into openEMS structures. pcbmodelgen also generates Octave function file with model mesh lines. You can use them as is or modify before adding to simulation structure.

Installation

Dependencies

Usage dependencies

  • AppCSXCAD
  • OpenEMS

Build dependencies

  1. TinyXML2 https://github.com/leethomason/tinyxml2 (packages available)
  2. TCLAP http://tclap.sourceforge.net/ (packages available)

Build

Linux

Download package and execute from root:

# Dependencies
sudo apt-get install libtinyxml2-dev libtclap-dev

# Build and install 
mkdir build
cd build
cmake ../
make
sudo make install

Usage

OpenEMS should be installed and the octave paths should be configured in ~/.octaverc as in:

addpath('/usr/share/octave/packages/openems-0.0.35/')
addpath('/usr/local/share/CSXCAD/matlab/')

The tool can be used as in:

# Example
pcbmodelgen -p board.kicad_pcb -c pcbmodelgen.json

# Extra help
pcbmodelgen -h

There are some examples in the example directory. Inside each example folder has a makefile to run the example.

# To run everything just type
make

# To generate the mesh of the Kicad's PCB
make run

# To simulate the generated mesh with openEMS
make sim

# To clean the generated files
make clean

These steps will show model using the AppCSXCAD and after exit from its 3D viewer, the next step is to run the simulation with openEMS and present results.

Windows users:

On Windows 10 it has been tested with the WLS Linux Ubuntu App, downloaded from Microsoft Store. The AppCSXCAD and the openEMS must be available on PATH environment variable so pcbgenmod can find required tools.

PCBModelGen Configuration (.json)

Description of some fields in pcbmodelgen config .json

Field Description
pcb_metal_zero_thick If this is true, then top and bottom copper has zero thickness. This allows for less mesh lines and faster simulation, but you must be careful how mesh lines are positioned otherwise you can get invalid results.
corner_approximation Impacts number of mesh lines and so the simulation time.
insert_automatic_mesh This controls automatic mesh line generation. You don't have to use it, you can generate the lines as needed manually or by some other automation process.
manual_mesh Insert your manual mesh line positions here. They will be inserted before automatic line generation.
min_cell_size, max_cell_size Sets needed cell size boundaries for simulation. This relates to your test signal bandwidth. Make as large as possible for used test signal frequency to minimize mesh line count.
pcb_z_lines will insert this amount of mesh lines between top and bottom copper layers, before performing automatic mesh line generation. This is needed because there is no geometry on the inside of PCB.
smooth_mesh_lines, smth_neighbor_size_diff This tries to make neighboring mesh cells not differ by more than smth_neighbor_size_diff times. This is needed for FTDT simulation. You can't have abrupt change in cell sizes - this can make your simulation invalid.
SimulationBox Sets simulation domain region. You need to place this around your geometry with sufficient distance.
use_box_fill Fill simulation domain with specified material.

One thing to notice regarding mesh line generation. Is is not always successful given needed parameters. If you request large min size and small difference between neighboring cells it will fail to comply and report errors you mentioned. This is also highly dependent on pcb geometry. As each geometry point contributes to mesh lines. This in itself doesn't mean that you can't use the results, but you need to evaluate resulting mesh and decide for yourself if given mesh is good enough for your usage case. You can explore generated kicad_pcb_mesh.m to see all warnings about non conforming mesh sizes.

Also if you are using pcb_metal_zero_thick you need to adjust mesh lines accordingly. For that there is boundary_one_third_rule.

pcbmodelgen's People

Contributors

callistopili avatar ehaag avatar jcyrax avatar julidau avatar leoheck avatar matthuszagh avatar thomhpl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pcbmodelgen's Issues

[SOLVED] can pcbmodelgen recognize primitive non-rectangular boards (Egde.Cuts)?

First of all, thanks for pcbmodelgen!
On Kicad v7.07, pcbmodelgen v0.2.0 correctly recognizes primitive rectangular boards (Egde.Cuts), but it doesn't recognize neither primitive circles nor polygons.

image
Below, we see the three geometries on the KiCad file. But the rectangle is the only one that pcbmodelgen passes ahead to OpenEMS. Am I missing something obvious? The native recognition of circular boards would be very beneficial, as OpenEMS stands out among other solvers for being able to work with polar coordinates. Thanks!

Screenshot from 2023-09-28 08-35-18
Screenshot from 2023-09-28 08-38-06

[SOLVED]
I found a simple solution for that. If you have a circular PCB in KiCad, first convert the primitive circle to a Polygon (Right click > Create from selection > Create Polygon from selection). Then convert the polygon to Lines (Right click > Create from selection > Create Lines from selection). Done. Now pcbmodelgen will recognize any Egde.Cuts.

Running with Kicad-5.1.6 instead of 5.99 (nightly builds)

The kicad_pcb created with Kicad-5.1.6 and the Kicad-5.99 (used on the examples) differs in the definitions.

Kicad-5.99, set layer names with quotation marks while the current version 5.1.6 doen't.

To validate this, I opened the text_pcb example on my Kicad. Then saved the board, then after diffing it, I saw these changes.

I was able to fix the board (by adding these quotation marks on the layer names) as needed for pcbmodelgen since it was fixed using Kicad-5.99 as the reference.

#!/bin/bash

board=$1

# Kicad-5.1.6
# - Layers do not have quotation marks
# - This fix "update" the board after saving it with Kicad-5.1.6

sed -i 's/F.Cu/"F.Cu"/g' $board
sed -i 's/B.Cu/"B.Cu"/g' $board
sed -i 's/F.Adhes/"F.Adhes"/g' $board
sed -i 's/F.Paste/"F.Paste"/g' $board
sed -i 's/F.SilkS/"F.SilkS"/g' $board
sed -i 's/B.Mask/"B.Mask"/g' $board
sed -i 's/F.Mask/"F.Mask"/g' $board
sed -i 's/Dwgs.User/"Dwgs.User"/g' $board
sed -i 's/Cmts.User/"Cmts.User"/g' $board
sed -i 's/Eco1.User/"Eco1.User"/g' $board
sed -i 's/Eco2.User/"Eco2.User"/g' $board
sed -i 's/Edge.Cuts/"Edge.Cuts"/g' $board
sed -i 's/Margin/"s/Margin"/g' $board
sed -i 's/B.CrtYd/"B.CrtYd"/g' $board
sed -i 's/F.CrtYd/"F.CrtYd"/g' $board
sed -i 's/F.Fab/"F.Fab"/g' $board
sed -i 's/Margin/"Margin"/g' board.kicad_pcb

# Extra (maybe not needed but it is also different between versions)
sed -i 's/A4/"A4"/g' board.kicad_pcb
sed -i 's/net_class Default/net_class "Default"/g' board.kicad_pcb

Failing to detect port

Hi,
I am new to Pcbmodelgen and trying to figure out how to run kicad layout rf simulation in pcbmodelgen.

I followed the following instructions properly:

sudo apt-get install libtinyxml2-dev libtclap-dev
mkdir build
cd build
cmake ../
make
sudo make install

and then, put

addpath('/usr/share/octave/packages/openems-0.0.35/')
addpath('/usr/local/share/CSXCAD/matlab/')

in the octaverc file.

After I try to run pcbmodelgen -p pcb.kicad_pcb -c pcbmodelgen.json for the branchline_coupler_example, I can see the 3d view of it and after i close the image, following error occurs:

Segmentation fault (core dumped)
error: load: unable to find file ./tmp/port_ut1
error: called from
ReadUI at line 64 column 9
simulation_script at line 150 column 3

Could you please guide me as to why am I getting this error. Also, I don't see any ports defined in the example. How would pcbmodelgen find out where the ports are? The error also asks about a port file in the temp folder.

Can you please clear my doubts here? Thanks.

Can't use example script

OS: OpenSUSE Tumbleweed
Octave 5.1.0
openEMS 0.0.35
openEMS FDTD startup Using Octave script files error: 'physical_constants' undefined near line 24 column 1 error: called from simulation_script at line 24 column 1 octave:1>

Project clarifications

Hi there, this looks like a very interesting project! πŸ™‚

Just wanted to clarify: is the main purpose of the work to automatically convert KiCAD PCBs to Octave models? My understanding is that this can already be done using the STL import offered by OpenEMS, by e.g. transforming a STEP model of the PCB into STLs for each object.

Or is the tricky part that this repository tackles the meshing itself? I've seen OpenEMS struggle with (manual) mesh generation a bit, so it'd be nice to hear that this project can help in that regard a bit.

Cheers,
Apostolos

Error in parser GetZone parser

When I add a keepout zone, I can set it to Top and Botom layer at the same time.
In that case the line for that zone becomes;
"(zone (net 0) (net_name "") (layers F&B.Cu) (tstamp 0)"
but the parser is looking for the string "layer" and misses the "layer–s–" string and throws an exception
what(): GetZone: no 'layer' field

preliminary workaround make two seperate keepout zones, one for each layer.
but a more permanent solution (in the parser) would be much better :-)

thanks for your work
kind regards
huegene

Running it on Ubuntu 20.04

Hello, I am testing your tool with Ubuntu 20.04.

I managed to install it and all required dependencies.
Maybe I can document this if you are interested in that.

I also had to add 2 paths on the .octaverc so the tools could find the OpenEMS and CSXCAD files as you said in another issue.

Right now I am running this for the first time using the test_pcb

image

I hope to be able to run this for my board.

KiCAD 8 fillets, pads and fills not rendered

KiCAD 8 (that was just released) now has actual arcs for fillets instead of creating lots of small straight track segments.
These arcs seem to be completely missing in the output of pcbmodelgen.
Also pads of components and filled zones are not rendered as well.

image
image

Issues with the commands in generate_model.sh

Hi, I am playing with the pcbmodelgen for the first time. Now I am running the generate_model.sh line by line.

I am seeing these 2 issues.

Warning: kicad_pcb file version not 4. May experience some errorsERROR: mesh gap size too large. Configured max size 1.000000, but actual size 1.322618

Maybe this is ok to ignore since the example uses Kicad 5.99, but I would like to understand what I can do to improve this. I saw that the last commit was trying to improve the tool for the Kicad 5.x with this message, upgraded sw PCB format to work with Kicad 5.x. So I am not sure if it is working for 5.x

ERROR: mesh gap size too large. Configured max size 1.000000, but actual size 1.200192

What I can do fix the error?
Where this GAP is configured?

I don't know where this config is placed. Is this something related to the config.json or the with test_pcb.kicad_pcb board itself?

pcbmodelgen on Kicad 6.0.8 doesn't generate output

I tried the example test_pcb.kicad_pcb and everything pretty much worked.

But when I tried it on my pcb, I got a warning, no errors, and no output:

  pcbmodelgen -p meander.kicad_pcb -c pcbmodelgen.json

Warning: kicad_pcb file version 4.x, may experience some issues
kicad_pcb version (generator pcbnew)

Happy to send the Kicad pcb file, but I can't attach that file type.

Accept pr with clang-format?

Thanks for writing this program!

I'm making some modifications to support kicad 5. I personally use clang-format and was wondering if you'd be open to accepting a pr with formatting changes from a clang-format script. I'll try to make it as similar to the existing style as possible (e.g. 4 spaces indent, etc.). Additionally, are you interested in the kicad 5 pr's when I get those working?

Bad copper infills (kicad 7.0.0)

When using a pcb file from kicad v7.0.0, some copper infills are not generated correctly due to some points being stored as "NaN" in the files generated by pcbmodelgen.

I tracked down the issue to a float issue in the function Zone::getVectAngle. I have made a quick fix and will issue a pull request soon.

Bad output

bad

Good output

good

Full kicad version info

Application: KiCad PCB Editor x64 on x64
Version: (7.0.0), release build
Libraries:
wxWidgets 3.2.1
FreeType 2.12.1
HarfBuzz 5.0.1
FontConfig 2.14.1
libcurl/7.83.1-DEV Schannel zlib/1.2.13
Platform: Windows 11 (build 22621), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
Date: Feb 12 2023 01:35:19
wxWidgets: 3.2.1 (wchar_t,wx containers)
Boost: 1.80.0
OCC: 7.6.2
Curl: 7.83.1-DEV
ngspice: 39
Compiler: Visual C++ 1934 without C++ ABI
Build settings:
KICAD_SPICE=ON

GCC 11: numeric_limits preventing compilation

Attempting to compile freshly cloned source according to the procedure given in the README fails when using GCC 11. See:
compile-time error.

This is expected, re: Porting to GCC 11. Adding #include <limits> to ems.cpp allows compilation to succeed.

I was bored, and wanted an excuse to learn more about Git. So I forked the repo, added the incredibly simple fix above, and will submit a pull request shortly.

Problem with bottom pads

Tried to convert a 2-Layer pcb with 1.6mm thickness and set
"pcb_metal_thickness" : 0.1,
"pcb_metal_zero_thick" : false,
Result: All the pads of the bottom layer are floating below the PCB. While the bottom layer itself is on Position 0 in Z-direction, the position of the Pads is exactly -1.7mm.

error: the type for the property "hole_fill" cannot be found

I am not sure what is this hole_fill do you have any idea what I have to look to fix/understand this property?
I just downloaded one board, that was built with Kicad 4.0 from Kicad demos.
And I am trying to run the simulation using configs (pcbmodelgen.json, simulation_script.m) from the test_pcb example.

Just a little bit of info about my background. I have zero experience with openEMS. I am just planning to play with it to learn something new. And I found that the pcbmodelgen would be a nice starting point.

Check generated mesh file for detailed warning information
octave --silent --persist simulation_script.m
openEMS FDTD startup
Using Octave script files
error: the type for the property "hole_fill" cannot be found
error: called from
    Add2Property at line 12 column 5
    AddLinPoly at line 41 column 5
    kicad_pcb_model at line 5953 column 5
    simulation_script at line 64 column 5
octave:1> 

KiCAD to OpenEMS (PCB export)

Hi Janis,

I have tried to convert .kicad to .json and see the below error.

$ pcbmodelgen -p Meander_2_4G.kicad_pcb -c Meander_2_4G.json
Error from reader: * Line 1, Column 1
Syntax error: value, object or array expected.
terminate called after throwing an instance of 'Json::RuntimeError'
what(): * Line 1, Column 1
Syntax error: value, object or array expected.

Aborted (core dumped)
$

Tools versions:
Kicad - 5.1.9
Ubuntu - 20.04.1 LTS

Thanks,
Mohan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.