C#ATIA

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

エンティティをダンプする

ちょっと苦しくなって来たので、ツール的なものを。

今回もそうなのですが、依然取り組んだ回転プロファイルの際に
津ようく感じていたのが "確認するためのダンプクラスが欲しい" 
でした。

数値で確認する方法もありますが、CAD画面上に点や線等が
表示された方が、位置関係が分かりやすいです。

それを毎回書いていたり、何処かからコピペしていたのですが
やっぱりそれっぽいものが欲しくなりました。
で、色々と不足していますが作りました。

# Fusion360API Python script

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

class DumpFactry():
    def __init__(
        self,
        debug: bool = True,
    ) -> None:

        self.debug = debug
        self.app: core.Application = core.Application.get()
        self.des: fusion.Design = self.app.activeProduct
        self.root: fusion.Component = self.des.rootComponent
        self.tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()


    def bodies(
        self,
        bodyLst: list,
        name: str = '',
    ) -> list:
        '''
        bodyリストの出力
        '''
        if not self.debug:
            return

        baseFeat: fusion.BaseFeature = None
        if self.des.designType == fusion.DesignTypes.ParametricDesignType:
            baseFeat = self.root.features.baseFeatures.add()

        bodies: fusion.BRepBodies = self.root.bRepBodies
        resBodies = []
        if baseFeat:
            try:
                baseFeat.startEdit()
                resBodies = [bodies.add(body, baseFeat) for body in bodyLst]
            except:
                pass
            finally:
                baseFeat.finishEdit()
        else:
            resBodies = [bodies.add(body) for body in bodyLst]

        if len(name) > 0:
            for b in resBodies:
                b.name = name

        return resBodies


    def curves(
        self,
        curves: list,
        name: str = '',
    ) -> fusion.Sketch:
        '''
        curve3dをスケッチポイントして描く
        '''
        if not self.debug:
            return

        nurbsLst = []
        for crv in curves:
            if hasattr(crv, 'asNurbsCurve'):
                crv = crv.asNurbsCurve
            nurbsLst.append(crv)

        skt: fusion.Sketch = self._init_skt(name)
        sktFixs: fusion.SketchFixedSplines= skt.sketchCurves.sketchFixedSplines

        skt.isComputeDeferred = True
        [sktFixs.addByNurbsCurve(c) for c in nurbsLst]
        skt.isComputeDeferred = False

        return skt


    def lines(
        self,
        lines: list,
        name: str = '',
    ) -> fusion.Sketch:
        '''
        line3dをスケッチポイントして描く
        '''
        if not self.debug:
            return

        l: core.Line3D = None
        pointSets = [(l.startPoint, l.endPoint) for l in lines]

        skt: fusion.Sketch = self._init_skt(name)
        sktLines: fusion.SketchLines = skt.sketchCurves.sketchLines

        skt.isComputeDeferred = True
        [sktLines.addByTwoPoints(p1, p2) for p1, p2 in pointSets]
        skt.isComputeDeferred = False

        return skt


    def points(
        self,
        points: list,
        name: str = '',
    ) -> fusion.Sketch:
        '''
        point3dをスケッチポイントして描く
        '''
        if not self.debug:
            return

        skt: fusion.Sketch = self._init_skt(name)
        sktPoints: fusion.SketchPoints = skt.sketchPoints

        skt.isComputeDeferred = True
        [sktPoints.add(p) for p in points]
        skt.isComputeDeferred = False

        return skt


    def _init_skt(
        self,
        name: str = '',
    ) -> fusion.Sketch:

        skt: fusion.Sketch = self.root.sketches.add(
            self.root.xYConstructionPlane
        )
        if len(name) > 0:
            skt.name = name

        return skt

このDumpFactryクラスはこんな感じで使う想定です。

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion
from .DumpFactry import DumpFactry

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface

        dump = DumpFactry()

        # 点
        coordinates = (
            (1,2,3),
            (-4,-5,-6),
            (7,-8,9),
        )
        pnts = [core.Point3D.create(x,y,z) for x,y,z in coordinates]
        dump.points(pnts)

        # 線
        coordinates = (
            (
                (-1,-2,3),
                (4,-5,-6)
            ),
            (
                (7,-8,-9),
                (-1,2,-3)
            )
        )
        pntSets = [
            (
                core.Point3D.create(ps1[0],ps1[1],ps1[2]),
                core.Point3D.create(ps2[0],ps2[1],ps2[2])
            ) for ps1, ps2 in coordinates]
        lines = [core.Line3D.create(p1, p2) for p1, p2 in pntSets]
        dump.lines(lines)

        # 曲線
        tmpMgr: fusion.TemporaryBRepManager = dump.tmpMgr
        wireBody: fusion.BRepBody = tmpMgr.createHelixWire(
            core.Point3D.create(3,5,7),
            core.Vector3D.create(0,0,1),
            core.Point3D.create(5,5,7),
            1.0,
            5.0,
            0.5,
        )
        dump.curves([wireBody.wires[0].edges[0].geometry])

        # ボディ
        bodies  = (
            tmpMgr.createSphere(
                core.Point3D.create(-3,-5,-7),
                1.5,
            ),
            tmpMgr.createCylinderOrCone(
                core.Point3D.create(5,2,0),
                0.5,
                core.Point3D.create(-5,-4,-1),
                1.5
            )
        )
        dump.bodies(bodies)

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

実行するとこんな感じです。

べたべたな関数しか無いですが、もうちょっと育てたい。