一、Units
① 查看其他单位转换成系统标准单位:
>>> tu = FreeCAD.Units.parseQuantity
>>> tu('10 m')
10000 mm
>>> tu('3/8 in')
9.525 mm
>>> tu('2*pi rad')
360 deg
>>> tu('sin(pi)')
1.22465e-016
>>> tu('200g')
0.2 kg
>>> tu('2 in')
50.8 mm
② 常见国际单位
| 类型 | 符号 |
|---|---|
| 长度单位 | mm |
| 重量单位 | kg |
| 角度单位 | deg |
| 时间单位 | s |
二、Builtin modules
Builtin模块是主要的FreeCAD模块。 它们包含用于处理常规FreeCAD配置、文档及其内容的工具。
| 模块名 | 作用 |
|---|---|
| FreeCAD | FreeCAD的主要(根)模块,也可以通过FreeCAD解释器中的“App”调用。它包含操作文档及其对象所需的所有内容。 |
| Base API | Base模块包含在FreeCAD模块中,包含FreeCAD中大量使用、不同类型的对象的构造函数。 |
| Vector API | 支持向量的add、cross、multiply、scale等操作 |
| Matrix API | 4x4 矩阵在整个FreeCAD中随处可用 |
| Console API | 该模块包含在FreeCAD模块中,包含将文本发送到FreeCAD输出控制台和状态栏的方法。 |
| FreeCADGui API | 该模块与FreeCAD模块相对应,它包含与用户界面和3D视图相关的所有内容。 |
| Selection API | Selection 子模块是FreeCADGui模块的一部分 |
| Placement API | 在FreeCAD中,Placement定义对象的位置和旋转。具体参考Placement中的Angle, Axis and Position |
| Document Object API | 作为参数,FreeCAD中的文档对象可以具有许多其他属性。这些属性是基本的,存在于每个FreeCAD文档对象中,只需按名称检索对象即可。比如Box对象的Length、Label、Placement等 |
| ViewObject API | 当GUI启动时,FreeCAD文档中的每个对象Object都有一个关联的ViewObject,它驻留在FreeCADGui文档中 |
>>> print FreeCADGui.Selection.getSelection()
[Mesh::Feature object at 00000190053A0AA0]
# 创建文档的对象,并打印对象的相关属性
>>> doc=App.newDocument("stu2")
>>> App.setActiveDocument("stu2")
>>> App.ActiveDocument=App.getDocument("stu2")
>>> Gui.ActiveDocument=Gui.getDocument("stu2")
>>> box=doc.addObject("Part::Box","box")
>>> doc.recompute()
1
>>> print box.PropertiesList
['AttacherType', 'AttachmentOffset', 'ExpressionEngine', 'Height', 'Label', 'Length', 'MapMode', 'MapPathParameter', 'MapReversed', 'Placement', 'Shape', 'Support', 'Width']
三、Workbench creation
① 工作台是FreeCAD命令的容器,用python、C ++或两者混合编码,这有利于将C ++的速度与python的灵活性结合起来。但是,在所有情况下,您的工作台将由一组两个python文件启动。创建方法: 你需要一个文件夹,你喜欢任何名字,放在Mod目录中。文件夹中需要包含Init.py文件,以及可选的InitGui.py文件。当FreeCAD以GUI的方式启动时执行Init.py文件,之后立即执行InitGui.py文件。如果在控制台模式下启动,则不会执行InitGui.py文件。FreeCAD在启动时找到工作台并将工作台的内容添加到界面中。
② 在Init.py文件中,即使FreeCAD在控制台模式下工作,也只需添加一些内容,例如文件导入器和导出器
# Part/Init.py
FreeCAD.addImportType("BREP format (*.brep *.brp)","Part")
FreeCAD.addExportType("BREP format (*.brep *.brp)","Part")
FreeCAD.addImportType("IGES format (*.iges *.igs)","Part")
FreeCAD.addExportType("IGES format (*.iges *.igs)","Part")
FreeCAD.addImportType("STEP with colors (*.step *.stp)","ImportGui")
FreeCAD.addExportType("STEP with colors (*.step *.stp)","ImportGui")
③ 在InitGui.py文件中,您通常会定义一个工作台,其中包含名称,图标和一系列FreeCAD命令。该工作台还定义了FreeCAD加载时执行的功能(尽可能少地执行,不然会是启动速度变慢),工作台激活时执行的功能(开发人员需要做大量工作的地方),工作台停用时执行的功能(这样你可以根据需要移除工件)。
④ 使用C++编写工作台: 在使用C ++时,从尊重FreeCAD的一个基本规则开始:在App部分(可以在控制台模式下运行,没有任何GUI元素)和Gui部分之间分离工作台。Gui部分只在FreeCAD以其完整的GUI环境运行时才会加载。开发C ++工作台时,实际上很可能会做两个模块:一个App和一个Gui,两个模块当然必须可以从python中调用。任何FreeCAD模块(App或Gui)至少包含模块初始化文件。
FreeCAD.addImportType()和addEXportType()函数允许您提供文件类型的名称和扩展名,以及负责导入的python模块。
⑤ 使用python编写工作台:
# Mod/Mesh/InitGui.py
class MeshWorkbench (Workbench):
"Mesh workbench object"
def __init__(self):
self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/Mesh/Resources/icons/MeshWorkbench.svg"
self.__class__.MenuText = "Mesh Design"
self.__class__.ToolTip = "Mesh design workbench"
def Initialize(self):
import Mesh
import MeshGui
try:
import flatmesh
import MeshFlatteningCommand
except ImportError as e:
import FreeCAD
FreeCAD.Console.PrintLog((str(e)))
def GetClassName(self):
return "MeshGui::Workbench"
Gui.addWorkbench(MeshWorkbench())
⑥ FreeCAD Command
FreeCAD命令是FreeCAD接口的基本构建块,它们可以展现为工具栏上的按钮,也可以展现为菜单中的菜单项。但它是同一个命令。命令是一个简单的python类,它必须包含一些预定义的属性和函数,包括定义命令的名称、它的图标以及激活命令时要执行的操作。
# ModDraftDraftTools.py Line命令的部分代码
class Line(Creator):
"The Line FreeCAD command definition"
def __init__(self, wiremode=False):
Creator.__init__(self)
self.isWire = wiremode
def GetResources(self):
return {'Pixmap' : 'Draft_Line',
'Accel' : "L,I", # 快捷键
'MenuText': QtCore.QT_TRANSLATE_NOOP("Draft_Line", "Line"),
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Draft_Line", "Creates a 2-point line. CTRL to snap, SHIFT to constrain")}
FreeCAD commands are the basic building block of the FreeCAD interface. They can appear as a button on toolbars, and as a menu entry in menus. But it is the same command. A command is a simple python class, that must contain a couple of predefined attributes and functions, that define the name of the command, its icon, and what to do when the command is activated.
最后
以上就是文艺萝莉最近收集整理的关于FreeCAD学习笔记——Units、Builtin modules和Workbench creation的全部内容,更多相关FreeCAD学习笔记——Units、Builtin内容请搜索靠谱客的其他文章。
发表评论 取消回复