FreeCAD二次开发案例:机械零件自动生成工具开发

FreeCAD二次开发案例:机械零件自动生成工具开发

【免费下载链接】FreeCAD This is the official source code of FreeCAD, a free and opensource multiplatform 3D parametric modeler. 【免费下载链接】FreeCAD 项目地址: https://gitcode.com/GitHub_Trending/fr/freecad

在机械设计领域,工程师常常需要重复创建标准零件,如齿轮、链轮等,传统手动建模不仅耗时且易出错。本文将以FreeCAD为基础,通过开发一个机械零件自动生成工具,展示如何利用FreeCAD的Python API实现参数化建模,显著提升设计效率。

开发环境与项目结构

FreeCAD作为一款开源的3D参数化建模软件,提供了丰富的二次开发接口。本工具基于FreeCAD的PartDesign模块开发,主要涉及以下文件和目录:

参数化建模基础

参数化建模是通过调整参数来驱动模型形状的技术。在FreeCAD中,可通过Python API创建自定义参数化对象。以下是创建基本参数化对象的框架:

def makeCustomPart(name):
    # 创建Part2DObjectPython对象
    obj = FreeCAD.ActiveDocument.addObject("Part::Part2DObjectPython", name)
    # 附加自定义属性和行为
    _CustomPart(obj)
    if FreeCAD.GuiUp:
        # 附加视图提供者
        _ViewProviderCustomPart(obj.ViewObject)
    return obj

class _CustomPart:
    def __init__(self, obj):
        # 设置对象类型
        self.Type = "CustomPart"
        # 添加自定义属性
        obj.addProperty("App::PropertyLength", "Length", "Dimensions", "Length of the part")
        obj.Length = "100 mm"
        obj.Proxy = self
    
    def execute(self, obj):
        # 模型生成逻辑
        shape = Part.makeBox(obj.Length.Value, 50, 20)
        obj.Shape = shape

齿轮生成工具实现

以渐开线齿轮为例,分析自动生成工具的实现过程。FreeCAD的PartDesign模块已内置齿轮生成功能,其核心实现位于src/Mod/PartDesign/InvoluteGearFeature.py

参数定义

_InvoluteGear类的初始化方法中,定义了齿轮的关键参数:

def _ensure_properties(self, obj, is_restore):
    # 添加齿轮参数
    obj.addProperty("App::PropertyInteger", "NumberOfTeeth", "Gear", "Number of gear teeth")
    obj.addProperty("App::PropertyLength", "Modules", "Gear", "Module of the gear")
    obj.addProperty("App::PropertyAngle", "PressureAngle", "Gear", "Pressure angle of gear teeth")
    # 设置默认值
    obj.NumberOfTeeth = 26
    obj.Modules = "2.5 mm"
    obj.PressureAngle = "20 deg"

几何生成

execute方法是生成齿轮几何形状的核心,通过调用fcgear库实现渐开线齿轮的计算:

def execute(self, obj):
    w = fcgear.FCWireBuilder()
    # 根据内外齿轮选择不同的生成函数
    generator_func = involute.CreateExternalGear if obj.ExternalGear else involute.CreateInternalGear
    # 调用齿轮生成函数
    generator_func(w, obj.Modules.Value, obj.NumberOfTeeth, obj.PressureAngle.Value,
        split=obj.HighPrecision, addCoeff=obj.AddendumCoefficient, dedCoeff=obj.DedendumCoefficient,
        filletCoeff=obj.RootFilletCoefficient, shiftCoeff=obj.ProfileShiftCoefficient)
    # 创建齿轮轮廓
    gearw = Part.Wire([o.toShape() for o in w.wire])
    obj.Shape = gearw

用户界面

通过任务面板实现参数交互界面,src/Mod/PartDesign/TaskExtrudeParameters.cpp定义了参数编辑窗口,用户可实时调整参数并预览结果:

class _InvoluteGearTaskPanel:
    def __init__(self, obj, mode):
        self.obj = obj
        # 加载UI文件
        self.form=FreeCADGui.PySideUic.loadUi("InvoluteGearFeature.ui")
        # 绑定参数变化事件
        self.form.spinBox_NumberOfTeeth.valueChanged.connect(self.updateGear)

自定义零件生成工具开发

基于上述齿轮生成原理,开发一个自定义零件生成工具的步骤如下:

1. 创建FeaturePython对象

class _CustomPart:
    def __init__(self, obj):
        self.Type = "CustomPart"
        # 添加自定义属性
        obj.addProperty("App::PropertyLength", "Length", "Dimensions", "Length of the part")
        obj.addProperty("App::PropertyLength", "Width", "Dimensions", "Width of the part")
        obj.addProperty("App::PropertyLength", "Height", "Dimensions", "Height of the part")
        obj.Length = "100 mm"
        obj.Width = "50 mm"
        obj.Height = "20 mm"
        obj.Proxy = self
    
    def execute(self, obj):
        # 创建长方体
        shape = Part.makeBox(obj.Length.Value, obj.Width.Value, obj.Height.Value)
        obj.Shape = shape

2. 实现视图提供者

class _ViewProviderCustomPart:
    def __init__(self, vobj):
        vobj.Proxy = self
    
    def getIcon(self):
        return ":/icons/PartDesign_CustomPart.svg"
    
    def attach(self, vobj):
        self.ViewObject = vobj
        self.Object = vobj.Object

3. 创建命令

class CommandCustomPart:
    def GetResources(self):
        return {'Pixmap': 'PartDesign_CustomPart',
                'MenuText': "Custom Part",
                'ToolTip': "Create a custom part"}
    
    def Activated(self):
        FreeCAD.ActiveDocument.openTransaction("Create custom part")
        obj = makeCustomPart("CustomPart")
        FreeCAD.ActiveDocument.recompute()
    
    def IsActive(self):
        return FreeCAD.ActiveDocument is not None

工具集成与扩展

添加到工作bench

修改src/Mod/PartDesign/Workbench.cpp,将自定义工具添加到PartDesign工作台:

void Workbench::Initialize()
{
    // 添加命令到工具栏
    Gui::ToolBarItem* partdesign = new Gui::ToolBarItem(Gui::ToolBarItem::getRoot(), "PartDesign");
    partdesign->setCommand("PartDesign");
    partdesign->addCommand("PartDesign_CustomPart");
}

支持更多零件类型

参考齿轮生成的实现,可扩展支持其他标准零件:

总结与展望

通过FreeCAD的二次开发接口,我们可以快速构建机械零件自动生成工具,显著提升设计效率。未来可进一步扩展:

  1. 参数化库:建立标准零件参数化库,如ISO标准件
  2. 批量生成:开发基于Excel表格的批量零件生成功能
  3. CAD标准检查:集成设计规则检查,确保零件符合行业标准

本案例展示了FreeCAD二次开发的基本流程和关键技术点,开发者可根据实际需求扩展更多功能。完整代码可参考src/Mod/PartDesign/目录下的相关文件。

【免费下载链接】FreeCAD This is the official source code of FreeCAD, a free and opensource multiplatform 3D parametric modeler. 【免费下载链接】FreeCAD 项目地址: https://gitcode.com/GitHub_Trending/fr/freecad

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值