Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Project Selected Bodies onto a sketch

Gianni.CaiafaXKRKB
Explorer

Project Selected Bodies onto a sketch

Gianni.CaiafaXKRKB
Explorer
Explorer

Hello all,

I'm trying to create a program where the user can select a body or number of bodies using the command input window and then project the result onto a sketch, so I have tried copying and editing the 'ApplyMaterialToSelection' example code. Below are the changes I made to the code, creating the sketch3, passing it through the function, then using the project function to project the selected bodies onto the sketch. When the code runs the new component and sketch within is created but nothing appears on it.

def notify(self, args):
        try:
            command = args.firingEvent.sender
            inputs = command.commandInputs
            for input in inputs:
                if input.id == commandId + '_selection':
                    selectionInput = input
                elif input.id == commandId + '_materialList':
                    materialListInput = input
                    
            design = adsk.fusion.Design.cast(app.activeProduct) 
            transform = adsk.core.Matrix3D.create()
            rootComp = design.rootComponent
            allOccs = rootComp.occurrences
            projectOcc = allOccs.addNewComponent(transform)
            projectComp = projectOcc.component
            sketches3 = projectComp.sketches
            sketch3 = sketches3.add(rootComp.xYConstructionPlane)
            
            entities = getSelectedEntities(selectionInput,sketch3)

def getSelectedEntities(selectionInput,sketch3):
    entities = []
    for x in range(0, selectionInput.selectionCount):
        mySelection = selectionInput.selection(x)
        selectedObj = mySelection.entity
        if type(selectedObj) is adsk.fusion.BRepBody or type(selectedObj) is adsk.fusion.Component:
            
            projectedEntities = sketch3.project(entities)

            entities.append(selectedObj)
        elif type(selectedObj) is adsk.fusion.Occurrence:
            entities.append(selectedObj.component)
    return entities

 

Another example code I have looked at is the 'Project to Surface API' example, but again this uses bodies and componenets created by the code itself rather than selected by the user. I am new to this so any explanation would be appreciated.

0 Likes
Reply
Accepted solutions (1)
362 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor
Accepted solution

Hi @Gianni.CaiafaXKRKB .

 

The reason why they are not projected to the sketch is that they were deselected when I created the new Occurrence.
Therefore, I decided to assign the selected elements to the list before creating a new Occurrence.

 

I'm not sure if I fully understand what you want to do.

I tried to project all the BRepBody of the selected element to the sketch.

I modified the ApplyMaterialToSelectionCommandExecuteHandler class in the "ApplyMaterialToSelection" sample code and created a new execProject function.

class ApplyMaterialToSelectionCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            command = args.firingEvent.sender
            inputs = command.commandInputs
            for input in inputs:
                if input.id == commandId + '_selection':
                    selectionInput: adsk.core.SelectionCommandInput = input
                elif input.id == commandId + '_materialList':
                    materialListInput = input

            # Get a list of selected elements in advance.
            selectEntities = []
            for idx in range(selectionInput.selectionCount):
                selectEntities.append(selectionInput.selection(idx).entity)

            design: adsk.fusion.Design = adsk.fusion.Design.cast(app.activeProduct) 
            transform: adsk.core.Matrix3D = adsk.core.Matrix3D.create()
            rootComp: adsk.fusion.Component = design.rootComponent
            allOccs: adsk.fusion.Occurrences = rootComp.occurrences

            # When the addNewComponent method is executed, the selectionInput will be deselected.
            print(f'before: {selectionInput.selectionCount}')
            projectOcc: adsk.fusion.Occurrence = allOccs.addNewComponent(transform)
            print(f'after: {selectionInput.selectionCount}')

            projectComp: adsk.fusion.Component = projectOcc.component
            sketches3: adsk.fusion.Sketches = projectComp.sketches
            # sketch3 = sketches3.add(rootComp.xYConstructionPlane)
            sketch3: adsk.fusion.Sketch = sketches3.add(projectComp.xYConstructionPlane)
            
            # entities = getSelectedEntities(selectionInput, sketch3)
            entities = execProject(sketch3, selectEntities)
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def execProject(
    sketch3: adsk.fusion.Sketch,
    selectEntities: list) -> list:

    # Get all bodies
    selectBodies = []
    for entity in selectEntities:
        if type(entity) is adsk.fusion.BRepBody:
            # BRepBody
            selectBodies.append(entity)

        elif type(entity) is adsk.fusion.Component:
            # RootComponent
            selectBodies.extend([b for b in entity.bRepBodies])

        elif type(entity) is adsk.fusion.Occurrence:
            # Occurrence
            for body in entity.component.bRepBodies:
                selectBodies.append(body.createForAssemblyContext(entity))

    # Project
    projectedSketchEntities = []
    for body in selectBodies:
        projectedEntities = sketch3.project(body)
        projectedSketchEntities.extend([e for e in projectedEntities])

    return projectedSketchEntities
0 Likes

Gianni.CaiafaXKRKB
Explorer
Explorer

The main idea was to do something similar to the 'project' function in the sketch workspace where the user would select the faces or bodies of the object they want to project onto a sketch similar to the picture below.

Screenshot (6).png

 

0 Likes