junfanbl
Marine/Ocean
- Jun 10, 2015
- 90
Hey everyone, I have been trying to build a journal to accomplish a specific task. What I am trying to do is place a datum plane using the bounding box coordinates of a model. I would like to fill in the bounding box in both the x and y directions. For the Z it should be the minimum value. That is how I currently have it set up in my code below.
However for some reason when I try and place the datum plane It gives me an error saying the "datum plane is undefinable".
If I record a journal while placing a datum plane by using Point and direction, the coordinates that it produces are very similar to what the bounding box is producing. I don't see why the plane won't place. In the code below I attempt to use the coefficient method. Not sure if that has anything to do with it but I thought that would be a better option.
Any ideas?
However for some reason when I try and place the datum plane It gives me an error saying the "datum plane is undefinable".
If I record a journal while placing a datum plane by using Point and direction, the coordinates that it produces are very similar to what the bounding box is producing. I don't see why the plane won't place. In the code below I attempt to use the coefficient method. Not sure if that has anything to do with it but I thought that would be a better option.
Any ideas?
Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Module NXJournal
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main(ByVal args() As String)
Dim theBody As Body
If SelectBody("select a body", theBody) = Selection.Response.Cancel Then
Return
End If
'Need to get bounding box of model to establish datum plane coordinates later on
Dim bbox(5) As Double
theUfSession.Modl.AskBoundingBox(theBody.Tag, bbox)
'lengths returned in part units
Dim bodyLengths(2) As Double
bodyLengths = GetBoundingBox(theBody)
'Open Information Window
If Not lw.IsOpen Then
lw.Open()
End If
'Body Lengths
lw.WriteLine("GET BODY LENGTHS...")
lw.WriteLine("X length: " & bodyLengths(0).ToString)
lw.WriteLine("Y length: " & bodyLengths(1).ToString)
lw.WriteLine("Z length: " & bodyLengths(2).ToString)
lw.WriteLine("")
'Min and Max values
lw.WriteLine("GET MIN/MAX VALUES...")
lw.WriteLine("min X: " & bbox(0) & " max X: " & bbox(3))
lw.WriteLine("min Y: " & bbox(1) & " max Y: " & bbox(4))
lw.WriteLine("min Z: " & bbox(2) & " max Z: " & bbox(5))
lw.WriteLine("")
'Insert Datum Planes based on Bounding Box Coordinates
'==========================================================================================================
Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing
'Set methods
Dim datumPlaneBuilder1 As NXOpen.Features.DatumPlaneBuilder
datumPlaneBuilder1 = workPart.Features.CreateDatumPlaneBuilder(nullNXOpen_Features_Feature)
Dim plane1 As NXOpen.Plane
plane1 = datumPlaneBuilder1.GetPlane()
'Set units
'Dim unit1 As NXOpen.Unit = CType(workPart.UnitCollection.FindObject("Inches"), NXOpen.Unit)
'Not sure what this does
Dim coordinates1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, bbox(2))
Dim point1 As NXOpen.Point
point1 = workPart.Points.CreatePoint(coordinates1)
'Determine Axis?
Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0)
Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0)
Dim direction1 As NXOpen.Direction
direction1 = workPart.Directions.CreateDirection(origin1, vector1, NXOpen.SmartObject.UpdateOption.WithinModeling)
'Not sure what this does
plane1.SetUpdateOption(NXOpen.SmartObject.UpdateOption.WithinModeling)
Dim geom1(-1) As NXOpen.NXObject
plane1.SetGeometry(geom1)
'Type of plane placement
plane1.SetMethod(NXOpen.PlaneTypes.MethodType.Coefficients)
'twice?
Dim geom2(-1) As NXOpen.NXObject
plane1.SetGeometry(geom2)
'Determine Origin?
Dim origin2 As NXOpen.Point3d = New NXOpen.Point3d(0, 0, 0)
plane1.Origin = origin2
Dim normal1 As NXOpen.Vector3d = New NXOpen.Vector3d(0, 0, 0)
plane1.Normal = normal1
'Not sure what this does
plane1.SetAlternate(NXOpen.PlaneTypes.AlternateType.One)
plane1.Evaluate()
'Change Offset of location
plane1.RemoveOffsetData()
plane1.Evaluate()
lw.WriteLine("min X: " & bbox(0) & " max X: " & bbox(3))
lw.WriteLine("min Y: " & bbox(1) & " max Y: " & bbox(4))
lw.WriteLine("min Z: " & bbox(2) & " max Z: " & bbox(5))
Dim corner1_1 As NXOpen.Point3d = New NXOpen.Point3d(bbox(0), bbox(1), bbox(2))
Dim corner2_1 As NXOpen.Point3d = New NXOpen.Point3d(bbox(3), bbox(1), bbox(2))
Dim corner3_1 As NXOpen.Point3d = New NXOpen.Point3d(bbox(0), bbox(4), bbox(2))
Dim corner4_1 As NXOpen.Point3d = New NXOpen.Point3d(bbox(3), bbox(4), bbox(2))
'Finalize placement of plane
datumPlaneBuilder1.SetCornerPoints(corner1_1, corner2_1, corner3_1, corner4_1)
datumPlaneBuilder1.ResizeDuringUpdate = True
Dim feature1 As NXOpen.Features.Feature
feature1 = datumPlaneBuilder1.CommitFeature()
Dim datumPlaneFeature1 As NXOpen.Features.DatumPlaneFeature = CType(feature1, NXOpen.Features.DatumPlaneFeature)
Dim datumPlane1 As NXOpen.DatumPlane
datumPlane1 = datumPlaneFeature1.DatumPlane
datumPlane1.SetReverseSection(False)
datumPlaneBuilder1.Destroy()
End Sub
Function SelectBody(ByVal prompt As String, ByRef selObj As TaggedObject) As Selection.Response
Dim theUI As UI = UI.GetUI
Dim title As String = "Select a solid body"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
Dim cursor As Point3d
Dim scope As Selection.SelectionScope = Selection.SelectionScope.WorkPart
Dim selectionMask_array(0) As Selection.MaskTriple
With selectionMask_array(0)
.Type = UFConstants.UF_solid_type
.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_SOLID_BODY
End With
Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj, cursor)
If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If
End Function
Private Function GetBoundingBox(ByVal solidBody As NXOpen.Body) As Double()
'AskBoundingBox returns min and max coordinates
'this function will simply return the box lengths (x, y, z)
Dim bboxCoordinates(5) As Double
Dim bboxLengths(2) As Double
Try
'get solid body bounding box extents
theUfSession.Modl.AskBoundingBox(solidBody.Tag, bboxCoordinates)
bboxLengths(0) = bboxCoordinates(3) - bboxCoordinates(0)
bboxLengths(1) = bboxCoordinates(4) - bboxCoordinates(1)
bboxLengths(2) = bboxCoordinates(5) - bboxCoordinates(2)
Return bboxLengths
Catch ex As NXException
MsgBox(ex.GetType.ToString & " : " & ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Solid Body Bounds Error!")
bboxLengths(0) = 0
bboxLengths(1) = 0
bboxLengths(2) = 0
Return bboxLengths
End Try
End Function
End Module