Hidden Geometric Constraints (Bug, feature or operator error?)

Hidden Geometric Constraints (Bug, feature or operator error?)

sgraves
Enthusiast Enthusiast
847 Views
4 Replies
Message 1 of 5

Hidden Geometric Constraints (Bug, feature or operator error?)

sgraves
Enthusiast
Enthusiast

I have run into some unexpected constraints when creating some curves (lines specifically).  I now know why they are happening and in most cases prevent them from happening.  The fact is, they are hidden constraints.  They don't show up on any of the constraint lists.  They happen when you refer to a defined point. For example, sketch.originPoint.  I can almost understand that constraint and why it would be hidden.  But it also happens when you refer to a point on another curve.

 

In the following script, a line is created, then another line the refers to the origin and endpoint of the first line.  The print statements show no constraints (Am I doing the prints right?) on the line or in the sketch.  But if we run the sketch we will find the second line constrained to the origin and the endpoint of the first line.  The constraint to the endpoint does show up when editing the sketch.  The constraint to the origin is truly hidden.

 

import adsk.core, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
    

        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = adsk.fusion.Design.cast(app.activeProduct)

        # Get the root component of the active design.
        rootComp = adsk.fusion.Component.cast(design.rootComponent)

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches;
        xyPlane = rootComp.xYConstructionPlane
        
        testSketch =  adsk.fusion.Sketch.cast(sketches.add(xyPlane))
        # Get testSketch health state
        health = testSketch.healthState
        if health == adsk.fusion.FeatureHealthStates.ErrorFeatureHealthState or health == adsk.fusion.FeatureHealthStates.WarningFeatureHealthState:        
            msg = testSketch.errorOrWarningMessage
            
        constructionLine = testSketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(0,1), adsk.core.Point3D.create(1,1))
        constructionLine.isConstruction = True
            
        sketchLine = testSketch.sketchCurves.sketchLines.addByTwoPoints(testSketch.originPoint, constructionLine.endSketchPoint)
        
        for aConstraint in sketchLine.geometricConstraints:
            print(aConstraint)
        
        for aConstraint in testSketch.geometricConstraints:
            print(aConstraint)
    
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Accepted solutions (1)
848 Views
4 Replies
Replies (4)
Message 2 of 5

ekinsb
Alumni
Alumni
Accepted solution

I can understand the confusion but I don't believe it's a bug or operator error.  I'm not sure I would call it a "feature" either but you can judge for yourself.

 

Sketches are a bit complicated and their behavior isn't very well described anywhere that I'm aware of.  I'll try and give a brief explanation here.  The most important thing to understand is that all sketch geometry is always defined by sketch points.  For example, a sketch line is always between two sketch points.  Without the points the line can't exist.  In the UI when you click twice to define the ends of a line, Fusion 360 creates two sketch points and the line is created on those points.  There aren't any constraints that define the connection between the line and the two points, they're just an inherent part of the line's definition and you can get them using the startSketchPoint and endSketchPoint properties of the SketchLine object.  Circular and elliptical arcs are dependent on a point for their center point and the start and end points of the arc.  Circles and Ellipses are dependent on a point for their centers.  Splines are depending on points for their start and end points and for the points they fit through.  The points that these objects are dependent on can't be deleted because they have entities that depend on their existence.

 

It's possible that entities can share the same point.  In the UI if you draw two lines by clicking three points, Fusion 360 creates two points to be able to create the first line and then one more point for the second line because it shares the same point as the end of the first line.  When you click on this shared point, the UI will display a glyph indicating that there's a coincident constraint between the two lines.  This isn't true because there isn't a constraint but the lines are just sharing the same sketch point.  If you delete the "constraint" that the UI is showing what really happens is that Fusion creates a new sketch point and moves one of the lines to that sketch point so they no longer share the same point.  Showing this as a constraint was just a UI decision as a way to provide the ability to break the two lines apart.

 

You did discover something that I was unaware of that maybe you could call a "feature" and there's also possibly a bug.  Through the API you can create something that the UI doesn't allow.  In your example code your creating a line from a point in space to the existing sketch point that represents the origin.  Fusion 360 creates a new sketch point as the start point and then uses the existing point as the end point of the line.  The next line uses the end point of the line, which is the origin sketch point and then a point in space.  When you click the origin point, I would have expected to see a coincident constraint glyph to allow me to separate the two lines but it's not displaying.

 

When I do the same thing in the UI, it doesn't ever re-use the origin point but instead creates a new point for the sketch entity and creates a coincident constraint to tie the point to the origin point. The code below demonstrates this.

 

        lines = testSketch.sketchCurves.sketchLines        
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(-2,2), testSketch.originPoint.geometry)
        line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(2,2))
        
        testSketch.geometricConstraints.addCoincident(line1.endSketchPoint, testSketch.originPoint)

 

Hopefully this short description helps you to better understand what's going on.


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

sgraves
Enthusiast
Enthusiast

Thank you for an excellent explanation!

 

I understand what is happening now.  It is something the API can do that the UI may or may not be doing.  In a way it is a feature.  I can choose to reuse points.  If don't want the same point, but one in the same spot (with or without a constraint) I can create a new one with the data from the one I am sitting on top of.  When I change the definition of the sketchLine as shown below, I get a line with points in the same positions but without constraints.  Actually this is useful.  The problem is when one assumes constraints are automatically being added, but in fact they aren't.  And since there were no constraints added in the below test, I am assuming that the API doesn't automatically add constraints like the ui.  Which makes sense.  The programmer should know when a constraint is needed or wanted and add them.

 

I vote for calling it a feature.  It just needs to be documented, otherwise it is a mysterious thing when you believe there are automatic constraints that you can't find or remove.  I spent a couple of hours trying to figure out what was happening.

 

        #sketchLine = testSketch.sketchCurves.sketchLines.addByTwoPoints(testSketch.originPoint, constructionLine.endSketchPoint)
        x = constructionLine.endSketchPoint.geometry.x
        y = constructionLine.endSketchPoint.geometry.y
        sketchLine = testSketch.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(0,0), adsk.core.Point3D.create(x,y))
0 Likes
Message 4 of 5

sgraves
Enthusiast
Enthusiast

BTW, when something strange is happening, I search the forum for the answer.  If everyone operates in this manner, then this "feature" is now documented!

 

0 Likes
Message 5 of 5

sgraves
Enthusiast
Enthusiast

I can't leave this post alone.  There is a joke here that I suspect many don't recognize.  I am a hardware engineer who got into embedded systems and went over to the dark side (writing software). 

 

The joke is that when an engineer or programmer finds some unexpected behavior in their hardware or software, they just document it and call it a feature.

0 Likes