Can you place the center of a sketch circle with absolute coordinates?

Can you place the center of a sketch circle with absolute coordinates?

MichaelAubry
Autodesk Autodesk
3,443 Views
8 Replies
Message 1 of 9

Can you place the center of a sketch circle with absolute coordinates?

MichaelAubry
Autodesk
Autodesk

Hi! I'm continuing to work on my fibonacci lofted path.  I've hit a snag with my sketch profile placement.  My sketches aren't lining up correctly. The root of the problem is my sketch plane tilts such that it isn't in line with the original orientation. This is becuase it's created from a plane along a path from a spline.  I'd like my circles to line up coincident with the endpoints of my spline. 

 

Is there a way to place a circle inside a sketch using absolute coordanates for the center point?

 

Thank you!

 

absolute.jpgrelative coordiantes.jpg

 


this is what I want.jpg

 

Here's my code:

 

import adsk.core, adsk.fusion

 

app= adsk.core.Application.get()

design = app.activeProduct

ui = app.userInterface

 

#**Default User Inputs**

steps = "5 cm" #How many steps of Fibonacci would you like to plot?

#(Note, while the steps variable should be unitless, right now our API can't handle unitless number.

#cm are the absolute units we use on the API side.)

length = "3 cm" #How long is the first segment? (cm)

 

input = steps

createInput = ui.inputBox('Enter Steps', 'Steps', input)

if createInput[0]:

(input, isCancelled) = createInput

unitsMgr = design.unitsManager

realSteps = unitsMgr.evaluateExpression(input, unitsMgr.defaultLengthUnits)

 

input = length

createInput = ui.inputBox('Enter Length', 'Length', input)

if createInput[0]:

(input, isCancelled) = createInput

realLength = unitsMgr.evaluateExpression(input, unitsMgr.defaultLengthUnits)

 

#Get root component

rootComp = design.rootComponent

#Create a new sketch on XY plane

sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)

 

# Create an object collection for the points.

points = adsk.core.ObjectCollection.create()

 

# R = total steps to be run thru the For loop

R = int(realSteps - 2)

 

#starting x and y coordiantes

x = 0

y = 0

 

#Create 1st coordinate

points.add(adsk.core.Point3D.create(x,y,0))

 

#Starting Loft Profile Diameter

loftProfile1 = realLength/2

 

#starting values for sequence

fib = 1

fib1 = 1

 

#Create 2nd coordinate

x = 1 * realLength

points.add(adsk.core.Point3D.create(x,y,0))

ui.messageBox('x: ' + str(x))

 

#bins for shifting x and y coordinates

Bin1 = range(0,R,4)

Bin2 = range(1,R,4)

Bin3 = range(2,R,4)

Bin4 = range(3,R,4)

BinLoft = range(0,R)

 

for i in range(R):

fib2 = fib + fib1

fib = fib1

fib1 = fib2

fibLength = fib*realLength #adds the scalar component to coordinates

ui.messageBox('fiblength: ' + str(fibLength))

 

if i in Bin1:

x = x

y = y + fibLength

points.add(adsk.core.Point3D.create(x,y,0))

if i in Bin2:

x = x - fibLength

y = y

points.add(adsk.core.Point3D.create(x,y,0))

if i in Bin3:

x = x

y = y - fibLength

points.add(adsk.core.Point3D.create(x,y,0))

if i in Bin4:

x = x + fibLength

y = y

points.add(adsk.core.Point3D.create(x,y,0))

if i in BinLoft:

loftProfile2 = fibLength/2 #Ending Loft Profile Diameter

 

# Create the spline.

sketch.sketchCurves.sketchFittedSplines.add(points)

 

 

# Create the Starting Loft Profile

spline1 = sketch.sketchCurves.sketchFittedSplines.item(0)

planeInput = rootComp.constructionPlanes.createInput() # you could also specify the occurrence in the parameter list

planeInput.setByDistanceOnPath(spline1, adsk.core.ValueInput.createByReal(0))

plane1 = rootComp.constructionPlanes.add(planeInput)

sketch1 = rootComp.sketches.add(plane1)

circles = sketch1.sketchCurves.sketchCircles

circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), loftProfile1)

 

# Create the End Loft Profile

# (This is the part that's giving me trouble)

 

planeInput.setByDistanceOnPath(spline1, adsk.core.ValueInput.createByReal(1))

plane2 = rootComp.constructionPlanes.add(planeInput)

 

sketch2 = rootComp.sketches.add(plane2)

circles = sketch2.sketchCurves.sketchCircles

circle2 = circles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), loftProfile2)

 

 

 

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Accepted solutions (1)
3,444 Views
8 Replies
Replies (8)
Message 2 of 9

casey.rogers
Alumni
Alumni

Hey Mike,

It should be an easier approach to simply apply a coincident constraint between a project of the spline's point and the center point of the circle, exactly the same as if you were creating the design using the GUI and not the API. If you feed a point3D object into the addByCenterRadius function (as you have in your script) the API will create a circle at that point but will not create any coincident constraints. However, if you use addByCenterRadius on a sketchPoint, the API (IIRC) will automatically create a coincident constraint between your circle's center point and the sketchPoint. The code to acheive this would look roughly like this:

centerPoint = sketch2.project(spline1.endSketchPoint)
circles = sketch2.sketchCurves.sketchCircles
circle2 = circles.addByCenterRadius(centerPoint, loftProfile2)

You can also manually create the coincident constraint, but I don't expect you to need to. Let me know if the above strategy does not work.

If you're not sure whether or not you should be projecting the endSketchPoint or the startSketchPoint of the spline, there are a couple ways to go about it, and probably at least one more elegant than what I'm about to suggest, but here is how I'd do that:

def isPointOnPlaneHelper(sketch, point):
    sp = sketch.project(point)
    if sp.geometry.isEqualTo(point):
        return sp
    else:
        sp.deleteMe()
        return None

Essentially, I'm testing if a point is on a sketch's plane. If it isn't, return None. If it is, return a sketchPoint created by projecting that point onto the sketch. You may find that in the context of your script you can reliably use the startSketchPoint or endSketchPoint depending on the context, in which case the above isn't necessary.

0 Likes
Message 3 of 9

ekinsb
Alumni
Alumni

I would suggest using the Sketch.modelToSketchSpace method.  You can pass in a point in model space and it will return the equivalent point in sketch space which you can then use as the coordinates for the circle center point.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 4 of 9

MichaelAubry
Autodesk
Autodesk

Thanks Casey,

 

The code you suggested is importing and projecting the the spline point but it isn't creating the circle. Is this the correct syntax?

 

# Create the End Loft Profile

 

planeInput.setByDistanceOnPath(spline1, adsk.core.ValueInput.createByReal(1))

plane2 = rootComp.constructionPlanes.add(planeInput)

 

sketch2 = rootComp.sketches.add(plane2)

centerPoint = sketch2.project(spline1.endSketchPoint)

circles = sketch2.sketchCurves.sketchCircles

 

circle2 = circles.addByCenterRadius(centerPoint, loftProfile2)

 

 

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Message 5 of 9

MichaelAubry
Autodesk
Autodesk

Thanks Brian,

Do you have an example using Sketch.modelToSketchSpace?  I'm having trouble figuring out how to make it work.

 

Thanks!

Mike

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Message 6 of 9

casey.rogers
Alumni
Alumni

Yes, as far as I can tell that code should work!

0 Likes
Message 7 of 9

ekinsb
Alumni
Alumni
Accepted solution

Here's the new last four lines of your program with my changes highlighted..

 

            sketch2 = rootComp.sketches.add(plane2)
            circles = sketch2.sketchCurves.sketchCircles
            skPosition = sketch2.modelToSketchSpace(spline1.endSketchPoint.geometry)
            circle2 = circles.addByCenterRadius(skPosition, loftProfile2)

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

MichaelAubry
Autodesk
Autodesk

Brian thanks a bunch! That works perfectly. The .geometry part was something I neglected.  Thanks!

 

Mike

 

fib7.jpg

Michael Aubry
Autodesk Fusion 360 Evangelist
0 Likes
Message 9 of 9

eric.d.vaughan.account
Observer
Observer

I am trying to do something very simular. I want to create lofted tubes that curve in spirals. Would it be possible to see the code that actually does the loft?

 

 

 

0 Likes