Project sketch curves from a sketch in one occurrence to new sketch in a different occurrence.

brad.bylls
Collaborator

Project sketch curves from a sketch in one occurrence to new sketch in a different occurrence.

brad.bylls
Collaborator
Collaborator

I am trying to write a script to project sketch curves in an occurrence sketch by clicking on the sketch name in the browser. 

I want them to project to a new sketch on a face of a different occurrence I selected.

I can't figure out how to get the fullPathName of the original sketch parent occurrence. 

 

Brad Bylls
0 Likes
Reply
Accepted solutions (1)
473 Views
4 Replies
Replies (4)

BrianEkins
Mentor
Mentor

The trick is having references to both the source and target sketches in the context of the top-level assembly.  Here's a simple script that demonstrates this using the attached design.  Of course, how you get the sketches will probably be different because of the workflow of your command.  It's even easier if the user is selecting things because the selection will already be a proxy in the context of the top assembly.

 

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

        design = adsk.fusion.Design.cast(app.activeProduct)
        root = design.rootComponent

        #### Get the source sketch in the context of the assembly

        # Get the occurrence that contains the source sketch.
        sourceOcc = root.occurrences.itemByName('Source:1')

        # Get the sketch from the referenced component. This results in
        # losing the assembly context.
        sourceSketch = sourceOcc.component.sketches.itemByName('Source')

        # Create a proxy of the sketch so we have it in assembly context.
        sourceSketch = sourceSketch.createForAssemblyContext(sourceOcc)

        #### Get the target sketch in the context of the assembly

        # Get the occurrence that contains the source sketch.
        targetOcc = root.occurrences.itemByName('Target:1')

        # Get the sketch from the referenced component. This results in
        # losing the assembly context.
        targetSketch = targetOcc.component.sketches.itemByName('Target')

        # Create a proxy of the sketch so we have it in assembly context.
        targetSketch = targetSketch.createForAssemblyContext(targetOcc)

        # Project each of the sketch curves.
        for curve in sourceSketch.sketchCurves:
            targetSketch.project(curve)
    except:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
2 Likes

brad.bylls
Collaborator
Collaborator

Thanks Brian.

The problem is, I don't know the name of the source sketch occurrence (Source:1) or the name of the sketch (Source) that the user will select.

I also don't know the name of the occurrence the new sketch will be on.

I get the correct components (A Plate and B Plate), but not the occurrence name (A Plate:1 and B Plate:1).

My code below:

selFace: adsk.fusion.BRepFace = _inputSelectFace.selection(0).entity
selFaceComp = selFace.nativeObject.body.parentComponent  ## A Plate (correct)
selSketch: adsk.fusion.Sketch = _inputSelectSketch.selection(0).entity
selSketchComp = selSketch.nativeObject.parentComponent  ## B Blate (correct)
selSketchComp = selSketchComp.occurrences  ## This line comes up 'count 0' no occurrences? (a test)
selSketchCompOcc = adsk.fusion.Components.itemByName(selSketchComp.name)  ### Problem line
newSketch: adsk.fusion.Sketch = selFaceComp.sketches.addWithoutEdges(selFace.nativeObject)
sketchPoints = selSketchComp.sketches.itemByName(selSketch.name).sketchPoints
sketchCurves = selSketchComp.sketches.itemByName(selSketch.name).sketchCurves

[newSketch.project(p.createForAssemblyContext(selSketchCompOcc)) for p in sketchPoints]
[newSketch.project(c.createForAssemblyContext(selSketchCompOcc)) for c in sketchCurves]

Been messing with this for about a week now.

Probably just having senior moments, but still frustrating me.

Thanks for the help.

Brad Bylls
0 Likes

BrianEkins
Mentor
Mentor
Accepted solution

You were so close.  Here's a modified version of your code where the sketch and face are coming from selections.

 

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

        design = adsk.fusion.Design.cast(app.activeProduct)
        root = design.rootComponent

        selSketch = ui.selectEntity('In the browser, select the sketch to project.', 'Sketches')
        sourceSketch: adsk.fusion.Sketch = selSketch.entity

        selFace = ui.selectEntity('Select the face to project to', 'Faces')
        face: adsk.fusion.BRepFace = selFace.entity

        faceComp = face.nativeObject.body.parentComponent
        faceOcc = face.assemblyContext
        faceSketch: adsk.fusion.Sketch = faceComp.sketches.addWithoutEdges(face.nativeObject)
        targetSketch = faceSketch.createForAssemblyContext(faceOcc)

        # Project each of the sketch curves.
        for curve in sourceSketch.sketchCurves:
            targetSketch.project(curve)
    except:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
1 Like

brad.bylls
Collaborator
Collaborator

Thanks Brian.

That did it.

Works great.

Brad Bylls
0 Likes