uPesy / easyeda2kicad.py

Convert any LCSC components (including EasyEDA) to KiCad library
GNU Affero General Public License v3.0
705 stars 69 forks source link

Adding Symbol fails #87

Open danielh186 opened 1 year ago

danielh186 commented 1 year ago

After editing a symbol with the Kicad Symbol Editor the script can't add symbols to the library anymore. There is no error shown by the script but KiCAD can't find the symbol inside the library. When I try to add the symbol a second time, an error is thrown ("This id is already in ...").

My workaround for now is just running the script on a symbol library that I don't edit and then importing the symbol into my real library using the Symbol Editor. But I would prefer not having to do this extra step.

The error can be replicated as follows:

  1. Create a clean symbol library
  2. Add components to the library with the script (works just fine)
  3. Edit any component in the symbol library using the KiCAD Symbol Editor (for example change Pin Type from "Unspecified" to "Input"
  4. Try to add another component using the script
sogaard commented 1 year ago

I noticed the same problem. I looked closer at the library file, and without fully knowing the sym syntax, it looks like a parenthesis mismatch issue. The new symbol was inserted outside the closing parenthesis. When I manually deleted the additional closing parenthesis in the middle, it sees the new parts.

dgbalharrie commented 11 months ago

I must have edited a symbol with the kicad editor because new LCSC components were not appearing in the symbol library. Afer reading this issue, I opened the easyedatokicad sym file in a text editor and found an extra parenthesis down the file. image

Once I removed this parentheis ( the one in red) all the components in the file beyond the removed bracket appeared in the library list. If you use NotePad++ and select the parenthesis at the top of the file, you will find the problem parenthesis highlighted in red. You will need to do this after editing a symbol in the Kicad symbol editor, otherwise newer symbols will not appear.

binary1230 commented 9 months ago

I ran into the same issue just now too with importing C53406

I had a library with 3 symbols I imported successfully with easyeda2kicad

When I imported the 4th, it inserted an extra paren in the middle of the file (like what was shown before).

The command I was using was: easyeda2kicad --lcsc_id=C53406 --project-relative --full --output .\LCSC-imported.kicad_sym --overwrite

When I manually removed the extra paren (as highlighted in red by a code editor), that fixed it right up.


I'll try and build a more concise repro if that's useful. I hit this a lot and this is the first time I happened to track down what happened.

I wonder if an easy thing to do to track this down is just to have easyeda2kicad validate the file format after the import but before it writes it to the disk, and throw an error if the validation fails.

Then you could get a bug report with the before and after.

If I get a minute I'll try and look into it a little if there's not a known fix.

This was happening on v0.6.3, and still happening when I upgraded to latest (0.6.6)


(btw THANK YOU for this amazing tool, it's such a timesaver)

binary1230 commented 9 months ago

tried to repro and couldn't get it to happen again.

I've checked my project into git and if I run into this again I'll try and see what changed in the diff.

The only other clue is possibly something I was doing where:

  1. importing a few symbols using easyeda2kicad
  2. manually editing those symbols in Kicad (to clean them up etc)
  3. importing additional symbols using easyeda2kicad

Also I'll note that one of my symbols has an open an close paren in the symbol name. It seems to be quoted fine so, might be just a red herring. image

binary1230 commented 9 months ago

Oh yea and,

When this happened, I had 4 symbols in my .kicad_sym file. However, the position of the bad paren was in the middle of the file after the 1st symbol.

Kicad appeared to be happy reading that first symbol but ignored all the other symbols after that point. (I guess when the final paren is closed in the file, it just ignores all the other stuff after that point. I mean.. what else can it do I guess?)

binary1230 commented 9 months ago

I THINK I FOUND IT and have good repro. It's related to the file ending in a newline or not and LF vs CRLF.

Repro:

  1. Make a file named '.\LCSC-imported.kicad_sym' and ensure:
    • it is CRLF (windows format)
    • with the following contents (Important: BE SURE IT includes the BLANK NEWLINE AT THE END)
    • (this is just a minimal example enough to repro the bug)
(kicad_symbol_lib (version 20220914) (generator kicad_symbol_editor)
  (symbol "TXB0108PWR" (in_bom yes) (on_board yes)
    (symbol "TXB0108PWR_1_1"
      (text "Low\nVoltage\nSide" (at -2.54 12.7 0)
        (effects (font (size 0.4 0.4)))
      )
    )
  )
)

line 10 should be a newline image

  1. run the following command: easyeda2kicad --lcsc_id=C2987682 --project-relative --output .\LCSC-imported.kicad_sym --symbol

EXPECTED: new symbol is imported and library works

ACTUAL: new symbol is imported after the closing parenthesis, and the import silently fails. KiCad will never recognize any imports from this file ever again.


Probably the key to the whole thing is, if you remove the newline so the last character is a parenthesis, everything works.

To repro that:

  1. take the same file but this time remove the newline at the end:
    (kicad_symbol_lib (version 20220914) (generator kicad_symbol_editor)
    (symbol "TXB0108PWR" (in_bom yes) (on_board yes)
    (symbol "TXB0108PWR_1_1"
      (text "Low\nVoltage\nSide" (at -2.54 12.7 0)
        (effects (font (size 0.4 0.4)))
      )
    )
    )
    )

note that there's no newline at the end this time: image

  1. run the same easyeda2kicad command above

EXPECTED and ACTUAL: imports successfully.


finally, the repro above does NOT trigger the bug IF the file is Unix linendings (LF)

I see the code is doing a bunch of matching of "\n" directly, maybe have to convert to python's lineseparator

I hope that helps. I'll try and look into it more if I have time tomorrow. Thanks!

channelsixlabs commented 3 months ago

I THINK I FOUND IT and have good repro. It's related to the file ending in a newline or not and LF vs CRLF.

Good work. Tracking this down in my symbols library has convinced the import script to begin working again.

Hopefully this can be addressed so it can't happen again. Perhaps some post-import step in the script?

binary1230 commented 3 months ago

I think there's probaby a silly but probably effective way to fix this which is, The first thing the script should do when reading the existing symbol file is convert all windows line endings to Unix. It seems like this would be a good thing to do regardless of whatever platform the script is actually running on to ensure line ending consistency

Then I bet everything else would work just fine. It is likely because the python script is doing matching on \n and not also \r\n that we are running into this.

The only caveat would be if this would break anything because it changes fields inside quoted strings and stuff like that. So at the end of the day this might have to be done at a more structured level that is aware of how things are parsed in KiCads symbol library format.

I suspect it would be good enough though and maybe including a command line to disable the automatic reformatting from Windows to Unix file and things would probably be sufficient if anybody runs into weird edge cases.

uPesy commented 3 months ago

@binary1230 A PR to fix this issue will be very apreciated.

binary1230 commented 3 months ago

No prob, I'll probably be doing some more electronics design work in the near future and will try and knock it out as part of that

On Mon, Jun 17, 2024, 2:35 PM uPesy Electronics @.***> wrote:

@binary1230 https://github.com/binary1230 A PR to fix this issue will be very apreciated.

— Reply to this email directly, view it on GitHub https://github.com/uPesy/easyeda2kicad.py/issues/87#issuecomment-2174144691, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJJRSHS4NMUO6GJS2KLYSLZH4T6PAVCNFSM6AAAAABJOJACT6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNZUGE2DINRZGE . You are receiving this because you were mentioned.Message ID: @.***>