How to build a Plugin for cleaning up generated DXF files?

Hi everyone,

I’m currently building a project (keyboard) where there is data from the PCB editor (User layer) that I export to DXF to import it into FreeCAD to generate a mechanical part from this data.

I ran into the problem, that many of the hundreds generated ARCs do not end exactly where the LINEs end. The mismatch is ever so tiny but FreeCAD refuses to accept this as a closed path.
When opening the generated DXF e.g. in LibreCAD and zooming in there are sub-micrometer gaps.

I now created a small Python script that reads the DXF using ezdxf and moves the endpoints of all LINEs to the closest ARC endpoint. (see here DXFcleaner.py · main · André S. / Kicad DXF cleaner · GitLab)

I would like to make this available to other people that may have experienced the same problem by creating a Kicad Plugin.

How can I do this? It needs a file-chooser dialog for input and output file and an input for the applicable tolerance.

Can I do this by using tkinter elements? Or is there a preferred way to do this in Kicad?
How do I make Kicad aware of the plugin?

Or should I file this as a bug and ask the developers to implement this in the DXF export dialog?

I would raise this as an issue.
If the DXF is invalid, that is a bug

1 Like

Re: Plugin’s
• Many posts on Plugin’s topic. Here’s a link to my video on making one and, be sure to click the ‘Show more’ to access the Link to official plugin info to learn what you need to know about Kicad Plugins

Re: DXF
• There are differences in the Codes that handle DXF so, the programs you use (LibreCAD, FreeCAD…etc) may do different management things.
I recommend you explore and test settings in FreeCAD/Libre and Kicad - then you’ll know how to Fish, as opposed to just eating Fish someone gave you. And, there are many posts on DXF’s and Kicad…

• Kicad plugins can use various GUI’s and success depends on what, and how, you do it and I’ve gotten many of them to work in Kicad plugins

• For the GUI Windows, Dialogs/Panels in the plugin’s, you can use basically what you want if you create full GUI code for them. The Kicad default is ‘wx’.

If I need only Message/Info, I use wx (see the Link above).
If I need Panels/Windows, I generally prefer to use wxFormBuilder to make them and create a Stand-Alone App using PyInstaller and call/run the App from the Plugin.

If you look at this link, you’ll see how to get Stand-Alone App’s to run for different OP SYS’s

Some Examples of Running different things:

1 Like

Hi @BlackCoffee,

thanks for your input, I will check this out.

However, since I could also read the generated DXF with ezdxf in Python and could calculate the difference between endpoints it looks definitely that Kicad does not produce valid output. Maybe it has something to do with rounding errors and how arcs are calculated.

Because after running the script over the DXF and using this as input for FreeCAD I was able to produce the expected result.

Therefore, I opened a bug report with Kicad to get this rectified.

I think it has far more to do with your Preference’s settings. I use the word ‘think’ but, the fact is that with the proper settings in Kicad and FreeCAD, you won’t need ‘ezdxf’ but, naturally, use whatever you like and works for you…

Follow-Up:

Many users like to use the KSU-StepUP plugin. It makes it easy to load Kicad PCB’s and get the correct board shape without fussing… And, naturally, can load the other Layers with geometries… Example below…

The PCB as-is in Kicad Note: I show I created some DXF’s but not using them in this video…

Loading it into FreeCAD via StepUp workbench

1 Like

The thing is: not all connections are bad. But if you have hundreds of them, some seem not to fit. And then try to find and fix them all by hand is impossible.

It seems to depend on slope of the line and start/end angle of the arc and the position relative to the underlying grid.

Arcs with 0°, 90° steps seem not to be affected too much even if the ends do not overlap exactly in the generated DXF.

See the check result from FreeCAD.

Since all these shapes come from the same footprint I would expect all of them to be fine or all be bad if it was caused by the footprint.

As for the settings: I could not find any setting in Kicad export oder FreeCAD import that provides the feature to align endpoints of elements.

If you have deeper insight, which settings could provide better results I’m all open for that.

Because we use StepUP a lot, dxf export is maybe under-tested

1 Like

@davidsrsb: I just checked KiCadStepUp but I could not find how to import one of the User.x layers.

It only provides import for some layers which are not even available any more in Kicad (renamed to User.*).

However, KicadStepUp seems to do on import exactly what I did with my script. They even choose the same tolerance:
grafik

Therefore, it may be that the underlying problem was seen by someone but not fixed in Kicad.

Please post your DXF file so I/we can check it out more thoroughly…

EDIT: Also post your Kicad file(s)

The opened gitlab issue: DXF export creates invalid paths (#15056) · Issues · KiCad / KiCad Source Code / kicad · GitLab

If the lines+arcs are converted to a polygone (pcbnew–>context-menu–>create polygon from selection) than we get a closed path in the dxf. original + workaround (dxf included in project archive):
dxf_unconnected_complete_project.zip (5.9 KB)

In import sketch you should be allowed to

it should simply work in kv5,6,7,8

It doesn’t matter if using StepUp or Not using StepUp, it’s all about the Pref’s settings in both Kicad and FreeCAD and understanding what setting’s to use/change for specific Output’s

FYI - I’m using Kicad 6.0.11, on Mac and FreeCAD 20.2

Video below shows various angles of the Boxes with Radius’d corners. Zero problems importing into FreeCAD and this time, I did Not use StepUp.

No DRC Errors or Warnings

Finishing-up my input… No problem with Arc’s of any Angle…

Imported into FreeCAD Not using StepUp

@AnSc : second workaround:
With QCAD (the original project, from which librecad branched, so maybe this works also with libracad):

  • import the resulting dxf
  • select all single items
  • left toolbar → polyline tools → create polyline from selection

This command seems also to close small gaps between lines/arcs

@AnSc
I’ve replied to your issue @kicadStepUp in GH

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.