Cancel
Showing results for 
Search instead for 
Did you mean: 

Constrain in sketch

Creator
Creator

Good morning,

 

Recently I had a problem with constrains in sketch enviroment.

 

I prepared some code(mostly from journal) :

 

        for (int i = 0; i < VectorLine.Length; i++)
        {
            GeoLine2Point = new NXOpen.Sketch.ConstraintGeometry();
            GeoLine2Point.Geometry = VectorLine[i];
            GeoLine2Point.PointType = NXOpen.Sketch.ConstraintPointType.StartVertex;
            GeoLine2Point.SplineDefiningPointIndex = 0;

            HPoint[i] = workPart.Points.CreatePoint(VLineCoordsStart[i]);

            GeoLine2Point2.Geometry = HPoint[i];

            GeoLine2Point2.PointType = NXOpen.Sketch.ConstraintPointType.None;
            GeoLine2Point2.SplineDefiningPointIndex = 0;

            sketchGeometricConstraint1 = theSession.ActiveSketch.CreateCoincidentConstraint(GeoLine2Point, GeoLine2Point2);


        }

Such method is used to make conincident constrain point (point is also constrained on curve) and Start Point of a line.

 

This is working "to hard", whenever I wish to move that point there is strange NX behaviour. I cant do this right away. I need to delete Conicident Constrain and do this manually. 

 

Why there is such behaviour ? Is there other way around ?

 

Regards

jilsu

3 REPLIES 3

Re: Constrain in sketch

Creator
Creator

I have tried another way:

 

        NXOpen.SketchConstraintBuilder ConstrainCoincident;
        ConstrainCoincident = workPart.Sketches.CreateConstraintBuilder();
        ConstrainCoincident.ConstraintType = NXOpen.SketchConstraintBuilder.Constraint.PointOnCurve;

        bool[] objConstraintCoincident = new bool[iloscPunktow];

        for (int i = 0; i < objConstraintCoincident.Length; i++)
        {

            objConstraintCoincident[i] = ConstrainCoincident.GeometryToConstrain.Add(VPointStartLine[i], workPart.ModelingViews.WorkView, VectorLine[i].StartPoint);

            ConstrainCoincident.GeometryToConstrainTo.SetValue(SplinePointSet, workPart.ModelingViews.WorkView, VectorLine[i].StartPoint);

        }


        NXOpen.NXObject objConstrainCoincidentXX;
        objConstrainCoincidentXX = ConstrainCoincident.Commit();

        NXOpen.NXObject[] objConstrainCoincidentXX2;
        objConstrainCoincidentXX2 = ConstrainCoincident.GetCommittedObjects();

        ConstrainCoincident.GeometryToConstrain.Clear();
        ConstrainCoincident.GeometryToConstrainTo.Value = null;
        ConstrainCoincident.Destroy();

But this throws me an exception : Object must be part of the sketch. It points a line with .Commit().

 

Did I miss something ?

 

Regards

jilsu

Re: Constrain in sketch

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

@jilsu wrote:

Good morning,

 

Recently I had a problem with constrains in sketch enviroment.

...code removed...

Such method is used to make conincident constrain point (point is also constrained on curve) and Start Point of a line.

 

This is working "to hard", whenever I wish to move that point there is strange NX behaviour. I cant do this right away. I need to delete Conicident Constrain and do this manually. 

 

Why there is such behaviour ? Is there other way around ?

 

Regards

jilsu


If the point is constrained to be coincident with the end point of the line, then the "point on curve" constraint seems to be redundant. Note that if the line is created and constrained, then a point is created and made to be coincident with the line end point, you won't be able to move the point without breaking the coincident constraint or changing the dimensions/constraints that define the line. On the other hand, if the line is not fully constrained before the coincident constraint is created, you should be able to move the point and the line end point would follow.

 

I don't think there is enough information here to really diagnose the issue. Can you upload an example part file and your full code?

Re: Constrain in sketch

Creator
Creator
using System;
using NXOpen;

public class PointOnSketch
{
    //public static void Main(string[] args = null)
    public static void Main(double[,] PointArr, double[,] PointArr2, int PointCounter, Spline SplinePointSet, Spline SplinePointSet2)
    {
        NXOpen.Session theSession = NXOpen.Session.GetSession();
        NXOpen.Part workPart = theSession.Parts.Work;
        NXOpen.Part displayPart = theSession.Parts.Display;
        // ----------------------------------------------
        //   Menu: Insert->Sketch in Task Environment
        // ----------------------------------------------


        //

        theSession.BeginTaskEnvironment();



        //


        NXOpen.Sketch nullNXOpen_Sketch = null;
        NXOpen.SketchInPlaceBuilder sketchInPlaceBuilder1;
        sketchInPlaceBuilder1 = workPart.Sketches.CreateSketchInPlaceBuilder2(nullNXOpen_Sketch);

        NXOpen.Point3d origin1 = new NXOpen.Point3d(0.0, 0.0, 0.0);
        NXOpen.Vector3d normal1 = new NXOpen.Vector3d(0.0, 0.0, 1.0);
        NXOpen.Plane plane1;
        plane1 = workPart.Planes.CreatePlane(origin1, normal1, NXOpen.SmartObject.UpdateOption.WithinModeling);

        sketchInPlaceBuilder1.PlaneReference = plane1;




        NXOpen.Unit unit1 = (NXOpen.Unit)workPart.UnitCollection.FindObject("Inch");
        NXOpen.Expression expression1;
        expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

        NXOpen.Expression expression2;
        expression2 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

        NXOpen.SketchAlongPathBuilder sketchAlongPathBuilder1;
        sketchAlongPathBuilder1 = workPart.Sketches.CreateSketchAlongPathBuilder(nullNXOpen_Sketch);

        sketchAlongPathBuilder1.PlaneLocation.Expression.RightHandSide = "0";


        NXOpen.Point3d coordinates1 = new NXOpen.Point3d(0.57809523119088202, 0.0, 8.8205347261383125);
        NXOpen.Point point1;
        point1 = workPart.Points.CreatePoint(coordinates1);

        NXOpen.DatumAxis datumAxis1 = (NXOpen.DatumAxis)workPart.Datums.FindObject("FIXED_DATUM_AXIS(3)");
        NXOpen.Direction direction1;
        direction1 = workPart.Directions.CreateDirection(datumAxis1, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling);

        bool success1;
        success1 = direction1.ReverseDirection();

        NXOpen.DatumPlane datumPlane1 = (NXOpen.DatumPlane)workPart.Datums.FindObject("LINKED_DATUM_PLANE(17)");
        NXOpen.Xform xform1;
        xform1 = workPart.Xforms.CreateXformByPlaneXDirPoint(datumPlane1, direction1, point1, NXOpen.SmartObject.UpdateOption.WithinModeling, 0.625, false, false);

        NXOpen.CartesianCoordinateSystem cartesianCoordinateSystem1;
        cartesianCoordinateSystem1 = workPart.CoordinateSystems.CreateCoordinateSystem(xform1, NXOpen.SmartObject.UpdateOption.WithinModeling);

        sketchInPlaceBuilder1.Csystem = cartesianCoordinateSystem1;

        NXOpen.Point3d origin2 = new NXOpen.Point3d(0.0, 0.0, 0.0);
        NXOpen.Vector3d normal2 = new NXOpen.Vector3d(0.0, 0.0, 1.0);
        NXOpen.Plane plane2;
        plane2 = workPart.Planes.CreatePlane(origin2, normal2, NXOpen.SmartObject.UpdateOption.WithinModeling);
        plane2.SetMethod(NXOpen.PlaneTypes.MethodType.Coincident);

        NXOpen.NXObject[] geom1 = new NXOpen.NXObject[1];
        geom1[0] = datumPlane1;
        plane2.SetGeometry(geom1);
        plane2.SetFlip(false);
        plane2.SetExpression(null);
        plane2.SetAlternate(NXOpen.PlaneTypes.AlternateType.One);
        plane2.Evaluate();


        NXOpen.Point3d origin3 = new NXOpen.Point3d(0.0, 0.0, 0.0);
        NXOpen.Vector3d normal3 = new NXOpen.Vector3d(0.0, 0.0, 1.0);
        NXOpen.Plane plane3;
        plane3 = workPart.Planes.CreatePlane(origin3, normal3, NXOpen.SmartObject.UpdateOption.WithinModeling);

        NXOpen.Expression expression3;
        expression3 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

        NXOpen.Expression expression4;
        expression4 = workPart.Expressions.CreateSystemExpressionWithUnits("0", unit1);

        plane3.SynchronizeToPlane(plane2);
        plane3.SetMethod(NXOpen.PlaneTypes.MethodType.Coincident);

        NXOpen.NXObject[] geom2 = new NXOpen.NXObject[1];
        geom2[0] = datumPlane1;
        plane3.SetGeometry(geom2);
        plane3.SetAlternate(NXOpen.PlaneTypes.AlternateType.One);
        plane3.Evaluate();

        NXOpen.NXObject nXObject1;
        nXObject1 = sketchInPlaceBuilder1.Commit();

        NXOpen.Sketch sketch1 = (NXOpen.Sketch)nXObject1;
        NXOpen.Features.Feature feature1;
        feature1 = sketch1.Feature;

        NXOpen.Session.UndoMarkId markId6;
        markId6 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "update");

        int nErrs1;
        nErrs1 = theSession.UpdateManager.DoUpdate(markId6);

        sketch1.Activate(NXOpen.Sketch.ViewReorient.True);
        
        sketchInPlaceBuilder1.Destroy();
        sketchAlongPathBuilder1.Destroy();

        try
        {
            // Expression is still in use.
            workPart.Expressions.Delete(expression2);
        }
        catch (NXException ex)
        {
            ex.AssertErrorCode(1050029);
        }

        try
        {
            // Expression is still in use.
            workPart.Expressions.Delete(expression1);
        }
        catch (NXException ex)
        {
            ex.AssertErrorCode(1050029);
        }

        plane1.DestroyPlane();

        try
        {
            // Expression is still in use.
            workPart.Expressions.Delete(expression4);
        }
        catch (NXException ex)
        {
            ex.AssertErrorCode(1050029);
        }

        try
        {
            // Expression is still in use.
            workPart.Expressions.Delete(expression3);
        }
        catch (NXException ex)
        {
            ex.AssertErrorCode(1050029);
        }

        plane3.DestroyPlane();




        string SketchTime = DateTime.Now.ToString();
        string SketchTimeConverted = SketchTime.Replace(":", "")
                                               .Replace(".", "")
                                               .Replace(" ", "");

        theSession.ActiveSketch.SetName("SKETCH_" + SketchTimeConverted.Remove(0,8));


        NXOpen.Point[] VPoint = new NXOpen.Point[PointCounter];
        NXOpen.Point3d[] Vcoords = new NXOpen.Point3d[PointCounter];


        for (int i = 0; i < VPoint.Length; i++)
        {
            Vcoords[i] = new Point3d(PointArr[i,0], PointArr[i,1], PointArr[i,2]);
            VPoint[i] = workPart.Points.CreatePoint(Vcoords[i]);

            theSession.ActiveSketch.AddGeometry(VPoint[i], NXOpen.Sketch.InferConstraintsOption.InferNoConstraints);

            VPoint[i].RemoveParameters();
            VPoint[i].RemoveViewDependency();

        }

        NXOpen.Point[] VPoint2 = new NXOpen.Point[PointCounter];
        NXOpen.Point3d[] Vcoords2 = new NXOpen.Point3d[PointCounter];


        for (int i = 0; i < VPoint2.Length; i++)
        {
            Vcoords2[i] = new Point3d(PointArr2[i, 0], PointArr2[i, 1], PointArr2[i, 2]);
            VPoint2[i] = workPart.Points.CreatePoint(Vcoords2[i]);

            theSession.ActiveSketch.AddGeometry(VPoint2[i], NXOpen.Sketch.InferConstraintsOption.InferNoConstraints);

            VPoint2[i].RemoveParameters();
            VPoint2[i].RemoveViewDependency();

        }


        theSession.ActiveSketch.Update();

        NXOpen.SketchConstraintBuilder sketchConstraintBuilder1;
        sketchConstraintBuilder1 = workPart.Sketches.CreateConstraintBuilder();
        sketchConstraintBuilder1.ConstraintType = NXOpen.SketchConstraintBuilder.Constraint.PointOnCurve;

        bool[] objConstraint = new bool[PointCounter];
        bool[] objConstraint2 = new bool[PointCounter];

        for (int i = 0; i < objConstraint.Length; i++)
        {
            objConstraint[i] = sketchConstraintBuilder1.GeometryToConstrain.Add(VPoint[i], workPart.ModelingViews.WorkView, Vcoords[i]);

            sketchConstraintBuilder1.GeometryToConstrainTo.SetValue(SplinePointSet, workPart.ModelingViews.WorkView, Vcoords[i]);

        }


        NXOpen.NXObject objPointOnCurveConstraint;
        objPointOnCurveConstraint = sketchConstraintBuilder1.Commit();

        NXOpen.NXObject[] objPointonCurveConstraint2;
        objPointonCurveConstraint2 = sketchConstraintBuilder1.GetCommittedObjects();



        sketchConstraintBuilder1.GeometryToConstrain.Clear();
        sketchConstraintBuilder1.GeometryToConstrainTo.Value = null;
        sketchConstraintBuilder1.Destroy();



        NXOpen.Point3d[] VLineCoordsStart = new NXOpen.Point3d[PointCounter];
        NXOpen.Point3d[] VLineCoordsEnd = new NXOpen.Point3d[PointCounter];
        
        NXOpen.Point[] VPointStartLine = new NXOpen.Point[PointCounter];
        NXOpen.Point[] VPointEndLine = new NXOpen.Point[PointCounter];

        NXOpen.Line[] VectorLine = new NXOpen.Line[PointCounter];

        for (int i = 0; i < VectorLine.Length; i++)
        {

            VLineCoordsStart[i] = new Point3d(PointArr[i, 0], PointArr[i, 1], PointArr[i, 2]);
            VLineCoordsEnd[i] = new Point3d(PointArr2[i, 0], PointArr2[i, 1], PointArr2[i, 2]);

        
            VectorLine[i] = workPart.Curves.CreateLine(VLineCoordsStart[i], VLineCoordsEnd[i]);
            theSession.ActiveSketch.AddGeometry(VectorLine[i], NXOpen.Sketch.InferConstraintsOption.InferCoincidentConstraints);

            VPointStartLine[i] = workPart.Points.CreatePoint(VectorLine[i].StartPoint);
            VPointEndLine[i] = workPart.Points.CreatePoint(VectorLine[i].EndPoint);
            
        }

        theSession.ActiveSketch.Update();








        // ----------------------------------------------
        //   Menu: Task->Finish Sketch
        // ----------------------------------------------

        theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.True, NXOpen.Sketch.UpdateLevel.Model);
        theSession.DeleteUndoMarksSetInTaskEnvironment();
        theSession.EndTaskEnvironment();

        // ----------------------------------------------
        //   Menu: Tools->Journal->Stop Recording
        // ----------------------------------------------

    }
    public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
}

Sorry that I couldnt write ealier. As you can see there are additional inputs to method:

 

double[,] PointArr - array of point coordinates from first selected PointSet

double[,] PointArr2 - array of point coordinates from second selected PointSet

int PointCounter - point counter

Spline SplinePointSet - spline of point set 1

Spline SplinePointSet2 - spline of point set 2

 

I hope you be able to help me understand why this is not working.

 

Regards,

jilsu