Skip to content

Commit 3ffe1e4

Browse files
authored
Merge pull request #48 from uPesy/dev
Add project relative path for 3D model in footprint lib file + handle EasyEDA API 404 response
2 parents 6e0cbba + d949556 commit 3ffe1e4

File tree

9 files changed

+61
-9
lines changed

9 files changed

+61
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ scripts/*
44
.vscode/*
55
*.sqlite3
66
TODO
7+
easyeda2kicad_config.json
78

89
# Byte-compiled / optimized / DLL files
910
__pycache__/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# easyeda2kicad v0.3.7
1+
# easyeda2kicad v0.4.0
22

33
_________________
44
[![PyPI version](https://badge.fury.io/py/easyeda2kicad.svg)](https://badge.fury.io/py/easyeda2kicad)

easyeda2kicad/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.3.7"
1+
__version__ = "0.4.0"
22
__author__ = "uPesy"
33
__email__ = "[email protected]"

easyeda2kicad/__main__.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from easyeda2kicad.easyeda.parameters_easyeda import EeSymbol
1818
from easyeda2kicad.helpers import (
1919
add_component_in_symbol_lib_file,
20+
get_local_config,
2021
id_already_in_symbol_lib,
2122
set_logger,
2223
update_component_in_symbol_lib_file,
@@ -66,7 +67,7 @@ def get_parser() -> argparse.ArgumentParser:
6667
parser.add_argument(
6768
"--output",
6869
required=False,
69-
metavar="file.lib",
70+
metavar="file.kicad_sym",
7071
help="Output file",
7172
type=str,
7273
)
@@ -88,6 +89,13 @@ def get_parser() -> argparse.ArgumentParser:
8889
action="store_true",
8990
)
9091

92+
parser.add_argument(
93+
"--project-relative",
94+
required=False,
95+
help="Sets the 3D file path stored relative to the project",
96+
action="store_true",
97+
)
98+
9199
return parser
92100

93101

@@ -111,6 +119,16 @@ def valid_arguments(arguments: dict) -> bool:
111119
kicad_version = KicadVersion.v5 if arguments.get("v5") else KicadVersion.v6
112120
arguments["kicad_version"] = kicad_version
113121

122+
if arguments["project_relative"] and not arguments["output"]:
123+
logging.error(
124+
"A project specific library path should be given with --output option when"
125+
" using --project-relative option\nFor example: easyeda2kicad"
126+
" --lcsc_id=C2040 --full"
127+
" --output=C:/Users/your_username/Documents/Kicad/6.0/projects/my_project"
128+
" --project-relative"
129+
)
130+
return False
131+
114132
if arguments["output"]:
115133
base_folder = "/".join(arguments["output"].replace("\\", "/").split("/")[:-1])
116134
lib_name = (
@@ -122,7 +140,7 @@ def valid_arguments(arguments: dict) -> bool:
122140
)
123141

124142
if not os.path.isdir(base_folder):
125-
logging.error("Can't find the folder")
143+
logging.error(f"Can't find the folder : {base_folder}")
126144
return False
127145
else:
128146
default_folder = os.path.join(
@@ -208,6 +226,8 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
208226
if not valid_arguments(arguments=arguments):
209227
return 1
210228

229+
# conf = get_local_config()
230+
211231
component_id = arguments["lcsc_id"]
212232
kicad_version = arguments["kicad_version"]
213233
sym_lib_ext = "kicad_sym" if kicad_version == KicadVersion.v6 else "lib"
@@ -216,6 +236,11 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
216236
api = EasyedaApi()
217237
cad_data = api.get_cad_data_of_component(lcsc_id=component_id)
218238

239+
# API returned no data
240+
if not cad_data:
241+
logging.error(f"Failed to fetch data from EasyEDA API for part {component_id}")
242+
return 1
243+
219244
# ---------------- SYMBOL ----------------
220245
if arguments["symbol"]:
221246
importer = EasyedaSymbolImporter(easyeda_cp_cad_data=cad_data)
@@ -269,7 +294,8 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
269294

270295
logging.info(f"Creating Kicad footprint library for LCSC id : {component_id}")
271296
ExporterFootprintKicad(footprint=easyeda_footprint).export(
272-
output_path=arguments["output"]
297+
output_path=arguments["output"],
298+
is_project_relative=arguments["project_relative"],
273299
)
274300

275301
# ---------------- 3D MODEL ----------------

easyeda2kicad/helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Global imports
2+
import json
23
import logging
4+
import os
35
import re
6+
from datetime import datetime
47

8+
from easyeda2kicad import __version__
59
from easyeda2kicad.kicad.parameters_kicad_symbol import KicadVersion, sanitize_fields
610

711
sym_lib_regex_pattern = {
@@ -104,3 +108,20 @@ def add_component_in_symbol_lib_file(
104108
"(generator https://github.com/uPesy/easyeda2kicad.py)",
105109
)
106110
)
111+
112+
113+
def get_local_config() -> dict:
114+
if not os.path.isfile("easyeda2kicad_config.json"):
115+
with open(file="easyeda2kicad_config.json", mode="w", encoding="utf-8") as conf:
116+
json.dump(
117+
{"updated_at": datetime.utcnow().timestamp(), "version": __version__},
118+
conf,
119+
indent=4,
120+
ensure_ascii=False,
121+
)
122+
logging.info("Create easyeda2kicad_config.json config file")
123+
124+
with open(file="easyeda2kicad_config.json", encoding="utf-8") as conf:
125+
local_conf: dict = json.load(conf)
126+
127+
return local_conf

easyeda2kicad/kicad/export_kicad_footprint.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ def generate_kicad_footprint(self) -> None:
435435
def get_ki_footprint(self) -> KiFootprint:
436436
return self.output
437437

438-
def export(self, output_path: str) -> None:
438+
def export(self, output_path: str, is_project_relative: bool) -> None:
439+
439440
ki = self.output
440441
ki_lib = ""
441442

@@ -486,8 +487,11 @@ def export(self, output_path: str) -> None:
486487
ki_lib += KI_TEXT.format(**vars(text))
487488

488489
if ki.model_3d is not None:
490+
model_3d_path = output_path
491+
if is_project_relative:
492+
model_3d_path = "${KIPRJMOD}/" + model_3d_path.split("/")[-1]
489493
ki_lib += KI_MODEL_3D.format(
490-
file_3d=f"{output_path}.3dshapes/{ki.model_3d.name}.wrl",
494+
file_3d=f"{model_3d_path}.3dshapes/{ki.model_3d.name}.wrl",
491495
pos_x=ki.model_3d.translation.x,
492496
pos_y=ki.model_3d.translation.y,
493497
pos_z=ki.model_3d.translation.z,

ressources/jlcpcb_banner.png

37.2 KB
Loading

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.3.7
2+
current_version = 0.4.0
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
),
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
14-
version="0.3.7",
14+
version="0.4.0",
1515
author="uPesy",
1616
author_email="[email protected]",
1717
url="https://github.com/uPesy/easyeda2kicad.py",

0 commit comments

Comments
 (0)