Sketcher scripting/ja

Pythonでのスケッチオブジェクトの作成方法

スケッチオブジェクトを作成するには下記のようにします。

import FreeCAD as App
import Part
import Sketcher

doc = App.newDocument()  

sketch = doc.addObject("Sketcher::SketchObject", "Sketch")
sketch.addGeometry(Part.LineSegment(App.Vector(1.2, 1.8, 0),
                                    App.Vector(5.2, 5.3, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(6.5, 1.5, 0),
                                    App.Vector(10.2, 5.0, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(12.2, 1.0, 0),
                                    App.Vector(15.4, 5.0, 0)), False)

doc.recompute()

上の例では、新しく作成したスケッチに3本の線分を作成しています。

Pythonでの拘束の作成方法

幾何拘束 は、Pythonコンソールあるいはマクロから、次のようにすれば作成できます。

sketch.addConstraint(Sketcher.Constraint(ConstraintType, EdgeOrPartOfEdge))

寸法拘束 および特殊拘束 Snell's lawは、Pythonコンソールあるいはマクロから、次のようにすれば作成できます。

sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge, App.Units.Quantity("float_value unit")))

具体的例としては

sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge, App.Units.Quantity("123.0 mm")))

最初の引数ConstraintTypeの詳細については、下記Constraint typesを参照してください。

A constraint can take up to six arguments which are edges or indicate which sub-part of an edge is used by the constraint. See the documentation of individual constraints for details on what combinations of edges and sub-parts of edges can be passed as arguments. The main issue with this function is to identify correctly the line number and the vertex number of the lines you want to process. The sections below describe how to identify the numbering of a line, and how to Identify the numbering of the sub-parts of a line.

Constraint types

For geometric constraints, the first argument is one of the following. See the corresponding feature page for the possible combinations of arguments allowed for each constraint.

Code Icon Feature
"Coincident" Coincident
"PointOnObject" Point On Object
"Vertical" Vertical
"Horizontal" Horizontal
"Parallel" Parallel
"Perpendicular" Perpendicular
"Tangent" Tangent
"Equal" Equal
"Symmetric" Symmetric
"Block" Block

For dimensional constraints, the first argument is one of the following. See the corresponding feature page for the possible combinations of arguments allowed for each constraint.

Code Icon Feature
"DistanceX" Horizontal distance
"DistanceY" Vertical distance
"Distance" Distance
"Radius" Radius
"Diameter" Diameter
"Angle" Angle
"AngleViaPoint" Angle

The Snell's law constraints behave like dimensional contraints for the purposes of scripting. Again, see the corresponding feature page for the possible combinations of arguments allowed for each constraint.

Code Icon Feature
"SnellsLaw" Snell's law

The Lock constraint is a GUI command which creates a Horizontal distance and a Vertical distance constraint, it is not a constraint of its own.

Identifying the numbering of a line

I have drawn three lines as shown in the following figure.

By moving the cursor of the mouse above the line you can see the line number at the bottom left of the FreeCAD windows, see next figure.

Unfortunately the numbering displayed on the FreeCAD windows start from 1 whereas the numbering of the line used to script start from 0: this means that you have to subtract one each time you want to refer to a line.

Positive numbers indicate sketch edges (straight lines, circles, conics, B-splines, and so on). The following values can be used to denote elements that are not sketch edges:

Identifying the numbering of the sub-parts of a line

When qualifying which part of a line is affected by a constraint, the following values can be used:

The vertices indicated by 1 and 2 are numbered according to their order of creation. To find out the order of their creation (If you have a lot of lines, you cannot remember which vertex you have created first), you just have to move the cursor of your mouse above the two vertices of one line, see following figure.

If you read e.g. 4 and 5, it means that the vertex with the lower number (4 in this example) will be referenced by using the number 1 (first in the script command and the vertex with the higher number (5 in this example) will be referenced by using the number 2 in the script command.

Example

Let us take the previous example of the three lines. The subsequent figure indicates the numbering of each line and their vertices according to the convention for scripting.

blue text: numbering of line, black text: numbering of vertices

The command sketch.addConstraint(Sketcher.Constraint("Coincident", 1, 2, 2, 1)) yields following result:

The full code to draw the three lines and add a Coincident constraint on two points from two lines is like this:

import FreeCAD as App
import Part
import Sketcher

doc = App.newDocument()  

sketch = doc.addObject("Sketcher::SketchObject", "Sketch")
sketch.addGeometry(Part.LineSegment(App.Vector(1.2, 1.8, 0),
                                    App.Vector(5.2, 5.3, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(6.5, 1.5, 0),
                                    App.Vector(10.2, 5.0, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(12.2, 1.0, 0),
                                    App.Vector(15.4, 5.0, 0)), False)
sketch.addConstraint(Sketcher.Constraint("Coincident", 1, 2, 2, 1))

doc.recompute()