Import multiple spline profiles and revolve each

Import multiple spline profiles and revolve each

Anonymous
1,255 Views
5 Replies
Message 1 of 6

Import multiple spline profiles and revolve each

Anonymous
Not applicable

Hi,

 

It's my first time using the Fusion API and I am struggling with it. I am using Windows 10 and Python. I want to import multiple splines into the Fusion (which currently works nicely with the "ImportSplineCSV" sample script or "ImportCSVPoints" Add-In by hanskellner. After importing the splines, I want to create closed profiles with them and revolve each profile around the same axis. Here is how it looks like the UI: 

 

panajotovic_0-1627633750748.pngThat's simple, but I would like to automate it since I will need to import dozens of splines at a later point and revolve each in the same manner. As many similar examples, I tried modding the "ImportSplineCSV" sample code to first batch import all three splines as .csv files as done in this thread (https://forums.autodesk.com/t5/fusion-360-api-and-scripts/trouble-with-batch-import-and-loft-of-csv-...

 

 

 sketches = []

for i in range(0, 3):  
filename = "C:/.../df_vis_{}.csv".format(i)        
        
   with io.open(filename, 'r', encoding='utf-8-sig') as f:         
      points = adsk.core.ObjectCollection.create()  
      line = f.readline() 
      data = []                                                  
         while line:
            pntStrArr = line.split(',')
            for pntStr in pntStrArr:  
               try:
                  data.append(float(pntStr))
               except:
                  break
            if len(data) >= 3 :                                                 
               point = adsk.core.Point3D.create(data[0], data[1], data[2])     
               points.add(point)                                                               
            line = f.readline()
            data.clear()

   if points.count:
      root = design.rootComponent
      sketch = root.sketches.add(root.xYConstructionPlane)
      sketch.sketchCurves.sketchFittedSplines.add(points)   
      sketches.append(sketch) 
   else:
      ui.messageBox('No valid points', title)  

 

This works well enough and I am able to import all three splines simultaneously. However, I am unsure how to create closed profiles from these splines using python API as I did manually on the middle image above. Once that is done, I would use the following piece of code to create the revolved solid body from each profile:

 

# Creating profiles from the imported splines

profiles = adsk.core.ObjectCollection.create()
for profile in sketch.profiles:
   profiles.add(profile)

# Revolve each profile around the X axis
revolves = root.features.revolveFeatures
input = revolves.createInput(profiles, sketch.sketchCurves.sketchLines.item(0), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
input.setAngleExtent(True, adsk.core.ValueInput.createByString('360 deg'))    
revolves.add(input)

 

 

 

Can someone point me in the right direction on how to create these closed profiles from imported splines? 

 

Another more general question: I would like Fusion to print imported points (like I would print contents of a list or data frame in VSCode terminal). Is this possible and if yes how?

 

Thank you in advance!

 

Stefan

 

0 Likes
Accepted solutions (3)
1,256 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @Anonymous .

 

I think we need to import all the splines into one sketch in order to create a profile.
That is the same as the GUI.

 

If you want to create a sketch for each spline, you may want to use the include method or addByNurbsCurve method to create a profile.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-e1e62cce-ab90-47f6-88af-2fc12e4c2b65 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-EAC7918A-A198-499D-A649-1547FF35FF92 

0 Likes
Message 3 of 6

BrianEkins
Mentor
Mentor

Profiles are closed planar areas in a sketch.  They are typically on the X-Y plane of the sketch.  In your case, you'll need to create the 3 splines within a single sketch and also draw the lines that serve to define the ends of profile.  These lines do not have to terminate exactly on the splines but can extend beyond.  Fusion will find all closed areas and treat that as a profile.  You can get the profiles Fusion has calculated by using the Sketch.profiles property.  Ideally, you'll get back a single profile.  If the geometry is more complex and has multiple closed areas you'll need to examine the returned profiles to determine which one is the one you want to revolve.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 4 of 6

Anonymous
Not applicable
Accepted solution

Hi again,

 

following the advice of @BrianEkins , I did manage to include all profiles into a single sketch, but instead of examining all profiles for the one I need, I thought that I could implement a for-loop and revolve each profile immediately after it is created in its own separate sketch. I would then repeat the process of how many profiles I need.

 

However, regardless of which approach I take, I cannot get the revolve feature to run. I tried modifying my code using the "Simple Revolve Feature API Sample":

try:
   app = adsk.core.Application.get()                       
   ui  = app.userInterface                                 
   design = app.activeProduct
   for i in range(0, 3): 
            filename = "C:/.../df_vis_{}.csv".format(i)        
            with io.open(filename, 'r', encoding='utf-8-sig') as f:         
                points = adsk.core.ObjectCollection.create()                       
                line = f.readline()                                         
                data = []                                                                                               
                while line:
                    pntStrArr = line.split(',')                             
                    for pntStr in pntStrArr:                                
                        try:
                            data.append(float(pntStr))                      
                        except:
                            break

                    if len(data) >= 3 :                                                 
                        point = adsk.core.Point3D.create(data[0], data[1], data[2])     
                        points.add(point)                                                                                                          

                    line = f.readline()
                    data.clear()

            if points.count:
                root = design.rootComponent                                     
                sketches = root.sketches
                xyPlane = root.xYConstructionPlane
                sketch = sketches.add(xyPlane)
                lines = sketch.sketchCurves.sketchLines                         
                axisLine = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(200, 0, 0))
                
                try:
                    for p in range(0, len(points)):
                        line = lines.addByTwoPoints(points[p], points[p+1])     
                    prof = sketch.profiles.item(0)                              
                    revolves = root.features.revolveFeatures
                    revInput = revolves.createInput(prof, axisLine, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)         
                    angle = adsk.core.ValueInput.createByReal(math.pi)          
                    revInput.setAngleExtent(False, angle)                       
                    ext = revolves.add(revInput)                                
                except:
                    continue
            else:
                ui.messageBox('No valid points')
except: 
   if ui:
       ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

The above code works until the "prof = sketch.profiles.item(0)" line. I am namely able to create the closed profile in separate sketches and a corresponding revolving axis, but not the actual revolved component. This is the result for three sets of points creating three closed profiles: 

panajotovic_0-1628585600207.png

 

Does someone have an idea where I am mistaken or how this can be done more efficiently? Thanks in advance!

 

Best regards

 

Stefan 

0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor
Accepted solution

Let me see if I can explain the concept of profiles better.  First, as was said before, the sketch geometry you want to participate in the profile needs to exist within a single sketch.  Here's an example of drawing geometry in a sketch (either with the UI or the API) and what's happening with respect to profiles.

 

First I draw three lines.  Because there aren't any closed areas, no profiles exist.

SketchExample.png

Next, I draw a vertical line. Still, there are no closed areas so no profiles exist.

 SketchExample2.png

Next, I draw a second vertical line.  Now there are two closed areas and as a result, two profiles now exist, as highlighted below.

SketchExample4.png

An important concept to understand is that you don't have anything to do with when and how profiles are calculated. You just draw the sketch geometry and as geometry is added to the sketch, Fusion automatically calculates and creates the possible parameters. You access these automatically created profiles using the Sketch.profiles property, which returns a Profiles collection object. You can use the functionality available on the Profile object to determine which profile is the profile you want.

 

In most cases, when using the API, you'll draw the exact shape you want so the result is a single profile. However, there are cases where you might have multiple profiles and need to find a specific one.  One approach is to use the area properties that are returned by each profile where you can get the area, perimeter, centroid, and some other area properties. Another approach is to use the ProfileLoop objects returned by a profile. This gives you geometric information about the shape of the profile and also its relationship to the original sketch geometry.

 

Once you have found the desired profile, you can use it to create a feature.  Adding additional geometry to the sketch after a feature has been created will possibly cause the feature to compute in an undesirable way because the profile that was used as input may not exist in the same way it existed when the feature was created.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 6

Anonymous
Not applicable
Accepted solution

Hi, 

 

thank you for the detailed explanation. I thought that I have understood the theory, yet my code was not working. Instead of creating a single sketch and then finding "the specific profile"; I decided to create a new sketch for each profile and simply revolve the "0" item in the profile collections. This approach was actually also proven as viable. The mistake in my code was that I used the "try-except" loop with "continue". The code just continued importing the next profile without revolving the previous one. After I removed the "continue", it worked like a charm. Thank you for your help! 

 

Stefan

0 Likes