Press selected faces by a script

Press selected faces by a script

michalKUPG8
Explorer Explorer
1,289 Views
8 Replies
Message 1 of 9

Press selected faces by a script

michalKUPG8
Explorer
Explorer

I am trying to make a script that shrinks bodies by specified size by selecting it faces that should be moved. Like press/pull command or Extrude with a cut. I tried via extrude, but it does not work.  Can you please help me how to do it? I am trying to do it in python...

 

 

 

selectedFace = ui.selectEntity('Select the fact to create a sketch', 'PlanarFaces').entity
face = adsk.fusion.BRepFace.cast(selectedFace)

distance = adsk.core.ValueInput.createByString('-2 mm')
extrudes = root.features.extrudeFeatures
extrudeInput = extrudes.createInput(face, adsk.fusion.FeatureOperations.CutFeatureOperation)
extrudeInput.setDistanceExtent(False, distance)
extrude = extrudes.add(extrudeInput)

 

 

 

0 Likes
1,290 Views
8 Replies
Replies (8)
Message 2 of 9

kandennti
Mentor
Mentor

Hi @michalKUPG8 .

 

The ExtrudeFeatures must be taken from the Component to which the selected face belongs.

        selectedFace = ui.selectEntity('Select the fact to create a sketch', 'PlanarFaces').entity
        face = adsk.fusion.BRepFace.cast(selectedFace)

        distance = adsk.core.ValueInput.createByString('-2 mm')
        
        # extrudes = root.features.extrudeFeatures
        comp: adsk.fusion.Component = face.body.parentComponent
        extrudes = comp.features.extrudeFeatures

        extrudeInput = extrudes.createInput(face, adsk.fusion.FeatureOperations.CutFeatureOperation)
        extrudeInput.setDistanceExtent(False, distance)
        extrude = extrudes.add(extrudeInput)
0 Likes
Message 3 of 9

j.han97
Advocate
Advocate

Hi @michalKUPG8,

 

Something you might want to know before you continue: I was interested in doing something similar as well but I gave up. The main reason being Extrudes only work for planar surfaces, and my working scope involved curved surfaces. Therefore 'PressPull' feature was a must, but sadly this feature is not available through API as far as I know.

 

Back to your topic, what error you are facing exactly? I couldn't tell because there are many reasons of failure. Please post the error message here, it might be helpful to identify the actual problem.

 

Looking back my old script (which should work) and it is actually similar to your code, with only one additional line:

extrudeInput.participantBodies = [face.body]

 But I'm not convinced that this will be the source of error.

Message 4 of 9

michalKUPG8
Explorer
Explorer

Than you guys for help. I can now use extrude via script.

Now there are more problems:

* The change is not rendered after the script is executed. When I select the face again it refreshes and it is rendered correctly... How do I refresh the view by the script itself?

* How Can I refer to the new face created by that extrude command? I want to change its color.

* How Do Iterate though all the selected faces_?

 

Yeah Press/Pull is what I need... but as it is not possible... I will have to do it via extrude... multiple times...

 

Maybe I will make an addin so I will select each face maually one by one and the moment I select it it happens...

0 Likes
Message 5 of 9

j.han97
Advocate
Advocate

1. I am thinking 

adsk.doEvents()

or

app.activeViewport.refresh()

 might be able to update the view.

 

2. I thought extrudes will not create extra faces, so the face you want is probably just the face you started with (the face being extruded).

If not, you could try looking into the faces property of your extrude feature:

extrude.faces

 This returns the faces created by this feature. (Personally I have not used this before)

 

3. Now you are using ui.selectEntity which is difficult to select multiple faces. It is better to create an add-in and provide a selectionCommandInput to select the faces. Then iterate through the selections of the selectionCommandInput. You can read more here:

Fusion 360 Help | SelectionCommandInput Object | Autodesk

 

I personally prefer writing an add-in but you can do this via a script (just have to create a command dialog).

Message 6 of 9

BrianEkins
Mentor
Mentor

A picture of your model and what you're trying to do would help. If you'll be offsetting all of the faces, what about creating a Shell feature to create an internal or external set of faces, depending on if you need the new model larger or smaller than the original. You can then use a Delete Face feature to remove all of the existing faces, leaving only the new offset faces.

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

michalKUPG8
Explorer
Explorer

Thank you for your reply. I have tried to make an add-in with select feature and my thought was I am going to extrude the face in the moment I select it... so I don't have a problem with non planar faces as extrude cannot handle it. It kinda works but the moment I deselect the face it jumps back and the extrude cut is un-done. Same when I select another one. Then I have to click the new face multiple times to make the extrude work and as soon as I select another one or deselect it it is gone.

 

Bodies are going to be just rectangular boxes and I want always to select one face of it, move it 2mm inside and color it. Maybe add a special property to it, so I can go via a cycle later through affected faces.

 

def command_created(args: adsk.core.CommandCreatedEventArgs):
    inputs = args.command.commandInputs
    tabCmdInput1 = inputs.addTabCommandInput('tab_1', 'Tab 1')
    tab1ChildInputs = tabCmdInput1.children
    selectionInput = tab1ChildInputs.addSelectionInput('selection', 'Select', 'Basic select command input')
    selectionInput.setSelectionLimits(1,1)

    cmd = args.command
    cmd.isExecutedWhenPreEmpted = True

    onSelect = MySelectHandler()
    cmd.select.add(onSelect)
    handlers.append(onSelect) 

class MySelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            selectedFace = adsk.fusion.BRepFace.cast(args.selection.entity) 
            if selectedFace:
                comp: adsk.fusion.Component = selectedFace.body.parentComponent
                extrudes = comp.features.extrudeFeatures
                extrudeInput = extrudes.createInput(selectedFace, adsk.fusion.FeatureOperations.CutFeatureOperation)
                distance = adsk.core.ValueInput.createByString('-2 mm')
                extrudeInput.setDistanceExtent(False, distance)
                extrude = extrudes.add(extrudeInput)
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

0 Likes
Message 8 of 9

michalKUPG8
Explorer
Explorer

OdebratHranu.PNG

 

Here i an example of an object I want to modify by the script. I have managed to reach functionality i need, also with the color change using extrude.faces[0] as reference to new face created by the extrude command... But now I dont know how to make this permanent... The extrude also disappears when I click OK on the popup...

0 Likes
Message 9 of 9

j.han97
Advocate
Advocate

Hi @michalKUPG8 ,

 

Not sure if you have figured it out or not: the actions should be placed in command.execute event instead of command.select event. The select event is more for preview or selection filtering purposes, while the execute event actually decides what the command will do.

 

Hope it is clear to you, if not, please let me know so I could explain them in more details. 

0 Likes