スケッチャー・スクリプティング
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 Constraint | |
"PointOnObject" |
Point-On-Object Constraint | |
"Vertical" |
Vertical Constraint | |
"Horizontal" |
Horizontal Constraint | |
"Parallel" |
Parallel Constraint | |
"Perpendicular" |
Perpendicular Constraint | |
"Tangent" |
Tangent/Collinear Constraint | |
"Equal" |
Equal Constraint | |
"Symmetric" |
Symmetric Constraint | |
"Block" |
Block Constraint |
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 Dimension | |
"DistanceY" |
Vertical Dimension | |
"Distance" |
Distance Dimension | |
"Radius" |
Radius Dimension | |
"Diameter" |
Diameter Dimension | |
"Angle" |
Angle Dimension | |
"AngleViaPoint" |
Angle Dimension |
The refraction constraint behave like dimensional constraints 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" |
Refraction Constraint |
The lock position constraint is a GUI command which creates a
horizontal dimension and a
vertical dimension 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:
-1denotes the horizontal x axis-2denotes the vertical y axis-ndenotes the external geometry element numbern-3(e.g. the external geometry element with index 0 in the flattened listsketch.ExternalGeometrywould be denoted by -3, the following element in the flattened list would be -4 and so on).
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:
0to indicate that the constraint affects the entire edge.1to indicate that the constraint affects the start point of the edge (a full circle has no starting point).2to indicate that the constraint affects the endpoint of the edge.3to indicate that the constraint affects the center point of the edge. Can only be used for circles, circular arcs, ellipses and elliptical arcs.nto indicate that the constraint affects the n-th pole of a B-spline.
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()
- 全般: スケッチを作成、スケッチを編集、スケッチをアタッチ、スケッチの方向を変更、スケッチを検証、スケッチをマージ、スケッチを鏡像化、スケッチの編集を終了、スケッチを表示、セクション表示、グリッドの表示を切り替え、スナップの切り替え、レンダリング順を設定、操作を停止
- スケッチャージオメトリー: 点を作成、ポリライン(折れ線)を作成、線分を作成、中心点から円弧を作成、3点指定円弧を作成、楕円弧を作成、双曲線の円弧を作成、放物線の円弧を作成、中心を指定して円を作成、3点で円を作成、中心点を指定して楕円を作成、3点を指定して楕円を作成、長方形を作成、中心配置長方形、角丸長方形、正三角形, 正方形、正五角形、正六角形、正七角形、正八角形、正多角形、長円形を作成、円弧状の長円形を作成、制御点によるBスプライン, 制御点によるBスプライン、制御点によるBスプライン、ノットによる周期的Bスプライン、構築ジオメトリの切り替え
- スケッチャー拘束:
- 寸法拘束: 寸法、水平距離拘束、垂直距離拘束、距離拘束、半径/直径を自動拘束、半径拘束、直径拘束、角度を拘束、ロック拘束
- 幾何拘束: 一致拘束(統合)、一致拘束、点がオブジェクト上にある拘束、水平/垂直拘束、水平拘束、垂直拘束、 並行拘束, 直角拘束、正接拘束または共線拘束、等値拘束、対称拘束、固定拘束
- その他の拘束: 屈折率拘束(スネルの法則)
- 拘束ツール: 駆動拘束/参照拘束の切り替え、駆動拘束/参照拘束の切り替え
- スケッチャーツール フレットを作成、面取りを作成、エッジをトリム、エッジを分割、エッジを延長、外部ジオメトリを作成、カーボンコピーを作成、原点を選択, 水平軸を選択、垂直軸を選択、配列変換, 軸周変換、スケール変換、オフセット、対称、軸方向の拘束を解除、すべてのジオメトリーを削除, すべての拘束を削除
- スケッチャーBスプラインツール: ジオメトリをB-スプラインに変換、Bスプラインの次数を増やす、Bスプラインの次数を減らす、ノット多重度を増やす, ノット多重度を減らす、ノットを挿入、曲線を結合
- スケッチャー表示ツール: 未拘束の自由度を選択、関連する拘束を選択、関連する要素を選択、冗長な拘束を選択、競合する拘束を選択、円弧の補助円を表示/非表示、Bスプライン次数の表示/非表示, Bスプライン制御ポリゴンの表示/非表示、Bスプライン曲率コームの表示/非表示、Bスプラインノット多重度の表示/非表示、Bスプライン制御点重みの表示/非表示、内部ジオメトリの表示/非表示、仮想スペース切り替え
- はじめてみよう
- インストール: ダウンロード、Windowsへのインストール、Linuxへのインストール、Macへのインストール、付加機能のインストール、Dockerでのコンパイル、AppImage、Ubuntu Snap
- 基本: FreeCADについて、ユーザー・インタフェース、Mマウス・ナビゲーション、オブジェクトの選択方法、オブジェクトの名前、設定、ワークベンチ、FreeCADファイルの構造、プロパティ、FreeCADへの貢献、寄付
- ヘルプ: チュートリアル、チュートリアル動画
- ワークベンチ: 共通ツール、アセンブリー、BIM、CAM、ドラフト、FEM、インスペクション、マテリアル、メッシュ、OpenSCAD、 パート、パートデザイン、ポイント、リバースエンジニアリング、ロボット、スケッチャー、スプレッドシート、サーフェス、テックドロー、テストフレームワーク
- 情報ハブ: ユーザー向けハブ、パワーユーザー向けハブ、開発者向けハブ

