Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Object Problem

Status
Not open for further replies.

Kishor khalane

Mechanical
Jul 27, 2018
9
Hello Guys,

How to remove following Find Object() method?

And what changes should be made there.



NXOpen.DatumAxis datumAxis1 = (NXOpen.DatumAxis)workPart.Datums.FindObject("DATUM_CSYS(0) Y axis");

Direction direction1;
direction1 = workPart.Directions.CreateDirection(datumAxis1, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling);

NXOpen.DatumPlane datumPlane1 = (NXOpen.DatumPlane)workPart.Datums.FindObject("DATUM_CSYS(0) YZ plane");
NXOpen.Features.DatumCsys datumCsys1 = (NXOpen.Features.DatumCsys)workPart.Features.FindObject("DATUM_CSYS(0)");
 
Replies continue below

Recommended for you

The GTAC solution center has an example showing how to get the various components of a datum csys feature.


Code:
This example demonstrates two different ways to find the individual objects that make up a DatumPlane feature.  They both work perfectly fine.  I tested NX8.5 & NX10.

The first way uses the Feature class method GetEntities and then sorts through the generic NXObject array to refine them to the more specific object types.  The DatumPlane and DatumAxis objects are then queried for their direction which is then used to sort them into to the appropriate variable names.  This is probably the safer way because it does the math rather than make any assumptions.

The second way uses the NXOpen.UF UFModl class method AskDatumCsysComponents.  This returns specific tags for the csys and origin point but the datum planes and axes are assigned into the various variable names based solely on their order in the returned tag arrays.  There is no guarantee that this order will not change, but then again there is no reason why it would change.

You might also choose to do a combination of the two methods where you use AskDatumCsysComponents but then do the math to sort out the DatumPlane and DatumAxis objects rather than assuming which is which based on their order in the array.

using System;
using NXOpen;
using NXOpen.UF;
using NXOpen.Utilities;

public class NXJournal
{
    static Session theSession = Session.GetSession();
    static UFSession theUFSession = UFSession.GetUFSession();
    static Part workPart = theSession.Parts.Work;

    static void DoIt()
    {
        NXOpen.Features.DatumCsys theDatumCsys;
        try
        {
            theDatumCsys =
                (NXOpen.Features.DatumCsys)select_a_feature("Select a DatumCsys");
        }
        catch { Echo("Feature selected was not a DatumCsys"); return; } 
 
        CartesianCoordinateSystem theCsys;
        Point theOrigin;
        DatumAxis theXAxis;
        DatumAxis theYAxis;
        DatumAxis theZAxis;
        DatumPlane theXYPlane;
        DatumPlane theYZPlane;
        DatumPlane theXZPlane;

        GetObjectsOfDatumCsys(theDatumCsys, out theCsys, out theOrigin, out theXAxis,
            out theYAxis, out theZAxis, out theXYPlane, out theYZPlane, out theXZPlane);
 
        CartesianCoordinateSystem theCsysI;
        Point theOriginI;
        DatumAxis theXAxisI;
        DatumAxis theYAxisI;
        DatumAxis theZAxisI;
        DatumPlane theXYPlaneI;
        DatumPlane theYZPlaneI;
        DatumPlane theXZPlaneI;

        GetObjectsOfDatumCsys_Interop(theDatumCsys, out theCsysI, out theOriginI, 
            out theXAxisI, out theYAxisI, out theZAxisI, 
            out theXYPlaneI, out theYZPlaneI, out theXZPlaneI);

        if (theCsys.Equals(theCsysI) && theOrigin.Equals(theOriginI) &&
            theXAxis.Equals(theXAxisI) && theYAxis.Equals(theYAxisI) &&
            theZAxis.Equals(theZAxisI) && theXYPlane.Equals(theXYPlaneI) &&
            theYZPlane.Equals(theYZPlaneI) && theXZPlane.Equals(theXZPlaneI))
        {
            Echo("Both methods found these same objects:");
            Echo("");

            NXObject[] proof = { theCsys, theOrigin, theXAxis, theYAxis, theZAxis, 
                theXYPlane, theYZPlane, theXZPlane };
            theSession.Information.DisplayObjectsDetails(proof);
        }
        else  // This doesn't happen - coded just in case something changes.
        {
            Echo("The objects found by both methods do not match!");
            Echo("");
            Echo("Doing the math found these:");
            Echo("");
            NXObject[] goof = { theCsys, theOrigin, theXAxis, theYAxis, theZAxis, 
                theXYPlane, theYZPlane, theXZPlane };
            theSession.Information.DisplayObjectsDetails(goof);

            Echo("");
            Echo("The interop method found these:");
            Echo("");
            NXObject[] goof1 = { theCsys, theOrigin, theXAxis, theYAxis, theZAxis, 
                theXYPlane, theYZPlane, theXZPlane };
            theSession.Information.DisplayObjectsDetails(goof1);
        }
    }

    static void GetObjectsOfDatumCsys(NXOpen.Features.DatumCsys theDatumCsys,
        out CartesianCoordinateSystem theCsys,
        out Point theOrigin,
        out DatumAxis theXAxis,
        out DatumAxis theYAxis,
        out DatumAxis theZAxis,
        out DatumPlane theXYPlane,
        out DatumPlane theYZPlane,
        out DatumPlane theXZPlane)
    {
        theCsys = null;
        theOrigin = null;
        theXAxis = null;
        theYAxis = null;
        theZAxis = null;
        theXYPlane = null;
        theYZPlane = null;
        theXZPlane = null;

        NXObject[] theEntities = theDatumCsys.GetEntities();

        DatumAxis[] theDatumAxes = new DatumAxis[3];
        int n_axes = 0;

        DatumPlane[] theDatumPlanes = new DatumPlane[3];
        int n_planes = 0;

        foreach (NXObject oneObject in theEntities)
        {
            if (oneObject.GetType().Equals(typeof(CartesianCoordinateSystem)))
            {
                theCsys = (CartesianCoordinateSystem)oneObject;
                continue;
            }

            if (oneObject.GetType().Equals(typeof(Point)))
            {
                theOrigin = (Point)oneObject;
                continue;
            }

            if (oneObject.GetType().Equals(typeof(DatumPlane)))
            {
                theDatumPlanes[n_planes] = (DatumPlane)oneObject;
                n_planes++;
                continue;
            }
 
            if (oneObject.GetType().Equals(typeof(DatumAxis)))
            {
                theDatumAxes[n_axes] = (DatumAxis)oneObject;
                n_axes++;
                continue;
            }

            // This doesn't happen - but just in case...
            Echo("GetEntities returned a " + oneObject.GetType().Name);
        }

        Matrix3x3 theMatrix = theCsys.Orientation.Element;
        double[] x_dir = { theMatrix.Xx, theMatrix.Xy, theMatrix.Xz };
        double[] y_dir = { theMatrix.Yx, theMatrix.Yy, theMatrix.Yz };
        double[] z_dir = { theMatrix.Zx, theMatrix.Zy, theMatrix.Zz };

        double tol;  // 0 is too tight!  My default was 0.5 which worked well.
        theUFSession.Modl.AskAngleTolerance(out tol);

        for (int ii = 0; ii < n_axes; ii++)
        {
            Vector3d axis_vec = theDatumAxes[ii].Direction;
            double[] axis_dir = { axis_vec.X, axis_vec.Y, axis_vec.Z };
            int is_parallel;
            theUFSession.Vec3.IsParallel(x_dir, axis_dir, tol, out is_parallel);
            if (is_parallel == 1) theXAxis = theDatumAxes[ii];

            theUFSession.Vec3.IsParallel(y_dir, axis_dir, tol, out is_parallel);
            if (is_parallel == 1) theYAxis = theDatumAxes[ii];

            theUFSession.Vec3.IsParallel(z_dir, axis_dir, tol, out is_parallel);
            if (is_parallel == 1) theZAxis = theDatumAxes[ii];
        }

        for (int ii = 0; ii < n_planes; ii++)
        {
            Vector3d normal_vec = theDatumPlanes[ii].Normal;
            double[] normal_dir = { normal_vec.X, normal_vec.Y, normal_vec.Z };
            int is_parallel;
            theUFSession.Vec3.IsParallel(z_dir, normal_dir, tol, out is_parallel);
            if (is_parallel == 1) theXYPlane = theDatumPlanes[ii];

            theUFSession.Vec3.IsParallel(y_dir, normal_dir, tol, out is_parallel);
            if (is_parallel == 1) theXZPlane = theDatumPlanes[ii];

            theUFSession.Vec3.IsParallel(x_dir, normal_dir, tol, out is_parallel);
            if (is_parallel == 1) theYZPlane = theDatumPlanes[ii];
        }
    }

    static void GetObjectsOfDatumCsys_Interop(NXOpen.Features.DatumCsys theDatumCsys,
        out CartesianCoordinateSystem theCsys,
        out Point theOrigin,
        out DatumAxis theXAxis,
        out DatumAxis theYAxis,
        out DatumAxis theZAxis,
        out DatumPlane theXYPlane,
        out DatumPlane theYZPlane,
        out DatumPlane theXZPlane)
    {
        Tag csys, origin;
        Tag[] daxes, dplanes;
        theUFSession.Modl.AskDatumCsysComponents(theDatumCsys.Tag, out csys, 
            out origin, out daxes, out dplanes);

        theCsys = (CartesianCoordinateSystem)NXObjectManager.Get(csys);
        theOrigin = (Point)NXObjectManager.Get(origin);
        theXAxis = (DatumAxis)NXObjectManager.Get(daxes[0]);
        theYAxis = (DatumAxis)NXObjectManager.Get(daxes[1]);
        theZAxis = (DatumAxis)NXObjectManager.Get(daxes[2]);
        theXYPlane = (DatumPlane)NXObjectManager.Get(dplanes[0]);
        theYZPlane = (DatumPlane)NXObjectManager.Get(dplanes[1]);
        theXZPlane = (DatumPlane)NXObjectManager.Get(dplanes[2]);
    }
    
    public static void Main(string[] args)
    {
        if (workPart != null) DoIt();
    }

    public static NXOpen.Features.Feature select_a_feature(string prompt)
    {
        Selection.SelectionType[] feats = { Selection.SelectionType.Features };
        TaggedObject theTO;
        Point3d cursor;

        Selection.Response resp = UI.GetUI().SelectionManager.SelectTaggedObject(
            prompt, prompt, Selection.SelectionScope.WorkPart, false, feats, 
            out theTO, out cursor);
        return (NXOpen.Features.Feature) theTO;
    }

    static void Echo(string output)
    {
        theSession.ListingWindow.Open();
        theSession.ListingWindow.WriteLine(output);
        theSession.LogFile.WriteLine(output);
    }

    public static int GetUnloadOption(string arg)
    {
        return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
    }
}

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor