EngProgrammer
Aerospace
- Jan 14, 2015
- 150
Dear Forum,
I am working with the NXOpen RegisterRpoRoutine method and trying to create hole constraints programmatically. I've tried several examples within the GTAC solutions and can't seem to get this functionality to work. I am developing in vb.net. I've created a function that creates a block with a hole. The code is attached below.
Thank you for your inputs.
I am working with the NXOpen RegisterRpoRoutine method and trying to create hole constraints programmatically. I've tried several examples within the GTAC solutions and can't seem to get this functionality to work. I am developing in vb.net. I've created a function that creates a block with a hole. The code is attached below.
Thank you for your inputs.
Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Module CreateBlock
Dim theSession As Session = Session.GetSession()
Dim SessionO_gl As Session = theSession
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim xDimension As String
Dim yDimension As String
Dim theXaxis As DatumAxis
Dim theYaxis As DatumAxis
Sub Main()
Dim displayPart As Part = SessionO_gl.Parts.Display
Dim workpart As Part = theSession.Parts.Work
'Get original WCS and information about it:
Dim WCSOrigin As CoordinateSystem = displayPart.WCS.CoordinateSystem
Dim coordOrigin() As Double = {WCSOrigin.Origin.X, WCSOrigin.Origin.Y, _
WCSOrigin.Origin.Z}
Dim originalWCSTag As NXOpen.Tag = Tag.Null
theUfSession.Csys.AskWcs(originalWCSTag)
Dim WcsMtxTag As NXOpen.Tag = Tag.Null
theUfSession.Csys.AskMatrixOfObject(originalWCSTag, WcsMtxTag)
'Create a new CSYS at the origin of the WCS,
'but aligned with the absolute
Dim identityMtx() As Double = {1, 0, 0, 0, 1, 0, 0, 0, 1}
Dim newMtxTag As NXOpen.Tag = Tag.Null
theUfSession.Csys.CreateMatrix(identityMtx, newMtxTag)
Dim newCsys As NXOpen.Tag = Tag.Null
theUfSession.Csys.CreateCsys(coordOrigin, newMtxTag, newCsys)
theUfSession.Csys.SetWcs(newCsys)
'Create the block
Dim xLen As Double = 100
Dim yWidth As Double = 50
Dim zHeight As Double = 25
Dim lgths() As String = {xLen.ToString, yWidth.ToString, zHeight.ToString}
Dim blockFeatureTag As NXOpen.Tag = Tag.Null
Dim blockBodyTag As NXOpen.Tag = Tag.Null
theUfSession.Modl.CreateBlock(FeatureSigns.Nullsign, NXOpen.Tag.Null, _
coordOrigin, lgths, blockFeatureTag)
theUfSession.Modl.AskFeatBody(blockFeatureTag, blockBodyTag)
Dim BlockBody As Body = CType(NXObjectManager.Get(blockBodyTag), Body)
'Revert to the original WCS
theUfSession.Csys.SetWcs(originalWCSTag)
theUfSession.Obj.DeleteObject(newCsys)
Dim topFace As Face = Nothing
Dim btmFace As Face = Nothing
Dim u1(2) As Double
Dim v1(2) As Double
Dim u2(2) As Double
Dim v2(2) As Double
Dim unit_normal(2) As Double
Dim normal As VectorArithmetic.Vector3
Dim radii(1) As Double
Dim newOrigin(2) As Double
Dim uvMinMax(3) As Double
Dim param(1) As Double
For Each iFace As Face In BlockBody.GetFaces()
theUfSession.Modl.AskFaceUvMinmax(iFace.Tag, uvMinMax)
param(0) = ((uvMinMax(0) + uvMinMax(1)) / 2)
param(1) = ((uvMinMax(2) + uvMinMax(3)) / 2)
theUfSession.Modl.AskFaceProps(iFace.Tag, param, newOrigin, u1, v1, u2, v2, unit_normal, radii)
normal = New VectorArithmetic.Vector3(unit_normal(0), unit_normal(1), unit_normal(2))
If normal.z.Equals(1) Then
topFace = iFace
topFace.SetName("TopFace")
topFace.Highlight()
End If
If normal.z.Equals(-1) Then
btmFace = iFace
btmFace.SetName("btmFace")
btmFace.Highlight()
End If
Next
Dim theCsys As CartesianCoordinateSystem = Nothing
Dim theOrigin As Point = Nothing
Dim theZAxis As DatumAxis = Nothing
Dim theXYPlane As DatumPlane = Nothing
Dim theYZPlane As DatumPlane = Nothing
Dim theXZPlane As DatumPlane = Nothing
Dim theDatumCsys As Features.DatumCsys
theDatumCsys = CType(workpart.Features.ToArray(0), Features.DatumCsys)
GetObjectsOfDatumCsys(theDatumCsys, theCsys, theOrigin, _
theXaxis, theYaxis, theZAxis,
theXYPlane, theYZPlane, theXZPlane)
Dim holeDiameter As Double = 3
Dim theHoleTag As Tag = Tag.Null
Try
xDimension = xLen / 2
yDimension = yWidth / 2
'theUfSession.Modl.RegisterRpoRoutine(AddressOf theUfSession.Modl.DefaultRpoMenu)
theUfSession.Modl.RegisterRpoRoutine(AddressOf PlaceHole)
theUfSession.Modl.CreateSimpleHole(New Double(2) {xLen / 2, yWidth / 2, zHeight}, _
New Double(2) {0, 0, -1}, _
holeDiameter.ToString(), "", "", _
topFace.Tag, btmFace.Tag, theHoleTag)
theUfSession.Modl.UnregisterRpoRoutine()
Catch ex As Exception
Throw New Exception("Failed Hole Creation")
End Try
End Sub
Function PlaceHole(ByVal theHoleTag As Tag) As Integer
Dim targets() As Tag = {theXAxis.Tag, theYAxis.Tag}
Dim target_qualifiers() As Integer = {0, 0} ' Not used
Dim tools() As Tag = {Nothing, Nothing}
Dim tool_qualifiers() As Integer = {UFConstants.UF_MODL_ARC_CENTER, UFConstants.UF_MODL_ARC_CENTER}
Dim constraintNames() As String = {"PERP_DIST_PARMS", "PERP_DIST_PARMS"}
Dim constraintValues() As String = {xDimension.ToString, yDimension.ToString}
theUFSession.Modl.CreateRpoConstraints(theHoleTag, Nothing, Nothing,
targets, target_qualifiers, tools, tool_qualifiers,
constraintValues, constraintNames, 2)
End Function
Private Sub GetObjectsOfDatumCsys(ByVal theDatumCsys As NXOpen.Features.DatumCsys,
ByRef theCsys As CartesianCoordinateSystem,
ByRef theOrigin As Point,
ByRef theXAxis As DatumAxis,
ByRef theYAxis As DatumAxis,
ByRef theZAxis As DatumAxis,
ByRef theXYPlane As DatumPlane,
ByRef theYZPlane As DatumPlane,
ByRef theXZPlane As DatumPlane)
theCsys = Nothing
theOrigin = Nothing
theXAxis = Nothing
theYAxis = Nothing
theZAxis = Nothing
theXYPlane = Nothing
theYZPlane = Nothing
theXZPlane = Nothing
Dim theEntities() As NXObject = theDatumCsys.GetEntities()
Dim theDatumAxes(2) As DatumAxis
Dim n_axes As Integer = 0
Dim theDatumPlanes(2) As DatumPlane
Dim n_planes As Integer = 0
For Each oneObject As NXObject In theEntities
If TypeOf oneObject Is CartesianCoordinateSystem Then
theCsys = CType(oneObject, CartesianCoordinateSystem)
Continue For
End If
If TypeOf oneObject Is Point Then
theOrigin = CType(oneObject, Point)
Continue For
End If
If TypeOf oneObject Is DatumPlane Then
theDatumPlanes(n_planes) = CType(oneObject, DatumPlane)
n_planes = n_planes + 1
Continue For
End If
If TypeOf oneObject Is DatumAxis Then
theDatumAxes(n_axes) = CType(oneObject, DatumAxis)
n_axes = n_axes + 1
Continue For
End If
' This doesn't happen - but just in case...
Echo("GetEntities returned a " + oneObject.GetType().Name)
Next
Dim theMatrix As Matrix3x3 = theCsys.Orientation.Element
Dim x_dir() As Double = {theMatrix.Xx, theMatrix.Xy, theMatrix.Xz}
Dim y_dir() As Double = {theMatrix.Yx, theMatrix.Yy, theMatrix.Yz}
Dim z_dir() As Double = {theMatrix.Zx, theMatrix.Zy, theMatrix.Zz}
Dim tol As Double ' 0 is too tight! My default was 0.5 which worked well.
theUFSession.Modl.AskAngleTolerance(tol)
For ii As Integer = 0 To (n_axes - 1)
Dim axis_vec As Vector3d = theDatumAxes(ii).Direction
Dim axis_dir() As Double = {axis_vec.X, axis_vec.Y, axis_vec.Z}
Dim is_parallel As Integer
theUFSession.Vec3.IsParallel(x_dir, axis_dir, tol, is_parallel)
If is_parallel = 1 Then theXAxis = theDatumAxes(ii)
theUFSession.Vec3.IsParallel(y_dir, axis_dir, tol, is_parallel)
If is_parallel = 1 Then theYAxis = theDatumAxes(ii)
theUFSession.Vec3.IsParallel(z_dir, axis_dir, tol, is_parallel)
If is_parallel = 1 Then theZAxis = theDatumAxes(ii)
Next
For ii As Integer = 0 To (n_planes - 1)
Dim normal_vec As Vector3d = theDatumPlanes(ii).Normal
Dim normal_dir() As Double = {normal_vec.X, normal_vec.Y, normal_vec.Z}
Dim is_parallel As Integer
theUFSession.Vec3.IsParallel(z_dir, normal_dir, tol, is_parallel)
If is_parallel = 1 Then theXYPlane = theDatumPlanes(ii)
theUFSession.Vec3.IsParallel(y_dir, normal_dir, tol, is_parallel)
If is_parallel = 1 Then theXZPlane = theDatumPlanes(ii)
theUFSession.Vec3.IsParallel(x_dir, normal_dir, tol, is_parallel)
If is_parallel = 1 Then theYZPlane = theDatumPlanes(ii)
Next
End Sub
Sub Echo(ByVal output As String)
theSession.ListingWindow.Open()
theSession.ListingWindow.WriteLine(output)
theSession.LogFile.WriteLine(output)
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Return Session.LibraryUnloadOption.Immediately
Return Session.LibraryUnloadOption.Explicitly
End Function
End Module