コンテンツにスキップ

linkcad.db

データベースモジュールは、LinkCAD のメモリ内図面データベースへのアクセスを提供します。すべてのデータベースオブジェクトは、性能のために C++ 実装をバックエンドに持ちます。

from linkcad.db import (
Drawing, Cell, Layer, Object, Shape, Polygon, Polyline,
Ref, Color, Property, ReadLock, WriteLock, Transaction, Unit
)

クラス

Drawing

すべてのレイアウトデータのトップレベルコンテナーです。

from linkcad.db import Drawing
dwg = Drawing.current() # Get the active drawing
プロパティ / メソッド説明
Drawing.current()クラスメソッド——アクティブな図面インスタンスを取得します
drawing.name図面名(読み取り専用)
drawing.unitsデータベース単位——Unit 列挙値(読み取り専用)
drawing.main_cellメイン(トップ)セルを取得または設定します
drawing.cells()すべてのセルを反復します
drawing.layers()すべてのレイヤーを反復します
drawing.delete_layer(layer)レイヤーを削除します

Cell

形状とセル参照を含む名前付きコンテナーです。

from linkcad.db import Cell
# Create
cell = Cell.create_instance(drawing, "MY_CELL")
# Look up
cell = Cell.lookup(drawing, "MY_CELL") # returns None if not found
プロパティ / メソッド説明
Cell.create_instance(drawing, name)クラスメソッド——新しいセルを作成します
Cell.lookup(drawing, name)クラスメソッド——名前でセルを検索します。見つからない場合は None を返します
cell.nameセル名(読み取り専用)
cell.shapes()このセル内の形状を反復します
cell.cell_objects()このセル内のすべてのオブジェクト(形状 + 参照)を反復します

Layer

表示プロパティを持つ名前付きレイヤーです。

プロパティ / メソッド説明
layer.nameレイヤー名
layer.numberレイヤー番号(GDSII で使用)
layer.datatypeレイヤーデータタイプ(GDSII で使用)
layer.visibleレイヤーが表示されるかどうか
layer.colorレイヤー表示色(Color

Object

セル内に存在できるすべてのオブジェクト(形状、セル参照など)の基底クラスです。

プロパティ / メソッド説明
obj.layerこのオブジェクトが属するレイヤー(参照では None の場合があります)
obj.destroy()このオブジェクトを削除して破棄します

Shape

ジオメトリ形状(ポリゴン、ポリライン、円などのテッセレーションされたプリミティブ)の基底クラスです。

プロパティ / メソッド説明
shape.layer()形状の Layer を取得します
shape.vertices()頂点座標を (x, y) タプルとして反復します
shape.closed形状が閉じている(ポリゴン状)場合は True
shape.area()形状の符号付き面積
shape.bounds()バウンディングボックス(Bounds
shape.destroy()この形状を削除して破棄します

Polygon

閉じた塗りつぶし形状です。Shape を拡張します。

from linkcad.db import Polygon
プロパティ / メソッド説明
polygon.vertices()頂点を (x, y) タプルとして反復します
polygon.area()符号付き面積
polygon.layer()所属レイヤー
polygon.destroy()削除して破棄します

Polyline

幅を任意で持つ開いた、または閉じたパスです。Shape を拡張します。

from linkcad.db import Polyline
プロパティ / メソッド説明
polyline.vertices()頂点を (x, y) タプルとして反復します
polyline.widthデータベース単位でのパス幅
polyline.closedパスが閉じている場合は True
polyline.layer()所属レイヤー
polyline.destroy()削除して破棄します

Ref

関連付けられた変換を持つ、別のセルの配置済みインスタンスです。

from linkcad.db import Ref
from linkcad.geom import Transformation
xform = Transformation()
xform.rotate(45.0)
xform.translate(1000, 2000)
Ref.create_instance(cell=parent_cell, transformation=xform, ref_cell=child_cell)
プロパティ / メソッド説明
Ref.create_instance(cell, transformation, ref_cell)クラスメソッド——セル参照を作成します
ref.ref_cell参照先の Cell
ref.transformation適用された Transformation
ref.destroy()この参照を削除して破棄します

Color

レイヤーに付加される RGB 色値です。

プロパティ説明
color.red赤チャンネル(0–255)
color.green緑チャンネル(0–255)
color.blue青チャンネル(0–255)

Property

データベースオブジェクトに付加される名前付き文字列プロパティです。

プロパティ説明
prop.nameプロパティ名
prop.valueプロパティ値

Unit

データベース座標単位の列挙型です。drawing.units から返されます。

説明
Unit.Nanometer1 データベース単位あたり 1 nm
Unit.Micrometer1 データベース単位あたり 1 µm
Unit.Millimeter1 データベース単位あたり 1 mm
Unit.Centimeter1 データベース単位あたり 1 cm
Unit.Meter1 データベース単位あたり 1 m
Unit.Mil1 データベース単位あたり 1 mil(0.001 inch)
Unit.Inch1 データベース単位あたり 1 inch

ロック

ロック要件は実行コンテキストによって異なります。

プラグインコンテキスト(@tool, @format_reader, @format_writer

フレームワークは run()read()write() を呼び出す前に適切なロックを保持します。読み取り操作に明示的なロックは不要です。 元に戻せる書き込み操作には Transaction が必要です。

from linkcad.db import Cell, Ref, Transaction
from linkcad.geom import Transformation
def run(self, drawing) -> None:
# Read access — no lock needed
for cell in drawing.cells():
print(cell.name)
# Write access — wrap in Transaction for undo support
with Transaction(drawing, "My Operation"):
new_cell = Cell.create_instance(drawing, "RESULT")
# ... build geometry ...
# auto-commits on exit; rolls back on exception

スタンドアロンスクリプト(linkcad --python-script

プラグインフレームワーク外で実行されるスクリプトは、明示的にロックを取得する必要があります。

from linkcad.db import Drawing, ReadLock, WriteLock
dwg = Drawing.current()
# Read access
with ReadLock():
for cell in dwg.cells():
print(cell.name)
# Write access (no undo support)
with WriteLock():
for layer in list(dwg.layers()):
if layer.name.startswith("TEMP_"):
dwg.delete_layer(layer)

ReadLock

スタンドアロンスクリプトで読み取り専用アクセスを行うためのコンテキストマネージャーです。

from linkcad.db import ReadLock
with ReadLock():
main = dwg.main_cell
for shape in main.shapes():
print(shape.bounds())

WriteLock

スタンドアロンスクリプトで書き込みアクセスを行うためのコンテキストマネージャーです。元に戻すエントリは作成しません

from linkcad.db import WriteLock
with WriteLock():
for cell in dwg.cells():
for obj in list(cell.cell_objects()):
if obj.layer and obj.layer.name == "SCRATCH":
obj.destroy()

Transaction

書き込みアクセス用のコンテキストマネージャーで、元に戻す履歴に単一のエントリを作成します。ツールプラグインで使用します。Edit メニューに表示される説明文字列を受け取ります。

from linkcad.db import Transaction
with Transaction(drawing, "Create Panel"):
cell = Cell.create_instance(drawing, "PANEL")
# ... build content ...
# auto-commits on clean exit; rolls back on exception