0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FreeCADのサイコロのマクロ「(その1/5) 展開図」をAI先生に教えてもらいました。

0
Last updated at Posted at 2026-02-26

(その1/5) 本ページ
(その2/5) 11パターン(Gemini中断)
(その3/5) 作成中
(その4/5) 作成中
(その5/5) 作成中

・FreeCAD 1.1.1 (2026-04-23) 
Screenshot 2026-04-23 20.21.48.png

・FreeCAD 1.0.2 (2026-04-28)
Screenshot 2026-04-28 13.09.49.png

・FreeCAD 0.20.0
Screenshot 2026-02-27 18.48.25.png

??? ナゼかですよね。
??? これから少しづづ聞いていきます。左図を無視してもらった方がヨカッタ。
(2025-02-26) タイトルを変更予定です。

・私のFreeCADは、最新Version でありません。Version: 0.20.2.です。

(本日の)ChatGPT先生へ

なし。

(本日の)Gemini3先生へ

FreeCADのマクロを教えて下さい。
立方体を作成
・6面に 文字①〜⑥をサイコロ配置で表示
・各面を 6色に着色
同時に、XY 平面上に 展開図を 作成
・6面に 文字①〜⑥を表示
・各面を 6色に着色

(途中回答省略)

import FreeCAD as App
import Part
import Draft
import os

def create_dice_with_unfold_v3():
    doc = App.activeDocument()
    if not doc:
        doc = App.newDocument("Dice_Adjusted_v3")

    size = 20.0  # 立方体の一辺
    f_size = size * 0.5  # 文字の大きさ
    off = f_size / 2.0   # 基本のオフセット
    gap = 0.1            # 面から浮かせる距離

    # フォントパス(Windowsを想定。環境に合わせて変更してください)
    font_path = "C:/Windows/Fonts/msgothic.ttc" 
    if not os.path.exists(font_path):
        font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"

    colors = [
        (1.0, 0.0, 0.0), (0.0, 0.8, 0.0), (0.0, 0.0, 1.0),
        (0.9, 0.9, 0.0), (1.0, 0.5, 0.0), (0.6, 0.0, 0.6)
    ]
    
    # 立方体用の配置データ
    # ("文字", 位置Vector, 回転Rotation)
    cube_faces = [
        ("1", App.Vector(size/2-off, size/2-off, size+gap), App.Rotation(App.Vector(0,0,1), 0)),    # 上
        ("6", App.Vector(size/2-off, size/2+off, -gap), App.Rotation(App.Vector(1,0,0), 180)),   # 下
        ("2", App.Vector(size/2-off, -gap, size/2-off), App.Rotation(App.Vector(1,0,0), 90)),    # 前
        ("5", App.Vector(size/2-off, size+gap, size/2+off), App.Rotation(App.Vector(1,0,0), -90)),  # 後
        # --- 3と4を重点的に調整 ---
        ("3", App.Vector(size+gap, size/2-off, size/2+off), App.Rotation(App.Vector(0,1,0), 90)),   # 右面
        ("4", App.Vector(-gap, size/2-off, size/2-off), App.Rotation(App.Vector(0,1,0), -90)),  # 左面
    ]

    unfold_offsets = [(0, 0), (0, -2), (0, -1), (0, 1), (1, -1), (-1, -1)]

    # 1. 立方体
    box = doc.addObject("Part::Box", "Dice_Body")
    box.Length = box.Width = box.Height = size
    box.ViewObject.ShapeColor = (0.9, 0.9, 0.9)
    box.ViewObject.Transparency = 50

    # 2. 文字と展開図
    for i, (text, pos, rot) in enumerate(cube_faces):
        # 立方体側の文字
        try:
            sh = Draft.make_shapestring(text, font_path, f_size)
            sh.Placement = App.Placement(pos, rot)
            sh.ViewObject.ShapeColor = colors[i]
        except:
            App.Console.PrintWarning(f"文字 {text} の作成に失敗しました。\n")

        # 展開図側
        off_x = (unfold_offsets[i][0] + 3) * size * 1.5
        off_y = (unfold_offsets[i][1] + 1) * size * 1.5
        
        rect = Draft.make_rectangle(size, size)
        rect.Placement = App.Placement(App.Vector(off_x, off_y, 0), App.Rotation())
        rect.ViewObject.ShapeColor = colors[i]
        rect.ViewObject.DisplayMode = "Flat Lines"
        
        sh_u = Draft.make_shapestring(text, font_path, f_size)
        sh_u.Placement = App.Placement(App.Vector(off_x + size/2-off, off_y + size/2-off, 0.1), App.Rotation())
        sh_u.ViewObject.ShapeColor = (0.0, 0.0, 0.0)

    doc.recompute()

create_dice_with_unfold_v3()

(続けて)Gemini3先生へ

以下の展開図は、バラバラ、離れているので、連結して下さい。
import FreeCAD as App
...

(途中回答省略)

ご提示いただいたスクリプトでは、展開図の各面を size * 1.5 倍のオフセットで配置しているため、面と面の間に隙間が空いてバラバラになっています。
これを一般的な 「十字型(1-4-1型)」の展開図 として連結するように修正しました。

import FreeCAD as App
import Part
import Draft
import os

def create_dice_with_unfold_connected():
    doc = App.activeDocument()
    if not doc:
        doc = App.newDocument("Dice_Connected_Unfold")

    size = 20.0  # 立方体の一辺
    f_size = size * 0.5  # 文字の大きさ
    off = f_size / 2.0   # 文字配置のオフセット
    gap = 0.1            # 文字を面から浮かせる距離

    # フォントパスの設定
    font_path = "C:/Windows/Fonts/msgothic.ttc" 
    if not os.path.exists(font_path):
        font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"

    colors = [
        (1.0, 0.0, 0.0), (0.0, 0.8, 0.0), (0.0, 0.0, 1.0),
        (0.9, 0.9, 0.0), (1.0, 0.5, 0.0), (0.6, 0.0, 0.6)
    ]
    
    # 立方体用の配置データ
    cube_faces = [
        ("1", App.Vector(size/2-off, size/2-off, size+gap), App.Rotation(App.Vector(0,0,1), 0)),    # 上
        ("6", App.Vector(size/2-off, size/2+off, -gap), App.Rotation(App.Vector(1,0,0), 180)),   # 下
        ("2", App.Vector(size/2-off, -gap, size/2-off), App.Rotation(App.Vector(1,0,0), 90)),    # 前
        ("5", App.Vector(size/2-off, size+gap, size/2+off), App.Rotation(App.Vector(1,0,0), -90)),  # 後
        ("3", App.Vector(size+gap, size/2-off, size/2+off), App.Rotation(App.Vector(0,1,0), 90)),   # 右
        ("4", App.Vector(-gap, size/2-off, size/2-off), App.Rotation(App.Vector(0,1,0), -90)),  # 左
    ]

    # --- 連結用のオフセット設定 (x, y) ---
    # 十字型展開図のレイアウト:
    #       [5] (後)
    # [4][1][3][6] (左,上,右,下)
    #       [2] (前)
    unfold_layout = [
        (0, 0),   # 1 (基準: 中央上)
        (0, -1),  # 2 (1の下)
        (1, 0),   # 3 (1の右)
        (-1, 0),  # 4 (1の左)
        (0, 1),   # 5 (1の上)
        (2, 0)    # 6 (3の右 = 1から2つ右)
    ]

    # 展開図の描画開始基準点 (立方体から離れた場所に配置)
    start_x = size * 3
    start_y = size * 2

    # 1. 立方体本体
    box = doc.addObject("Part::Box", "Dice_Body")
    box.Length = box.Width = box.Height = size
    box.ViewObject.ShapeColor = (0.9, 0.9, 0.9)
    box.ViewObject.Transparency = 50

    # 2. 文字と展開図の作成
    for i, (text, pos, rot) in enumerate(cube_faces):
        # --- 立方体側の文字作成 ---
        try:
            sh = Draft.make_shapestring(text, font_path, f_size)
            sh.Placement = App.Placement(pos, rot)
            sh.ViewObject.ShapeColor = colors[i]
        except:
            App.Console.PrintWarning(f"Failed to create text {text} on cube.\n")

        # --- 展開図側の面と文字作成 ---
        # 座標計算: 隙間をなくすため size をそのまま掛ける
        ux = start_x + unfold_layout[i][0] * size
        uy = start_y + unfold_layout[i][1] * size
        
        # 面 (正方形)
        rect = Draft.make_rectangle(size, size)
        rect.Placement = App.Placement(App.Vector(ux, uy, 0), App.Rotation())
        rect.ViewObject.LineColor = (0.0, 0.0, 0.0)
        rect.ViewObject.ShapeColor = colors[i]
        rect.ViewObject.DisplayMode = "Flat Lines"
        
        # 展開図の文字
        try:
            sh_u = Draft.make_shapestring(text, font_path, f_size)
            # 面の中央に配置
            sh_u.Placement = App.Placement(App.Vector(ux + size/2-off, uy + size/2-off, 0.1), App.Rotation())
            sh_u.ViewObject.ShapeColor = (0.0, 0.0, 0.0)
        except:
            pass

    doc.recompute()

create_dice_with_unfold_connected()

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)

いつもと違うおすすめです。共通編

Qiita 内

FreeCAD manual

・ジャンプ?が、私の環境で上手くいきません。

FreeCAD doc

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?