FreeCAD的python脚本编写

简介

FreeCAD是一款强大的开源CAD软件,可以与python无缝对解,使用python来驱动三维几何的构建,具有很高的灵活性。本文主要讨论一下录制宏的方法,以及如何驱动特定参数

方法

打开FreeCAD软件,点击录制宏 按钮后,建立如下简单的三维模型
模型建好之后,结束宏录制,可以得到一段python代码
阅读代码,不难理解何处对哪些尺寸进行了赋值。所以只需要将这些"尺寸数值"用python变量替代即可实现参数化驱动。

以下代码利用python的random库,将圆柱半径、长度、矩形孔宽度和高度设置为随机数,并把生成的几何保存为webGL格式

python 复制代码
# -*- coding: utf-8 -*-

# Macro Begin: /home/liwentao/.local/share/FreeCAD/Macro/myRandomCoin.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import PartDesign
import PartDesignGui
import Sketcher
import random

R= random.uniform(20, 40)
L=random.uniform(50, 200)
DX=random.uniform(5, 15)
DY=random.uniform(5, 15)

### Begin command Std_Delete
if App.getDocument('Unnamed').getObject('Body') is not None:
	App.getDocument('Unnamed').getObject('Body').removeObjectsFromDocument()
	App.getDocument('Unnamed').removeObject('Body')
	App.getDocument('Unnamed').recompute()
### End command Std_Delete

### Begin command PartDesign_Body
App.activeDocument().addObject('PartDesign::Body','Body')
App.ActiveDocument.getObject('Body').Label = '实体'
App.ActiveDocument.recompute()
### End command PartDesign_Body
# Gui.Selection.addSelection('Unnamed','Body')
# Gui.Selection.clearSelection()
# Gui.Selection.addSelection('Unnamed','Body','Origin.XY_Plane.')
App.getDocument('Unnamed').getObject('Body').newObject('Sketcher::SketchObject','Sketch')
App.getDocument('Unnamed').getObject('Sketch').Support = (App.getDocument('Unnamed').getObject('XY_Plane'),[''])
App.getDocument('Unnamed').getObject('Sketch').MapMode = 'FlatFace'
App.ActiveDocument.recompute()
# Gui.Selection.clearSelection()
App.getDocument('Unnamed').getObject('Sketch').addGeometry(Part.Circle(App.Vector(0.0,0.000000,0),App.Vector(0,0,1),R),False)
App.getDocument('Unnamed').getObject('Sketch').addConstraint(Sketcher.Constraint('PointOnObject',0,3,-1)) 
App.ActiveDocument.recompute()
# Gui.Selection.addSelection('Unnamed','Body','Sketch.Edge1',19.808,16.0901,0)
App.getDocument('Unnamed').getObject('Sketch').addConstraint(Sketcher.Constraint('Diameter',0,R*2))
#App.getDocument('Unnamed').getObject('Sketch').setDatum(1,App.Units.Quantity('49.570000 mm'))
App.ActiveDocument.recompute()
App.ActiveDocument.recompute()
# Gui.Selection.clearSelection()
# Gui.Selection.addSelection('Unnamed','Body','Sketch.Vertex1',0.954255,0,0)
# Gui.Selection.addSelection('Unnamed','Body','Sketch.V_Axis',0,-0.04614,0)
App.getDocument('Unnamed').getObject('Sketch').addConstraint(Sketcher.Constraint('PointOnObject',0,3,-2))
App.ActiveDocument.recompute()
# Gui.Selection.clearSelection()
App.ActiveDocument.recompute()
# Gui.Selection.addSelection('Unnamed','Body','Sketch.')
App.getDocument('Unnamed').recompute()
### Begin command PartDesign_Pad
App.getDocument('Unnamed').getObject('Body').newObject('PartDesign::Pad','Pad')
App.getDocument('Unnamed').getObject('Pad').Profile = App.getDocument('Unnamed').getObject('Sketch')
App.getDocument('Unnamed').getObject('Pad').Length = L
App.ActiveDocument.recompute()
App.getDocument('Unnamed').getObject('Pad').ReferenceAxis = (App.getDocument('Unnamed').getObject('Sketch'),['N_Axis'])
App.getDocument('Unnamed').getObject('Sketch').Visibility = False
App.ActiveDocument.recompute()
### End command PartDesign_Pad
# Gui.Selection.clearSelection()
App.getDocument('Unnamed').getObject('Pad').TaperAngle = 0.000000
App.getDocument('Unnamed').getObject('Pad').UseCustomVector = 0
App.getDocument('Unnamed').getObject('Pad').Direction = (0, 0, 1)
App.getDocument('Unnamed').getObject('Pad').ReferenceAxis = (App.getDocument('Unnamed').getObject('Sketch'), ['N_Axis'])
App.getDocument('Unnamed').getObject('Pad').AlongSketchNormal = 1
App.getDocument('Unnamed').getObject('Pad').Type = 0
App.getDocument('Unnamed').getObject('Pad').UpToFace = None
App.getDocument('Unnamed').getObject('Pad').Reversed = 0
App.getDocument('Unnamed').getObject('Pad').Midplane = 0
App.getDocument('Unnamed').getObject('Pad').Offset = 0
App.getDocument('Unnamed').recompute()
App.getDocument('Unnamed').getObject('Sketch').Visibility = False
# Gui.Selection.addSelection('Unnamed','Body')
# Gui.Selection.clearSelection()
# Gui.Selection.addSelection('Unnamed','Body','Origin.XY_Plane.',-18.5177,-32.2205,0)
App.getDocument('Unnamed').getObject('Body').newObject('Sketcher::SketchObject','Sketch001')
App.getDocument('Unnamed').getObject('Sketch001').Support = (App.getDocument('Unnamed').getObject('XY_Plane'),[''])
App.getDocument('Unnamed').getObject('Sketch001').MapMode = 'FlatFace'
App.ActiveDocument.recompute()
# Gui.Selection.clearSelection()
### Begin command Sketcher_ViewSketch
### End command Sketcher_ViewSketch
### Begin command Sketcher_ViewSection
#ActiveSketch.ViewObject.TempoVis.sketchClipPlane(ActiveSketch, None, False)

### End command Sketcher_ViewSection
App.getDocument('Unnamed').getObject('Sketch001').addConstraint(Sketcher.Constraint('Coincident',4,1,-1,1)) 
App.ActiveDocument.recompute()
# Gui.Selection.addSelection('Unnamed','Body','Sketch001.Edge4',2.50686,-10.9,0)
App.getDocument('Unnamed').getObject('Sketch001').addConstraint(Sketcher.Constraint('DistanceX',3,2,3,1,DX))
#App.getDocument('Unnamed').getObject('Sketch001').setDatum(10,App.Units.Quantity('21.870000 mm'))
App.ActiveDocument.recompute()
App.ActiveDocument.recompute()
# Gui.Selection.clearSelection()
# Gui.Selection.addSelection('Unnamed','Body','Sketch001.Edge3',10.935,6.8364,0)
App.getDocument('Unnamed').getObject('Sketch001').addConstraint(Sketcher.Constraint('DistanceY',2,2,2,1,DY))
#App.getDocument('Unnamed').getObject('Sketch001').setDatum(11,App.Units.Quantity('21.800000 mm'))
App.ActiveDocument.recompute()
App.ActiveDocument.recompute()
# Gui.Selection.clearSelection()
App.ActiveDocument.recompute()
# Gui.Selection.addSelection('Unnamed','Body','Sketch001.')
App.getDocument('Unnamed').recompute()
### Begin command PartDesign_Pocket
App.getDocument('Unnamed').getObject('Body').newObject('PartDesign::Pocket','Pocket')
App.getDocument('Unnamed').getObject('Pocket').Profile = App.getDocument('Unnamed').getObject('Sketch001')
App.getDocument('Unnamed').getObject('Pocket').Length = 5
App.ActiveDocument.recompute()
App.getDocument('Unnamed').getObject('Pocket').ReferenceAxis = (App.getDocument('Unnamed').getObject('Sketch001'),['N_Axis'])
App.getDocument('Unnamed').getObject('Sketch001').Visibility = False
App.ActiveDocument.recompute()
### End command PartDesign_Pocket
# Gui.Selection.clearSelection()
App.getDocument('Unnamed').getObject('Pocket').UseCustomVector = 0
App.getDocument('Unnamed').getObject('Pocket').Direction = (0, 0, -1)
App.getDocument('Unnamed').getObject('Pocket').ReferenceAxis = (App.getDocument('Unnamed').getObject('Sketch001'), ['N_Axis'])
App.getDocument('Unnamed').getObject('Pocket').AlongSketchNormal = 1
App.getDocument('Unnamed').getObject('Pocket').Type = 1
App.getDocument('Unnamed').getObject('Pocket').UpToFace = None
App.getDocument('Unnamed').getObject('Pocket').Reversed = 1
App.getDocument('Unnamed').getObject('Pocket').Midplane = 0
App.getDocument('Unnamed').getObject('Pocket').Offset = 0
App.getDocument('Unnamed').recompute()
App.getDocument('Unnamed').getObject('Pad').Visibility = False
App.getDocument('Unnamed').getObject('Sketch001').Visibility = False

### Begin command Std_Export
__objs__ = []
__objs__.append(FreeCAD.getDocument("Unnamed").getObject("Body"))
import importWebGL
if hasattr(importWebGL, "exportOptions"):
    options = importWebGL.exportOptions(u"/home/liwentao/demo/freeCAD/RandomCoin.html")
    importWebGL.export(__objs__, u"/home/liwentao/demo/freeCAD/RandomCoin.html", options)
else:
    importWebGL.export(__objs__, u"/home/liwentao/demo/freeCAD/RandomCoin.html")
del __objs__
### End command Std_Export
# Macro End: /home/liwentao/.local/share/FreeCAD/Macro/myRandomCoin.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++

保存得到的webGL格式文件可用浏览器打开

相关推荐
啟明起鸣9 小时前
【Go 与云原生】先从 Go 对与云原生的依赖关系讲起,再讲讲 一个简单的 Go 项目热热身
开发语言·云原生·golang
oioihoii10 小时前
《C语言点滴》——笑着入门,扎实成长
c语言·开发语言
B站_计算机毕业设计之家10 小时前
深度学习:python人脸表情识别系统 情绪识别系统 深度学习 神经网络CNN算法 ✅
python·深度学习·神经网络·算法·yolo·机器学习·cnn
waves浪游10 小时前
基础开发工具(下)
linux·运维·服务器·开发语言·c++
QX_hao10 小时前
【Go】--log模块的使用
开发语言·后端·golang
爱编程的鱼10 小时前
ESLint 是什么?
开发语言·网络·人工智能·网络协议
小陈不好吃10 小时前
Spring Boot配置文件加载顺序详解(含Nacos配置中心机制)
java·开发语言·后端·spring
合作小小程序员小小店10 小时前
web网页开发,在线%聚类,微博,舆情%系统,基于python,pycharm,django,nlp,kmeans,mysql
python·pycharm·kmeans·聚类·sklearn·kmean
Dan.Qiao10 小时前
python读文件readline和readlines区别和惰性读
开发语言·python·惰性读文件
渡我白衣11 小时前
链接的迷雾:odr、弱符号与静态库的三国杀
android·java·开发语言·c++·人工智能·深度学习·神经网络