OpenSCAD

TE10 OpenSCAD サンプル作成

この記事は約25分で読めます。

OpenSCADで作成したモデルです。基本作業はLLMによるコード生成です。

文字のプロンプト

テーブル

LLM: Gemini 2.0 Flash

以下のテーブルを作成するopenSCADのコードを作成してください。

4本脚のテーブル
テーブルの天面高さ:1
天板のサイズ
幅:2
奥行:1
厚み:0.1
天板の下端の高さ:0.3

脚の下端は、Z軸の0にしてください。
天板の位置を脚の上端にくるようにしてください。

上記では天板の位置がZ軸方向にずれていたため、数回の修正依頼をしています。

// テーブルのパラメータ
table_height = 1;          // テーブルの天面高さ
table_width = 2;           // 天板の幅
table_depth = 1;           // 天板の奥行
table_thickness = 0.1;       // 天板の厚み
table_leg_width = 0.1;        // 脚の幅
table_leg_depth = 0.1;        // 脚の奥行
table_bottom_height = 0.3;   // 天板の下端の高さ
table_leg_height = table_height - table_thickness; // 脚の高さ

// 天板のモジュール
module table_top() {
    cube([table_width, table_depth, table_thickness]);
}

// 脚のモジュール
module table_leg() {
    cube([table_leg_width, table_leg_depth, table_leg_height]);
}

// テーブル全体の組み立て
module table() {
    // 天板
    translate([0, 0, table_leg_height])
        table_top();
    
    // 4本の脚
    translate([0, 0, 0]) 
        table_leg();
    translate([table_width - table_leg_width, 0, 0]) 
        table_leg();
    translate([0, table_depth - table_leg_depth, 0]) 
        table_leg();
    translate([table_width - table_leg_width, table_depth - table_leg_depth, 0]) 
        table_leg();
}

table(); // テーブルを表示

修正

脚を円柱に修正

追加で指示した場合です。

脚を円柱に変えてもらえますか。
円柱の外径を滑らかに表示するには
// テーブルのパラメータ
table_height = 1;          // テーブルの天面高さ
table_width = 2;           // 天板の幅
table_depth = 1;           // 天板の奥行
table_thickness = 0.1;       // 天板の厚み
table_leg_radius = 0.05;   // 脚の半径
table_bottom_height = 0.3; // 天板の下端の高さ
table_leg_height = table_height - table_thickness; // 脚の高さ

$fn = 50; // 円柱の滑らかさを制御する

// 天板のモジュール
module table_top() {
    cube([table_width, table_depth, table_thickness]);
}

// 脚のモジュール
module table_leg() {
    cylinder(h = table_leg_height, r = table_leg_radius, center = false);
}

// テーブル全体の組み立て
module table() {
    // 天板
    translate([0, 0, table_leg_height])
        table_top();
    
    // 4本の脚
    translate([table_leg_radius, table_leg_radius, 0]) 
        table_leg();
    translate([table_width - table_leg_radius, table_leg_radius, 0]) 
        table_leg();
    translate([table_leg_radius, table_depth - table_leg_radius, 0]) 
        table_leg();
    translate([table_width - table_leg_radius, table_depth - table_leg_radius, 0]) 
        table_leg();
}

table(); // テーブルを表示

画像のプロンプト

椅子

イラスト画像を指示に使っています。現在では調整しないと近いものはできませんでした。(2025/04)
LLM:GPT-4o

画像も生成AIで作成

画像のような椅子のコードを生成するようにしてもらいます。

// 木製のクラシックな椅子の改良モデル

// パラメータ
seat_radius = 20;         // 座面の半径
seat_thickness = 3;       // 座面の厚さ
leg_length = 40;          // 脚の長さ
leg_thickness = 2.5;      // 脚の太さ
backrest_width = 25;      // 背もたれの幅
backrest_height = 10;     // 背もたれの高さ
backrest_thickness = 2;   // 背もたれの厚さ
spindle_count = 3;        // 背もたれの支柱の本数
spindle_thickness = 1.5;  // 背もたれの支柱の太さ

module chair() {
  // 座面
  translate([0, 0, leg_length])
    cylinder(h = seat_thickness, r = seat_radius, center = false);

  // 脚 (Z軸に平行)
  for (angle = [45, 135, 225, 315]) {
    translate([seat_radius * 0.7 * cos(angle), seat_radius * 0.7 * sin(angle), 0])
      cylinder(h = leg_length, r = leg_thickness, center = false);
  }

  // 背もたれの支柱
  for (i = [0 : spindle_count - 1]) {
    x_offset = (i - (spindle_count - 1) / 2) * (backrest_width / (spindle_count - 1));
    translate([x_offset, -seat_radius * 0.6, leg_length + seat_thickness])
      cylinder(h = backrest_height, r = spindle_thickness, center = false);
  }

  // 背もたれ
  translate([0, -seat_radius * 0.6 - backrest_thickness / 2, leg_length + seat_thickness + backrest_height])
    rotate([90, 0, 0])
      union() {
        // 中央の長方形部分
        cube([backrest_width, backrest_thickness, backrest_height], center = true);
        // 両端の丸み
        translate([-backrest_width / 2, 0, 0])
          cylinder(h = backrest_thickness, r = backrest_height / 2, center = true);
        translate([backrest_width / 2, 0, 0])
          cylinder(h = backrest_thickness, r = backrest_height / 2, center = true);
      }
}

// 椅子を描画
chair();

画像を言語化して指示

以下の情報を追加して作成してください。 全体高さ 約850mm
座面高さ 約450mm
座面幅 約450mm
座面奥行 約400mm
背もたれ高さ 約400mm
材質 オーク無垢材
脚部 幅40mm×奥行40mmの角材
背もたれ 厚み20mmのオーク板
座面 厚み25mmのオーク材、緩やかに湾曲した形状
仕上げ 天然オイル塗装

追加情報も生成AIで作成

// オーク無垢材のシンプルなチェアモデル

// パラメータ
seat_width = 45;          // 座面の幅 (mm)
seat_depth = 40;          // 座面の奥行 (mm)
seat_thickness = 2.5;     // 座面の厚さ (cm)
leg_length = 45;          // 脚の長さ (cm)
leg_width = 4;            // 脚の幅 (cm)
leg_depth = 4;            // 脚の奥行 (cm)
backrest_height = 40;     // 背もたれの高さ (cm)
backrest_thickness = 2;   // 背もたれの厚さ (cm)
seat_curve_depth = 1;     // 座面の湾曲の深さ (cm)

// モジュール: 座面
module seat() {
  difference() {
    // 座面の基本形状
    translate([0, 0, leg_length])
      cube([seat_width, seat_depth, seat_thickness], center = true);
    // 座面の湾曲
    translate([0, 0, leg_length + seat_thickness])
      rotate([0, 0, 0])
        cylinder(h = seat_curve_depth, r = seat_width / 2, center = true);
  }
}

// モジュール: 脚
module leg(x, y) {
  translate([x, y, 0])
    cube([leg_width, leg_depth, leg_length], center = false);
}

// モジュール: 背もたれ
module backrest() {
  translate([0, -seat_depth / 2 - backrest_thickness / 2, leg_length + seat_thickness])
    cube([seat_width, backrest_thickness, backrest_height], center = true);
}

// 椅子全体
module chair() {
  // 座面
  seat();

  // 脚
  leg(-seat_width / 2 + leg_width, -seat_depth / 2 + leg_depth);
  leg(seat_width / 2 - leg_width, -seat_depth / 2 + leg_depth);
  leg(-seat_width / 2 + leg_width, seat_depth / 2 - leg_depth);
  leg(seat_width / 2 - leg_width, seat_depth / 2 - leg_depth);

  // 背もたれ
  backrest();
}

// 椅子を描画
chair();

LLM確認

LLMを変えて作成してみたものです。

Claude 3.5 Sonnet

プロンプト
上記の情報を引き継いでいます。

椅子の画像を作成できますか。
// オーク無垢材のシンプルなチェアモデル

// パラメータ
seat_width = 45;          // 座面の幅 (mm)
seat_depth = 40;          // 座面の奥行 (mm)
seat_thickness = 2.5;     // 座面の厚さ (cm)
leg_length = 45;          // 脚の長さ (cm)
leg_width = 4;            // 脚の幅 (cm)
leg_depth = 4;            // 脚の奥行 (cm)
backrest_height = 40;     // 背もたれの高さ (cm)
backrest_thickness = 2;   // 背もたれの厚さ (cm)
seat_curve_depth = 1;     // 座面の湾曲の深さ (cm)

// モジュール: 座面
module seat() {
  translate([0,0,30])
    cube([40,40,4]);
}

// モジュール: 脚
module leg(x, y) {
  translate([x, y, 0])
    cube([4,4,30]);
}

// モジュール: 背もたれ
module backrest() {
  translate([0,36,30])
    cube([40,4,40]);
}

// 椅子全体
module chair() {
  // 座面
  seat();

  // 脚
  leg(0, 0);     // 左前
  leg(36, 0);    // 右前
  leg(0, 36);    // 左後
  leg(36, 36);   // 右後

  // 背もたれ
  backrest();
}

// 椅子を描画
chair();

構造 アッシー・パーツ部

ソファーの形を指示した場合です。いくつかのパーツからなります。

横長のソファーの形にできますか。

VScodeで作業する場合には、ワークスペースの作成が行われます。(意図的に指示してもいいかもしれません)
以下の構造になります。

sofa-model
├── src
│ ├── sofa.scad
│ ├── components
│ │ ├── base.scad
│ │ ├── cushions.scad
│ │ ├── armrests.scad
│ │ └── legs.scad
│ └── lib
│ ├── constants.scad
│ └── utils.scad
├── tests
│ └── sofa_test.scad
└── README.md

複数のフォルダやファイルが作成されますが、中には正常に作成されていない部分がありました。
このような場合には、修正を指示する必要があるかもしれません。
 (今回は参照がされていないなどの修正を行っています。)
エラーを指摘すると修正してくれます。
 単品の修正が得意なLLMなどで行ったりといったコツがあるかもしれません。

ワークスペースの追加
  • README.md
  • sofa_test.scad
  • sofa.scad
  • armrests.scad
  • base.scad
  • cushions.scad
  • legs.scad
# Sofa Model Project

このプロジェクトは、横長のソファーの形を作成するためのOpenSCADモデルです。このモデルは、さまざまなコンポーネントを組み合わせて全体のソファー形状を作成します。

## プロジェクト構成

- `src/sofa.scad`: ソファーモデルのメインエントリーポイント。さまざまなコンポーネントを組み合わせて全体の形状を作成します。
- `src/components/base.scad`: ソファーの基礎構造を定義し、寸法や形状を含みます。
- `src/components/cushions.scad`: ソファーのクッションを定義し、寸法や配置を含みます。
- `src/components/armrests.scad`: ソファーのアームレストを定義し、形状や位置を詳細に説明します。
- `src/components/legs.scad`: ソファーの脚を定義し、高さやスタイルを含みます。
- `src/lib/constants.scad`: プロジェクト全体で使用される定数値を含み、寸法や材料特性を定義します。
- `src/lib/utils.scad`: ソファーモデルの作成や操作を支援するユーティリティ関数を含みます。
- `tests/sofa_test.scad`: ソファーモデルのテストを含み、コンポーネントが正しくレンダリングされ、指定された寸法を満たしていることを確認します。

## 使用方法

1. OpenSCADを開き、`src/sofa.scad`ファイルをロードします。
2. モデルをレンダリングして、ソファーの形状を確認します。
3. 必要に応じて、コンポーネントの寸法や形状を調整します。

## 依存関係

特にありませんが、OpenSCADが必要です。

# ソファモデル

## プロジェクト構造
```
sofa-model
├── src
│ ├── sofa.scad
│ ├── components
│ │ ├── base.scad
│ │ ├── cushions.scad
│ │ ├── armrests.scad
│ │ └── legs.scad
│ └── lib
│ ├── constants.scad
│ └── utils.scad
├── tests
│ └── sofa_test.scad
└── README.md
```
// filepath: sofa-model/tests/sofa_test.scad
// ソファモデルのテスト

include <../src/sofa.scad>

// テスト: ソファの基本的な形状を確認
module test_sofa_shape() {
    sofa();  // ソファを描画
}

// テストを実行
test_sofa_shape();
use <components/base.scad>
use <components/armrests.scad>
use <components/cushions.scad>
use <components/legs.scad>
use <components/backrest.scad>  // 背面部分をインポート

// ソファのパラメータ
sofa_width = 180;      // ソファの幅 (cm)
sofa_depth = 80;       // ソファの奥行き (cm)
seat_height = 20;      // 座面の高さ (cm)
leg_height = 10;       // 脚の高さ (cm)
// ソファのパラメータ
backrest_thickness = 10; // 背面部分の厚さ (cm)

// モジュール: ソファ全体
module sofa() {
    // ベース部分
    color("SaddleBrown") translate([0, 0, leg_height]) sofa_base();
    
    // 脚部
    for (x = [10, sofa_width - 20]) {
        for (y = [10, sofa_depth - 20]) {
            color("SaddleBrown") translate([x, y, 0]) leg();
        }
    }
    
    // アームレスト
    color("SaddleBrown") translate([0, 0, seat_height]) armrest();
    color("SaddleBrown") translate([sofa_width - 20, 0, seat_height]) armrest();
    
    // クッション(背面側に移動)
    color("Tan") translate([20, sofa_depth - 70, seat_height]) seat_cushion();
    color("Tan") translate([20, sofa_depth - 20, seat_height + 10]) back_cushion();
    
    // 背面部分
    color("SaddleBrown") translate([0, sofa_depth - backrest_thickness, seat_height]) backrest();
}
// filepath: sofa-model/src/components/armrests.scad
// ソファのアームレストを定義するモジュール

armrest_width = 20;  // アームレストの幅
armrest_depth = 80;  // アームレストの奥行き
armrest_height = 65; // アームレストの高さ

module armrest() {
    cube([armrest_width, armrest_depth, armrest_height]);
}

// アームレストを描画
armrest();
// ソファベース部分のパラメータ
base_width = 180;    // ベースの幅 (cm)
base_depth = 80;     // ベースの奥行き (cm)
base_height = 10;    // ベースの高さ (cm)

// モジュール: ソファのベース部分
module sofa_base() {
    cube([base_width, base_depth, base_height]);
}
// filepath: sofa-model/src/components/cushions.scad
// ソファのクッションを定義するモジュール

// パラメータ
cushion_width = 60;      // クッションの幅 (cm)
cushion_depth = 40;      // クッションの奥行 (cm)
cushion_thickness = 10;   // クッションの厚さ (cm)

seat_cushion_width = 140;  // 座面クッションの幅
seat_cushion_depth = 60;   // 座面クッションの奥行き
seat_cushion_height = 10;  // 座面クッションの高さ

back_cushion_width = 140;  // 背もたれクッションの幅
back_cushion_depth = 10;   // 背もたれクッションの奥行き
back_cushion_height = 30;  // 背もたれクッションの高さ

// モジュール: クッション
module cushion() {
    translate([0, 0, 0])
        cube([cushion_width, cushion_depth, cushion_thickness]);
}

module seat_cushion() {
    cube([seat_cushion_width, seat_cushion_depth, seat_cushion_height]);
}

module back_cushion() {
    cube([back_cushion_width, back_cushion_depth, back_cushion_height]);
}

// クッションを描画
cushion();
// filepath: sofa-model/src/components/legs.scad
// ソファの脚の定義

// パラメータ
leg_height = 10; // 脚の高さ
leg_radius = 5;  // 脚の半径

// モジュール: 脚
module leg() {
    cylinder(h = leg_height, r = leg_radius);
}

// 脚を描画
translate([0, 0, 0]) leg(); // 前左脚
translate([40, 0, 0]) leg(); // 前右脚
translate([0, 30, 0]) leg(); // 後左脚
translate([40, 30, 0]) leg(); // 後右脚

sofa_test.scad ファイルがモデルヘッダーになります。

構造をLLMに出力させます。(プロンプト:ファイル構造をアスキーツリーで描画してください。)

sofa-model
├── src
│ ├── sofa.scad
│ ├── components
│ │ ├── base.scad
│ │ ├── armrests.scad
│ │ ├── cushions.scad
│ │ ├── legs.scad
│ │ └── backrest.scad
├── tests
│ └── sofa_test.scad

説明
src: ソファモデルの主要なコードが含まれるディレクトリ。
sofa.scad: ソファ全体を組み立てるメインファイル。
components: ソファの各部品(ベース、アームレスト、クッション、脚、背面)を定義するファイルを格納。
base.scad: ソファのベース部分。
armrests.scad: アームレスト部分。
cushions.scad: クッション部分。
legs.scad: 脚部分。
backrest.scad: 背面部分。
tests: テスト用のファイルを格納するディレクトリ。
sofa_test.scad: ソファ全体を描画してテストするファイル。
この構造に基づいて、各部品を独立して管理し、sofa.scadで統合しています。

sofa_test.scad > sofa.scad > components のように親から子を構成しています。

構造調整

パーツの寸法などをパラメータ化してまとめます。
作成はLLMに指示して行ってもらいました。

LLMにパラメータ化してもらいます。

モデルのパラメータを集めて管理しやすくします。

parameters.scadのパラメータをまとめたファイルを作成しました。

注記(2025/04)

変数の宣言をあまり考慮していない場合があるので注意する。
仕様を明記しておくファイルを用意する。

作成物

LLMによる作成物です。あまり厳密に指示していません。
ぽんち絵のような用途です。

ベアリング LLM:Claude 3.5 Sonnet

bearing-model/
├── src/
│ ├── bearing.scad # メインファイル
│ └── components/
│ ├── inner_ring.scad # 内輪定義
│ ├── outer_ring.scad # 外輪定義
│ ├── ball.scad # ボール定義
│ └── cage.scad # 保持器定義
└── README.md

ge_OSC_TE10_bearing-model.zip

クランクスライダー LLM :cursor(Version: 0.50.4)

cursorを使ってコードを作成しています。(ホビーユーザー 2025/5)

crank and slider/
├── config.scad # 共通設定(寸法、色など)
├── base.scad # ベースパーツ
├── slider_rod.scad # スライダーロッド
├── crank.scad # クランク
├── connecting_rod.scad # 接続ロッド
└── assembly.scad # アセンブリ用メインファイル

ge_OSC_TE10_crank_and_slider.zip

モデルの位置決めは行っていません。FreeCADでおこなったものは以下。

コメント

Translate »
タイトルとURLをコピーしました