Adding constraint via API changes user constraint-setting-default

Adding constraint via API changes user constraint-setting-default

josh.nieman
Advocate Advocate
594 Views
6 Replies
Message 1 of 7

Adding constraint via API changes user constraint-setting-default

josh.nieman
Advocate
Advocate

I've combed through the code in my application to figure out why/how this is happening but to no avail.  Does anyone have any clues or ideas of where to look?

Here's the issue:

 

Before running the code, user has "Default to undirected" checked as below.

joshnieman_0-1680555669588.png


After running a simple application function of mine that adds a fastener automatically, and constrains it appropriately, this box is unchecked.

This is the /only/ methods that call any function related to constraints:

 

HoleConst1 = AsmDef.Constraints.AddMateConstraint2(Z, ScrewHole1, "0",                                                               InferredTypeEnum.kInferredLine,                                                               InferredTypeEnum.kInferredLine,                                                               MateConstraintSolutionTypeEnum.kUndirectedSolutionType)
            FaceConst2 = AsmDef.Constraints.AddFlushConstraint(XY, FastenFace1, MateOffset)

 

 
Once complete, the next time the user opens the Constraint dialog to manually constrain something, the hole-to-hole "default to undirected" is unchecked

0 Likes
Accepted solutions (1)
595 Views
6 Replies
Replies (6)
Message 2 of 7

josh.nieman
Advocate
Advocate

While ideally I would prevent this from happening in the first place, I would also accept a workaround of just saving the setting before I do work, then restoring it at the end.

However, I can't find that this user-setting is in the API anywhere that's exposed for use.  Any tips on that?  Maybe it's not exposed, but maybe I just haven't searched the right term.

0 Likes
Message 3 of 7

J-Camper
Advisor
Advisor

@josh.nieman,

 

Per help documentation, the default SolutionType for the method AddMateConstraint2 is "kOpposedSolutionType".  Maybe it is changing the default when you call the method without specifying a SolutionType when you make the constraint. 

0 Likes
Message 4 of 7

josh.nieman
Advocate
Advocate

The formatting botched my code snippet, but I am specifying a solutiontype of "undirected"

However, if that is changing the default, I consider that a bug.

Specifying an optional parameters on an 'addConstraint' function shouldn't change a user-set default value.  That is the very reason 'defaults' exist.  So it would default to a thing unless otherwise stated: implying that it's ok to state otherwise without changing things.

Can anyone else replicate the behavior (trying a second code snip that better shows my code)

    <Extension()>
    Public Function ConstrainIt(TheScrew As ComponentOccurrence, Optional WasherBool As Boolean = False, Optional TheWasher As ComponentOccurrence = Nothing) As Boolean
        Dim ScrewDef As PartComponentDefinition = TheScrew.Definition
        Dim WashDef As PartComponentDefinition = Nothing
        If WasherBool Then
            Debug.Print("constrain0")
            WashDef = TheWasher.Definition
        End If

        Debug.Print("constrain1")
        Dim MateOffset As String = "0"

        Dim XY As WorkPlaneProxy = Nothing
        TheScrew.CreateGeometryProxy(ScrewDef.WorkPlanes.Item(3), XY)
        Dim Z As WorkAxisProxy = Nothing
        TheScrew.CreateGeometryProxy(ScrewDef.WorkAxes.Item(3), Z)

        If ScrewType = "FHCS" Then
            'offset by some factor (or get the parameter from the part) so we're flush
            MateOffset = 1
        ElseIf ScrewType = "SHCS" OrElse
                ScrewType = "HHCS" OrElse
                ScrewType = "BHCS" OrElse
                ScrewType = "LHCS" Then
            If WasherBool Then
                Debug.Print(WasherUsed.Thickness & " washer thickness")
                MateOffset = "-" & WasherUsed.Thickness ' negative value
                Debug.Print("Mate Offset = " & MateOffset)
            End If
            HoleConst1 = AsmDef.Constraints.AddMateConstraint2(Z, ScrewHole1, "0",
                                                               InferredTypeEnum.kInferredLine,
                                                               InferredTypeEnum.kInferredLine,
                                                               MateConstraintSolutionTypeEnum.kUndirectedSolutionType)
            FaceConst2 = AsmDef.Constraints.AddFlushConstraint(XY, FastenFace1, MateOffset)
            If WasherBool AndAlso WashDef IsNot Nothing Then
                Dim washXY As WorkPlaneProxy = Nothing
                TheWasher.CreateGeometryProxy(WashDef.WorkPlanes.Item(3), washXY)
                Dim washZ As WorkAxisProxy = Nothing
                TheWasher.CreateGeometryProxy(WashDef.WorkAxes.Item(3), washZ)
                HoleConstW1 = AsmDef.Constraints.AddMateConstraint2(washZ, ScrewHole1, "0",
                                                                    InferredTypeEnum.kInferredLine,
                                                                    InferredTypeEnum.kInferredLine,
                                                                    MateConstraintSolutionTypeEnum.kUndirectedSolutionType)
                FaceConstW2 = AsmDef.Constraints.AddFlushConstraint(washXY, FastenFace1, WasherUsed.Thickness * 2.54 / 2 * -1) 'half washer thickness, inch to cm, and negative
            End If
        Else
            ' not FHCS/SHCS/HHCS/BHCS/LHCS so maybe they hit escape to cancel or something
            Return False
        End If
        Return True
    End Function




0 Likes
Message 5 of 7

josh.nieman
Advocate
Advocate

I cannot find anything that seems like it should be changing a default setting.

By calling "AddMate/FlushConstraint" I don't feel like it should be changing any /default/ setting.  If it does, though, I'd certainly want to "put it back" to whatever it was before my code caused the change, and I cannot find a way I can do that.  Does anyone know of a way?

0 Likes
Message 6 of 7

josh.nieman
Advocate
Advocate

Bump?

No ideas?  😞  This is a bummer.

0 Likes
Message 7 of 7

josh.nieman
Advocate
Advocate
Accepted solution

Well I updated a bit of completely unrelated code, and re-built the DLL and noticed the problematic behavior has stopped so far.

Ghosts in the machine, gremlins in the code... I don't even know, but I'm moving on unless this crops up again 😄

Apologies to anyone who comes across this thread in the future, who might be searching for resolution to similar troubles.  Good luck.

0 Likes