FreeCAD学习笔记——Mesh Scripting、Part Module和Code snippets

一、 Mesh Scripting

一个完整的例子,计算一个球体和一个与球体相交的圆柱体之间的交集。

>>> import Mesh, BuildRegularGeoms
>>> sphere = Mesh.Mesh( BuildRegularGeoms.Sphere(5.0, 50) )
>>> cylinder = Mesh.Mesh( BuildRegularGeoms.Cylinder(2.0, 10.0, True, 1.0, 50) )
>>> diff = sphere
>>> diff = diff.difference(cylinder)
>>> d = FreeCAD.newDocument()
>>> App.setActiveDocument("Unnamed")
>>> App.ActiveDocument=App.getDocument("Unnamed")
>>> Gui.ActiveDocument=Gui.getDocument("Unnamed")
>>> d.addObject("Mesh::Feature","Diff_Sphere_Cylinder").Mesh=diff
>>> d.recompute()
0

在这里插入图片描述
在这里插入图片描述

二、Part Module

① 神奇的发现,使用Part模块也能绘制各种二维图形。终于明白了FreeCAD中App和Gui的区别!!!

>>> import Part,PartGui 
>>> doc=App.newDocument()  
>>> App.setActiveDocument("Unnamed")
>>> App.ActiveDocument=App.getDocument("Unnamed")
>>> Gui.ActiveDocument=Gui.getDocument("Unnamed")
>>> l=Part.LineSegment()
>>> l.StartPoint=(0.0,0.0,0.0)
>>> l.EndPoint=(1.0,1.0,1.0)
>>> doc.addObject("Part::Feature","Line").Shape=l.toShape() 
>>> doc.recompute()
1

在这里插入图片描述
也可以使用已经封装好的函数:

>>> l2=Part.makeLine((0,0,0),(10,0,0))
>>> doc.addObject("Part::Feature","line").Shape=l2
>>> doc.recompute()
1

深入学习链接:
Topological data scripting
pythonOCC – 3D CAD for python

三、其他

① 界面相关

Scripted objects:除了标准对象类型(如Mesh和Part对象)之外,FreeCAD还提供了构建100%python脚本对象(称为Python Features)的惊人可能性。这些对象的行为与任何其他FreeCAD对象完全相同,并在文件保存/加载时自动保存和恢复。

这些对象使用python的json模块保存在FreeCAD的FcStd文件中。json模块将python对象转换为字符串,允许将其添加到已保存的文件中。在加载时,json模块使用该字符串重新创建原始对象,前提是它可以访问创建该对象的源代码。这意味着如果您保存这样的自定义对象,并在没有生成该对象的python代码的机器上打开它,则不会重新创建该对象。如果将这些对象分发给其他人,则需要分发创建它的python脚本

Python Features遵循与所有FreeCAD features相同的规则:它们分为App和GUI部分。App部分,Document对象,定义了对象的几何信息。而GUI部分,View Provider Object,定义了如何在屏幕上绘制对象。与任何其他FreeCAD feature一样,View Provider Object仅在GUI中运行FreeCAD时可用。属性必须是FreeCAD提供的任何预定义属性类型,并且将显示在属性视图窗口中,以便用户可以编辑它们。这样,FeaturePython对象就是真正完全参数化的。您可以单独定义Object及其ViewObject的属性。

Code snippets

  • InitGui.py的例子
  • 典型的module file
  • 导入一个新的文件类型(涉及Init.py和open()
  • 使用doc.addObject("Part::Feature","Line").Shape=l.toShape()的方式制一条线
  • 使用p=doc.addObject("Part::Polygon","Polygon") .Nodes=n的方式绘制一个多边形
  • 向group中添加或移除对象
  • 添加一个网格
  • 使用 doc.addObject("Part::Feature", "Circle") .Shape = c.toShape()的方式绘制一个圆
  • 获取并更改对象视图属性(颜色、线宽)
  • 通过Python在3D查看器中观察鼠标事件
  • report view展示按下的键和事件命令
  • 在Python中操作scenegraph
  • 在scenegraph中添加和删除对象
  • Saves the sceneGraph with a rotation in a series of 36 files in the X Y Z axis
  • 将自定义小部件添加到界面
  • 将选项卡添加到组合视图
  • 启用或禁用窗口
  • 打开自定义网页
  • 获取打开的网页的HTML内容
  • 检索并使用3个选定点或对象的坐标
  • 列出所有对象
  • 列出对象的尺寸,并给出其名称
  • 使用鼠标单击操作的驻留功能
  • 查找 - 选择光标下方的所有元素
  • 列出对象的组件
  • 列出Properties列表
  • 添加一个 Property “Comment”
  • 查找和解码对象信息
  • 手动搜索带标签的元素FreeCAD.ActiveDocument.getObjectsByLabel(“Cylindre”)[0]
  • 显示所选项目的笛卡尔坐标
  • 选择文档中的所有对象(遍历文档对象、通过对象名查找对象)
  • 选择对象的一个面Gui.Selection.addSelection(loch,faceSelect)
  • 创建一个对象到相机的位置
  • 找到一个面的法向量
  • 读写表达式doc.getObject(obj.Name).setExpression('Radius', u'2mm')
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Module developer’s guide to FreeCAD source code by Qingfeng Xia http://www.iesensor.com • 2015-09-18 version 0.1 for FreeCAD version 0.16-dev • 2016-09-18 version 0.2 for FreeCAD version 0.17-dev License of this book This ebook is licensed the same as FreeCAD document license CC-BY 3.0 http://creativecommons.org/licenses/by/3.0/Contents 1 FreeCAD overview and architecture 7 1.1 Introduction to FreeCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2 Key features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3 Software architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.1 Key software libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.2 Mixed python and c++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3.3 GPL code will not be included into installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4 How 3D model are renderred . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4.1 Selection of 3D visualization libarary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4.2 Discussion of 3D rendering library selection on FreeCAD Forum . . . . . . . . . . . . . . . . . . . . . 8 1.5 Roadmap of FreeCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5.1 Keep updated with main components: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5.2 C++11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.5.3 Pyside 2 project for Qt 5.x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2 Organisation of FreeCAD source code 11 2.1 Build system for FreeCAD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.1.1 Analysis of src/cMake/SMesh.cMake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.2 List of files and folders in FreeCAD source folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 List of modules in FreeCAD Mod folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4 Learning path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5 Learning OpenInventor/Coin3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5.1 OpenInventor in FreeCAD’s ViewProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.5.2 Important classes in OpenInventor/Coin3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.5.3 Window System integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5.4 Pivy: Coin3D ’s Python wrapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3 Base, App and Main module 19 3.1 List of header files in Base folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.1.1 Frequently included headers files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1.2 Correct way of using Sequencer in try-catch block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1.3 String enconding utf8 and conversion into wchar_t QString . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2 Type, BaseClass, PyObjectBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.1 Type system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.2.2 src/Base/BaseClass.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2.3 src/Base/PyObjectBase.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.4 src/Base/Persistence.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.5 GeoFeature: Base class of all geometric document objects . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.3 Unit scheme for physial quantity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.3.1 src/Base/Unit.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.3.2 src/Base/Quantity.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.4 List of header files in App folder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.5 Property framewrok . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.5.1 Naming of property and PropertyEditor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.5.2 src/App/PropertyStandard.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5.3 PropertyEnumeration, see src/App/Enumeration.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5.4 Geometry related property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5.5 File related property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值