'NurbsCurve3D.extract' method does not work

'NurbsCurve3D.extract' method does not work

kandennti
Mentor Mentor
579 Views
2 Replies
Message 1 of 3

'NurbsCurve3D.extract' method does not work

kandennti
Mentor
Mentor

Hi there.

 

If anyone knows, please let me know.(Fusion360 Ver2.0.8335)

I used extract method to get a part of NurbsCurve3D, but I get an error.

The following example creates a partial sketch curve in a new sketch by selecting the on-screen sketch curve (Spline).
1.png

import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des  :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # select SketchCurve
        msg :str = 'Select SketchCurve'
        selFiltter :str = 'SketchCurves'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        # get NurbsCurve3D
        geo :adsk.core.NurbsCurve3D = sel.entity.geometry

        # get Parameter
        eva :adsk.core.SurfaceEvaluator = geo.evaluator
        _, sPrm, ePrm = eva.getParameterExtents()
        print('SelectParameter:{} - {}'.format(sPrm, ePrm))

        # new Parameter
        unitPrm = (ePrm - sPrm) * 0.25
        sPrmNew = sPrm + unitPrm
        ePrmNew = sPrm + (unitPrm * 3)
        print('NewParameter:{} - {}'.format(sPrmNew, ePrmNew))

        # extract
        print('hasattr-extract? - {}'.format(hasattr(geo,'extract')))
        splitCrv :adsk.core.NurbsCurve3D = geo.extract(sPrm, ePrm) # err!
        # https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-979a5453-b28b-42d2-8816-d76b47d4ee24

        # draw sketch
        skt :adsk.fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
        sktFits :adsk.fusion.SketchFittedSplines = skt.sketchCurves.sketchFittedSplines
        sktFits.addByNurbsCurve(splitCrv)

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

def selectEnt(
        msg :str, 
        filtterStr :str) -> adsk.core.Selection :

    try:
        sel = _ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None​

An error will occur in the extract method.
Is my processing wrong?

I don't know if this is what happened from the previous version.
0 Likes
580 Views
2 Replies
Replies (2)
Message 2 of 3

BrianEkins
Mentor
Mentor

There is a bug in your sample but even after fixing that, it still fails.  The bug in the original sample is that it's using the original parameter values in the extract call.  That should work too with the result being that the full curve is returned.  However, even after fixing that it still fails.  Autodesk has logged the bug.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 3

kandennti
Mentor
Mentor

@BrianEkins , thank you for pointing out the mistake.


It doesn't have the same geometry, but I decided to use the getStrokes method instead.

import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        des  :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # select SketchCurve
        msg :str = 'Select SketchCurve'
        selFiltter :str = 'SketchCurves'
        sel :adsk.core.Selection = selectEnt(msg ,selFiltter)
        if not sel: return

        # get NurbsCurve3D
        geo :adsk.core.NurbsCurve3D = sel.entity.geometry

        # get Parameter
        eva :adsk.core.SurfaceEvaluator = geo.evaluator
        _, sPnt, ePnt = eva.getEndPoints()
        _, [sPrm, ePrm] = eva.getParametersAtPoints([sPnt, ePnt])
        print('SelectParameter:{} - {}'.format(sPrm, ePrm))

        # new Parameter
        unitPrm = (ePrm - sPrm) * 0.25
        sPrmNew = sPrm + unitPrm
        ePrmNew = sPrm + (unitPrm * 3)
        print('NewParameter:{} - {}'.format(sPrmNew, ePrmNew))

        # get Transit point
        _, pnts = eva.getStrokes(sPrmNew, ePrmNew, 0.01)
        pntLst = adsk.core.ObjectCollection.create()
        [pntLst.add(p) for p in pnts]

        # draw sketch
        skt :adsk.fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
        skt.arePointsShown = False
        sktFits :adsk.fusion.SketchFittedSplines = skt.sketchCurves.sketchFittedSplines
        sktFits.add(pntLst)

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

def selectEnt(
        msg :str, 
        filtterStr :str) -> adsk.core.Selection:

    try:
        sel = _ui.selectEntity(msg, filtterStr)
        return sel
    except:
        return None

 

0 Likes