本記事では、下図のようなCADファイルをPythonスクリプトで作成する雛形コードを載せました。FreeCADをインストールするとそのフォルダにPythonも含有しており、それを使用します。
■本コード
import FreeCAD
import Part
import Import
doc_name = 'new_doc'
obj_name = 'Box_1'
FreeCAD.newDocument(doc_name)
box = Part.makeBox(10, 20, 30)
radius = 5
height = 25
cylinder = Part.makeCylinder(radius, height)
target_coordinate = box.CenterOfMass
target_coordinate.x = 0
bbox = box.BoundBox
target_coordinate.z = bbox.ZMax - height
cylinder.translate(target_coordinate)
hole = box.cut(cylinder)
cut_obj = FreeCAD.ActiveDocument.addObject("Part::Feature", "Cut")
cut_obj.Shape = hole
FreeCAD.ActiveDocument.recompute()
filename = f'{doc_name}.fcstd'
FreeCAD.getDocument(doc_name).saveAs(filename)
__objs__ = [cut_obj]
step_filename = f'{doc_name}-{obj_name}.step'
Import.export(__objs__, step_filename)
実行は、コマンドプロンプトで例えば次のように行います。FreeCAD内のpython.exeを指定します。この時、パスは両端をダブルコーテーション””で囲んで指定します。
"C:\Program Files\FreeCAD 0.20\bin\python.exe" "D:/program/python/CADファイル作成/10_create_cad_freecad.py"
ちなみに、生成したCADファイルは汎用のSTEP書式であるため、下図のようにメッシュ分割も手軽に行えます。
その方法は下記リンク先を参考にして下さい。
hk29.hatenablog.jp
以上
<広告>
リンク
リンク