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: 

SketchLine.midSketchPoint property?

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
dahousecat
644 Views, 9 Replies

SketchLine.midSketchPoint property?

I can get a SketchPoint for the start and end of a SketchLine with the startSketchPoint and endSketchPoint properties.

 

I would like to draw a new line and specify one end to be at the centre of an existing line.

 

I was expecting to be able to use something like a midSketchPoint property of the existing line but it seems this doesn't exist.

 

What is the easiest way to create a SketchPoint at the centre of an existing SketchLine?

 

 

9 REPLIES 9
Message 2 of 10
MichaelT_123
in reply to: dahousecat

Mr. DaHouseCat,

 

Take  line.startPoint.geometry and line.endPoint.geometry.

Take averages of their respective coordinates e.g. x_ave = (x_sP + x_eP)/2

Create based on them the new Point3D and a corresponding sketch point (directly or indirectly by the creation of a new line). 

 

Good Luck

MichaelT

 

 

MichaelT
Tags (3)
Message 3 of 10
dahousecat
in reply to: MichaelT_123

Hi MichaelT,

 

Yes of course. Thanks for the pointer.

 

This fuction ended up working great for me:

 

def lineMidPoint(sketchLine):
    sx = sketchLine.startSketchPoint.worldGeometry.x
    sy = sketchLine.startSketchPoint.worldGeometry.y
    sz = sketchLine.startSketchPoint.worldGeometry.z

    ex = sketchLine.endSketchPoint.worldGeometry.x
    ey = sketchLine.endSketchPoint.worldGeometry.y
    ez = sketchLine.endSketchPoint.worldGeometry.z
    
    ax = (sx + ex) / 2
    ay = (sy + ey) / 2
    az = (sz + ez) / 2
    
    return adsk.core.Point3D.create(ax, ay, az)
Message 4 of 10
MichaelT_123
in reply to: dahousecat

Perfect DaHouseCat,

 

You might consider, however:

- If you know that the line is 2D you can omit z coordinate as it will always be 0.

- if you just dealing with the sketch without considering broader design context geometry instead wordGeometry would be sufficient. It is supposedly cheaper. 

- If you want some a little bit more advanced way to do the same, look at curve evaluator functionality.

  It might be overkill in this case though.

 

Regards

MichaelT

MichaelT
Message 5 of 10
BrianEkins
in reply to: dahousecat

To get the same result as when sketching in the user-interface and so your line will remain connected to the center of the other line if you make changes, you can use the midpoint constraint. Something like the code below.  The initial position of the second point of line2 doesn't matter because it will get repositioned when the constraint is added.

 

 

pnt1 = adsk.core.Point3D.create(0,0,0)
pnt2 = adsk.core.Point3D.create(8,9,0)
line1 = lines.addByTwoPoints(pnt1, pnt2)

pnt3 = adsk.core.Point3D.create(0,5,0)
pnt4 = adsk.core.Point3D.create(4,4,0)
line2 = lines.addByTwoPoints(pnt3, pnt4)

sk.geometryConstraints.addMidPoint(line2.endSketchPooint, line1)
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 10
MichaelT_123
in reply to: BrianEkins

Hi Mr. Ekins,

This is exquisite and in many circumstances handy way...

It might be useful to note that 'the direct' and 'the constrained' method are not 1:1 equivalent.

The difference is in the final relations (constrained or not) of created lines and as such subject of design intentions.

With Regards

MichaelT

 

MichaelT
Message 7 of 10
unclematt
in reply to: BrianEkins

Mr. Brian Ekins - 

 

I entered this script, and it creates the 2 lines, but it gives an error on the line that creates the midpoint constraint.

 

Can you test the script I pasted below and see if you get the same error, and if you can tell what the problem is?  I tried it on both Mac and Windows and get the same error in both places.

midpoint_constraint_error.png

 

Here is the exact code I am running (made some tweaks to turn it into a full ready-to-run script):

 

#Author- sample from Fusion API forum by Brian Ekins 
#Description-midpoint constraint https://forums.autodesk.com/t5/fusion-360-api-and-scripts/sketchline-midsketchpoint-property/td-p/8453832

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        #des = adsk.fusion.Design.cast(app.activeProduct)        
        #root = des.rootComponent            
        
        drawLinesWithMidpointContraint()
            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def createNewComponent():
    # Get the active design.
    app = adsk.core.Application.get()
    product = app.activeProduct
    design = adsk.fusion.Design.cast(product)
    rootComp = design.rootComponent
    allOccs = rootComp.occurrences
    newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
    return newOcc.component
    
def drawLinesWithMidpointContraint():
    try:    
        newComp = createNewComponent()
        
         # Create a new sketch.
        sketches = newComp.sketches
        xyPlane = newComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
                
        pnt1 = adsk.core.Point3D.create(0,0,0)
        pnt2 = adsk.core.Point3D.create(8,9,0)
        line1 = sketch.sketchCurves.sketchLines.addByTwoPoints(pnt1, pnt2)
    
        pnt3 = adsk.core.Point3D.create(0,5,0)
        pnt4 = adsk.core.Point3D.create(4,4,0)
        line2 = sketch.sketchCurves.sketchLines.addByTwoPoints(pnt3, pnt4)
        
        sketch.geometryConstraints.addMidPoint(line2.endSketchPoint, line1)
    
    except:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        

 

 

 

Message 8 of 10
BrianEkins
in reply to: unclematt

The sketch property is geometricConstraints, not geometryConstraints.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 9 of 10
unclematt
in reply to: BrianEkins

>> The sketch property is geometricConstraints, not geometryConstraints.

 

Rats! Yes, this fixed the problem. I really wish the Spyder IDE editor would have given a complaint that the code was referencing an invalid namespace.

 

So, you might want to go edit the code sample in your initial reply, because the code is wrong there. Just don't want anyone else to try using that code and have the same issue.

 

Thanks for helping me.

Message 10 of 10
JesusFreke
in reply to: unclematt

If you want to use PyCharm instead, it has much better support for that sort of thing. See: https://forums.autodesk.com/t5/fusion-360-api-and-scripts/now-available-fusion-360-plugin-for-intell...

 

It does a pretty good job inferring the type of a variable, and it will give you warnings about things like this when it can. In some cases, it can't infer the type, but you can use python type hints to help it along.

 

geometryConstraintWarning.png

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