C#ATIA

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

メッシュの六角形分割に挑む10

こちらの続きです。
メッシュの六角形分割に挑む9 - C#ATIA

六角柱をBRepBodyで作成します。
後の事を考慮して任意の位置に任意のサイズで作ります。

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import math


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

        if app.activeDocument.design.designType != adsk.fusion.DesignTypes.DirectDesignType:
            ui.messageBox('履歴がキャプチャされている為、中止します')
            return

        root: adsk.fusion.Component = app.activeProduct.rootComponent
        initHexagon(
            root,
            adsk.core.Point3D.create(1, 2, 3),
            adsk.core.Vector3D.create(3, 2, 1),
            adsk.core.Vector3D.create(-2, 2, 5),
            5,
            20
        )

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


def initHexagon(
        comp: adsk.fusion.Component,
        center: adsk.core.Point3D,
        vecX: adsk.core.Vector3D,
        vecY: adsk.core.Vector3D,
        length: float,
        height: float):

    def initHexagonProfile(
        skt: adsk.fusion.Sketch,
        center: adsk.core.Point3D,
        vecX: adsk.core.Vector3D,
        vecY: adsk.core.Vector3D,
        length: float
    ) -> adsk.fusion.Profile:

        # points
        pnts = []
        for idx in range(6):
            theta = math.radians(30 + 60 * idx)
            pnts.append(
                adsk.core.Point3D.create(
                    math.cos(theta) * length,
                    math.sin(theta) * length)
            )

        # lines
        pnts.append(pnts[0])
        for p1, p2 in zip(pnts, pnts[1:]):
            skt.sketchCurves.sketchLines.addByTwoPoints(p1, p2)

        return skt.profiles[0]

    def initExtrude(
            profile: adsk.fusion.Profile,
            height: float) -> adsk.fusion.ExtrudeFeature:

        comp: adsk.fusion.Component = profile.parentSketch.parentComponent
        exts: adsk.fusion.ExtrudeFeatures = comp.features.extrudeFeatures
        extIpt: adsk.fusion.ExtrudeFeatureInput = exts.createInput(
            profile,
            adsk.fusion.FeatureOperations.NewBodyFeatureOperation
        )
        extIpt.setSymmetricExtent(
            adsk.core.ValueInput.createByReal(height),
            True
        )

        return exts.add(extIpt)

    def initCloneTransform(
            body: adsk.fusion.BRepBody,
            mat: adsk.core.Matrix3D) -> adsk.fusion.BRepBody:

        tmpMgr: adsk.fusion.TemporaryBRepManager = adsk.fusion.TemporaryBRepManager.get()
        clone: adsk.fusion.BRepBody = tmpMgr.copy(body)

        tmpMgr.transform(clone, mat)

        comp: adsk.fusion.Component = body.parentComponent
        return comp.bRepBodies.add(clone)

    # ----------
    # vecter
    vecZ: adsk.core.Vector3D = vecX.crossProduct(vecY)
    vecY = vecX.crossProduct(vecZ)
    vecX.normalize()
    vecY.normalize()
    vecZ.normalize()

    # sketch
    skt: adsk.fusion.Sketch = comp.sketches.add(comp.xYConstructionPlane)

    # profile
    pro: adsk.fusion.Profile = initHexagonProfile(
        skt, center, vecX, vecY, length)

    # extrude
    extFeat: adsk.fusion.ExtrudeFeature = initExtrude(pro, height)

    # matrix
    mat: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
    mat.setWithCoordinateSystem(center, vecX, vecY, vecZ)

    # Clone Transform
    body: adsk.fusion.BRepBody = initCloneTransform(
        extFeat.bodies[0],
        mat
    )

    # remove
    extFeat.bodies[0].deleteMe()
    extFeat.dissolve()
    skt.deleteMe()

f:id:kandennti:20211007142720p:plain
ダイレクトモードでのみ動きます。
又、途中の要らないものは削除しています。