How to make points in inches instead of cm

How to make points in inches instead of cm

jakedbirk
Enthusiast Enthusiast
823 Views
2 Replies
Message 1 of 3

How to make points in inches instead of cm

jakedbirk
Enthusiast
Enthusiast

Hello, 

 

I have been learning about and working in the Fusion 360 API environment for quite some time now and am starting to get a good grasp of understanding the documentation and object model. 

 

I am trying to make an API that will model stairs infusion 360 using nothing but the script.  

 

One problem I keep running into is units, particularly when plotting points for a sketch, is creating points in inches. I need everything to be in inches to work properly. (This is the for USA construction industry, that's how it is.)

 

I've read and understand most of the "Understanding Units in Fusion 360" documentation but still can't figure out how to use inches when entering a float value or integer value. I know that I can use createByString to make a Value Input in inches (for something like an extrusion), but I can't enter those when making points. Anything else (like making points) goes by the database units (cm) which I don't want. 

 

Of course, I could divide all of the parameters by 2.54, but that seems like a very indirect and inefficient way of solving the problem. 

 

So is there anyway to change the fusion 360 API database units to inches so I could for example create a point using x,y,z coordinates in inches?

 

#Note: This code was copy and pasted from "SD468474
#Getting Started with the Fusion 360 API
#Patrick Rainsberry
#Autodesk"
import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
ui = None
 try:


  app = adsk.core.Application.get()
  ui = app.userInterface


  design = app.activeProduct
  rootComp = design.rootComponent
  sketches = rootComp.sketches 
  xyPlane = rootComp.xYConstructionPlane
  sketch = sketches.add(xyPlane)
  lines = sketch.sketchCurves.sketchLines

#Make Points for Sketch 
  point0 = adsk.core.Point3D.create(0, 0, 0)#These three values are the ones 
  that I would like to create using inches
  point1 = adsk.core.Point3D.create(0, 1, 0)
  point2 = adsk.core.Point3D.create(1, 1, 0)
  point3 = adsk.core.Point3D.create(1, 0, 0)

  #This way the lines they create would also be in inches 
  lines.addByTwoPoints(point0, point1)  
  lines.addByTwoPoints(point1, point2)
  lines.addByTwoPoints(point2, point3) 
  lines.addByTwoPoints(point3, point0)


  profile = sketch.profiles.item(0)
  extrudes = rootComp.features.extrudeFeatures
  ext_input = extrudes.createInput(profile, 
  adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

#I understand how to change this distance ValueInput to inches by using #createByString

#This would be for CM# distance = adsk.core.ValueInput.createByReal(1)
  distance = adsk.core.ValueInput.createByString("2 in")#This would make the 
  extrusion 2 inches
  ext_input.setDistanceExtent(False, distance)
  ext_input.isSolid = True
  extrudes.add(ext_input)
  except:
 if ui:
  ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

The code that I am working on is getting a bit long and so I've attached a sample code so it will be easier to answer and make more sense for future readers!

0 Likes
Accepted solutions (1)
824 Views
2 Replies
Replies (2)
Message 2 of 3

BrianEkins
Mentor
Mentor
Accepted solution

Internally, the database units of Fusion are ALWAYS centimeters with no way to change that. The API tries to be a light layer over the Fusion's internal implementation and we didn't want to add code to the API to convert units. For cases that expect a value that is a distance unit, like when specifying the coordinates for a sketch point, you'll have to specify those values in centimeters.

 

The UnitsManager object has a function to convert units, but I think since in your case it will always be inches to centimeters, using that function is a bit overkill and you can do the conversion directly, (val/2.54).

 

You can create functions to help simplify this if you want.  Below are a couple of possible examples.

 

 

# Creates a Point3D given the x, y, z coordinates in inches.
def createInchPoint(x: float, y: float, z: float) -> adsk.core.Point3D:
    return adsk.core.Point3D.create(x * 2.54, y * 2.54, z * 2.54)

# Add a new SketchPoint to the given sketch where the input x, y, z coordinates are in inches.
def addInchPoint(sk: adsk.fusion.Sketch, x: float, y: float, z: float) -> adsk.fusion.SketchPoint:
    pnt = adsk.core.Point3D.create(x * 2.54, y * 2.54, z * 2.54)
    return sk.sketchPoints.add(pnt)

 

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

jakedbirk
Enthusiast
Enthusiast

Brian, 

 

Thank you for the detailed answer, I will integrate the addInchPoint and createInchPoint as you defined them in your reply. You've saved me a lot of time trying to figure out how to convert naturally!

 

Appreciatively,

Jake Birkmaier

0 Likes