how to dimension sketch elements programmatically?

how to dimension sketch elements programmatically?

AagAag
Advocate Advocate
2,485 Views
4 Replies
Message 1 of 5

how to dimension sketch elements programmatically?

AagAag
Advocate
Advocate

I am starting to play around with the python API. I have a small script that draws a bunch of circles. Might a good soul explain to me how I should dimension said circles programmatically, so that they will be fully constrained?

See:

 

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

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        unitsMgr = design.fusionUnitsManager
        unitsMgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits

        # 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)
        circles = sketch.sketchCurves.sketchCircles
        
        diameterFirst = 1
        circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, diameterFirst, 0), diameterFirst)
        yRange=range(0,100,10)
        for yn in yRange:
            xRange=range(0,9,1
            x=0
            y = yn * diameterFirst/9
            circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, y, 0), diameterFirst)
            for n in xRange:
                diameterNext=diameterFirst + .1
                x=x+diameterFirst+diameterNext+0.2
                pointNext = adsk.core.Point3D.create(x, y, 0)
                circleInner = circles.addByCenterRadius(pointNext, diameterNext)
                diameterFirst=diameterNext
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
 

image.png

 

0 Likes
Accepted solutions (1)
2,486 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor
Accepted solution

You use methods on the SketchDimensions collection, which you obtain from the Sketch object.  I modified your code to add this.  When placing a dimension, you need to specify the position of the text.  I tried using the center of the circle but for some reason, Fusion doesn't allow this so I offset it slightly to not be at the center.  I also added the use of the isComputeDeferred property of the sketch.  With this, the processing when from about 25 seconds to 2 seconds.

 

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
        unitsMgr = design.fusionUnitsManager
        unitsMgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits

        # 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)
        circles = sketch.sketchCurves.sketchCircles
        dims = adsk.fusion.SketchDimensions.cast(sketch.sketchDimensions)
        
        sketch.isComputeDeferred = True
        diameterFirst = 1
        circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, diameterFirst, 0), diameterFirst)
        center =  circle1.centerSketchPoint.geometry
        center.x += 0.01
        center.y += 0.01
        center.z += 0.01
        dims.addRadialDimension(circle1, center)
        yRange=range(0,100,10)
        for yn in yRange:
            xRange=range(0,9,1) 
            x=0
            y = yn * diameterFirst/9
            circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, y, 0), diameterFirst)
            center =  circle1.centerSketchPoint.geometry
            center.x += 0.01
            center.y += 0.01
            center.z += 0.01
            dims.addRadialDimension(circle1, center)
            for n in xRange:
                diameterNext=diameterFirst + .1
                x=x+diameterFirst+diameterNext+0.2
                pointNext = adsk.core.Point3D.create(x, y, 0)
                circleInner = circles.addByCenterRadius(pointNext, diameterNext)
                center =  circleInner.centerSketchPoint.geometry
                center.x += 0.01
                center.y += 0.01
                center.z += 0.01
                dims.addRadialDimension(circleInner, center)
                diameterFirst=diameterNext

        sketch.isComputeDeferred = False
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5

AagAag
Advocate
Advocate

thank you for your invaluable help, which is greatly appreciated. I hope that I will not abuse your kindness by asking a further related question: how do I programmatically dimension the distance of each circle in the XY space, e.g. by specifying the distance to the origin, so that the sketch will be fully constrained? Thanks in advance!

0 Likes
Message 4 of 5

kandennti
Mentor
Mentor

Hi @AagAag .

 

Because it looks interesting, I tried it.

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = app.activeProduct
        unitsMgr = design.fusionUnitsManager
        unitsMgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits

        # 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)
        circles = sketch.sketchCurves.sketchCircles
        dims = adsk.fusion.SketchDimensions.cast(sketch.sketchDimensions)
        
        # extension method
        adsk.fusion.SketchCircle.setDimension = setDimension

        sketch.isComputeDeferred = True
        diameterFirst = 1
        circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, diameterFirst, 0), diameterFirst)
        center =  circle1.centerSketchPoint.geometry
        center.x += 0.01
        center.y += 0.01
        center.z += 0.01
        dims.addRadialDimension(circle1, center)
        circle1.setDimension()

        yRange=range(0,100,10)
        for yn in yRange:
            xRange=range(0,9,1) 
            x=0
            y = yn * diameterFirst/9
            circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, y, 0), diameterFirst)
            center =  circle1.centerSketchPoint.geometry
            center.x += 0.01
            center.y += 0.01
            center.z += 0.01
            dims.addRadialDimension(circle1, center)
            circle1.setDimension()
            for n in xRange:
                diameterNext=diameterFirst + .1
                x=x+diameterFirst+diameterNext+0.2
                pointNext = adsk.core.Point3D.create(x, y, 0)
                circleInner = circles.addByCenterRadius(pointNext, diameterNext)
                center =  circleInner.centerSketchPoint.geometry
                center.x += 0.01
                center.y += 0.01
                center.z += 0.01
                dims.addRadialDimension(circleInner, center)
                circleInner.setDimension()
                diameterFirst=diameterNext

        sketch.isComputeDeferred = False
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def setDimension(
    self :adsk.fusion.SketchCircle):

    skt :adsk.fusion.Sketch = self.parentSketch
    ori :adsk.fusion.SketchPoint = skt.sketchPoints.item(0)
    ctr :adsk.fusion.SketchPoint = self.centerSketchPoint
    ary = [(o + c) * 0.5 for (o, c) in 
        zip(ori.geometry.asArray(), ctr.geometry.asArray())]
    mid :adsk.core.Point3D = ori.geometry.copy()
    mid.setWithArray(ary)

    dims :adsk.fusion.SketchDimensions = skt.sketchDimensions
    h = adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation
    v = adsk.fusion.DimensionOrientations.VerticalDimensionOrientation

    dims.addDistanceDimension(ori, ctr, h, mid, True)
    dims.addDistanceDimension(ori, ctr, v, mid, True)
Message 5 of 5

BrianEkins
Mentor
Mentor

If your end goal is to create a fully constrained sketch, I would recommend just "fixing" the circle.  In the UI you do this by adding a Fix constraint.  In the API you can set the isFixed property of the SketchCircle object to True.  The only reason to add dimension constraints is if you want to edit the diameter or positions of the circles parametrically.  Changing them to be fixed will be easier and faster.

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