# Python脚本教程

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎română • ‎čeština • ‎русский • ‎中文（中国大陆）‎ • ‎日本語

## Introduction

• 创建新的对象
• 修改已存在的对象
• 修改那些对象的3D表示

• 借助外部脚本。这可用来编写更复杂的程序。比如编写整个工作台

• Redirect internal Python output to report view
• Redirect internal Python errors to report view

• Report view

## 编写python代码

FreeCAD提供了两种环境供用户方便地编写python代码：python控制台(可从View → Panels → Python console菜单开启)或宏编辑器(Tools → Macros)。在控制台中，您可以逐行地编写python命令，按下回车键后即刻执行。而宏中则可容纳更复杂的多行脚本，只有在执行宏时才会执行相应代码。

There are two ways to write Python code in FreeCAD. In the Python console (select View → Panels → Python console from the menu) or in the Macro editor (select Macro → Macros... from the menu). In the console you write Python commands one by one, executing them by pressing Enter, while macros can contain more complex code made up of several lines, executed only when the macro is executed.

Let's start by creating a new empty document:

```doc = FreeCAD.newDocument()
```

```doc.
```

```box = doc.addObject("Part::Box", "myBox")
```

```doc.recompute()
```

```box.
```

```box.Height
```

```box.Height = 5
```

## 向量与方位

Vectors are a very fundamental concept in any 3D application. A vector is a list of 3 numbers (x, y and z), describing a point or position in 3D space. Many things can be done with vectors, such as additions, subtractions, projections and much more. In FreeCAD vectors work like this:

```myvec = FreeCAD.Vector(2, 0, 0)
myvec.x
myvec.y
```

```box.Placement
box.Placement.Base
box.Placement.Base = sumvec

box.Placement = otherpla
```

## App 与 Gui

FreeCAD has been designed so that it can also be used without its user interface, as a command-line application. Almost every object in FreeCAD therefore consists of two parts: an `Object`, its "geometry" component, and a `ViewObject`, its "visual" component. When you work in command-line mode, the geometry part is present, but the visual part is disabled.

```vo = box.ViewObject
```

```vo.Transparency = 80
vo.hide()
vo.show()
```

## 模块

The true power of FreeCAD lies in its faithful modules. The FreeCAD base application is more or less an empty container. Without its modules it can do little more than create new, empty documents. Each module not only adds new workbenches to the interface, but also new Python commands and new object types. As a result several different, and even totally incompatible, object types can coexist in the same document. The most important modules in FreeCAD that we'll look at in this tutorial are: Part, Mesh, Sketcher and Draft.

```doc.supportedTypes()
```

```import Part
Part.
```

## 网格

```import Mesh
mymesh = Mesh.createSphere()
mymesh.Facets
mymesh.Points

meshobj.Mesh = mymesh
doc.recompute()
```

## 零件

The Part Module is the most powerful module in the whole of FreeCAD. It allows you to create and manipulate BRep objects. BRep stands for "Boundary Representation". A BRep object is defined by surfaces that enclose and define an inner volume. Unlike meshes, BRep objects can have a wide variety of components from planar faces to very complex NURBS surfaces.

```import Part
myshape = Part.makeSphere(10)
myshape.Volume
myshape.Area

shapeobj.Shape = myshape
doc.recompute()
```

```Part.show(myshape)
```

## 底图

FreeCAD features many more modules, such as Sketcher and Draft, that also create Part objects. These modules add additional parameters to the objects created, or even implement a whole new way to handle the Part geometry in them. Our box example above is a perfect example of a parametric object. All you need to define the box is to specify the parameters height, width and length. Based on those, the object will automatically calculate its Part shape. FreeCAD allows you to create such objects in Python.

```import Draft
rec = Draft.makeRectangle(5, 2)
Draft.move(rec, mvec)
Draft.move(box, mvec)
```

## 界面

The FreeCAD user interface is made with Qt, a powerful graphical interface system, responsible for drawing and handling all the controls, menus, toolbars and buttons around the 3D view. Qt provides a module, PySide, which allows Python to access and modify Qt interfaces such as FreeCAD's. Let's try to fiddle with the Qt interface and produce a simple dialog:

```from PySide import QtGui
QtGui.QMessageBox.information(None, "Apollo program", "Houston, we have a problem")
```