Freecad支持C++、Python插件。
2024.3.5,当前最新版本Freecad 0.21.2。
一、Python Console
打开Freecad的 菜单: View -> Panels -> Python Console
可以在控制台输入:
dir()
将显示当前所有已载入模型。
dir(App)
将显示App的模块。
注意大小写。
from PySide.QtWidgets import QWidget, QPushButton, QApplication, QMessageBox
QMessageBox.information(None, 'about Exploded Assembly', 'created by stonewu。2024')
注意当前是qt5。
二、插件HelloWorld
在FreeCAD_0.21.2\Mod 的目录下创建一个目录:Stone
在Stone目录下放置python脚本:InitGui.py
class StoneWorkbench ( Workbench ):
"Stone workbench object"
def __init__(self):
self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/Start/Resources/icons/StartWorkbench.svg"
self.__class__.MenuText = "Stone2024"
self.__class__.ToolTip = "Stone workbench"
def Initialize(self):
# load the module
import StartGui
import Start
def GetClassName(self):
return "StartGui::Workbench"
Gui.addWorkbench(StoneWorkbench())
重启Freecad后,在Workbenches列表里,就可以看到一个新插件Stone2024。
当然,这个插件啥功能也没有。
三、装配动画插件
ExplodedAssembly
Features
- Create nice explosions of assemblies graphically (no code at all!)
- Create sub-exploded groups
- Give rotation to screws and nuts for realistic disassembles
- Use the provided auxiliary assembly tools to place your parts together
- TODO feature: create trajectory from wires and sketches
插件演示效果如下:
动画演示的插件,我做了简单修改:添加了2个工具按钮,不再创建空白文档。
原始插件是外国人开发,代码下载地址: stoneold/ExplodedAssembly
修改InitGui.py
__title__="Exploded Assembly Workbench for FreeCAD"
__author__ = "stonewu/Javier Martínez García"
__url__ = "https://blog.csdn.net/stonewu"
import FreeCAD
import FreeCADGui
class ExplodedAssembly(Workbench):
import EAInit# this is needed to load the workbench icon
# __dir__ = os.path.dirname( __file__ ) # __file__ is not working
Icon = EAInit.__dir__ + '/icons/WorkbenchIcon.svg'
MenuText = 'AssemblyExploded'
ToolTip = 'Assemble parts and create exploded drawings and animations'
def GetClassName(self):
return 'Gui::PythonWorkbench'
def Initialize(self):
import EAInit
self.CreateExplodedAssemblyTools = ['CreateExplodedAssembly',
'About']
#added by stone 2024-03-05 添加工具栏按钮
self.CreationTools = ['CreateBoltGroup',
'CreateSimpleGroup',
'ModifyIndividualObjectTrajectory',
'PlaceBeforeSelectedTrajectory',
'ToggleTrajectoryVisibility']
self.CameraAnimation = ['CreateManualCamera',
'CreateEdgeCamera',
'CreateFollowCamera']
self.AnimationControlTools = ['GoToStart',
'PlayBackward',
'StopAnimation',
'PlayForward',
'GoToEnd',
'GoToSelectedTrajectory']
self.AuxiliaryAssemblyTools = ['AlignToEdge',
'Rotate15',
'PointToPoint',
'PlaceConcentric']
self.Menu_tools = ['CreateExplodedAssembly',
'CreateBoltGroup',
'CreateSimpleGroup',
'ModifyIndividualObjectTrajectory',
'PlaceBeforeSelectedTrajectory',
'GoToSelectedTrajectory',
'GoToStart',
'PlayBackward',
'StopAnimation',
'PlayForward',
'GoToEnd',
'ToggleTrajectoryVisibility',
'AlignToEdge',
'Rotate15',
'PointToPoint',
'PlaceConcentric',
'LoadExampleFile',
'LoadExample2']
#added by stone 2024-03-05 添加工具栏
self.appendToolbar('ExplodedAssemblyTools', self.CreateExplodedAssemblyTools)
self.appendToolbar('ExplodedAssemblyCreationTools', self.CreationTools)
#self.appendToolbar('ExplodedAssemblyCameraTools', self.CameraAnimation)
self.appendToolbar('ExplodedAssemblyAnimationControlTools', self.AnimationControlTools)
self.appendToolbar('ExplodedAssemblyAuxiliarAssemblyTools', self.AuxiliaryAssemblyTools)
self.appendMenu('ExplodedAssembly', self.Menu_tools)
def Activated(self):
#import ExplodedAssembly as ea
#if not(FreeCAD.ActiveDocument):
# FreeCAD.newDocument()
#ea.checkDocumentStructure()
FreeCAD.Console.PrintMessage('Exploded Assembly workbench loaded\n')
FreeCADGui.addWorkbench(ExplodedAssembly)
修改EAInit.py
#请参考原代码,在合适位置 添加以下代码
# 有疑问,欢迎咨询stone wu 微信:23245175
from PySide.QtWidgets import QWidget, QPushButton, QApplication, QMessageBox
class CreateExplodedAssembly:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/WorkbenchIcon.svg',
'MenuText': 'CreateExplodedAssembly',
'ToolTip': 'CreateExplodedAssembly'}
def IsActive(self):
if not(FreeCADGui.ActiveDocument):
return False
return True
def Activated(self):
import ExplodedAssembly as ea
ea.checkDocumentStructure()
class About:
def GetResources(self):
return {'Pixmap': __dir__ + '/icons/ShareCenter.svg',
'MenuText': 'About ExplodedAssembly',
'ToolTip': 'ExplodedAssembly created by stonewu'}
def IsActive(self):
return True
def Activated(self):
FreeCAD.Console.PrintMessage('about, Exploded Assembly created by stonewu\n')
QMessageBox.information(None, 'about Exploded Assembly', 'created by stonewu。2024')
if FreeCAD.GuiUp:
FreeCAD.Gui.addCommand('CreateExplodedAssembly', CreateExplodedAssembly())
FreeCAD.Gui.addCommand('About', About())
修改ExplodedAssembly.py
# 只修改checkDocumentStructure方法
def checkDocumentStructure():
# checks the existence of the folder 'ExplodedAssembly' and creates it if not
try:
folder = FreeCAD.ActiveDocument.ExplodedAssembly
FreeCAD.Console.PrintMessage('Exploded Assembly exist\n')
except:
folder = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'ExplodedAssembly')
ExplodedAssemblyFolder(folder)
ExplodedAssemblyFolderViewProvider(folder.ViewObject)
FreeCAD.Console.PrintMessage('Exploded Assembly created\n')
四、继续学习使用ExplodedAssembly
装配动画:
学习制作装配体,并制作动画
最近在做利用知识图谱,自动解析装配模型,并生成装配模型。
欢迎3维几何内核相关研发人员联系交流,共同进步。微信:23245175 ,加好友,敬请备注:几何内核。