Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

C# Programming using NX/Open

Status
Not open for further replies.

Kishor khalane

Mechanical
Jul 27, 2018
9
0
0
IN
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
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; }
}
 
Replies continue below

Recommended for you

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.
 
Status
Not open for further replies.
Back
Top