当前位置:   article > 正文

Freecad插件开发入门、装配动画_freecad 设计python gui插件

freecad 设计python gui插件

Freecad支持C++、Python插件。

2024.3.5,当前最新版本Freecad 0.21.2。

一、Python Console

打开Freecad的 菜单: View  -> Panels -> Python Console

可以在控制台输入:

dir()

将显示当前所有已载入模型。

dir(App)

将显示App的模块。

注意大小写。

  1. from PySide.QtWidgets import QWidget, QPushButton, QApplication, QMessageBox
  2. QMessageBox.information(None, 'about Exploded Assembly', 'created by stonewu。2024')

注意当前是qt5

二、插件HelloWorld

在FreeCAD_0.21.2\Mod  的目录下创建一个目录:Stone

在Stone目录下放置python脚本:InitGui.py

  1. class StoneWorkbench ( Workbench ):
  2. "Stone workbench object"
  3. def __init__(self):
  4. self.__class__.Icon = FreeCAD.getResourceDir() + "Mod/Start/Resources/icons/StartWorkbench.svg"
  5. self.__class__.MenuText = "Stone2024"
  6. self.__class__.ToolTip = "Stone workbench"
  7. def Initialize(self):
  8. # load the module
  9. import StartGui
  10. import Start
  11. def GetClassName(self):
  12. return "StartGui::Workbench"
  13. Gui.addWorkbench(StoneWorkbench())

重启Freecad后,在Workbenches列表里,就可以看到一个新插件Stone2024。

当然,这个插件啥功能也没有。

三、装配动画插件

ExplodedAssembly

Features

  1. Create nice explosions of assemblies graphically (no code at all!)
  2. Create sub-exploded groups
  3. Give rotation to screws and nuts for realistic disassembles
  4. Use the provided auxiliary assembly tools to place your parts together
  5. TODO feature: create trajectory from wires and sketches

插件演示效果如下:

动画演示的插件,我做了简单修改:添加了2个工具按钮,不再创建空白文档。

原始插件是外国人开发,代码下载地址: stoneold/ExplodedAssembly

修改InitGui.py

  1. __title__="Exploded Assembly Workbench for FreeCAD"
  2. __author__ = "stonewu/Javier Martínez García"
  3. __url__ = "https://blog.csdn.net/stonewu"
  4. import FreeCAD
  5. import FreeCADGui
  6. class ExplodedAssembly(Workbench):
  7. import EAInit# this is needed to load the workbench icon
  8. # __dir__ = os.path.dirname( __file__ ) # __file__ is not working
  9. Icon = EAInit.__dir__ + '/icons/WorkbenchIcon.svg'
  10. MenuText = 'AssemblyExploded'
  11. ToolTip = 'Assemble parts and create exploded drawings and animations'
  12. def GetClassName(self):
  13. return 'Gui::PythonWorkbench'
  14. def Initialize(self):
  15. import EAInit
  16. self.CreateExplodedAssemblyTools = ['CreateExplodedAssembly',
  17. 'About']
  18. #added by stone 2024-03-05 添加工具栏按钮
  19. self.CreationTools = ['CreateBoltGroup',
  20. 'CreateSimpleGroup',
  21. 'ModifyIndividualObjectTrajectory',
  22. 'PlaceBeforeSelectedTrajectory',
  23. 'ToggleTrajectoryVisibility']
  24. self.CameraAnimation = ['CreateManualCamera',
  25. 'CreateEdgeCamera',
  26. 'CreateFollowCamera']
  27. self.AnimationControlTools = ['GoToStart',
  28. 'PlayBackward',
  29. 'StopAnimation',
  30. 'PlayForward',
  31. 'GoToEnd',
  32. 'GoToSelectedTrajectory']
  33. self.AuxiliaryAssemblyTools = ['AlignToEdge',
  34. 'Rotate15',
  35. 'PointToPoint',
  36. 'PlaceConcentric']
  37. self.Menu_tools = ['CreateExplodedAssembly',
  38. 'CreateBoltGroup',
  39. 'CreateSimpleGroup',
  40. 'ModifyIndividualObjectTrajectory',
  41. 'PlaceBeforeSelectedTrajectory',
  42. 'GoToSelectedTrajectory',
  43. 'GoToStart',
  44. 'PlayBackward',
  45. 'StopAnimation',
  46. 'PlayForward',
  47. 'GoToEnd',
  48. 'ToggleTrajectoryVisibility',
  49. 'AlignToEdge',
  50. 'Rotate15',
  51. 'PointToPoint',
  52. 'PlaceConcentric',
  53. 'LoadExampleFile',
  54. 'LoadExample2']
  55. #added by stone 2024-03-05 添加工具栏
  56. self.appendToolbar('ExplodedAssemblyTools', self.CreateExplodedAssemblyTools)
  57. self.appendToolbar('ExplodedAssemblyCreationTools', self.CreationTools)
  58. #self.appendToolbar('ExplodedAssemblyCameraTools', self.CameraAnimation)
  59. self.appendToolbar('ExplodedAssemblyAnimationControlTools', self.AnimationControlTools)
  60. self.appendToolbar('ExplodedAssemblyAuxiliarAssemblyTools', self.AuxiliaryAssemblyTools)
  61. self.appendMenu('ExplodedAssembly', self.Menu_tools)
  62. def Activated(self):
  63. #import ExplodedAssembly as ea
  64. #if not(FreeCAD.ActiveDocument):
  65. # FreeCAD.newDocument()
  66. #ea.checkDocumentStructure()
  67. FreeCAD.Console.PrintMessage('Exploded Assembly workbench loaded\n')
  68. FreeCADGui.addWorkbench(ExplodedAssembly)

 修改EAInit.py

  1. #请参考原代码,在合适位置 添加以下代码
  2. # 有疑问,欢迎咨询stone wu 微信:23245175
  3. from PySide.QtWidgets import QWidget, QPushButton, QApplication, QMessageBox
  4. class CreateExplodedAssembly:
  5. def GetResources(self):
  6. return {'Pixmap': __dir__ + '/icons/WorkbenchIcon.svg',
  7. 'MenuText': 'CreateExplodedAssembly',
  8. 'ToolTip': 'CreateExplodedAssembly'}
  9. def IsActive(self):
  10. if not(FreeCADGui.ActiveDocument):
  11. return False
  12. return True
  13. def Activated(self):
  14. import ExplodedAssembly as ea
  15. ea.checkDocumentStructure()
  16. class About:
  17. def GetResources(self):
  18. return {'Pixmap': __dir__ + '/icons/ShareCenter.svg',
  19. 'MenuText': 'About ExplodedAssembly',
  20. 'ToolTip': 'ExplodedAssembly created by stonewu'}
  21. def IsActive(self):
  22. return True
  23. def Activated(self):
  24. FreeCAD.Console.PrintMessage('about, Exploded Assembly created by stonewu\n')
  25. QMessageBox.information(None, 'about Exploded Assembly', 'created by stonewu。2024')
  26. if FreeCAD.GuiUp:
  27. FreeCAD.Gui.addCommand('CreateExplodedAssembly', CreateExplodedAssembly())
  28. FreeCAD.Gui.addCommand('About', About())

修改ExplodedAssembly.py

  1. # 只修改checkDocumentStructure方法
  2. def checkDocumentStructure():
  3. # checks the existence of the folder 'ExplodedAssembly' and creates it if not
  4. try:
  5. folder = FreeCAD.ActiveDocument.ExplodedAssembly
  6. FreeCAD.Console.PrintMessage('Exploded Assembly exist\n')
  7. except:
  8. folder = FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython', 'ExplodedAssembly')
  9. ExplodedAssemblyFolder(folder)
  10. ExplodedAssemblyFolderViewProvider(folder.ViewObject)
  11. FreeCAD.Console.PrintMessage('Exploded Assembly created\n')

四、继续学习使用ExplodedAssembly

装配动画:

学习制作装配体,并制作动画

最近在做利用知识图谱,自动解析装配模型,并生成装配模型。

欢迎3维几何内核相关研发人员联系交流,共同进步。微信:23245175 ,加好友,敬请备注:几何内核。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/865241
推荐阅读
相关标签
  

闽ICP备14008679号