Cancel
Showing results for 
Search instead for 
Did you mean: 

Find Object Problem

Builder
Builder

Hello Guys,

How to remove following Find Object() method?

And 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)");

2 REPLIES 2
Highlighted

Re: Find Object Problem

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

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

http://solutions.industrysoftware.automation.siemens.com/view.php?si=nx_api5082

 

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);
    }
}

Re: Find Object Problem

Builder
Builder
Thank You Sir for replying.
There code which creates polygon and extrude it.
I have remove some of find object method.
But still there are some find object method at starting of code that I dont know how to remove.
Any solution to this problem.

using System;
using NXOpen;
using NXOpen.Features;


public class NXJournal
{
public static void Main(string[] args)
{
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
// ----------------------------------------------
// Menu: Insert->Sketch Curve->Polygon...
// ----------------------------------------------

NXOpen.SketchPolygon nullNXOpen_SketchPolygon = null;
NXOpen.SketchPolygonBuilder sketchPolygonBuilder1;
sketchPolygonBuilder1 = workPart.Sketches.CreateSketchPolygonBuilder(nullNXOpen_SketchPolygon);

sketchPolygonBuilder1.RadiusDimension.RightHandSide = "70";

sketchPolygonBuilder1.LengthDimension.RightHandSide = "100";

sketchPolygonBuilder1.RotationDimension.RightHandSide = "360";

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)");

Point3d origin1 = new Point3d(0, 0, 0);
Point point1;
point1 = workPart.Points.CreatePoint(origin1);
// NXOpen.Point point1 = (NXOpen.Point)datumCsys1.FindObject("POINT 1");
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);

NXOpen.Features.Feature nullNXOpen_Features_Feature = null;
NXOpen.Features.DatumCsysBuilder datumCsysBuilder1;
datumCsysBuilder1 = workPart.Features.CreateDatumCsysBuilder(nullNXOpen_Features_Feature);

datumCsysBuilder1.Csys = cartesianCoordinateSystem1;

datumCsysBuilder1.DisplayScaleFactor = 1.25;

NXOpen.Features.Feature feature1;
feature1 = datumCsysBuilder1.CommitFeature();

datumCsysBuilder1.Destroy();

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

sketchInPlaceBuilder1.Csystem = cartesianCoordinateSystem1;

sketchInPlaceBuilder1.PlaneOption = NXOpen.Sketch.PlaneOption.Inferred;

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

sketchInPlaceBuilder1.Destroy();

NXOpen.Sketch sketch1 = (NXOpen.Sketch)nXObject1;
sketch1.Activate(NXOpen.Sketch.ViewReorient.False);

NXOpen.Features.DatumCsys datumCsys2 = (NXOpen.Features.DatumCsys)feature1;
// NXOpen.Point point2 = (NXOpen.Point)datumCsys2.FindObject("POINT 1");
Point3d origin2 = new Point3d(0.0, 0, 0);
Point point2;
point2 = workPart.Points.CreatePoint(origin2);
NXOpen.Xform nullNXOpen_Xform = null;
NXOpen.Point point3;
point3 = workPart.Points.CreatePoint(point2, nullNXOpen_Xform, NXOpen.SmartObject.UpdateOption.WithinModeling);

sketchPolygonBuilder1.CenterPoint = point3;
point3.RemoveParameters();

NXOpen.Point3d coordinates1 = new NXOpen.Point3d(0.0, 0.0, 0.0);
point3.SetCoordinates(coordinates1);

theSession.ActiveSketch.AddGeometry(point3, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints);

NXOpen.NXObject nXObject2;
nXObject2 = sketchPolygonBuilder1.Commit();

theSession.ActiveSketch.Update();

sketchPolygonBuilder1.Destroy();

// ----------------------------------------------
// Menu: File->Finish Sketch
// ----------------------------------------------
NXOpen.Sketch sketch2;
sketch2 = theSession.ActiveSketch;
theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.False, NXOpen.Sketch.UpdateLevel.Model);

// ----------------------------------------------
// Menu: Insert->Design Feature->Extrude...
// ----------------------------------------------

NXOpen.Features.ExtrudeBuilder extrudeBuilder1;
extrudeBuilder1 = workPart.Features.CreateExtrudeBuilder(nullNXOpen_Features_Feature);

NXOpen.Section section1;
section1 = workPart.Sections.CreateSection(0.0095, 0.01, 0.5);

extrudeBuilder1.Section = section1;

extrudeBuilder1.AllowSelfIntersectingSection(true);


NXOpen.Features.Feature[] features1 = new NXOpen.Features.Feature[1];

features1[0] = sketch2.Feature;
NXOpen.CurveFeatureRule curveFeatureRule1;
curveFeatureRule1 = workPart.ScRuleFactory.CreateRuleCurveFeature(features1);

section1.AllowSelfIntersection(true);

NXOpen.SelectionIntentRule[] rules1 = new NXOpen.SelectionIntentRule[1];
rules1[0] = curveFeatureRule1;


NXObject[] geoms = sketch2.GetAllGeometry();
NXObject nx=geoms[0];
NXOpen.NXObject nullNXOpen_NXObject = null;
NXOpen.Point3d helpPoint1 = new NXOpen.Point3d(0,0,0);

section1.AddToSection(rules1, nx, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, false);

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

extrudeBuilder1.Direction = direction2;

extrudeBuilder1.ParentFeatureInternal = false;

NXOpen.Features.Feature feature2;
feature2 = extrudeBuilder1.CommitFeature();
extrudeBuilder1.Destroy();

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