C#ATIA

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

Fusion360APIでLine Arc Nurbsを作成してみる2

こちらの続き
Fusion360APIでLine Arc Nurbsを作成してみる1 - C#ATIA

少しマシになりましたが、ちょっと遠回りだな…

#FusionAPI_python
#Author-kantoku
#Description-CurvesTest2
#Line Arc Nurbs作成テスト

import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.get()
_ui = _app.userInterface

def run(context):
    try:
        global _app, _ui
        des = adsk.fusion.Design.cast(_app.activeProduct)
        comp = des.rootComponent
        
        crv_infos = []
        crv_infos.append(["Siren::Line",[[1,2,3],[7, 8, 9]]])  
        crv_infos.append(["Siren::Circle",
                           [[9.999999998279, 10.999999997875, 11.999999999064],
                            [24.682458367311, 23.30947502125, 21.936491673766],
                            [4.9999999978654, 14.99999999931, 25.000000001367]]])                            
        crv_infos.append(["Siren::BSCurve",
                           [[10, -20, -10], 
                            [9.587310041, -19.66378473, -7.111170289], 
                            [9.066687233, -19.24725683, -4.318376656], 
                            [8.414915532, -18.73943189, -1.64725845], 
                            [7.064932775, -17.72835072, 2.46828599], 
                            [5.183948194, -16.46330539, 5.802194902], 
                            [4.342483009, -15.92222727, 6.978456889], 
                            [2.558949274, -14.84247725, 8.853440155], 
                            [0.4455897088, -13.72427175, 9.964573277], 
                            [-0.6232459496, -13.19898599, 10.30174101], 
                            [-2.929444025, -12.16203711, 10.59962188], 
                            [-5.413111131, -11.2518361, 10.14198332], 
                            [-6.714932791, -10.8317392, 9.723113911], 
                            [-10.15600129, -9.869701012, 8.217440862], 
                            [-13.65008051, -9.267637855, 5.788366698], 
                            [-15.79519881, -9.037527212, 4.03034148], 
                            [-17.91158269, -8.933957503, 2.08841731], 
                            [-20, -8.944926835, 0]],
                            5,
                            [0, 17.39738523, 28.8962564, 
                             39.23864718, 50.07377854, 67.53908293],
                            [6, 3, 3, 3, 3, 6],
                            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
        #ここまでは基本的に読み込み
        
        #スケッチ作成
        sk = comp.sketches.add(comp.xYConstructionPlane)
        
        #Geometry
        geos = curve_Factory(crv_infos)
        
        #include
        include_Geo(sk, geos)
        
        _ui.messageBox('done')
    except:
        if _ui:
            _ui.messageBox('エラー\n{}'.format(traceback.format_exc()))

def include_Geo(sk, geos):
    for geo in geos:
        geo_type = geo.classType()
        if geo_type == "adsk::core::Line3D":
            fs = sk.sketchCurves.sketchLines.addByTwoPoints(
                geo.startPoint, 
                geo.endPoint)
        elif geo_type == "adsk::core::Arc3D":
            fs = sk.sketchCurves.sketchArcs.addByThreePoints(
                geo.startPoint, 
                geo.midPoint,
                geo.endPoint)
        elif geo_type == "adsk::core::NurbsCurve3D":
            fs = sk.sketchCurves.sketchFittedSplines.addByNurbsCurve(geo)
            fs.isFixed = True            
        else:        
            pass

        sk.include(fs)
        fs.deleteMe()
    return

def curve_Factory(lst):
    geos = []
    for cv_info in lst:
        cv_type = cv_info[0]
        if cv_type == "Siren::Line":
            geos.append(create_Line(cv_info))
        elif cv_type == "Siren::Circle":
            geos.append(create_Arc(cv_info))
        elif cv_type == "Siren::BSCurve":
            geos.append(create_BSCurve(cv_info))
        else:
            pass
    return geos
    
def create_Point(pos):
    return adsk.core.Point3D.create(
        pos[0] * 0.1, pos[1] * 0.1, pos[2] * 0.1)
                
def create_Line(lst):
    pos = lst[1]
    return adsk.core.Line3D.create(
        create_Point(pos[0]), 
        create_Point(pos[1]))

def create_Arc(lst):
    pos = lst[1]
    mid_pnt = create_Point(pos[1])
    arc = adsk.core.Arc3D.createByThreePoints(
            create_Point(pos[0]), 
            mid_pnt, 
            create_Point(pos[2]))
    arc.midPoint = mid_pnt
    return arc

def create_BSCurve(lst):
    poles = [create_Point(pos) for pos in lst[1]]
    degree = lst[2]
    knots = []
    for i, m in enumerate(lst[4]):
        for k in range(0, m):
            knots.append(lst[3][i])
    weights = lst[5]
    
    return adsk.core.NurbsCurve3D.createRational(
        poles, degree, knots, weights, False)

create_Arc関数部分で、インスタンス変数の動的追加と言う方法を利用しました。
勉強になりました。