Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

C# Programming

Status
Not open for further replies.

Kishor khalane

Mechanical
Jul 27, 2018
9
0
0
IN
Hello Guys,
Below is the code for two cylinder in C#. Please tell how to UNITE this 2 cylinder



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NXOpen;

namespace cylinderbuilder
{
public class Class1
{
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 = "20";
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();
cylinderbuilder.Destroy();


// Another Cylinder
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(20, 0, 0);
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;

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

// Define Cylinder Height
string heightString1 = "30";
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();
}
public static int GetUnloadOption(string dummy) { return (int)NXOpen.Session.LibraryUnloadOption.Immediately; }
}
}


 
Replies continue below

Recommended for you

Hi,

Change the following line

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

To

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

Regards

Regards
Didier Psaltopoulos
 
Hello Didier Psaltopoulos Sir,
Thank for your help.
I tried this as you wrote that change that line.
But I didn't get unite those cylinderbuilder.
If you have any other please kindly share.
Kishor

 
Didier mean to change only for
The second cylinder.

If you done this and fail.

Try record a journal for the unit
Of two cylinders.
 
Hello Didier Psaltopoulos Sir,
If possible and you have code for uniting 2 cylinder in VB.
you could possibly share it for understanding purpose.
Because C# and VB is somewhat similar.
I know basic knowledge of VB also.

Thank You
 
Hello Didier Psaltopoulos Sir,
Here is code in VB for hole generation but it is getting ERROR.
Please suggest where to modified this code.

Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF




Module HOle
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim ufs As UFSession = UFSession.GetUFSession()
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Public Sub Main(ByVal args() As String)

Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

'How to make body select??
Dim body1 As NXOpen.Body
body1 = SelectSolid()

Dim itargetBodies(0) As NXOpen.Body
itargetBodies(0) = body1

If Not workPart.Preferences.Modeling.GetHistoryMode Then
Throw (New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

CreateHole(0, 0, itargetBodies)
CreateHole(15, 0, itargetBodies)
CreateHole(30, 0, itargetBodies)

End Sub


Sub CreateHole(xcoord As Double, ycoord As Double, targetBodies As NXOpen.Body())

Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing

Dim datumCsysBuilder1 As NXOpen.Features.DatumCsysBuilder
datumCsysBuilder1 = workPart.Features.CreateDatumCsysBuilder(nullNXOpen_Features_Feature)

Dim cartesianCoordinateSystem1 As NXOpen.CartesianCoordinateSystem
cartesianCoordinateSystem1 = workPart.WCS.CoordinateSystem

'Define origin

Dim orPt As Point3d
orPt.X = xcoord
orPt.Y = ycoord
orPt.Z = 0
'New origin for CSYS
cartesianCoordinateSystem1.Origin = WCS2Abs(orPt)

datumCsysBuilder1.Csys = cartesianCoordinateSystem1

datumCsysBuilder1.DisplayScaleFactor = 0.5

Dim nXObject1 As NXOpen.NXObject
nXObject1 = datumCsysBuilder1.Commit()

Dim datumCsys2 As NXOpen.Features.DatumCsys = CType(nXObject1, NXOpen.Features.DatumCsys)

'Need to select datum origin.

Dim point1 As NXOpen.Point = CType(datumCsys2.FindObject("POINT 1"), NXOpen.Point)

'Dim point1 As NXOpen.Point = CType(datumCsys2.Location, NXOpen.Point)

datumCsysBuilder1.Destroy()

'Create Hole Part
Dim nullNXOpen_Features_HolePackage As NXOpen.Features.HolePackage = Nothing

Dim holePackageBuilder1 As NXOpen.Features.HolePackageBuilder
holePackageBuilder1 = workPart.Features.CreateHolePackageBuilder(nullNXOpen_Features_HolePackage)

'Point

Dim points1(0) As NXOpen.Point
points1(0) = point1
Dim curveDumbRule1 As NXOpen.CurveDumbRule
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)


With holePackageBuilder1

.HolePosition.AllowSelfIntersection(True)
.HolePosition.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)
.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Subtract
.BooleanOperation.SetTargetBodies(targetBodies)

End With

'Hole Type 6
With holePackageBuilder1

.GeneralHoleForm = Features.HolePackageBuilder.HoleForms.Simple
.HoleDepthLimitOption = Features.HolePackageBuilder.HoleDepthLimitOptions.ThroughBody
.GeneralSimpleHoleDiameter.RightHandSide = "6"
.GeneralSimpleHoleDepth.RightHandSide = "12"
.Tolerance = 0.001

End With

'Commit Hole
Dim nXObject2 As NXOpen.NXObject
nXObject2 = holePackageBuilder1.Commit()

holePackageBuilder1.Destroy()


End Sub




Function SelectSolid() As Body

Dim ui As UI = ui.GetUI
Dim message As String = "Select solid body"
Dim title As String = "Selection"

Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim keepHighlighted As Boolean = False
Dim includeFeatures As Boolean = True

Dim selectionAction As Selection.SelectionAction = _
Selection.SelectionAction.ClearAndEnableSpecific

Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.Subtype = 0
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With

Dim selectedObject As NXObject = Nothing
Dim cursor As Point3d

ui.SelectionManager.SelectObject(message, title, scope, selectionAction, includeFeatures, _
keepHighlighted, selectionMask_array, _
selectedObject, cursor)

Dim solid As Body = CType(selectedObject, Body)

If solid Is Nothing Then
Return Nothing
End If

Return solid

End Function


Function Abs2WCS(ByVal inPt As Point3d) As Point3d
Dim pt1(2), pt2(2) As Double

pt1(0) = inPt.X
pt1(1) = inPt.Y
pt1(2) = inPt.Z

ufs.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_COORDS, pt1, _
UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt2)

Abs2WCS.X = pt2(0)
Abs2WCS.Y = pt2(1)
Abs2WCS.Z = pt2(2)

End Function

Function WCS2Abs(ByVal inPt As Point3d) As Point3d
Dim pt1(2), pt2(2) As Double

pt1(0) = inPt.X
pt1(1) = inPt.Y
pt1(2) = inPt.Z

ufs.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt1, _
UFConstants.UF_CSYS_ROOT_COORDS, pt2)

WCS2Abs.X = pt2(0)
WCS2Abs.Y = pt2(1)
WCS2Abs.Z = pt2(2)

End Function


Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

End Function

Thank You


End Module
 
Status
Not open for further replies.
Back
Top