How to create a coincident constraint between a line end-point and another mid-point

How to create a coincident constraint between a line end-point and another mid-point

jimsneddon6
Participant Participant
605 Views
3 Replies
Message 1 of 4

How to create a coincident constraint between a line end-point and another mid-point

jimsneddon6
Participant
Participant

Requirement:
I hope someone can outline the python code workflow to set a coincident constraint between the end-point of a line (lineA) and the mid-point of another line (lineB). I'm still new to the Fusion API and have tried everything I could think of with my limited experience.

 

Problem:

1) I cannot see anywhere in the API that I can acquire a SketchPoint reference to my SketchLine (lineB) mid-point, which led me to believe this may well be the wrong approach anyway.
2) Failed attempt A: I created a Point3D from the calculated mid-point coordinates (mp:Point3D), but the sketch.geometricConstraints.addCoincident() method requires SketchPoint arguments. I tried it anyway, but as expected the following call raised a runtime type error:
sketch.geometricConstraints.add(lineA.endSketchPoint, mp, ,,)
3) Failed attempt B: Like 2, I created a Point3D from the calculated mid-point coordinates and used sp:SketchPoint = sketch.sketchPoints.add(mp:Point3D) to get a SketchPoint instance with the correct coordinates. As assumed, it creates a new unique SketchPoint on the sketch which has no relationship to lineB. So, when I tried the following call it made the new SketchPoint coincident with LineA.endSketchPoint and did not affect lineB:
sketch.geometricConstraints.add(lineA.endSketchPoint, sp, ,,)

 

Code Snippets:

# Code for (2) above that raised runtime type error
lineA:SketchPoint
lineB:SketchPoint
mp:Point3D = lineMidPoint(lineB) # A method I wrote myself
sketch.geometricConstraints.addCoincident(lineA.endSketchPoint, mp)

 

# Code for (3) above created the coincident constraint between the 'wrong' sketch entities
lineA:SketchPoint
lineB:SketchPoint
mp:Point3D = lineMidPoint(lineB) # A method I wrote myself
smp:SketchPoint = sketch.sketchPoints.add(mp)
sketch.geometricConstraints.addCoincident(lineA.endSketchPoint, smp)

 

0 Likes
Accepted solutions (2)
606 Views
3 Replies
Replies (3)
Message 2 of 4

jimsneddon6
Participant
Participant
Accepted solution

Replying to myself to let folk's know I found 'a' solution, although with little experience of the API I cannot claim it is necessarily the best solution. Anyway I carried on changing search queries to throw into the web-ether; lo and behold I got an answer. So, unless someone can suggest a better solution I've only had to change my original 'Failed attempt B' in my original post:

 

# Modified 'Code for (3)' which works
Adding the single line (marked # *2) appears to take the independent SketchPoint added to the sketch (# *1) and 'tie' it to the mipoint of lineB, which allows my original .addConstraint() call to produce the desired result:

 

lineA:SketchPoint
lineB:SketchPoint
mp:Point3D = lineMidPoint(lineB) # A method I wrote myself
smp:SketchPoint = sketch.sketchPoints.add(mp) # *1, as original code

sketch.geometricConstraints.addMidPoint(smp, lineB) # *2, added to original code

 

sketch.geometricConstraints.addCoincident(lineA.endSketchPoint, smp)  # Arg order irrelevant

0 Likes
Message 3 of 4

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

You can use the end (o start) SketchPoint of a curve to build the midPoint constraint; this way you don't need to build another SketchPoint for it.

 

In the following snipped I build 4 couple of lines (with 10cm displacement over the X axis) constraining the end point of lineA to the midpoint of lineB, with some variants so it could be defined which line to move:

def mid_point_constraint() -> None:
    if root.sketches.count:
        skt = root.sketches[0]
    else:
        skt = root.sketches.add(root.xYConstructionPlane)

    for k in range(4):
        lineA = skt.sketchCurves.sketchLines.addByTwoPoints(
            adsk.core.Point3D.create(k*10 + 1, 1, 0),
            adsk.core.Point3D.create(k*10 + 5, 1, 0)
        )
        lineB = skt.sketchCurves.sketchLines.addByTwoPoints(
            adsk.core.Point3D.create(k*10 + 7, -1, 0),
            adsk.core.Point3D.create(k*10 + 7, 9, 0)
        )

        if k == 1:
            lineA.startSketchPoint.isFixed = True
        elif k == 2:
            lineB.isFixed = True
        elif k == 3:
            lineA.isFixed = True
            lineB.isFixed = True

        try:
            mid_point_constr = skt.geometricConstraints.addMidPoint(lineA.endSketchPoint, lineB)
        except:
            app.log('Failed:\n{}'.format(traceback.format_exc()))

 

The results are as follows:

1. LineA is kept in place while lineB is moved so that its midpoint is coincident to end of lineA.

2. LineA start point is fixed, so end point of lineA is the one who moves to the mid point of lineB.  LineB doesn't move.

3. The lineB is fixed, so the lineA is moved so its end point is coincident with midpoint of lineB.

4. LineA and lineB are fixed: it generate an exception because the constraint couldn't be applied (lines couldn't be moved at all):

 

 Failed:
Traceback (most recent call last):
  File "C:\fusion/fusion_script/testings\_t26_apply_constraints.py", line 47, in mid_point_constraint
    mid_point_constr = skt.geometricConstraints.addMidPoint(lineA.endSketchPoint, lineB)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users/Usuario/AppData/Local/Autodesk/webdeploy/production/c4a5520f9bb0f0174c02662af8bd1ab67cee6298/Api/Python/packages\adsk\fusion.py", line 26320, in addMidPoint
    return _fusion.GeometricConstraints_addMidPoint(self, point, midPointCurve)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: 3 : failed to create offset: VCS_SKETCH_SOLVING_FAILED - Failed to solve. Please try revising dimensions or constraints.

Jorge_Jaramillo_1-1731871678384.png

 

 

I hope this can help you.

 

Regards,

Jorge Jaramillo

 

 

 

0 Likes
Message 4 of 4

jimsneddon6
Participant
Participant

Hey Jorge,

 

Nice sample code and graphics to back it up, as usual. Your sample showed no need to add an unnecessary point to the sketch, keeping it clean, as well as highlighting the different outcomes based on the constraints in place at the time.

 

Also, I didn't realise I'd get kudos for my own 'solution' to my own problem - I don't think the algorithm should do this. If I do find something that works myself, which as in this case worked but was not the cleanest way, I will report it in the thread as I did but will not 'accept' it as a solution. I thought I was helping but I now see it as a 'cheat' to boost ones own ranking, which was not my intent.

0 Likes