How can create a patch by a loop

How can create a patch by a loop

ho-kou
Advocate Advocate
1,430 Views
4 Replies
Message 1 of 5

How can create a patch by a loop

ho-kou
Advocate
Advocate

Hi everybody,
I want to create a patch using the loop inside Fig.1 and Fig.2 according to the following flow.
①Gets all loops for each face of the target body.
②If the loop obtained in ① has an internal shape, use this loop to create a sketch.
③Create a patch using the sketch profile created in ②.
Issue: Depending on the shape of the internal loop, the sketch profile for patch creation may not be generated.
For example, [sample_a.step] is NG. [Sample_b.step] is OK.
Can you tell me how to create a patch even with the shape of [sample_a.step]?

ask_1.png

ask_2.png

 

 

 

# Assuming you have not changed the general structure of the template no modification is needed in this file.
from . import commands
from .lib import fusion360utils as futil

import adsk.core, adsk.fusion, adsk.cam, traceback


_app = None
_ui  = None
_design = None
_rootComp = None

def run(context):
    try:
        # This will run the start function in each of your commands as defined in commands/__init__.py
        commands.start()

        # fusion情報を取得
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface

        product = _app.activeProduct
        global _design
        _design = adsk.fusion.Design.cast(product)

        global _rootComp
        _rootComp = _design.rootComponent

        allBodies = _rootComp.bRepBodies

        bodycount = allBodies.count
        if allBodies.count < 1:
            _ui.messageBox('Body count is not exist')
            return
        newbody = allBodies.item(0)      

        # create patch
        oresult = [-1]
        CreatePatch(newbody, oresult)
        
        return

    except:
        futil.handle_error('run')

# create patch
def CreatePatch(targetbody,
                oresult):
    try:
        oresult[0] = -1
        i = 0
        j = 0
        for face in targetbody.faces:
            for Loop in face.loops:

                if Loop.isOuter:
                    # if Loop is Outer
                    continue
              
                # create sketch by inter loop
                brepEdges = Loop.edges
                skt = createsketch(brepEdges)

                # can not get sketch's profile
                profilecurve = skt.profiles[0]

                # Create the patch feature by sketch's profile
                patches = _rootComp.features.patchFeatures
                patchInput = patches.createInput(profilecurve, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
                patches.add(patchInput)

        oresult[0] = 0
    except:
         _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))    

# crate loop's sketch
def createsketch(brepEdges):
    try:
        oresult = -1
        skt :adsk.fusion.Sketch = _rootComp.sketches.add(_rootComp.xYConstructionPlane)
        skt.arePointsShown = False

        edge = brepEdges.item(0)
        edgegeo :adsk.core.NurbsCurve3D = None
        curveType = edge.geometry.curveType
        
        sktFits = None
        if curveType == 2:
            # Circle3DCurveType
            sktFits :adsk.fusion.SketchFixedSplines = skt.sketchCurves.sketchFixedSplines
        else:
            # NurbsCurve3DCurveType
            sktFits :adsk.fusion.SketchFittedSplines = skt.sketchCurves.sketchFittedSplines
        
        for edge in brepEdges:
        
            if curveType == 2:
                # Circle3DCurveType
                edgegeo = edge.geometry.asNurbsCurve
                
                sktFits.addByNurbsCurve(edgegeo)
            else:
                # NurbsCurve3DCurveType
                edgegeo = edge.geometry
                eva :adsk.core.SurfaceEvaluator = edgegeo.evaluator
                _, sPnt, ePnt = eva.getEndPoints()
                _, [sPrm, ePrm] = eva.getParametersAtPoints([sPnt, ePnt])

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

        skt.name = 'test'
        
        return skt
    except:
        _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    try:
        # Remove all of the event handlers your app has created
        futil.clear_handlers()

        # This will run the start function in each of your commands as defined in commands/__init__.py
        commands.stop()

    except:
        futil.handle_error('stop')

 

 

 

0 Likes
Accepted solutions (2)
1,431 Views
4 Replies
Replies (4)
Message 2 of 5

tykapl.breuil
Advocate
Advocate
Accepted solution

The problem you encounter is that a SketchProfile is, in my understanding, planar. You logically encounter a problem when trying to make a profile from a non planar edge. However, according to the documentation, a PatchFeature can take BRepGeometry, thus we should be able to forego using a sketch all together. I've tried to throw in a sample code, tell me if it works out for you.

 

# create patch
def CreatePatch(targetbody,
                oresult):
    try:
        oresult[0] = -1
        i = 0
        j = 0
        for face in targetbody.faces:
            for Loop in face.loops:
                Loop: adsk.fusion.BRepLoop
                if Loop.isOuter:
                    # if Loop is Outer
                    continue
              
                # create sketch by inter loop
                brepEdges = Loop.edges
                pathEdgeCollection = adsk.core.ObjectCollection.create()
                [pathEdgeCollection.add(edge) for edge in brepEdges]
                path = adsk.fusion.Path.create(pathEdgeCollection, 0)

                # Create the patch feature by sketch's profile
                global _rootComp
                patches = _rootComp.features.patchFeatures
                patchInput = patches.createInput(
                    path,
                    adsk.fusion.FeatureOperations.NewBodyFeatureOperation
                )
                patches.add(patchInput)

        oresult[0] = 0
    except:
         _ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 

 

For the first sample, which gave you trouble, the script produced this :

Capture d’écran 2022-02-25 093143.png

Message 3 of 5

MichaelT_123
Advisor
Advisor
Accepted solution

Hi Mr. Ho-Kou,

 

Consider ... instead of creating sketch with profile to be used as a patch, split a face of target body with the surface body tool, and use the resulting cut-off face as a patch(s). Contrary to sketch profiles they don't need to be flat.

Look at the attached file. I believe that converting this idea to respective script would be ... a piece of cake 👩‍🍳 .

Test_Patch.png

Regards

MichaelT

 

MichaelT
Message 4 of 5

ho-kou
Advocate
Advocate

hi,Paul Breuil

Use your code,  i am succeed to create the patch,

thank you very much.

 

Message 5 of 5

ho-kou
Advocate
Advocate

hi Michael Tomsia

thanks for your solution.

I try to do it.

0 Likes