Skip to content

Commit

Permalink
dfn: Generate 3D models
Browse files Browse the repository at this point in the history
  • Loading branch information
ubruhin committed Nov 15, 2024
1 parent 876d49e commit 9a3ef10
Show file tree
Hide file tree
Showing 2 changed files with 351 additions and 7 deletions.
94 changes: 87 additions & 7 deletions generate_dfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Generate DFN packages
"""
import sys
from os import path
from uuid import uuid4

Expand Down Expand Up @@ -82,6 +83,7 @@ def generate_pkg(
keywords: str,
config: DfnConfig,
make_exposed: bool,
generate_3d_models: bool,
create_date: Optional[str] = None,
) -> str:
category = 'pkg'
Expand Down Expand Up @@ -445,23 +447,99 @@ def _generate_footprint(key: str, name: str, pad_extension: float) -> None:
value=Value('{{VALUE}}'),
))

# Approvals
package.add_approval(
"(approved missing_footprint_3d_model\n" +
" (footprint {})\n".format(uuid_footprint) +
")"
)

# Apply function to available footprints
_generate_footprint('reflow', 'reflow', 0.0)
_generate_footprint('hand-soldering', 'hand soldering', 0.3)

# Generate 3D models
uuid_3d = _uuid('3d')
if generate_3d_models:
generate_3d(full_name, uuid_pkg, uuid_3d, config, make_exposed)
package.add_3d_model(Package3DModel(uuid_3d, Name(full_name)))
for footprint in package.footprints:
footprint.add_3d_model(Footprint3DModel(uuid_3d))

# Save package
package.serialize(path.join('out', config.library, category))
return full_name


def generate_3d(
full_name: str,
uuid_pkg: str,
uuid_3d: str,
config: DfnConfig,
make_exposed: bool,
) -> None:
import cadquery as cq

from cadquery_helpers import StepAssembly, StepColor

print(f'Generating pkg 3D model "{full_name}": {uuid_3d}')

dot_diameter = min(config.width * 0.2, 0.6)
dot_position = min(config.width * 0.2, 0.8)
dot_depth = 0.05 if config.width < 3 else 0.15
lead_standoff = 0.02
lead_height = 0.2

dot_center = (
-(config.width / 2) + dot_position,
(config.length / 2) - dot_position,
lead_standoff + config.height_nominal - dot_depth
)

body = cq.Workplane('XY', origin=(0, 0, lead_standoff + (config.height_nominal / 2))) \
.box(config.width, config.length, config.height_nominal) \
.workplane(origin=(dot_center[0], dot_center[1]), offset=(config.height_nominal / 2) - dot_depth) \
.cylinder(5, dot_diameter / 2, centered=(True, True, False), combine='cut')
dot = cq.Workplane('XY', origin=dot_center) \
.cylinder(0.05, dot_diameter / 2, centered=(True, True, False))
lead = cq.Workplane("ZY") \
.box(lead_height, config.lead_width, config.lead_length)
if make_exposed:
exposed_lead = cq.Workplane('XY', origin=(0, 0, (lead_height / 2))) \
.box(config.exposed_length, config.exposed_width, lead_height)

assembly = StepAssembly(full_name)
assembly.add_body(body, 'body', StepColor.IC_BODY)
assembly.add_body(dot, 'dot', StepColor.IC_PIN1_DOT)
pins_per_side = config.pin_count // 2
for i in range(0, config.pin_count):
side = -1 if (i < pins_per_side) else 1
y1 = get_y(1 if (i < pins_per_side) else pins_per_side,
pins_per_side, config.pitch, False)
y_index = i % pins_per_side
assembly.add_body(
lead,
'lead-{}'.format(i + 1), StepColor.LEAD_SMT,
location=cq.Location((
(((config.width - config.lead_length) / 2) + lead_standoff) * side,
y1 + y_index * config.pitch * side,
lead_height / 2,
))
)
if make_exposed:
assembly.add_body(exposed_lead, 'lead-exposed', StepColor.LEAD_SMT)

# Save without fusing for massively better minification!
out_path = path.join('out', config.library, 'pkg', uuid_pkg, f'{uuid_3d}.step')
assembly.save(out_path, fused=False)


if __name__ == '__main__':
if '--help' in sys.argv or '-h' in sys.argv:
print(f'Usage: {sys.argv[0]} [--3d]')
print()
print('Options:')
print(' --3d Generate 3D models using cadquery')
sys.exit(1)

generate_3d_models = '--3d' in sys.argv
if not generate_3d_models:
warning = 'Note: Not generating 3D models unless the "--3d" argument is passed in!'
print(f'\033[1;33m{warning}\033[0m')

generated_packages: List[str] = []

for config in JEDEC_CONFIGS:
Expand All @@ -488,6 +566,7 @@ def _generate_footprint(key: str, name: str, pad_extension: float) -> None:
keywords='dfn,dual flat no-leads,mo-229f',
config=config,
make_exposed=make_exposed,
generate_3d_models=generate_3d_models,
create_date='2019-01-17T06:11:43Z',
)
if name not in generated_packages:
Expand Down Expand Up @@ -518,6 +597,7 @@ def _generate_footprint(key: str, name: str, pad_extension: float) -> None:
keywords='dfn,dual flat no-leads',
config=config,
make_exposed=make_exposed,
generate_3d_models=generate_3d_models,
create_date=config.create_date,
)
if name not in generated_packages:
Expand Down
Loading

0 comments on commit 9a3ef10

Please sign in to comment.