wrong syntax in Script

wrong syntax in Script

jean-michel_legoff
Collaborator Collaborator
781 Views
6 Replies
Message 1 of 7

wrong syntax in Script

jean-michel_legoff
Collaborator
Collaborator

Hi,

 

I'm trying my first script in Fusion.

 

To help me, I asked Chat GPT to write the script, but it looks that it miss something.

 

Here is what I want to do:

I have a design with some named Sketches.

for each of this sketch, I wnat to generate a new component with a extrusion ot this sketch.

And name the component with the name of the sketch.

 

could someone help me to correct this script?
################################

#Author-
#Description-

import adsk.core, adsk.fusion, traceback

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

        # Get the active design
        design = app.activeProduct

        # Get the root component of the active design
        root_comp = design.rootComponent

        # Get the sketches in the design
        sketches = root_comp.sketches

        # Iterate through each sketch
        for sketch in sketches:
            # Create a new component for each sketch
            new_comp = root_comp.occurrences.addNewComponent(adsk.core.Matrix3D.create())

            # Extrude the sketch
            extrudes = new_comp.component.features.extrudeFeatures
            ext_input = extrudes.createInput(sketch.profiles.item(0), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
            distance = adsk.core.ValueInput.createByReal(5)
            ext_input.setDistanceExtent(False, distance)
            extrudes.add(ext_input)

            # Name the component with the sketch name
            new_comp.name = sketch.name

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

0 Likes
Accepted solutions (1)
782 Views
6 Replies
Replies (6)
Message 2 of 7

kandennti
Mentor
Mentor
Accepted solution

Hi @jean-michel_legoff -San.

 

Try fixing it as follows.

・・・
            # Name the component with the sketch name
            # new_comp.name = sketch.name
            new_comp.component.name = sketch.name
・・・
0 Likes
Message 3 of 7

jean-michel_legoff
Collaborator
Collaborator
Many Thanks...

That's it...
Message 4 of 7

jean-michel_legoff
Collaborator
Collaborator

Hello,

I want to modify my script a little.

 

After making the componenet, I want to copy the current sketch in this new componenet.

Iv' tried to add this line but it's not working:

 

jeanmichellegoff_0-1695715038497.png

 

can you see what's wrong?

Many Thanks

0 Likes
Message 5 of 7

jean-michel_legoff
Collaborator
Collaborator

Hello @kandennti

 

have you any hint to help me moving the sketch in the component as well?

 

Many Thanks

0 Likes
Message 6 of 7

kandennti
Mentor
Mentor

@jean-michel_legoff -San.

 

It is possible that I am not understanding what you mean.

You can move elements of a sketch by using the Sketch.move method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-908cc941-6522-4a24-94d5-90f98dce20fd 


The following sample moves all the curves in the first sketch of the first component (occurrence) by 10mm.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        # get occurrence - component
        occ: fusion.Occurrence = root.occurrences[0]
        occComp: fusion.Component = occ.component

        # get sketch
        skt: fusion.Sketch = occComp.sketches[0]
        skt = skt.createForAssemblyContext(occ)

        # create move matrix
        mat: core.Matrix3D = core.Matrix3D.create()
        mat.translation = core.Vector3D.create(1,0,0)

        # move sketch curves
        skt.move(
            core.ObjectCollection.createWithArray(
                list(skt.sketchCurves)
            ),
            mat,
        )

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


However, this may not be possible due to dimensional constraints, etc.

If you can attach more specific data, it will make it easier for us to answer, including others.

0 Likes
Message 7 of 7

jean-michel_legoff
Collaborator
Collaborator
Hello,
Sorry for the confusion,

By moving, I don't speak about geometric transformation...
I was saying moving in the construction tree and sliding the Sketch from Roo_Comopent to a new one.
But the answer is given in another thread by Copying Sketches entities from the root sketch to the component sketch.

Sorry for the annoyance.