Newbie API user - how do I constrain a SketchText?

Newbie API user - how do I constrain a SketchText?

jimsneddon6
Participant Participant
522 Views
4 Replies
Message 1 of 5

Newbie API user - how do I constrain a SketchText?

jimsneddon6
Participant
Participant

Using the API I located an 'anchor point' on my sketch (2mm down and right of the top left point shown in the image below), and a SketchText ("F0"). I could not find any guidance in the API documentation or code samples, or samples on the internet, to apply the constrains to the SketchText that I wanted:

 

 References Point & SearchText.jpg

 

I could however apply them through Fusion's UI:

 

UI Action 1 - Coincidence Constraint:

I can set a coincident constraint between my 'anchor point' and the top-left green 'control point' of the SketchText's dotted 'boundary frame': 

Coincident Setup.jpg <---before and after ---> Coincident Result.jpg 

 

UI Action 2 - Horizontal Constraint:

Through the UI I can set a horizontal constraint for the top horizontal line of the SketchText's dotted 'boundary frame': 

 Horizontal Setup.jpg     <---before and after ---> Horizontal Result.jpg

UI Action - Dimension

Through the UI can dimension the left vertical line of the SketchText's dotted 'boundary frame':

Dimensioning Setup.jpg<---before and after --->  Dimensioning Result.jpg

 

Being new to the API my idea was to simply replicate the above actions through the API. If this is a reasonable approach can anyone guide me in how these can be achieved. Alternatively, is there is a better way to achieve similar results through the API's exposed interfaces?

 

The actions above still leave the right hand vertical edge unconstrained, but I do not want to lock this with a horizontal dimension as typing additional text causes soft line breaks to be added. I am happy to alter the text and simply drag the left hand vertical line until the line breaks disappear, unless someone has a neater solution.

 

Thanks in anticipation. 

 

0 Likes
523 Views
4 Replies
Replies (4)
Message 2 of 5

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

Here you have the required API examples.

 

I draw the same lines and text as your example like so:

Jorge_Jaramillo_0-1730344003862.png

I added points (red) and lines (blue) numbers, taking the numbers (indexes) from the first part of the code below.

 

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

app = adsk.core.Application.get()
doc = app.activeDocument
des: adsk.fusion.Design = adsk.fusion.Design.cast(app.activeProduct)
root = des.rootComponent

geom_to_str = lambda x: functools.reduce(lambda a,b: f'{a}, {b}', [f'{r}: {s}' for r,s in zip("xyz", [f'{v*10:.4f} mm' for v in x.asArray()])])

def run(context) -> None:

    try:
        skt = root.sketches[0]

        # print geometry of current points and lines on first sketch
        app.log('points:')
        for k,po in enumerate(skt.sketchPoints):
            app.log(f'[{k}] {geom_to_str(po.geometry)}')
        app.log('lines:')
        for k, li in enumerate(skt.sketchCurves.sketchLines):
            app.log(f'[{k}] {geom_to_str(li.startSketchPoint.geometry)} - {geom_to_str(li.endSketchPoint.geometry)}')

        # action #1: coincident on points #3 and #8
        point3 = skt.sketchPoints[3]
        point8 = skt.sketchPoints[8]
        cc = skt.geometricConstraints.addCoincident(point3, point8)

        # action #2: horizontal constraint on line #6
        line6 = skt.sketchCurves.sketchLines[6]
        skt.geometricConstraints.addHorizontal(line6)

        # action #3: dimension over line #7 between points #3 and #8
        point5 = skt.sketchPoints[5]
        dist_dim = skt.sketchDimensions.addDistanceDimension(
                        point5, 
                        point8, 
                        adsk.fusion.DimensionOrientations.VerticalDimensionOrientation,
                        adsk.core.Point3D.create(0.5, -1.5, 0),
                        False)
        dist_dim.parameter.expression = "12 mm"

    except:
        app.log('Failed:\n{}'.format(traceback.format_exc()))
    adsk.terminate()

 

This is the result after running the three actions:

Jorge_Jaramillo_1-1730344686525.png

 

 

I hope this can help.

 

Regards,

Jorge Jaramillo

 

Message 3 of 5

jimsneddon6
Participant
Participant

Thanks Jorge, your guidance was extremely well presented by your excellent diagram linking into your example code.

 

I wanted to put in a fuller reply explaining how your advice helped me reach a solution; it may help other newbies like myself. I'm not claiming it is the best solution but it gets me the results you achieved using methods:

 

   skt.geometricConstraints.addConcident()

   skt.geometricConstraints.addHorizontal()

   skt.sketchDimensions.addDistanceDimension()

 

I think my confusion has been my expectation that artefacts I wanted to constrain would present an interface to let me apply constraints applicable to that object e.g.:

 

(i) SketchText::makeCoincidentTopLef(myDefinedAnchorPoint:Point3D)   # NOT REAL CODE

 

or that they would be the source of sketch entities applicable, such as lines and points e.g.:

 

(ii) addCoincident(SketchText::getTopLeftPoint(), myDefinedAnchorPoint::Point3D)   # NOT REAL CODE 

 

Your example shows all sketch entities used in the constraints come from the Sketch instance itself.

This led me back to (ii) and how you find those points. It finally sunk in that I needed to constrain the SketchText as soon as I added it to the sketch, at which time the points I needed would be the last four points in the Sketch's points collection:

     sketch.sketchPoints(x)

     where x = sketch.sketchPoints - 1, sketch.sketchPoints-2, sketch.sketchPoints-3, & sketch.sketchPoints-4

 

After that I reasoned the same approach would apply to any lines required and I found  sketch.sketchCurves() in the online API documentation, which also holds all straight lines including the last 4 I could access for my new SketchText .

 

A big thanks once again Jorge for not only sorting out my SketchText issues, but giving me a better understanding of the Fusion API environment. 

0 Likes
Message 4 of 5

Jorge_Jaramillo
Collaborator
Collaborator
Hi,
It is nice to know that it help you.
Regarding indexes in sketch elements, my experience is always to look for them based on their coordinates rather than the index itself, because they could be re-arranged with some operations. I used the index in the example just to clarify the constraints and dimensions operations.

Regards,
Jorge
0 Likes
Message 5 of 5

BrianEkins
Mentor
Mentor

Your original problem description wasn't clear about where the geometry you're constraining comes from. From your original question, it seemed the geometry was already there, and you needed to find it and add constraints. That's the most difficult situation because you can't guarantee what the user has created, and what you're looking for may or may not be there or may not be there in the way you expect. But then, from your follow-up response, it sounds like you're creating the geometry. That makes it MUCH easier.

 

When you create an entity, like a line or text, that entity is returned. You can keep a reference to them as you construct the rest of the geometry and then use them when creating the constraints.

 

For example, when you call the SketchTexts.add method, it returns a SketchText object. From this object, you can call the definition property to get a MultLineTextDefinition object. This has the rectangleLines property that returns the four lines surrounding the text. It's not documented, but I would expect these lines to be returned in a consistent order. A SketchLine provides the SketchPoint objects at its start and end. The sketch objects do this, so there's no reason to find them. Instead, use the API to get them explicitly.

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