freecad 脚本

freecad 的脚本是使用的python解释器, 但是freecad内部的python只有基础的包,对第三方开发包支持度不是很高.有两种方式解决.

一种是使用外部python是可以控制freecad进行绘图的. 具体的可以找网上查查, 好像是安装一个freecad的包即可.

另外一种我认为比较方便, freecad内部直接调用外部python环境的包的方式实现.

下面是第二种的实现过程.
首先是导入外部python环境.

import sys
sys.path += ['~/anaconda3/bin', '', '/opt/ros/noetic/lib/python3/dist-packages', '~/anaconda3/lib/python38.zip', '~/anaconda3/lib/python3.8', '~/anaconda3/lib/python3.8/lib-dynload', '~/.local/lib/python3.8/site-packages', '~/anaconda3/lib/python3.8/site-packages', '~/anaconda3/lib/python3.8/site-packages/web.py-0.62-py3.8.egg', '~/anaconda3/lib/python3.8/site-packages/skidl-0.0.30-py3.8.egg', '~/anaconda3/lib/python3.8/site-packages/graphviz-0.16-py3.8.egg', '~/anaconda3/lib/python3.8/site-packages/kinparse-0.1.2-py3.8.egg', '~/anaconda3/lib/python3.8/site-packages/pycairo-1.20.1-py3.8-linux-x86_64.egg', '~/anaconda3/lib/python3.8/site-packages/unzip_gbk-1.0-py3.8.egg', '~/anaconda3/lib/python3.8/site-packages/locket-0.2.1-py3.8.egg', '~/anaconda3/lib/python3.8/site-packages/IPython/extensions', '~/.ipython']

这里 pyexcel 包在freecad的python内部是没有的, 所以调用外部环境的pyexcel包.

import pyexcel as p
import os

ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def digit(alphabet):
    """26进制 -> 10进制"""
    return sum(ALPHABET.index(a) * (26 ** e) for e, a in enumerate(reversed(alphabet)))

def alph(digit):
    """10进制 -> 26进制"""
    mod, remainder = divmod(digit, 26)
    alphabet = ALPHABET[remainder]
    while mod:
        mod, remainder = divmod(mod, 26)
        alphabet = ALPHABET[remainder] + alphabet
    return alphabet

# 表格信息,下面有格式
tab_nm ='~/freecad_auto_gen/data/rebot_arm.xls'
# doc_nm = 'joint02_2'
book = p.get_book(file_name=tab_nm)
ss = book.to_dict()
for spsheet_dname in ss:

    sp = App.ActiveDocument.getObject(spsheet_dname)
    if sp is None:
        App.activeDocument().addObject('Spreadsheet::Sheet',spsheet_dname)
        sp = App.ActiveDocument.getObject(spsheet_dname)

    sheet = book[spsheet_dname]
    # 清除以前的緩存信息
    for il, l in enumerate(sheet.row):
        for jc, c in enumerate(l):
            sp.setAlias(alph(jc)+str(il+1),'')

    key_ind = sheet.row[0].index("key")
    val_ind = sheet.row[0].index("value")

    for jc, c in enumerate(sheet.row[0]):
        sp.set(alph(jc)+str(1), str(c))

    for il, l in enumerate(sheet.row[1:]):
        for jc, c in enumerate(l):
            sp.set(alph(jc)+str(il+2), str(c))
        el_key = alph(key_ind)+str(il+2)
        el_val = alph(val_ind)+str(il+2)
        sp.setDisplayUnit(el_val, 'mm')
        if l[key_ind] == '' or l[val_ind] == '':
            continue
        sp.setAlias(el_val, str(l[key_ind]))
        sp.set(alph(jc+1)+str(il+2), '<<'+spsheet_dname+'>>.'+l[key_ind])
#重新模型
App.ActiveDocument.recompute()
tab_nm ='~/freecad_auto_gen/data/rebot_arm.xls' 的格式

在这里插入图片描述

ps: 可能需要注意外部环境的python和内部环境的版本问题.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值