Cancel
Showing results for 
Search instead for 
Did you mean: 

C# Programming using NXOpen

Pioneer
Pioneer

Hello Guys,
Here is code for hole Creation in Cylinder.
Although code is not showing error in the visual studio.
But while running in NX, It is getting error such as INVALID CAST.
Please anybody can correct it.

using System;
using NXOpen;

class NXJournal
{
public static void Main()
{

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

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

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

// Define the Cylinder Axis
Point3d origin = new Point3d(0, 0, 0);
Vector3d vector = new Vector3d(1, 0, 0);
Direction direction;
direction = workPart.Directions.CreateDirection(origin, vector, SmartObject.UpdateOption.WithinModeling);
Axis axis;
axis = cylinderbuilder.Axis;
axis.Direction = direction;

// Define Cylinder Diameter
string diamString = "100";
cylinderbuilder.Diameter.RightHandSide = diamString;

// Define Cylinder Height
string heightString = "50";
cylinderbuilder.Height.RightHandSide = heightString;

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

// Destroy the builder to free memory
NXObject cylinder = cylinderbuilder.Commit();


//Hole Feature

NXOpen.Features.HolePackageBuilder holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(null);
holePackageBuilder1.Type = NXOpen.Features.HolePackageBuilder.Types.GeneralHole;
holePackageBuilder1.GeneralSimpleHoleDiameter.RightHandSide = "40";
holePackageBuilder1.HoleDepthLimitOption = NXOpen.Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody;

holePackageBuilder1.Tolerance = 0.01;
holePackageBuilder1.HolePosition.DistanceTolerance = 0.01;

NXOpen.Body[] targetBodies1 = new NXOpen.Body[1];

 

// Error is coming here while running in NX although I have cast cylinder feature to body.
targetBodies1[0] = (NXOpen.Body)cylinder;

 

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


Point3d origin1 = new Point3d(50, 0, 0);
Point point1;
point1 = workPart.Points.CreatePoint(origin1);
NXOpen.Point[] points1 = new NXOpen.Point[1];
points1[0] = point1;
NXOpen.CurveDumbRule curveDumbRule1;
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1);

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

holePackageBuilder1.Commit();
holePackageBuilder1.Destroy();


}


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

4 REPLIES 4

Re: C# Programming using NXOpen

Siemens Phenom Siemens Phenom
Siemens Phenom

There may be other errors, too, but the first problem is that you are trying to cast an object of type NXOpen.Features.Cylinder to the type NXOpen.Body. That will never work, as the error message tells you. If you want to get the body from your "cylnder" object, use the NXOpen.Features.BodyFeature.GetBodies function.

 

Your code is equivalent to

NXOpen.Body[] targetBodies1 = {  (NXOpen.Body)cylinder };

Instead, you need

NXOpen.Body[] targetBodies1 = cylinder.GetBodies();
山田
yamada

Re: C# Programming using NXOpen

Pioneer
Pioneer
Hello Sir, Thank You for replying
Problem is that, I am not getting that getbodies() method while putting dot to cylinder feature.
Is there any other way to solve?
Sent from Mail for Windows 10

Re: C# Programming using NXOpen

Legend
Legend
using System;
using NXOpen;

class NXJournal
{
public static void Main()
{

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

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

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

// Define the Cylinder Axis
Point3d origin = new Point3d(0, 0, 0);
Vector3d vector = new Vector3d(1, 0, 0);
Direction direction;
direction = workPart.Directions.CreateDirection(origin, vector, SmartObject.UpdateOption.WithinModeling);
Axis axis;
axis = cylinderbuilder.Axis;
axis.Direction = direction;

// Define Cylinder Diameter
string diamString = "100";
cylinderbuilder.Diameter.RightHandSide = diamString;

// Define Cylinder Height
string heightString = "50";
cylinderbuilder.Height.RightHandSide = heightString;

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

// Destroy the builder to free memory
NXObject cylinder = cylinderbuilder.Commit();


//Hole Feature

NXOpen.Features.HolePackageBuilder holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(null);
holePackageBuilder1.Type = NXOpen.Features.HolePackageBuilder.Types.GeneralHole;
holePackageBuilder1.GeneralSimpleHoleDiameter.RightHandSide = "40";
holePackageBuilder1.HoleDepthLimitOption = NXOpen.Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody;

holePackageBuilder1.Tolerance = 0.01;
holePackageBuilder1.HolePosition.DistanceTolerance = 0.01;

NXOpen.Body[] targetBodies1 = new NXOpen.Body[1];
Tag bodyTag = Tag.Null;
theUfSession.Modl.AskFeatBody(cylinder.tag , out bodyTag);

// Error is coming here while running in NX although I have cast cylinder feature to body.
// I have collected the body Tag from the feature and typecasted it to body
targetBodies1[0] = (NXOpen.Body)NXOpen.Utilities.NXObjectManager.Get(bodyTag);

 

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


Point3d origin1 = new Point3d(50, 0, 0);
Point point1;
point1 = workPart.Points.CreatePoint(origin1);
NXOpen.Point[] points1 = new NXOpen.Point[1];
points1[0] = point1;
NXOpen.CurveDumbRule curveDumbRule1;
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1);

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

holePackageBuilder1.Commit();
holePackageBuilder1.Destroy();


}


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

I have modified some piece of your code. Check it.
 as @Yamada  suggested you can also collect bodies from its feature. In order to do it. 

try this

 

 

using System;
using NXOpen;

class NXJournal
{
public static void Main()
{

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

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

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

// Define the Cylinder Axis
Point3d origin = new Point3d(0, 0, 0);
Vector3d vector = new Vector3d(1, 0, 0);
Direction direction;
direction = workPart.Directions.CreateDirection(origin, vector, SmartObject.UpdateOption.WithinModeling);
Axis axis;
axis = cylinderbuilder.Axis;
axis.Direction = direction;

// Define Cylinder Diameter
string diamString = "100";
cylinderbuilder.Diameter.RightHandSide = diamString;

// Define Cylinder Height
string heightString = "50";
cylinderbuilder.Height.RightHandSide = heightString;

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

// Destroy the builder to free memory
NXObject cylinder = cylinderbuilder.Commit();
NXopen.Features.Cylinder theCyl = (NXopen.Features.Cylinder)cylinder;


//Hole Feature

NXOpen.Features.HolePackageBuilder holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(null);
holePackageBuilder1.Type = NXOpen.Features.HolePackageBuilder.Types.GeneralHole;
holePackageBuilder1.GeneralSimpleHoleDiameter.RightHandSide = "40";
holePackageBuilder1.HoleDepthLimitOption = NXOpen.Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody;

holePackageBuilder1.Tolerance = 0.01;
holePackageBuilder1.HolePosition.DistanceTolerance = 0.01;

NXOpen.Body[] targetBodies1 = new NXOpen.Body[1];


// Error is coming here while running in NX although I have cast cylinder feature to body.

targetBodies1[0] = theCyl.getbodies()[0];

 

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


Point3d origin1 = new Point3d(50, 0, 0);
Point point1;
point1 = workPart.Points.CreatePoint(origin1);
NXOpen.Point[] points1 = new NXOpen.Point[1];
points1[0] = point1;
NXOpen.CurveDumbRule curveDumbRule1;
curveDumbRule1 = workPart.ScRuleFactory.CreateRuleCurveDumbFromPoints(points1);

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

holePackageBuilder1.Commit();
holePackageBuilder1.Destroy();


}


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

There might be some Typo error because I updated this code using mobile. 
You have to destroy all builder which you have created. check it on you code. 

伊尔凡
Highlighted

Re: C# Programming using NXOpen

Pioneer
Pioneer
Thank You so muchhhh Sir,
It is working as expected.
I spent almost 4 to 5 days finally succeed just because of you.