FreeCAD的python脚本编写

简介

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

方法

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

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

# -*- coding: utf-8 -*-# Macro Begin: /home/liwentao/.local/share/FreeCAD/Macro/myRandomCoin.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import PartDesign
import PartDesignGui
import Sketcher
import randomR= 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格式文件可用浏览器打开

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/668032.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

建筑行业数字化:从设计到运维的全面革新

随着科技的快速发展,数字化技术在各行各业中的应用越来越广泛。建筑行业作为传统产业,也在积极拥抱数字化技术,以提高效率、降低成本并实现可持续发展。本文将主要探讨建筑行业数字化的几个关键领域,包括建筑设计数字化、施工管理…

配置visualsvn提交后自动邮件通知

参考: https://blog.csdn.net/wiker_yong/article/details/10334967 # -*- coding: utf-8 -*- import sys import os import smtplib from email.mime.text import MIMEText from email.header import Headermail_host smtp.163.com #发送邮件的smtp地址 mail_us…

JS第二天、原型、原型链、正则

☆☆☆☆ 什么是原型? 构造函数的prototype 就是原型 专门保存所有子对象共有属性和方法的对象一个对象的原型就是它的构造函数的prototype属性的值。prototype是哪来的?所有的函数都有一个prototype属性当函数被创建的时候,prototype属性…

记录分享Spring Boot集成MQTT(配有简单实现示例)

Spring Boot集成MQTT是一个相对复杂的过程,需要涉及到多个步骤和配置。以下是一个详细的集成文档,包括操作步骤和简单的示例。 一、背景介绍 MQTT是一种轻量级的发布/订阅消息传输协议,广泛应用于物联网领域。Spring Boot是一个快速构建Spr…

vscode的vetur文档格式化失效

如果vscode安装了vetur插件之后,shiftAltF又无法格式化vue文件代码。 解决办法:打开文件 ---> 首选项 ---> 设置,搜索 vetur.format.defaultFormatter.html后将prettier替换勾选为js-beautify-html 注:设置下划线了并可以在…

常见Web前端开发框架推荐

在当今的Web开发领域,前端开发框架扮演着越来越重要的角色。它们不仅简化了开发过程,提高了开发效率,而且为开发者提供了丰富的特性和工具,使得创建复杂、高效的Web应用成为可能。以下是一些常见的Web前端开发框架,以及…

Lightroom Classic 2024 --- LR2024

Lightroom Classic 2024是一款专业的图片编辑和照片管理软件,旨在帮助摄影师高效地处理、编辑和展示他们的作品。通过强大的组织工具,用户可以轻松地管理、搜索和筛选大量的照片。该软件提供了全面的编辑工具,包括调整颜色、曝光、对比度、白…

Linux网络通信——TCP/OSI七层模型/TCP/IP(五层或四层模型)/HTTP报文传输原理

文章目录 消息的传输什么是OSI七层模型OSI七层模型的内容物理层(Physical Layer):数据链路层(Data Link Layer):网络层(Network Layer):传输层(Transport Lay…

On the Spectral Bias of Neural Networks论文阅读

1. 摘要 众所周知,过度参数化的深度神经网络(DNNs)是一种表达能力极强的函数,它甚至可以以100%的训练精度记忆随机数据。这就提出了一个问题,为什么他们不能轻易地对真实数据进行拟合呢。为了回答这个问题,研究人员使用傅里叶分析…

Oracle12c之Sqlplus命令行窗口基本使用

Oracle12c之Sqlplus命令行窗口基本使用 文章目录 Oracle12c之Sqlplus命令行窗口基本使用1. 连接1. 超级用户2. 普通用户1. 创建普通用2. 连接 2. 修改用户连接数1. 查看默认连接最多用户数1. PL/SQL developer中查看2. Sqlplus中查看 2. 查看目前已经连接的用户数3. 修改用户连…

ThinkPHP6进阶教程:如何优雅地使用Auth模块进行权限验证

thinkphp6是一款优秀的php框架,它为我们提供了很多高效的工具和功能。其中,auth授权是一个非常强大的功能,可以帮助我们在应用程序中进行权限管理。本文将介绍如何使用thinkphp6的auth授权。 安装Auth组件 首先,我们需要安装Aut…

设计模式-行为型模式(上)

行为型模式用于描述程序在运行时复杂的流程控制,即描述多个类或对象之间怎样相互协作共同完成单个对象都无法单独完成的任务,它涉及算法与对象间职责的分配。 行为型模式分为类行为模式和对象行为模式,前者采用继承机制来在类间分派行为&…

Docker 搭建mysql 集群(二)

PXC方案 很明显 PXC方案在任何一个节点写入的数据都会同步到其他节点,数据双向同步的(在任何节点上都可以同时读写) 创建MySQL PXC集群 1 安装PXC镜像 docker pull percona/percona-xtradb-cluster:5.7.21 2 为PXC镜像改名 docker tag pe…

如何选择旅游路线,使得假期旅游路费最少?

旅行是许多人的热爱,但是在规划一个完美的假期时,找到最经济的路线常常是一个挑战。这里就需要引入一个著名的优化问题——旅行商问题。本文将介绍TSP的基础知识,并使用MTZ消除子环方法优化一个简单的TSP问题的示例。 旅行商问题简介 TSP&a…

用友U8 Cloud ReportDetailDataQuery SQL注入漏洞复现(QVD-2023-47860)

0x01 产品简介 用友U8 Cloud 提供企业级云ERP整体解决方案,全面支持多组织业务协同,实现企业互联网资源连接。 U8 Cloud 亦是亚太地区成长型企业最广泛采用的云解决方案。 0x02 漏洞概述 用友U8 cloud ReportDetailDataQuery 接口处存在SQL注入漏洞,攻击者未经授权可以访…

自然语言nlp学习五

6-10 文本生成--介绍_哔哩哔哩_bilibili 在自然语言处理(NLP, Natural Language Processing)领域,“sequence”通常是指一个有序的数据集合,它由一系列元素按照特定顺序排列而成。这些元素可以是单词、字符、句子或其他文本单位。…

NLP_语言模型的雏形N-Gram

文章目录 N-Gram 模型1.将给定的文本分割成连续的N个词的组合(N-Gram)2.统计每个N-Gram在文本中出现的次数,也就是词频3.为了得到一个词在给定上下文中出现的概率,我们可以利用条件概率公式计算。具体来讲,就是计算给定前N-1个词时&#xff0…

Adobe Illustrator CEP插件开发入门指南

引言 Adobe Creative Cloud(创意云)中的Illustrator作为一款全球领先的矢量图形设计软件,为设计师提供了丰富的功能和无限的创作可能性。为了进一步增强其功能并满足个性化工作流程需求,Adobe引入了Common Extensibility Platform…

【ROS机器人系统】实验1 熟悉ROS操作系统、熟悉ROS通信架构

文章目录 实验1 熟悉ROS操作系统、熟悉ROS通信架构1、实验目的2、实验设备3、实验内容3.1 安装ROS3.2 ROS常用指令以及小海龟示例roscore命令rosrun命令rosnode 命令roscd 命令rostopic 命令 动手实现1:rosservice 命令roslaunch 命令 动手实现2:动手实现…

免费代理IP的弊端有哪些?使用代理IP前要这样哪些事项?

随着互联网的普及,越来越多的人开始需要使用代理IP来保护自己的隐私或突破网络限制。然而,免费代理IP并非完美的解决方案,它们也存在一些弊端。在本文中,我们将探讨免费代理IP的弊端以及使用代理IP前需要注意的事项。 免费代理IP的…