Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Script misbehaves, scale wrong by 10x.

5 REPLIES 5
Reply
Message 1 of 6
DaveGadgeteer
568 Views, 5 Replies

Script misbehaves, scale wrong by 10x.

I needed a real spiral, so I used the trick of making a set of points in a spreadsheet and importing the .csv file to make a spline using the provided script. I used the Python one, because the Javascript one alway fails with a misspelled error something like "0 ponit length".

 
This works great, except that the size is off by being a factor of 10 too big. Reading the script, which is pretty simple, doesn't show any reason why this should be so. The problem has to be inside something this script calls. So, I can't fix it.
I finally built the object as enlarged then scaled it down by 10, which is a workaround, but it's not handy. Seems like this ought to be easy to fix and ought to be fixed. 
 
Also it seems weird that there's no way to scale an item in a sketch. You can only scale the whole sketch, and I didn't discover that even for a long time, so I had to build a solid 10x too large in order to have a solid object that could be scaled down to size!
 
These scripts look like really useful tools. I'll look at more of them to see if there's enough info to make it possible for me to solve some of my problems by writing scripts. I assume there's not much documentation on the Python interface yet, but I may assume wrongly...
Tags (2)
5 REPLIES 5
Message 2 of 6
ekinsb
in reply to: DaveGadgeteer

The sample scripts aren't doing anything smart about units and are creating the sketch geometry based on Fusion's internal distance units, which are centimeters.  So the units in the csv file are always assumed to be in centimeters.  The script could be enhanced to see what the current document units are and use that or possibly add the ability to define what the units are in the csv file, but for now they are centimeters.  It sounds like you want them to be millimeters so if you just multiply all of the values in the csv file by 10 you should be good.

 

I also verified the problem you reported with the JavaScript version of the script and we'll get that fixed.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 6
DaveGadgeteer
in reply to: ekinsb

Aha! 

It never even occurred to me that the software didn't know what units I was using!

Thanks for clearing that up. And I'd never have guessed centimeters would be the default! That's kind of an anomaly for scientific work, which is supposed to prefer units that differ from the base unit (meters) by any power of 1000. Surprising.

 

My spiral comes out 10x too big, so I'll just change the spreadsheet to divide the results by 10 and I'm set.

Message 4 of 6
mbyasar75
in reply to: DaveGadgeteer

Hi,

 

As of today, the problem still exists. I used ImportSplineCSV add-in and it draws every coordinate point in my csv file (attached to this message) in a 10x multiplied fashion.

Message 5 of 6
ekinsb
in reply to: mbyasar75

Maybe you missed the explanation, but there wasn't a problem to be fixed.  The simple sample script always assumes that the CSV data is defined in centimeters.  I made a small change to the script so that it now assumes that the CSV data is defined in whatever the current document units are.  Here's the changed version of the script.

 

import adsk.core, adsk.fusion, traceback

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

        # Get the active design.        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        title = 'Import Spline csv'
        if not design:
            ui.messageBox('No active Fusion design', title)
            return
            
        # Determine the scale factor to convert from the document units to centimeters.
        # This assumes that the CSV data is defined in the current document units.
        scaleFact = design.unitsManager.convert(1, design.unitsManager.defaultLengthUnits, 'cm')
        
        # Prompt the user for the CSV file to import.
        dlg = ui.createFileDialog()
        dlg.title = 'Open CSV File'
        dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
        if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
            return      
        filename = dlg.filename
        
        # Read the point data from the file.
        f = open(filename, 'r')
        points = adsk.core.ObjectCollection.create()
        line = f.readline()
        data = []
        while line:
            line = line.strip()
            pntStrArr = line.split(',')
            for pntStr in pntStrArr:
                try:
                    data.append(float(pntStr) * scaleFact)
                except:
                    pass
        
            if len(data) >= 3 :
                point = adsk.core.Point3D.create(data[0], data[1], data[2])
                points.add(point)
            line = f.readline()
            data.clear()            
        f.close()        
        root = design.rootComponent;
        sketch = root.sketches.add(root.xYConstructionPlane);
        sketch.sketchCurves.sketchFittedSplines.add(points);     
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 6 of 6
cadTPLWJ
in reply to: ekinsb

I do not know if it is appropriate to highjack this thread or if I should have made a new thread.

My question is a very small variation on the original question.

I am new to this API and new to Python, however I did quit some LISP work in Autocad a few decades ago.

 

My question 1: Why is this parabola -20 to 20 in X and 0 to 40 in Z? Would have expected Z to be 0 to 400  I just found out, I am scaling in the wrong place, compute everything in millimeters and scaleonly  when submitting the points.

            pnts.add(adsk.core.Point3D.create(x * scaleFact, -z * scaleFact,0))

My question 2: Why -z?

 

import adsk.core, adsk.fusion, traceback 
import math 
def run(context):
    ui = None 
    try: 
        app = adsk.core.Application.get()
        ui = app.userInterface
        des = app.activeProduct
        root = des.rootComponent
        sketch = root.sketches.add(root.xZConstructionPlane)
        # Default unit is cm, convert to current unit
        scaleFact = des.unitsManager.convert(1, des.unitsManager.defaultLengthUnits, 'cm')
        pnts = adsk.core.ObjectCollection.create()
        startX = -20 * scaleFact
        stopX = 20 * scaleFact
        pts=20
        for pt in range(0, pts+1):
            x = (stopX -startX) * pt / pts + startX
            # Actual function here
            z = x**2
            pnts.add(adsk.core.Point3D.create(x,-z,0))
        
        sketch.sketchCurves.sketchFittedSplines.add(pnts) 
    except: 
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

20-400.jpg

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report