Creating a sphere

Creating a sphere

dinocoglitore
Advocate Advocate
4,961 Views
7 Replies
Message 1 of 8

Creating a sphere

dinocoglitore
Advocate
Advocate

Hi all.

It seems to me that easier is the problem and harder is to find a solution !

Someone can help me ? I expect that the solution is so simple that  I cannot find it !

 

I’m trying to write a simple script to visualize a sphere. What could be simpler ?

 

I’ve tried the Sphere object

sphere = adsk.core.Sphere.create(point, 5)

but that is  transient so I cannot see anything

A question could be: how can I convert it to a visible entity ?

 

In a post I found an hint  telling that to create solids must be used the Feature object.

Looking at the examples about this argument I found that a lot of examples deals with extrusion but I cannot find any example about sphere or cylinder.

 

Using features I arrive to create the spheres collection without errors:

rootComp = design.rootComponent

features = rootComp.features

spheres = features.sphereFeatures

 

But I don’t know how to continue to add a visible sphere

I cannot find no createInput or add method (?)

 

I searched for the forum but I did not found no solution.

 

Many thanks

Dino

0 Likes
Accepted solutions (1)
4,962 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
Accepted solution

To create a sphere, one way is to use the revolve feature. I used the example code from http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-407f1b28-fd08-11e4-a671-3417ebd3d5be which is used to create a revolve and modified it slightly to create a sphere. There could be a more straightforward way to create a sphere, however.

 

import adsk.core, adsk.fusion, math, traceback

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

        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

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

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches;
        xyPlane = rootComp.xYConstructionPlane;
        sketch = sketches.add(xyPlane)

        # Draw a circle.
        circles = sketch.sketchCurves.sketchCircles
        circle = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 2)
        
        # Draw a line to use as the axis of revolution.
        lines = sketch.sketchCurves.sketchLines
        axisLine = lines.addByTwoPoints(adsk.core.Point3D.create(-3, 0, 0), adsk.core.Point3D.create(3, 0, 0))

        # Get the profile defined by half of the circle.
        prof = sketch.profiles.item(0)

        # Create an revolution input to be able to define the input needed for a revolution
        # while specifying the profile and that a new component is to be created
        revolves = rootComp.features.revolveFeatures
        revInput = revolves.createInput(prof, axisLine, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)

        # Define that the extent is an angle of 2*pi to get a sphere
        angle = adsk.core.ValueInput.createByReal(2*math.pi)
        revInput.setAngleExtent(False, angle)

        # Create the extrusion.
        ext = revolves.add(revInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Message 3 of 8

ekinsb
Alumni
Alumni

The suggestion by jeblick is the way to create a sphere and also one way to create a cylinder.  A cylinder can also be created by extruding a circle.  The API doesn't support the ability to create the primitives like you can in the user-interface.  The reason for this is because of how the primitives are implemented internally, which you can also see when you use the commands.  For example, when you run the sphere command, it first prompts you to select a plane and then you position a point on that plane.  You can also see in the browser and timeline that there is a sketch temporarily created that this point is defined on.  There isn't a good way to expose this behavior in the API.  By creating a sphere as a revolved feature you have the advantage that you have a real sketch and can control the position of the center of the sphere using standard sketch dimensions and constraints.  The current sphere primitive command is limited in how you can control the position of the sphere.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 4 of 8

dinocoglitore
Advocate
Advocate

Hi jeblick and ekinsb, thank you very much for your rapid reply. I didn’t imagine that the answer to my question needed so much use of APIs. I thought that the solution could be easier !

Now the question is: what is the use of SphereFeature object ? Only to get the properties of the sphere created with the revolve method that you propose ?

Thanks

Dino

0 Likes
Message 5 of 8

commonmandan
Contributor
Contributor

The SphereFeature object is used to get properties of existing sphere primitives. The proposed revolve method can make geometry that looks the same, but will not be able to accessed by the SphereFeature object because it is built using different tools. The proposed method can be used to make a wide range of geometry by changing the profile in the sketch and by changing the properties of the revolve feature.

0 Likes
Message 6 of 8

dinocoglitore
Advocate
Advocate

Thank you for the reply, Daniel.

So, if the SphereFeature is used to get properties from sphere primitives that are not those proposed by jeblick, when or where it can be used ? How can I create a sphere primitive ?

There is something that is missing in my comprehension of the APIs. Where can I find these informations ? Have some sample code to better let understand these concepts ?

Can you help me ?

Thanks Dino

0 Likes
Message 7 of 8

commonmandan
Contributor
Contributor

The original way to make a sphere in Fusion 360 (through the User Interface) was to draw a circle and revolve it. This method or workflow of sketching and then applying an extrusion, revolve, loft, or sweep is very common in 3D CAD software and is generally the practice that most people follow. The code that jeblick provided is the automation of this workflow and where the case is a sphere.

 

The Fusion 360 team decided to simplify this workflow for creating basic shapes by creating primitives. Currently, like Brain said, Fusion 360 does not have an API for creating primitives. However it would cause issues if add-ins or scripts had to work with components that contained primitives, so they exposed the sphereFeature object in the API. When they expose functions through the API, they are basically locked into what they provide, unable to improve or change things, so they need to be very careful.

 

I know you would like a simpler way to create a sphere, but in reality that means that the Fusion 360 team is just putting a black box around the current proposed solution. You can do what they did not by creating a function that wraps a good chunk of that code above, then calling that function when you need to create a sphere. The fact of the matter is that programmers can create functions that remove repetitive tasks, while end users can not. You can also create functions to group code and hide complexity. It might look like a lot of code, but in the long run your be better off for having a solution that can be modified to create other shapes.

 

I think the current documentation requires a lot of 3D CAD and programming experience to understand the API. Basically you have to put yourself in the shoes of the people making Fusion 360 and the workflows that their imagining for their end users. Hopefully as time progresses there will be more examples, until then everyone will have to use the forum to ask questions in order to wrap their brains around the API.

 

 

 

Message 8 of 8

dinocoglitore
Advocate
Advocate

Hi Daniel, I’m very happy for your answer and I thank you a lot. You clarify me a lot of things.

I’m very sorry that the method to learn Fusion API is to disturb forum experts. For my training I’m used to study a lot, trying to understand the concepts and using programming to apply and verify what I’ve learned and understood.

It seems that, in this case, I have to use programming to learn the concepts. In this way the learning process become very long. I will change my study method.  

I thank you again.

Dino

0 Likes