Cancel
Showing results for 
Search instead for 
Did you mean: 

Threaded Hole

Builder
Builder

Hello Guys,

Below is the code for thread hole creation.

Although it is not showing any error in VS but while running in NX.

This error is coming that you are providing INVALID THREADED HOLE DATA.

Although I provided correct data using recorded journal.

Please find any solution to this.

using System;
using NXOpen;
using NXOpen.Features;

class NXJournal
{
public static void Main()
{

Session theSession = Session.GetSession();
Part workPart = theSession.Parts.Work;
Part displayPart = theSession.Parts.Display;

NXOpen.Features.CylinderBuilder cylinderbuilder1;
cylinderbuilder1 = workPart.Features.CreateCylinderBuilder(null);

// Specify the cylinder definition type(AxisDiameterAndHeight)
cylinderbuilder1.Type = NXOpen.Features.CylinderBuilder.Types.AxisDiameterAndHeight;

// Define the Cylinder Axis
Point3d origin1 = new Point3d(0, 0, 0);
Point point1;
point1 = workPart.Points.CreatePoint(origin1);

Vector3d vector1 = new Vector3d(1, 0, 0);
Direction direction1;
direction1 = workPart.Directions.CreateDirection(origin1, vector1, SmartObject.UpdateOption.WithinModeling);
Axis axis1;
axis1 = cylinderbuilder1.Axis;
axis1.Direction = direction1;
Point point11;
point11 = axis1.Point;
axis1.Point = point1;

// Define Cylinder Diameter
string diamString1 = "100";
cylinderbuilder1.Diameter.RightHandSide = diamString1;

// Define Cylinder Height
string heightString1 = "60";
cylinderbuilder1.Height.RightHandSide = heightString1;

// Define the boolean option (Create, unite, etc.)
cylinderbuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;

// Destroy the builder to free memory
NXObject cylinder1 = cylinderbuilder1.Commit();
cylinderbuilder1.Destroy();
NXOpen.Features.Cylinder Cyl = (NXOpen.Features.Cylinder)cylinder1;


//Hole Feature
NXOpen.Features.HolePackage nullNXOpen_Features_HolePackage = null;
NXOpen.Features.HolePackageBuilder holePackageBuilder1;
holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullNXOpen_Features_HolePackage);
// NXOpen.Features.HolePackageBuilder holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(null);
holePackageBuilder1.Type = NXOpen.Features.HolePackageBuilder.Types.ThreadedHole;
//holePackageBuilder1.GeneralHoleForm = NXOpen.Features.HolePackageBuilder.HoleForms.Simple;

holePackageBuilder1.ThreadedStartChamferDiameter.RightHandSide = "48";
holePackageBuilder1.ThreadedStartChamferAngle.RightHandSide = "45";
// holePackageBuilder1.ThreadedReliefDiameter.RightHandSide = "48";
// holePackageBuilder1.ThreadedReliefDepth.RightHandSide = "24";
holePackageBuilder1.ThreadedHoleDepth.RightHandSide = "96";
holePackageBuilder1.ThreadedEndChamferDiameter.RightHandSide = "48";
holePackageBuilder1.ThreadedEndChamferAngle.RightHandSide = "45";
holePackageBuilder1.ThreadDepth.RightHandSide = "48";
holePackageBuilder1.TapDrillDiameter.RightHandSide = "43.129";
//holePackageBuilder1.HoleDepthLimitOption = NXOpen.Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody;
holePackageBuilder1.Tolerance = 0.01;

// holePackageBuilder1.ThreadedTipAngle.RightHandSide = "118";

NXOpen.Body[] targetBodies3 = new NXOpen.Body[1];
targetBodies3[0] = Cyl.GetBodies()[0];

holePackageBuilder1.BooleanOperation.SetTargetBodies(targetBodies3);
holePackageBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract;


Point3d origin2 = new Point3d(60, 0, 0);
Point point21;
point21 = workPart.Points.CreatePoint(origin2);
NXOpen.Point[] points2 = new NXOpen.Point[1];
points2[0] = point21;
NXOpen.CurveDumbRule curveDumbRule1;
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points2);

holePackageBuilder1.HolePosition.AllowSelfIntersection(true);

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


NXObject nullNXObject = null;
Point3d helpPoint1 = new Point3d(0.0, 0.0, 0.0);
holePackageBuilder1.HolePosition.AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, NXOpen.Section.Mode.Create, false);


NXObject hole= holePackageBuilder1.Commit();
holePackageBuilder1.Destroy();
}


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

11 REPLIES 11

Re: Threaded Hole

One error is with

NXOpen.Body[] targetBodies3 = new NXOpen.Body[1];
targetBodies3[0] = Cyl.GetBodies()[0];

You cannot get a body from the cylinder feature.  You need to use the body feature to get the body.

 

Frank Swinkels

 

 

Re: Threaded Hole

Builder
Builder

Hello Sir,

Although by using bodyfeature() method same error is coming.

any solution to this.

I have attach an image wherein details of error has been given.

Re: Threaded Hole

Phenom
Phenom

Does your recorded journal work fine?  If so, use the complete recorded code.  If you think it has extraneous code, start by commenting out those lines one by one and test.

Suresh
Technisites Australia
Production: NX10.0.3 Developing: NX11.0.2.7, NX12.0.2, NX1847,
Development: C++, C# .Net, VB .Net, Python, KF

Re: Threaded Hole

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

@FrankSwinks1 wrote:

One error is with

NXOpen.Body[] targetBodies3 = new NXOpen.Body[1];
targetBodies3[0] = Cyl.GetBodies()[0];

You cannot get a body from the cylinder feature.  You need to use the body feature to get the body.

 

Frank Swinkels

 

 


The cylinder feature class inherits from the BodyFeature class; in other words, the cylinder feature is a type of body feature. You can use the .GetBodies method of the cylinder feature to get the body.

Re: Threaded Hole

Legend
Legend

@Krishna11 

public static void Main()
    {

        Session theSession = Session.GetSession();
        Part workPart = theSession.Parts.Work;
        Part displayPart = theSession.Parts.Display;

        NXOpen.Features.CylinderBuilder cylinderbuilder1;
        cylinderbuilder1 = workPart.Features.CreateCylinderBuilder(null);

        // Specify the cylinder definition type(AxisDiameterAndHeight)
        cylinderbuilder1.Type = NXOpen.Features.CylinderBuilder.Types.AxisDiameterAndHeight;

        // Define the Cylinder Axis
        Point3d origin1 = new Point3d(0, 0, 0);
        Point point1;
        point1 = workPart.Points.CreatePoint(origin1);

        Vector3d vector1 = new Vector3d(1, 0, 0);
        Direction direction1;
        direction1 = workPart.Directions.CreateDirection(origin1, vector1, SmartObject.UpdateOption.WithinModeling);
        Axis axis1;
        axis1 = cylinderbuilder1.Axis;
        axis1.Direction = direction1;
        Point point11;
        point11 = axis1.Point;
        axis1.Point = point1;

        // Define Cylinder Diameter
        string diamString1 = "100";
        cylinderbuilder1.Diameter.RightHandSide = diamString1;

        // Define Cylinder Height
        string heightString1 = "60";
        cylinderbuilder1.Height.RightHandSide = heightString1;

        // Define the boolean option (Create, unite, etc.)
        cylinderbuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;

        // Destroy the builder to free memory
        NXObject cylinder1 = cylinderbuilder1.Commit();
        cylinderbuilder1.Destroy();
        NXOpen.Features.Cylinder Cyl = (NXOpen.Features.Cylinder)cylinder1;


        //Hole Feature
        NXOpen.Features.HolePackage nullNXOpen_Features_HolePackage = null;
        NXOpen.Features.HolePackageBuilder holePackageBuilder1;
        holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullNXOpen_Features_HolePackage);
        NXOpen.Features.HolePackageBuilder holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(null);
        holePackageBuilder1.Type = NXOpen.Features.HolePackageBuilder.Types.ThreadedHole;
        //holePackageBuilder1.GeneralHoleForm = NXOpen.Features.HolePackageBuilder.HoleForms.Simple;

        holePackageBuilder1.ThreadedStartChamferDiameter.RightHandSide = "48";
        holePackageBuilder1.ThreadedStartChamferAngle.RightHandSide = "45";
        holePackageBuilder1.ThreadedReliefDiameter.RightHandSide = "48";
        holePackageBuilder1.ThreadedReliefDepth.RightHandSide = "24";
        holePackageBuilder1.ThreadedHoleDepth.RightHandSide = "96";
        holePackageBuilder1.ThreadedEndChamferDiameter.RightHandSide = "48";
        holePackageBuilder1.ThreadedEndChamferAngle.RightHandSide = "45";
        holePackageBuilder1.ThreadDepth.RightHandSide = "48";
        holePackageBuilder1.TapDrillDiameter.RightHandSide = "43.129";
        holePackageBuilder1.HoleDepthLimitOption = NXOpen.Features.HolePackageBuilder.HoleDepthLimitOptions.Value ; // change the type here to desired limit
        holePackageBuilder1.Tolerance = 0.01;

        holePackageBuilder1.ThreadedTipAngle.RightHandSide = "118";

        NXOpen.Body[] targetBodies3 = new NXOpen.Body[1];
        targetBodies3[0] = Cyl.GetBodies()[0];

        holePackageBuilder1.BooleanOperation.SetTargetBodies(targetBodies3);
        holePackageBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract;


        Point3d origin2 = new Point3d(60, 0, 0); // check whether the point lies on the cylinder
        Point point21;
        point21 = workPart.Points.CreatePoint(origin2);
        NXOpen.Point[] points2 = new NXOpen.Point[1];
        points2[0] = point21;
        NXOpen.CurveDumbRule curveDumbRule1;
        curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points2);

        holePackageBuilder1.HolePosition.AllowSelfIntersection(true);

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


        NXObject nullNXObject = null;
        Point3d helpPoint1 = new Point3d(0.0, 0.0, 0.0);
        holePackageBuilder1.HolePosition.AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, NXOpen.Section.Mode.Create);


        NXObject hole = holePackageBuilder1.Commit();
        holePackageBuilder1.Destroy();
    }

 I have edited it without testing in NX, This may work or fail. Test this and give result.

伊尔凡

Re: Threaded Hole

Builder
Builder
Hello Sir,
same error is coming.
Below error details has been seen in NX log file.

Caught exception while running: Main
NXOpen.NXException: Thread standard data not found. Check standards data installation.
at NXOpen.Builder.Commit()
at NXJournal.Main() in c:\Users\Kishor\Documents\Visual Studio 2012\Projects\ThreadedHole\ThreadedHole\Class1.cs:line 107
&MACRO MESSAGE_BOX -2 Error in external library. See system log for details
&MACRO MESSAGE_TEXT
&MACRO MESSAGE_TEXT File name: C:\Users\Kishor\Documents\Visual Studio 2012\Projects\ThreadedHole\ThreadedHole\bin\Debug\ThreadedHole.dll
&MACRO MESSAGE_TEXT
&MACRO MESSAGE_TEXT Function name: Main
&MACRO MESSAGE_TEXT
Unloading C:\Users\Kishor\Documents\Visual Studio 2012\Projects\ThreadedHole\ThreadedHole\bin\Debug\ThreadedHole.dll

Re: Threaded Hole

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

I'm not sure if this is the issue, but I remember some past threads mentioning that you should not run the debug build of the dll. I don't remember the details though. If you search the forum, you should find more info on this.

Re: Threaded Hole

Legend
Legend

Ok, Try this, Record a journal and try to run it again. If that works. Take that code and compare with your existing code. You might missed something. 

 

伊尔凡

Re: Threaded Hole

Below is working code.  I use vb but it should be easy to see the requirement to get your code to work.

 

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.UF

Module ThreadedHoleTest
    Public s As Session = Session.GetSession()
    Public theUI As UI = UI.GetUI()
    Public ufs As UFSession = UFSession.GetUFSession()
    Sub Main()
        Dim theCylinder As Cylinder = Nothing
        CreateTheCylinder(theCylinder)
        InsertTheThread(theCylinder)
    End Sub
    Private Sub CreateTheCylinder(ByRef theCylinder As Cylinder)
        Dim workpart As Part = s.Parts.Work
        Dim theorigin As Point3d = New Point3d(0, 0, 0)
        Dim thepoint As Point = workpart.Points.CreatePoint(theorigin)
        Dim thevector As Vector3d = New Vector3d(1.0, 0, 0)
        Dim thedirection As Direction = workpart.Directions.CreateDirection(theorigin, thevector, SmartObject.UpdateOption.WithinModeling)
        Dim cylinderbuilder1 As CylinderBuilder = workpart.Features.CreateCylinderBuilder(Nothing)
        Dim theaxis As Axis = cylinderbuilder1.Axis
        theaxis.Direction = thedirection
        theaxis.Point = thepoint
        cylinderbuilder1.Diameter.RightHandSide = "100"
        cylinderbuilder1.Height.RightHandSide = "60"
        cylinderbuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create
        theCylinder = cylinderbuilder1.Commit
        cylinderbuilder1.Destroy()
    End Sub
    Private Sub InsertTheThread(ByVal theCylinder As Cylinder)
        Dim workpart As Part = s.Parts.Work
        Dim nullNXOpen_Features_HolePackage As NXOpen.Features.HolePackage = Nothing
        Dim holePackageBuilder1 As NXOpen.Features.HolePackageBuilder = Nothing
        holePackageBuilder1 = workpart.Features.CreateHolePackageBuilder(nullNXOpen_Features_HolePackage)
        holePackageBuilder1.Tolerance = 0.01
        holePackageBuilder1.ThreadStandard = "Metric Coarse"
        holePackageBuilder1.ThreadSize = "M48 x 5"
        holePackageBuilder1.TapDrillDiameter.RightHandSide = "43.129"
        holePackageBuilder1.ThreadedReliefDepth.RightHandSide = "24"
        holePackageBuilder1.ThreadedReliefDiameter.RightHandSide = "48"
        holePackageBuilder1.ThreadedHoleDepth.RightHandSide = "96"
        holePackageBuilder1.Type = NXOpen.Features.HolePackageBuilder.Types.ThreadedHole
        holePackageBuilder1.ThreadLengthOption = NXOpen.Features.HolePackageBuilder.ThreadLengthOptions.Custom
        holePackageBuilder1.ThreadDepth.RightHandSide = "72"
        holePackageBuilder1.DrillSizeHoleDepth.RightHandSide = "50"
        holePackageBuilder1.DrillSizeTipAngle.RightHandSide = "118"
        holePackageBuilder1.ThreadedHoleDepth.RightHandSide = "96"
        holePackageBuilder1.ThreadedTipAngle.RightHandSide = "118"
        holePackageBuilder1.ThreadedReliefAngle.RightHandSide = "118"
        holePackageBuilder1.RadialEngageOption = "Custom"
        holePackageBuilder1.DepthOption = NXOpen.Features.HolePackageBuilder.HoleDepthOptions.ToConeTip

        Dim thebodyfeat As BodyFeature = DirectCast(theCylinder, BodyFeature)
        Dim thebodies() As Body = thebodyfeat.GetBodies
        Dim thebody As Body = thebodies(0)

        Dim targetBodies1(0) As NXOpen.Body
        targetBodies1(0) = thebody
        holePackageBuilder1.BooleanOperation.SetTargetBodies(targetBodies1)
        holePackageBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract

        Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
        Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(-1.0, -0.0, -0.0)
        Dim direction1 As NXOpen.Direction = Nothing
        direction1 = workpart.Directions.CreateDirection(origin1, vector1, NXOpen.SmartObject.UpdateOption.WithinModeling)

        holePackageBuilder1.ProjectionDirection.ProjectVector = direction1
        holePackageBuilder1.HolePosition.DistanceTolerance = 0.01
        holePackageBuilder1.HolePosition.ChainingTolerance = 0.0095
        holePackageBuilder1.HolePosition.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyPoints)

        Dim theOrigin As Point3d = New Point3d(60, 0, 0)
        Dim point1 As Point = workpart.Points.CreatePoint(theOrigin)
        Dim points1(0) As NXOpen.Point
        points1(0) = point1
        Dim curveDumbRule1 As NXOpen.CurveDumbRule = Nothing
        curveDumbRule1 = workpart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1)

        Dim rules1(0) As NXOpen.SelectionIntentRule
        rules1(0) = curveDumbRule1
        Dim nullNXOpen_NXObject As NXOpen.NXObject = Nothing

        Dim helpPoint1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
        holePackageBuilder1.HolePosition.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)
        holePackageBuilder1.ProjectionDirection.ProjectDirectionMethod = NXOpen.GeometricUtilities.ProjectionOptions.DirectionType.Vector
        holePackageBuilder1.ProjectionDirection.ProjectVector = direction1

        Dim nXObject2 As NXOpen.NXObject = Nothing
        nXObject2 = holePackageBuilder1.Commit()

        holePackageBuilder1.Destroy()

    End Sub
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
    End Function

End Module

Frank Swinkels