Fusionです。思うところあってのテスト的なスクリプトです。
今Fusionで選択している面でこちらから見えている面のみを選択したい時有りますよね?
こんなデータで事前に選択します。左上から右下にドラッグで選択すると面が選択状態となります。

裏面はこんな感じで選択されています。

で、選択した状態でこちらのスクリプトを実行です。
import time
import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion
EPS: float = 0.001
def run(context: dict) -> None:
"""スクリプトのエントリーポイント。
選択中の面それぞれについて可視判定を行い、
画面上で見えている面だけを選択状態として残す。
Args:
context: Fusion から渡される実行コンテキスト(未使用)。
"""
ui: core.UserInterface = None
try:
app: core.Application = core.Application.get()
ui = app.userInterface
des: fusion.Design = app.activeProduct
root: fusion.Component = des.rootComponent
faces: list[fusion.BRepFace] = get_selected_faces()
if len(faces) < 1:
ui.messageBox("先に面を窓選択してから実行してください")
return
cam: core.Camera = app.activeViewport.camera
viewDir: core.Vector3D = cam.eye.vectorTo(cam.target)
viewDir.normalize()
camInfo: dict = {
"eye": cam.eye,
"viewDir": viewDir,
"isOrtho": cam.cameraType == core.CameraTypes.OrthographicCameraType,
}
t0: float = time.perf_counter()
counts: dict[str, int] = {"visible": 0, "hidden": 0, "back": 0, "occluded": 0}
keep: list[fusion.BRepFace] = []
for face in faces:
res: str = is_visible_face(face, camInfo, root)
counts[res] += 1
if res == "visible":
keep.append(face)
elapsed: float = time.perf_counter() - t0
sels: core.Selections = ui.activeSelections
sels.clear()
for face in keep:
sels.add(face)
ui.messageBox(
"選択面: {} → 残り: {}\n".format(len(faces), len(keep))
+ "除外 非表示: {} / 裏面: {} / 遮蔽: {}\n".format(
counts["hidden"], counts["back"], counts["occluded"]
)
+ "処理時間: {:.3f} 秒".format(elapsed)
)
except Exception:
if ui:
ui.messageBox("Failed:\n{}".format(traceback.format_exc()))
def get_selected_faces() -> list[fusion.BRepFace]:
"""現在の選択から BRepFace だけを抽出して返す。
Returns:
選択中の BRepFace のリスト。面以外の選択要素は除外される。
"""
app: core.Application = core.Application.get()
sels: core.Selections = app.userInterface.activeSelections
faces: list[fusion.BRepFace] = []
for i in range(sels.count):
ent = sels.item(i).entity
if isinstance(ent, fusion.BRepFace):
faces.append(ent)
return faces
def is_visible_face(
face: fusion.BRepFace,
camInfo: dict,
root: fusion.Component,
) -> str:
"""面が画面上で見えているかを判定する。
判定順: ボディ非表示 → 裏面(ソリッドのみ) → 他形状による遮蔽。
面上の代表点 1 点からカメラ方向へレイを飛ばして遮蔽を調べる簡易判定。
Args:
face: 判定対象の面。
camInfo: カメラ情報。キーは "eye"(視点 Point3D)、
"viewDir"(視線方向の単位 Vector3D)、"isOrtho"(平行投影か)。
root: レイキャストに使うルートコンポーネント。
Returns:
"visible"(見えている) / "hidden"(ボディ非表示) /
"back"(裏面) / "occluded"(他の面に遮蔽) のいずれか。
"""
body: fusion.BRepBody = face.body
if not body.isVisible:
return "hidden"
p: core.Point3D = face.pointOnFace
eye: core.Point3D = camInfo["eye"]
maxDist: float | None
if camInfo["isOrtho"]:
toCam: core.Vector3D = camInfo["viewDir"].copy()
toCam.scaleBy(-1)
maxDist = None
else:
toCam = p.vectorTo(eye)
maxDist = toCam.length
toCam.normalize()
if body.isSolid:
ok: bool
normal: core.Vector3D
ok, normal = face.evaluator.getNormalAtPoint(p)
if ok and normal.dotProduct(toCam) <= 0:
return "back"
offset: core.Vector3D = toCam.copy()
offset.scaleBy(EPS)
origin: core.Point3D = p.copy()
origin.translateBy(offset)
hitPoints: core.ObjectCollection = core.ObjectCollection.create()
hits: core.ObjectCollection = root.findBRepUsingRay(
origin,
toCam,
fusion.BRepEntityTypes.BRepFaceEntityType,
-1,
True,
hitPoints,
)
for i in range(hits.count):
hit: fusion.BRepFace = hits.item(i)
if is_same_face(hit, face):
continue
if maxDist is not None:
if origin.distanceTo(hitPoints.item(i)) > maxDist:
continue
return "occluded"
return "visible"
def is_same_face(a: fusion.BRepFace, b: fusion.BRepFace) -> bool:
"""2 つの面が同一かを判定する。
オブジェクト比較で一致しない場合は entityToken で比較する。
Args:
a: 比較する面 1。
b: 比較する面 2。
Returns:
同一の面なら True。比較に失敗した場合は False。
"""
try:
if a == b:
return True
return a.entityToken == b.entityToken
except Exception:
return False
実行後です。1秒かからないですね。

裏面の見えていなかったところの選択は外れています。

もっと手間を省くのであれば、事前に面の選択をしないで
画面方向から見えているはずのすべての面を選択するバージョン
import time
import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion
EPS: float = 0.001
def run(context: dict) -> None:
"""スクリプトのエントリーポイント。
選択中の面それぞれについて可視判定を行い、
画面上で見えている面だけを選択状態として残す。
Args:
context: Fusion から渡される実行コンテキスト(未使用)。
"""
ui: core.UserInterface = None
try:
app: core.Application = core.Application.get()
ui = app.userInterface
des: fusion.Design = app.activeProduct
root: fusion.Component = des.rootComponent
faces: list[fusion.BRepFace] = get_all_faces(root)
if len(faces) < 1:
ui.messageBox("表示されている面がありません")
return
cam: core.Camera = app.activeViewport.camera
viewDir: core.Vector3D = cam.eye.vectorTo(cam.target)
viewDir.normalize()
camInfo: dict = {
"eye": cam.eye,
"viewDir": viewDir,
"isOrtho": cam.cameraType == core.CameraTypes.OrthographicCameraType,
}
t0: float = time.perf_counter()
counts: dict[str, int] = {"visible": 0, "hidden": 0, "back": 0, "occluded": 0}
keep: list[fusion.BRepFace] = []
for face in faces:
res: str = is_visible_face(face, camInfo, root)
counts[res] += 1
if res == "visible":
keep.append(face)
elapsed: float = time.perf_counter() - t0
sels: core.Selections = ui.activeSelections
sels.clear()
for face in keep:
sels.add(face)
ui.messageBox(
"対象面: {} → 選択: {}\n".format(len(faces), len(keep))
+ "除外 非表示: {} / 裏面: {} / 遮蔽: {}\n".format(
counts["hidden"], counts["back"], counts["occluded"]
)
+ "処理時間: {:.3f} 秒".format(elapsed)
)
except Exception:
if ui:
ui.messageBox("Failed:\n{}".format(traceback.format_exc()))
def get_all_faces(root: fusion.Component) -> list[fusion.BRepFace]:
"""表示されている全ての面を取得して返す。
モデル全体のバウンディングボックスの中心から、対角線長を許容差として
findBRepUsingPoint を実行し、全ての面を取得する。
Args:
root: 検索に使うルートコンポーネント。
Returns:
表示されている BRepFace のリスト。
"""
bb: core.BoundingBox3D = root.boundingBox
minP: core.Point3D = bb.minPoint
maxP: core.Point3D = bb.maxPoint
center: core.Point3D = core.Point3D.create(
(minP.x + maxP.x) * 0.5,
(minP.y + maxP.y) * 0.5,
(minP.z + maxP.z) * 0.5,
)
tol: float = minP.distanceTo(maxP)
res: core.ObjectCollection = root.findBRepUsingPoint(
center,
fusion.BRepEntityTypes.BRepFaceEntityType,
tol,
True,
)
return [res.item(i) for i in range(res.count)]
def is_visible_face(
face: fusion.BRepFace,
camInfo: dict,
root: fusion.Component,
) -> str:
"""面が画面上で見えているかを判定する。
判定順: ボディ非表示 → 裏面(ソリッドのみ) → 他形状による遮蔽。
面上の代表点 1 点からカメラ方向へレイを飛ばして遮蔽を調べる簡易判定。
Args:
face: 判定対象の面。
camInfo: カメラ情報。キーは "eye"(視点 Point3D)、
"viewDir"(視線方向の単位 Vector3D)、"isOrtho"(平行投影か)。
root: レイキャストに使うルートコンポーネント。
Returns:
"visible"(見えている) / "hidden"(ボディ非表示) /
"back"(裏面) / "occluded"(他の面に遮蔽) のいずれか。
"""
body: fusion.BRepBody = face.body
if not body.isVisible:
return "hidden"
p: core.Point3D = face.pointOnFace
eye: core.Point3D = camInfo["eye"]
maxDist: float | None
if camInfo["isOrtho"]:
toCam: core.Vector3D = camInfo["viewDir"].copy()
toCam.scaleBy(-1)
maxDist = None
else:
toCam = p.vectorTo(eye)
maxDist = toCam.length
toCam.normalize()
if body.isSolid:
ok: bool
normal: core.Vector3D
ok, normal = face.evaluator.getNormalAtPoint(p)
if ok and normal.dotProduct(toCam) <= 0:
return "back"
offset: core.Vector3D = toCam.copy()
offset.scaleBy(EPS)
origin: core.Point3D = p.copy()
origin.translateBy(offset)
hitPoints: core.ObjectCollection = core.ObjectCollection.create()
hits: core.ObjectCollection = root.findBRepUsingRay(
origin,
toCam,
fusion.BRepEntityTypes.BRepFaceEntityType,
-1,
True,
hitPoints,
)
for i in range(hits.count):
hit: fusion.BRepFace = hits.item(i)
if is_same_face(hit, face):
continue
if maxDist is not None:
if origin.distanceTo(hitPoints.item(i)) > maxDist:
continue
return "occluded"
return "visible"
def is_same_face(a: fusion.BRepFace, b: fusion.BRepFace) -> bool:
"""2 つの面が同一かを判定する。
オブジェクト比較で一致しない場合は entityToken で比較する。
Args:
a: 比較する面 1。
b: 比較する面 2。
Returns:
同一の面なら True。比較に失敗した場合は False。
"""
try:
if a == b:
return True
return a.entityToken == b.entityToken
except Exception:
return False
早いんですよ・・・処理が・・・とても