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のマクロ「立方体(solid)1個。座標表示付き」をAI先生に教えてもらいました。

0
Last updated at Posted at 2026-03-07

(旧タイトル)FreeCADの立方体(solid)1個のマクロをAI先生に教えてもらいました。座標表示付き

・一度の質問(prompt?,LLM?)を教えて下さい。
・FreeCAD 0.20.2です。バージョンの壁がかなりありました。V1でお願いします。
・x,y,z軸。赤色,緑色,青色です。
・ラベルと属性の順番があと一歩です。エディタで直した方がはやいかも。
・作図マクロと視点回転マクロの連結です。
>CAD操作を覚えたら
そうかも

00.png

(本日の)ChatGPT先生 と Gemini3先生の合作です。

FreeCADの立方体solidの作図マクロを教えて下さい。
・solidの一辺の長さが2
・solidの位置はx軸、y軸、z軸に沿う
・標準機能のxyz軸も表示
・solidの透過度は反対側が見える

・(延々とです。)過程省略

# FreeCAD Macro: Generates a semi-transparent cube with swapped labels (C-D, G-H) and optimized camera view.
import FreeCAD as App
import FreeCADGui as Gui
import Part
import Draft

# 1. 新規ドキュメントの作成
doc = App.newDocument("CubeWithLabels")

# 2. 立方体の作成 (2.0 x 2.0 x 2.0)
cube = doc.addObject("Part::Box", "Cube")
cube.Length = 2.0
cube.Width  = 2.0
cube.Height = 2.0

doc.recompute()

# 3. 外観の設定(半透明・シェーディングモード)
cube_view = Gui.ActiveDocument.getObject(cube.Name)
cube_view.Transparency = 70
cube_view.DisplayMode = "Shaded"

# 4. 頂点座標とラベルの作成
# ご要望通り、C-D および G-H のラベル位置を入れ替えています
points = [
    ("E", (0, 0, 0)), ("F", (2, 0, 0)), ("H", (0, 2, 0)), ("G", (2, 2, 0)),
    ("A", (0, 0, 2)), ("B", (2, 0, 2)), ("D", (0, 2, 2)), ("C", (2, 2, 2))
]

# Z軸周りに180度回転させたテキスト配置用の回転オブジェクト
rotation_180 = App.Rotation(App.Vector(0, 0, 1), 180)

for name, v in points:
    p = App.Vector(v[0], v[1], v[2])
    txt = f"{name}({v[0]},{v[1]},{v[2]})"
    
    # テキストオブジェクトの作成(少し上に浮かせ、180度回転を適用)
    t = Draft.make_text(
        [txt],
        App.Placement(p + App.Vector(0, 0, 0.15), rotation_180)
    )
    t.ViewObject.FontSize = 0.25  # 文字サイズを微調整

doc.recompute()

# 5. ビューの操作とカメラ角度の微調整
view = Gui.ActiveDocument.ActiveView
if view:
    # 座標軸の表示
    view.setAxisCross(True)
    
    # 等角投影図(アクソノメトリック)に設定
    view.viewAxonometric()
    
    # 現在のカメラの向きを取得
    camera = view.getCameraNode()
    sb_rot = camera.orientation.getValue()
    current_rot = App.Rotation(
        sb_rot.getValue()[0], 
        sb_rot.getValue()[1], 
        sb_rot.getValue()[2], 
        sb_rot.getValue()[3]
    )
    
    # Z軸周りにさらに90度回転させて視点を調整
    rot_90_z = App.Rotation(App.Vector(0, 0, 1), 90)
    new_rot = rot_90_z.multiply(current_rot)
    
    view.setCameraOrientation(new_rot)
    
    # 全体を表示
    Gui.SendMsgToActiveView("ViewFit")

print("Macro executed: Cube generated with swapped labels C-D/G-H and adjusted camera.")

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

いつもと違うおすすめです。

共通編
https://qiita.com/mrrclb48z/items/2d338620031f25e1c85f#%E3%81%84%E3%81%A4%E3%82%82%E3%81%A8%E9%81%95%E3%81%86%E3%81%8A%E3%81%99%E3%81%99%E3%82%81%E3%81%A7%E3%81%99%E5%85%B1%E9%80%9A%E7%B7%A8

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?