C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

メッシュボディのシルエットをスケッチで取得する1

時間が無い、XY平面のみですが、これ!

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        create_polygon_silhouette_sketch(root.meshBodies[0])

        ui.messageBox('Done')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def create_polygon_silhouette_sketch(
    mesh: fusion.MeshBody
) -> fusion.Sketch:

    triMesh: fusion.TriangleMesh = mesh.displayMesh

    nodeIndices = triMesh.nodeIndices

    n = 3
    triIndices = [nodeIndices[i: i+n] for i in range(0, len(nodeIndices), n)]

    def z_reset(p: core.Point3D) -> core.Point3D:
        p.z = 0
        return p

    nodePoints = [z_reset(p) for p in triMesh.nodeCoordinates]

    def init_lines(idxs: list):
        def init_line(p1: core.Point3D, p2: core.Point3D):
            return core.Line3D.create(p1, p2)

        idxs = list(idxs)
        idxs.append(idxs[0])
        return [init_line(nodePoints[i1], nodePoints[i2]) for i1, i2 in zip(idxs, idxs[1:])]

    lineSets = [init_lines(idxs) for idxs in triIndices]

    tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()

    def init_wireBody(crvs: list):
        wireBody, _ = tmpMgr.createWireFromCurves(crvs)
        return wireBody

    triWires = [init_wireBody(lines) for lines in lineSets]
    triBodies = [tmpMgr.createFaceFromPlanarWires([b]) for b in triWires]

    body: fusion.BRepBody = triBodies.pop()
    def unionBody(b: fusion.BRepBody):
        tmpMgr.booleanOperation(
            body,
            b,
            fusion.BooleanTypes.UnionBooleanType)

    [unionBody(b) for b in triBodies]
    
    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent
    skt: fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
    skt.name = f'Polygon Silhouette From {mesh.name}'

    skt.isComputeDeferred = True

    sktPnts: fusion.SketchPoints = skt.sketchPoints
    sktPointLst = [sktPnts.add(e.startVertex.geometry) for e in body.edges]
    sktPointLst.append(sktPointLst[0])

    lines: fusion.SketchLines = skt.sketchCurves.sketchLines
    [lines.addByTwoPoints(p1, p2) for p1, p2 in zip(sktPointLst, sktPointLst[1:])]

    skt.isComputeDeferred = False

    return skt

出来たと思ったけど、ちょっと精度が甘い。

まぁ許してよ。
後は平面を指定して、平面に投影するようにします!