[Experiments][EasyEDA] Generate material definitions
- Avoid repeating the materials over and over - First step towards KiCad materials
This commit is contained in:
parent
6b89f42399
commit
aa17497a26
|
|
@ -10,26 +10,23 @@ import requests
|
|||
import re
|
||||
import textwrap
|
||||
#
|
||||
# import pprint
|
||||
import pprint
|
||||
import json
|
||||
import pickle
|
||||
|
||||
|
||||
API_ENDPOINT = "https://easyeda.com/api/products/{lcsc_id}/components?version=6.4.19.5"
|
||||
ENDPOINT_3D_MODEL = "https://easyeda.com/analyzer/api/3dmodel/{uuid}"
|
||||
VRML_HEADER = """#VRML V2.0 utf8
|
||||
# 3D model generated by easyeda2kicad.py (https://github.com/uPesy/easyeda2kicad.py)
|
||||
"""
|
||||
# ------------------------------------------------------------
|
||||
VRML_HEADER = "#VRML V2.0 utf8\n# 3D model generated by KiBot (using easyeda2kicad.py code)\n"
|
||||
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0'
|
||||
|
||||
|
||||
class EasyedaApi:
|
||||
def __init__(self) -> None:
|
||||
self.headers = {
|
||||
"Accept-Encoding": "gzip, deflate",
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"User-Agent": "easyeda2kicad v0.6.2",
|
||||
}
|
||||
self.headers = {"Accept-Encoding": "gzip, deflate",
|
||||
"Accept": "application/json, text/javascript, */*; q=0.01",
|
||||
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"User-Agent": USER_AGENT}
|
||||
|
||||
def get_info_from_easyeda_api(self, lcsc_id: str) -> dict:
|
||||
r = requests.get(url=API_ENDPOINT.format(lcsc_id=lcsc_id), headers=self.headers)
|
||||
|
|
@ -168,6 +165,7 @@ def get_materials(obj_data: str) -> dict:
|
|||
material["transparency"] = value.split(" ")[1]
|
||||
|
||||
materials[material_id] = material
|
||||
# logging.error(materials)
|
||||
return materials
|
||||
|
||||
|
||||
|
|
@ -186,10 +184,29 @@ def generate_wrl_model(model_3d: Ee3dModel) -> Ki3dModel:
|
|||
vertices = get_vertices(obj_data=model_3d.raw_obj)
|
||||
|
||||
raw_wrl = VRML_HEADER
|
||||
# Define all the materials
|
||||
for id, mat in materials.items():
|
||||
mat_str = textwrap.dedent(
|
||||
f"""
|
||||
Shape {{
|
||||
appearance Appearance {{
|
||||
material DEF MATERIAL_{id} Material {{
|
||||
ambientIntensity 0.2
|
||||
diffuseColor {' '.join(mat['diffuse_color'])}
|
||||
specularColor {' '.join(mat['specular_color'])}
|
||||
shininess 0.5
|
||||
transparency 0.0
|
||||
}}
|
||||
}}
|
||||
}}"""
|
||||
)
|
||||
raw_wrl += mat_str
|
||||
# Define the shapes
|
||||
shapes = model_3d.raw_obj.split("usemtl")[1:]
|
||||
for shape in shapes:
|
||||
lines = shape.splitlines()
|
||||
material = materials[lines[0].replace(" ", "")]
|
||||
material_id = lines[0].replace(" ", "")
|
||||
material = materials[material_id]
|
||||
index_counter = 0
|
||||
link_dict = {}
|
||||
coord_index = []
|
||||
|
|
@ -212,16 +229,7 @@ def generate_wrl_model(model_3d: Ee3dModel) -> Ki3dModel:
|
|||
|
||||
shape_str = textwrap.dedent(
|
||||
f"""
|
||||
Shape{{
|
||||
appearance Appearance {{
|
||||
material Material {{
|
||||
diffuseColor {' '.join(material['diffuse_color'])}
|
||||
specularColor {' '.join(material['specular_color'])}
|
||||
ambientIntensity 0.2
|
||||
transparency {material['transparency']}
|
||||
shininess 0.5
|
||||
}}
|
||||
}}
|
||||
Shape {{
|
||||
geometry IndexedFaceSet {{
|
||||
ccw TRUE
|
||||
solid FALSE
|
||||
|
|
@ -234,12 +242,12 @@ def generate_wrl_model(model_3d: Ee3dModel) -> Ki3dModel:
|
|||
{"".join(coord_index)}
|
||||
]
|
||||
}}
|
||||
appearance Appearance {{material USE MATERIAL_{material_id}}}
|
||||
}}"""
|
||||
)
|
||||
|
||||
raw_wrl += shape_str
|
||||
|
||||
logging.error('Aca')
|
||||
return Ki3dModel(
|
||||
translation=None, rotation=None, name=model_3d.name, raw_wrl=raw_wrl
|
||||
)
|
||||
|
|
@ -256,7 +264,6 @@ class Exporter3dModelKicad:
|
|||
|
||||
def export(self, lib_path: str) -> None:
|
||||
if self.output:
|
||||
logging.error('Aca 1')
|
||||
with open(
|
||||
file=f"{lib_path}.3dshapes/{self.output.name}.wrl",
|
||||
mode="w",
|
||||
|
|
@ -265,12 +272,27 @@ class Exporter3dModelKicad:
|
|||
my_lib.write(self.output.raw_wrl)
|
||||
|
||||
|
||||
component_id = 'C181094'
|
||||
a = EasyedaApi()
|
||||
res = a.get_cad_data_of_component(component_id)
|
||||
# print(pprint.pformat(res))
|
||||
c = Easyeda3dModelImporter(res, True)
|
||||
# print(pprint.pformat(c.__dict__))
|
||||
component_id = 'C2895617'
|
||||
#
|
||||
if False:
|
||||
a = EasyedaApi()
|
||||
res = a.get_cad_data_of_component(component_id)
|
||||
print(pprint.pformat(res))
|
||||
if not res:
|
||||
logging.error('Not found')
|
||||
exit(1)
|
||||
c = Easyeda3dModelImporter(res, True)
|
||||
# print("********************************************")
|
||||
# print(pprint.pformat(c.__dict__))
|
||||
with open('model.pkl', 'wb') as file:
|
||||
pickle.dump(c, file)
|
||||
else:
|
||||
with open('model.pkl', 'rb') as file:
|
||||
c = pickle.load(file)
|
||||
# print(pprint.pformat(c.__dict__))
|
||||
with open('model.obj', 'w') as file:
|
||||
file.write(c.output.raw_obj)
|
||||
|
||||
exporter = Exporter3dModelKicad(model_3d=c.output)
|
||||
|
||||
os.makedirs('a.3dshapes', exist_ok=True)
|
||||
|
|
|
|||
Loading…
Reference in New Issue