LoginSignup
3
1

More than 1 year has passed since last update.

Python&HolePlateMakerでLEGOテクニックに互換する部品をコードから自作する

Last updated at Posted at 2021-12-03

はじめに

今回の記事は昨年公開したHolePlateMakerの改良についてまとめたものです。
↓前回の記事

要は何が変わったの?

  • 開けられる穴の種類にLEGOテクニックに互換しそうな穴が加わった。また、円形の穴の他に十字形(Axle)の穴も出力できるようになった。
  • pythonパッケージについて非推奨だった項目を見直した。2021/10/31(日)確認では実行時のWarningがなくなっていたことを確認した。

事前準備

こちらの記事などを参考にvenvが使える状態にする。

なお、今回の検証にはmacOSをつかっているためvenvの実行コマンド等は適宜書き換えること。

 入手先github

インストールなどの環境構築方法はgithubにあるため割愛します。
pip 21.3.1、Pythonは3.9.1を使用しました。

新機能のLEGOっぽい穴を確認してみる

まずはコードをcloneします。

git clone
git clone https://github.com/henjin0/HolePlateMaker.git

Clone後のフォルダに移動し、仮想環境を作成する。

仮想環境
cd HolePlateMaker
python3 venv -m hpl
source ./bin/activate

pipを使ってパッケージをインストールします。もしpyqt5などのインストールでエラーが出力された際にはpip install -U pip などでnpmのアップデートを実行してください。

パッケージ
pip install -r requirements.txt

インストールができたら、下記のプログラムを作成します。
実をいうと同名のファイルでgithubにも入っていたり。

 sample_npArrayToHolePlate.py
import numpy as np
from stl import mesh
import matplotlib
matplotlib.use('Qt5Agg')
from mpl_toolkits import mplot3d
from matplotlib import pyplot
from HolePlateMaker import NumpyArrayToHolePlate

# OS等の環境依存せずに文字が表示できるようにするためにフォント変更
pyplot.rcParams["font.family"] = "Arial" 

# 0:穴が生成されない 1:丸い穴が開く 2:十字形(Axle)の穴が開く(4.8mm穴のみ)
plateData = np.array([[1,0,2],
    [2,0,1],
    [1,1,1,],
    [0,0,2],
    [0,0,1],])

# 2番目の引数typeには穴の大きさの'3mm'と'4.8mm'が指定可能。
curMesh = NumpyArrayToHolePlate.NumpyArrayToPlate(plateData,'4.8mm')
curMesh.save('plate.stl')

figure = pyplot.figure()
axes = figure.add_subplot(111, projection='3d')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(curMesh.vectors))

# Auto scale to the mesh size
scale = curMesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

# Show the plot to the screen
pyplot.show()

LEGOテクニックに互換する4.8 mm穴に関するplateDataの対応はこちら。

plateData 4.8 mm穴
生成なし 0
1
2

実行コマンドはこちら

 exec
python sample_npArrayToHolePlate.py

出力される結果はこちら

sample_npArrayToHolePlate.py出力結果

積層型3Dプリンタで出力された結果はこちら
写真上側にある黒い部品はLEGOテクニック部品のリフトアームで、それぞれのAxleのピッチが一致していることが確認できます。 

sample_npArrayToHolePlate.py出力結果

まとめ

  • HolePlateMakerに対してLEGOテクニック部品に互換する自作部品を新たに出力できるようにしました。
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1