Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Need help in VB journal

Status
Not open for further replies.

Ehaviv

Computer
Jul 2, 2003
1,012
Hi

This journal work OK when I removed the Resize plane code segment
and its also run with it but the orientation and location
of the plane allwase in the abs coord.

can someomne help correct it

Thank you in advanced

Code:
Option Strict Off  
Imports System  
Imports NXOpen  
Imports NXOpen.UF  
Imports NXOpenUI  

Module create_and_resize_plane

 Sub Main

   Dim theSession As Session = Session.GetSession()
   Dim ufs As UFSession = UFSession.GetUFSession()

   create_plane( )

 End Sub

'*********************************************************************

 Sub create_plane( )

  '   ****** Create Datum plane ******

  Dim theSession As Session = Session.GetSession()
  Dim workPart As Part = theSession.Parts.Work
  Dim displayPart As Part = theSession.Parts.Display
  Dim ufs As UFSession = UFSession.GetUFSession()

  Dim wcs_origin As Point3d = workPart.WCS.Origin
  Dim wcs_matrix3x3 As Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element
  Dim featdatum_tag As Tag
  Dim featdatum_obj As NXObject

  Dim xyplane As DatumPlane
  xyplane = workPart.Datums.CreateFixedDatumPlane(wcs_origin,wcs_matrix3x3)
  xyplane.SetName("_XY_")
  ufs.Modl.AskObjectFeat(xyplane.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  Dim xy_PlaneFeat As Features.DatumPlaneFeature = CType(featdatum_obj,Features.DatumPlaneFeature)
  xy_PlaneFeat.SetName("_XY_")

'==================== Resize plane start ========================

   Dim nullFeature As NXOpen.Features.Feature = Nothing
   Dim resizePlaneBuilder1 As Features.ResizePlaneBuilder
   resizePlaneBuilder1 = workPart.Features.CreateResizePlaneBuilder(nullFeature)
   resizePlaneBuilder1.Plane.Value = xyplane
   resizePlaneBuilder1.ResizeDuringUpdate = False
   Dim d As Double = 3.0
   Dim p As Double = 1.0
   Dim n As Double = -1.0
   d = d/2
   Dim corners As Point3d() = {New Point3d(n*d, n*d, 0.0),New Point3d(p*d, n*d, 0.0), _
                                      New Point3d(p*d, p*d, 0.0),New Point3d(n*d, p*d, 0.0)}

   resizePlaneBuilder1.SetCornerPoints(corners)
    
   resizePlaneBuilder1.Commit()
   resizePlaneBuilder1.Destroy()

'====================== Resize plane end ======================

  Dim Xdir As New Point3d(1,0,0)
  Xdir = WCS2Abs(Xdir)
  Dim Xaxis As DatumAxis
  Xaxis = workPart.Datums.CreateFixedDatumAxis(wcs_origin,Xdir)
  Xaxis.SetName("_X_")
  ufs.Modl.AskObjectFeat(Xaxis.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  Dim x_AxisFeat As Features.DatumAxisFeature = CType(featdatum_obj,Features.DatumAxisFeature)
  x_AxisFeat.SetName("_X_")

  Dim Ydir As New Point3d(0,1,0)
  Ydir = WCS2Abs(Ydir)
  Dim Yaxis As DatumAxis
  Yaxis = workPart.Datums.CreateFixedDatumAxis(wcs_origin,Ydir)
  Yaxis.SetName("_Y_")
  ufs.Modl.AskObjectFeat(Yaxis.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  Dim y_AxisFeat As Features.DatumAxisFeature = CType(featdatum_obj,Features.DatumAxisFeature)
  y_AxisFeat.SetName("_Y_")

 End Sub

'---------------------------------------------------------------------

 Function WCS2Abs(ByVal inPt As Point3d) As Point3d
  
  Dim ufs As UFSession = UFSession.GetUFSession()
   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.Immediately  

 End Function  
'---------------------------------------------------------------------

End Module
 
Replies continue below

Recommended for you

Hi
I added this code to check plane corners and I get all plane corners equale to zero
Do someone can see and help vwhats wrong here

Dim crnr1,crnr2,crnr3,crnr4 As Point3d
Dim crnrs As Point3d() = {crnr1,crnr2,crnr3,crnr4}
resizePlaneBuilder1.GetCornerPoints(crnrs)
msgbox(crnrs(0).ToString)
msgbox(crnrs(1).ToString)
msgbox(crnrs(2).ToString)
msgbox(crnrs(3).ToString)


Code:
Option Strict Off  
Imports System  
Imports NXOpen  
Imports NXOpen.UF  
Imports NXOpenUI  

Module create_and_resize_plane

 Sub Main

   Dim theSession As Session = Session.GetSession()
   Dim ufs As UFSession = UFSession.GetUFSession()

   create_plane( )

 End Sub

'*********************************************************************

 Sub create_plane( )

  '   ****** Create Datum plane ******

  Dim theSession As Session = Session.GetSession()
  Dim workPart As Part = theSession.Parts.Work
  Dim displayPart As Part = theSession.Parts.Display
  Dim ufs As UFSession = UFSession.GetUFSession()

  Dim wcs_origin As Point3d = workPart.WCS.Origin
  Dim wcs_matrix3x3 As Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element
  Dim featdatum_tag As Tag
  Dim featdatum_obj As NXObject

  Dim xyplane As DatumPlane
  xyplane = workPart.Datums.CreateFixedDatumPlane(wcs_origin,wcs_matrix3x3)
  xyplane.SetName("_XY_")
  ufs.Modl.AskObjectFeat(xyplane.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  Dim xy_PlaneFeat As Features.DatumPlaneFeature = CType(featdatum_obj,Features.DatumPlaneFeature)
  xy_PlaneFeat.SetName("_XY_")

'==================== Resize plane start ========================

   Dim nullFeature As NXOpen.Features.Feature = Nothing
   Dim resizePlaneBuilder1 As Features.ResizePlaneBuilder
   resizePlaneBuilder1 = workPart.Features.CreateResizePlaneBuilder(nullFeature)
   resizePlaneBuilder1.Plane.Value = xyplane
   resizePlaneBuilder1.ResizeDuringUpdate = False

   Dim crnr1,crnr2,crnr3,crnr4 As Point3d
   Dim crnrs As Point3d() = {crnr1,crnr2,crnr3,crnr4}
   resizePlaneBuilder1.GetCornerPoints(crnrs)
   msgbox(crnrs(0).ToString)
   msgbox(crnrs(1).ToString)
   msgbox(crnrs(2).ToString)
   msgbox(crnrs(3).ToString)

   Dim d As Double = 3.0
   Dim p As Double = 1.0
   Dim n As Double = -1.0
   d = d/2
   Dim corner1 As Point3d = New Point3d(n*d, n*d, 0.0)
   Dim corner2 As Point3d = New Point3d(p*d, n*d, 0.0)
   Dim corner3 As Point3d = New Point3d(p*d, p*d, 0.0)
   Dim corner4 As Point3d = New Point3d(n*d, p*d, 0.0)
   Dim corners As Point3d() = {corner1,corner2,corner3,corner4}

   resizePlaneBuilder1.SetCornerPoints(corners)
    
   resizePlaneBuilder1.Commit()
   resizePlaneBuilder1.Destroy()

'====================== Resize plane end ======================

  Dim Xdir As New Point3d(1,0,0)
  Xdir = WCS2Abs(Xdir)
  Dim Xaxis As DatumAxis
  Xaxis = workPart.Datums.CreateFixedDatumAxis(wcs_origin,Xdir)
  Xaxis.SetName("_X_")
  ufs.Modl.AskObjectFeat(Xaxis.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  Dim x_AxisFeat As Features.DatumAxisFeature = CType(featdatum_obj,Features.DatumAxisFeature)
  x_AxisFeat.SetName("_X_")

  Dim Ydir As New Point3d(0,1,0)
  Ydir = WCS2Abs(Ydir)
  Dim Yaxis As DatumAxis
  Yaxis = workPart.Datums.CreateFixedDatumAxis(wcs_origin,Ydir)
  Yaxis.SetName("_Y_")
  ufs.Modl.AskObjectFeat(Yaxis.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  Dim y_AxisFeat As Features.DatumAxisFeature = CType(featdatum_obj,Features.DatumAxisFeature)
  y_AxisFeat.SetName("_Y_")

 End Sub

'---------------------------------------------------------------------

 Function WCS2Abs(ByVal inPt As Point3d) As Point3d
  
  Dim ufs As UFSession = UFSession.GetUFSession()
   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.Immediately  

 End Function  
'---------------------------------------------------------------------

End Module
 
I played around with the resize plane builder a little bit. It seems buggy or at least does not behave in an expected way. Below is a quick concept work-around that does not use the resize plane builder. Note that you can get the corner points from the datum plane object and you can specify the corner points in the datum plane builder object. By rewriting the code, you could create the plane and specify the corner points through the use of the datum plane builder object. Also note that this quick example assumes the WCS is set to absolute; if this is not the case, the new corner points will probably need to be translated to the desired csys.

Code:
Option Strict Off  
Imports System  
Imports System.IO  
Imports System.Windows.Forms  
Imports NXOpen  
Imports NXOpen.UF  
Imports NXOpenUI  
Imports Microsoft.VisualBasic
Imports System.Environment

Imports NXOpen.Features

Module create_pattern_sketch_from_list_and_file_options

 Sub Main

  Dim theSession As Session = Session.GetSession()
  Dim workPart As Part = theSession.Parts.Work
  Dim displayPart As Part = theSession.Parts.Display
  Dim ufs As UFSession = UFSession.GetUFSession()
  Dim lw as listingwindow = theSession.ListingWindow
  lw.open
  
  Dim wcs_origin As Point3d = workPart.WCS.Origin
  Dim wcs_matrix3x3 As Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element
  Dim featdatum_tag As Tag
  Dim featdatum_obj As NXObject

  Dim xyplane As DatumPlane
  xyplane = workPart.Datums.CreateFixedDatumPlane(wcs_origin,wcs_matrix3x3)
  'xyplane.SetName("_XY_")
  ufs.Modl.AskObjectFeat(xyplane.Tag,featdatum_tag)
  featdatum_obj = NXOpen.Utilities.NXObjectManager.Get(featdatum_tag)
  'lw.writeline("featdatum_obj type: " & featdatum_obj.Gettype.tostring)
  
  dim corner1 as point3d
  dim corner2 as point3d
  dim corner3 as point3d
  dim corner4 as point3d
  
  xyplane.GetCornerPoints(corner1, corner2, corner3, corner4)
  
  lw.writeline("initial corner 1: " & corner1.ToString)
  lw.writeline("initial corner 2: " & corner2.ToString)
  lw.writeline("initial corner 3: " & corner3.ToString)
  lw.writeline("initial corner 4: " & corner4.ToString)
  
  corner1.X = -4.0
  corner1.Y = -4.0
  
  corner2.X = 4.0
  corner2.Y = -4.0
  
  corner3.X = 4.0
  corner3.Y = 4.0
  
  corner4.X = -4.0
  corner4.Y = 4.0
  
  dim datumPlaneBuilder1 as features.DatumPlaneBuilder
  datumPlaneBuilder1 = workpart.Features.CreateDatumPlaneBuilder(featdatum_obj)
  datumPlaneBuilder1.SetCornerPoints(corner1, corner2, corner3, corner4)
  datumPlaneBuilder1.commit
  datumPlaneBuilder1.destroy
  
 End Sub

End Module

www.nxjournaling.com
 

Cowski thank you a lot

I see in the datumplane class there is also SetCornerPoints method
do I can use thise instead of building a DatumPlaneBuilder

xyplane.SetCornerPoints(corner1, corner2, corner3, corner4)

and also is the corners value is from the abs origin the class not say anything about

Thank you again
 
I created a datum plane then recorded a journal while resizing the plane, expecting it to return code showing how to use the resize plane builder. Instead, it returned code using the datum plane builder. I played around with the resize plane builder, but could not get it to do anything useful. That's when I went back to the recorded code and resized the plane with the datum plane builder. I didn't even notice that the plane object also has a .SetCornerPoints method; it should work, at least it is worth a try.

www.nxjournaling.com
 

I succeeded to record it but I'm still stacked

Code:
' NX 8.5.3.3
' Journal created by I'm on Wed Apr 26 18:30:55 2017 Jerusalem Daylight Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String) 

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: Edit->Feature->Resize Datum Plane...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim nullFeatures_Feature As Features.Feature = Nothing


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

Dim resizePlaneBuilder1 As Features.ResizePlaneBuilder
resizePlaneBuilder1 = workPart.Features.CreateResizePlaneBuilder(nullFeatures_Feature)

theSession.SetUndoMarkName(markId1, "Resize Datum Plane Dialog")

Dim datumPlane1 As DatumPlane = CType(workPart.Datums.FindObject("FIXED_DATUM_PLANE(4)"), DatumPlane)

resizePlaneBuilder1.Plane.Value = datumPlane1

Dim coordinates1 As Point3d = New Point3d(19.0, 40.0, 0.0)
Dim point1 As Point
point1 = workPart.Points.CreatePoint(coordinates1)

Dim coordinates2 As Point3d = New Point3d(19.0, 40.0, 0.0)
Dim point2 As Point
point2 = workPart.Points.CreatePoint(coordinates2)

workPart.Points.DeletePoint(point1)

Dim coordinates3 As Point3d = New Point3d(19.0, 40.0, 0.0)
Dim point3 As Point
point3 = workPart.Points.CreatePoint(coordinates3)

workPart.Points.DeletePoint(point2)

Dim coordinates4 As Point3d = New Point3d(12.5056039948752, 46.7561165003932, -1.86223808775998)
Dim point4 As Point
point4 = workPart.Points.CreatePoint(coordinates4)

workPart.Points.DeletePoint(point3)

Dim coordinates5 As Point3d = New Point3d(19.0, 40.0, 0.0)
Dim point5 As Point
point5 = workPart.Points.CreatePoint(coordinates5)

workPart.Points.DeletePoint(point4)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Resize Datum Plane")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Resize Datum Plane")

resizePlaneBuilder1.ResizeDuringUpdate = False

Dim cornerpoints1(3) As Point3d
cornerpoints1(0) = New Point3d(9.38738304061681, 30.0, -2.75637355816999)
cornerpoints1(1) = New Point3d(28.6126169593832, 30.0, 2.75637355816999)
cornerpoints1(2) = New Point3d(28.6126169593832, 50.0, 2.75637355816999)
cornerpoints1(3) = New Point3d(9.38738304061681, 50.0, -2.75637355816999)
resizePlaneBuilder1.SetCornerPoints(cornerpoints1)

Dim nXObject1 As NXObject
nXObject1 = resizePlaneBuilder1.Commit()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "Resize Datum Plane")

resizePlaneBuilder1.Destroy()

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module
 
Sorry I forgot to say that to build this I used
datum right click and chose the resize icon.
 
Update: the resize plane builder can be made to work. The API reference shows that you should pass a reference to a datum plane feature to the builder (NX 9 docs), but the journal recorder disagrees. The journal recorder passes in a null feature and specifies the plane to be the plane of the datum plane feature; this method works. Note that the corner points will need to be transformed to absolute coordinates.

www.nxjournaling.com
 
Hi Cowski
Thank you for the update.(make it clear for me which one work pass by reference or pass by null ?)

I found this code in the internet
can you learn something from it

Thank you again

Code:
def datum_plane(self, datum_origin, datum_normals, datum_corners):
        
        self.theLw.WriteLine('\n')
        self.theLw.WriteLine("Datum Plane function")

        
        for i in range(len(datum_corners)):
            datumPlaneBuilder = workPart.Features.CreateDatumPlaneBuilder(NXOpen.Features.Feature.Null)
            self.theLw.WriteLine('\n')
            self.theLw.WriteLine('\n')
            self.theLw.WriteLine('\n')

            #returns an NX.Open Plane object, from DatumPlaneBuilder class
            plane = datumPlaneBuilder.GetPlane()
            geom = []
            plane.SetGeometry(geom)
            plane.SetUpdateOption(NXOpen.SmartObject.UpdateOption.WithinModeling)

            #using dist from plane to origin and a surface normal to create the plane
            plane.SetMethod(NXOpen.PlaneTypes.MethodType.Coefficients)
            
            origin = NXOpen.Point3d(datum_origin[i][0], datum_origin[i][1], datum_origin[i][2])
            
            plane.Origin = origin

            #a, b, c coefficients
            normal = NXOpen.Vector3d(datum_normals[i][0], datum_normals[i][1], datum_normals[i][2])
            plane.Normal = normal
            plane.Evaluate()

            datumPlaneBuilder.ResizeDuringUpdate=False
            
            datum_feat=datumPlaneBuilder.CommitFeature()
            
            #need to get journal Identifier of datum plane and pass it as an object
            self.theLw.WriteLine("Datum feature is "+str(datum_feat.JournalIdentifier))
            
            #create instance of resize plane
            resizePlaneBuilder = workPart.Features.CreateResizePlaneBuilder(NXOpen.Features.Feature.Null)   
            datum_object = workPart.Datums.FindObject(str(datum_feat.JournalIdentifier))
            resizePlaneBuilder.Plane.Value = datum_object

            resizePlaneBuilder.ResizeDuringUpdate = False
    
            corner_pts = [None] * 4 
            
            #multiplying by a scale factor just moves the planes further out
            # to adjust size, recalculate your corner points
            corner_pts[0] = NXOpen.Point3d(datum_corners[i][0][0],datum_corners[i][0][1],datum_corners[i][0][2])
            corner_pts[1] = NXOpen.Point3d(datum_corners[i][1][0],datum_corners[i][1][1],datum_corners[i][1][2])
            corner_pts[2] = NXOpen.Point3d(datum_corners[i][3][0],datum_corners[i][3][1],datum_corners[i][3][2])
            corner_pts[3] = NXOpen.Point3d(datum_corners[i][2][0],datum_corners[i][2][1],datum_corners[i][2][2])
            resizePlaneBuilder.SetCornerPoints(corner_pts)
    
            resizePlaneBuilder.Commit()
            resizePlaneBuilder.Destroy()

            datumPlaneBuilder.Destroy()
 
The code creates a datum plane then resizes it according to the corner coordinates it was given. What issue are you having with the code?

www.nxjournaling.com
 
Cowski I'lm very thank you

I don't successed to do this task
and I want to trying this.

PLEASE can translate it to VB
for me

Thank you in advanced
 
The below is essentially a straight conversion from the Python code to VB. I make no guarantees on its fitness for use.

Code:
    Sub CreateDatumPlanes(ByVal datumOrigins As List(Of Point3d),
                         ByVal datumNormals As List(Of Vector3d),
                         ByVal datumCorners As List(Of List(Of Point3d)))

        For i As Integer = 0 To datumOrigins.Count - 1
            Dim dPlaneBuilder1 As Features.DatumPlaneBuilder = theSession.Parts.Work.Features.CreateDatumPlaneBuilder(Nothing)
            Dim thePlane As Plane = dPlaneBuilder1.GetPlane
            Dim geom(-1) As NXObject
            thePlane.SetGeometry(geom)
            thePlane.SetUpdateOption(SmartObject.UpdateOption.WithinModeling)
            thePlane.SetMethod(PlaneTypes.MethodType.Coefficients)

            thePlane.Origin = datumOrigins.Item(i)

            thePlane.Normal = datumNormals.Item(i)
            thePlane.Evaluate()

            dPlaneBuilder1.ResizeDuringUpdate = False

            Dim datumFeature As Features.DatumPlaneFeature = dPlaneBuilder1.CommitFeature
            dPlaneBuilder1.Destroy()

            Dim resizePlaneBuilder1 As Features.ResizePlaneBuilder = theSession.Parts.Work.Features.CreateResizePlaneBuilder(Nothing)
            resizePlaneBuilder1.Plane.Value = datumFeature.DatumPlane

            resizePlaneBuilder1.SetCornerPoints(datumCorners.Item(i).ToArray)
            resizePlaneBuilder1.Commit()
            resizePlaneBuilder1.Destroy()

        Next

    End Sub

www.nxjournaling.com
 
Cowski thank you for the time
you invested for me.
 
Hi Cowski

I played around with your ("I played around with the resize plane builder a little bit")
and finally arrived to this working journal. (your help is active and I'm thank you)

now I go to do the same for the X,Y axis (I hope to succede)
my goal is to adjust a working plane to my sizes

Code:
Option Strict Off  
Imports System  
Imports System.IO  
Imports System.Windows.Forms  
Imports NXOpen  
Imports NXOpen.UF  
Imports NXOpenUI  
Imports Microsoft.VisualBasic
Imports System.Environment

Imports NXOpen.Features

Module create_pattern_sketch_from_list_and_file_options

 Sub Main

  Dim theSession As Session = Session.GetSession()
  Dim workPart As Part = theSession.Parts.Work
  Dim displayPart As Part = theSession.Parts.Display
  Dim ufs As UFSession = UFSession.GetUFSession()
  Dim lw as listingwindow = theSession.ListingWindow
  lw.open
  
  Dim wcs_origin As Point3d = workPart.WCS.Origin
  Dim wcs_matrix3x3 As Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element
  Dim featdatum_tag As Tag
  Dim featdatum_obj As NXObject

  Dim xyplane As DatumPlane
  xyplane = workPart.Datums.CreateFixedDatumPlane(wcs_origin,wcs_matrix3x3)
  
  dim corner1 as point3d
  dim corner2 as point3d
  dim corner3 as point3d
  dim corner4 as point3d
  
  xyplane.GetCornerPoints(corner1, corner2, corner3, corner4)
  
  lw.writeline("initial corner 1: " & corner1.ToString)
  lw.writeline("initial corner 2: " & corner2.ToString)
  lw.writeline("initial corner 3: " & corner3.ToString)
  lw.writeline("initial corner 4: " & corner4.ToString)

  Dim plane_size As Double = 40
  
  corner1.X = -0.5*plane_size
  corner1.Y = -0.5*plane_size
  corner1.Z = 0
  
  corner2.X = 0.5*plane_size
  corner2.Y = -0.5*plane_size
  corner2.Z = 0
  
  corner3.X = 0.5*plane_size
  corner3.Y = 0.5*plane_size
  corner3.Z = 0
  
  corner4.X = -0.5*plane_size
  corner4.Y = 0.5*plane_size
  corner4.Z = 0

  corner1 = WCS2Abs(corner1)
  corner2 = WCS2Abs(corner2)
  corner3 = WCS2Abs(corner3)
  corner4 = WCS2Abs(corner4)

  xyplane.SetCornerPoints(corner1, corner2, corner3, corner4)

  xyplane.GetCornerPoints(corner1, corner2, corner3, corner4)
  lw.writeline("=======================================================")  
  lw.writeline("last corner 1: " & corner1.ToString)
  lw.writeline("last corner 2: " & corner2.ToString)
  lw.writeline("last corner 3: " & corner3.ToString)
  lw.writeline("last corner 4: " & corner4.ToString)
    
 End Sub

'---------------------------------------------------------------------

 Function WCS2Abs(ByVal inPt As Point3d) As Point3d
  
  Dim ufs As UFSession = UFSession.GetUFSession()
   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   

'---------------------------------------------------------------------

End Module
 

Final solution

Code:
'
Option Strict Off  
Imports System  
Imports System.IO  
Imports System.Windows.Forms  
Imports NXOpen  
Imports NXOpen.UF  
Imports NXOpenUI  
Imports Microsoft.VisualBasic
Imports System.Environment
Imports NXOpenUI.NXInputBox

Module create_plane_and_axis_on_xcyc_xczc_yczc

 Sub Main

  Dim theSession As Session = Session.GetSession()
  Dim workPart As Part = theSession.Parts.Work
  Dim displayPart As Part = theSession.Parts.Display
  Dim dpsize As Double = 0
  Dim dPlane As String
  Dim mPlaneDir As Integer = 1
  Dim mPlaneSize As Double = 25
        
  Dim wcs_origin As Point3d = workPart.WCS.Origin
  Dim wcs_matrix3x3 As Matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element

  Dim f1 As UserInputDialog = New UserInputDialog
  f1.dPlaneDir = mPlaneDir
  f1.dPlaneSize = mPlaneSize
  If f1.ShowDialog() <> DialogResult.OK Then Exit Sub
  mPlaneDir = f1.dPlaneDir
  mPlaneSize = f1.dPlaneSize

  Dim my_markId1 As Session.UndoMarkId = _
               theSession.SetUndoMark(Session.MarkVisibility.Visible,"Create plane")
 
  If mPlaneDir = 1 Then
      dPlane = "XY"
  Else If mPlaneDir = 2 Then
      dPlane = "XZ"
  Else If mPlaneDir = 3 Then
      dPlane = "YZ"
  End If
  dpsize = mPlaneSize

  Dim wcs_org_matrix3x3 As Matrix3x3 = CopyMatrix3x3(wcs_matrix3x3) 'save original WCS direction

  If dplane = "XZ" Then
     displayPart.WCS.Rotate(WCS.Axis.XAxis, 90) ' rotate XY to simulate the XZ plane
  End If
  If dplane = "YZ" Then
     displayPart.WCS.Rotate(WCS.Axis.YAxis, 90) ' rotate XY to simulate the YZ plane
  End If
  wcs_matrix3x3 = workPart.WCS.CoordinateSystem.Orientation.Element 'get matrix of rotation

'---- XY segment start

  Dim xyplane As DatumPlane
  xyplane = workPart.Datums.CreateFixedDatumPlane(wcs_origin,wcs_matrix3x3)  
  If dplane = "XY" Then
    xyplane.Feature.SetName("_XY_")
  Else If dplane = "XZ" Then
    xyplane.Feature.SetName("_XZ_")
  Else If dplane = "YZ" Then
    xyplane.Feature.SetName("_YZ_")
  End If
  Dim corner1,corner2,corner3,corner4 As point3d  
  xyplane.GetCornerPoints(corner1, corner2, corner3, corner4)  
  Dim plane_size As Double = dpsize  
  corner1.X = -0.5*plane_size : corner1.Y = -0.5*plane_size : corner1.Z = 0  
  corner2.X = 0.5*plane_size : corner2.Y = -0.5*plane_size : corner2.Z = 0  
  corner3.X = 0.5*plane_size : corner3.Y = 0.5*plane_size : corner3.Z = 0  
  corner4.X = -0.5*plane_size : corner4.Y = 0.5*plane_size : corner4.Z = 0  
  corner1 = WCS2Abs(corner1) : corner2 = WCS2Abs(corner2)  
  corner3 = WCS2Abs(corner3) : corner4 = WCS2Abs(corner4)
  xyplane.SetCornerPoints(corner1, corner2, corner3, corner4)

  workPart.WCS.SetOriginAndMatrix (wcs_origin,wcs_org_matrix3x3) 'restore original WCS direction

'---- X segment start

  Dim Xdir As Point3d
  If dplane = "XY" Then  
     Xdir = New Point3d(1,0,0)
  Else If dplane = "XZ" Then
     Xdir = New Point3d(1,0,0)
  Else If dplane = "YZ" Then
     Xdir = New Point3d(0,1,0)
  End If
  Xdir = WCS2Abs(Xdir)
  Dim Xaxis As DatumAxis
  Xaxis = workPart.Datums.CreateFixedDatumAxis(wcs_origin,Xdir)
  Dim start1,end1 As Point3d
  Xaxis.GetEndPoints(start1,end1)
  If dplane = "XY" Then  
    Xaxis.Feature.SetName("_X_") 
    end1.X = 0.75*dpsize : end1.Y = 0 : end1.Z = 0
  Else If dplane = "XZ" Then
    Xaxis.Feature.SetName("_X_") 
    end1.X = 0.75*dpsize : end1.Y = 0 : end1.Z = 0
  Else If dplane = "YZ" Then
    Xaxis.Feature.SetName("_Y_") 
    end1.X = 0 : end1.Y = 0.75*dpsize  : end1.Z = 0
  End If
  end1 = WCS2Abs(end1)
  Xaxis.SetEndPoints(start1,end1)

'---- Y segment start
 
  Dim Ydir As Point3d
  If dplane = "XY" Then  
     Ydir = New Point3d(0,1,0)
  Else If dplane = "XZ" Then
     Ydir = New Point3d(0,0,1)
  Else If dplane = "YZ" Then
     Ydir = New Point3d(0,0,1)
  End If
  Ydir = WCS2Abs(Ydir)
  Dim Yaxis As DatumAxis
  Yaxis = workPart.Datums.CreateFixedDatumAxis(wcs_origin,Ydir)
  If dplane = "XY" Then  
    Yaxis.Feature.SetName("_Y_") 
    end1.X = 0 : end1.Y = 0.75*dpsize : end1.Z = 0
  Else If dplane = "XZ" Then
    Yaxis.Feature.SetName("_Z_") 
    end1.X = 0 : end1.Y = 0 : end1.Z = 0.75*dpsize
  Else If dplane = "YZ" Then
    Yaxis.Feature.SetName("_Z_") 
    end1.X = 0 : end1.Y = 0 : end1.Z = 0.75*dpsize
  End If
  end1 = WCS2Abs(end1)
  Yaxis.SetEndPoints(start1,end1)

 End Sub
'=============================   Routines   ====================================
 Function WCS2Abs(ByVal inPt As Point3d) As Point3d  
  Dim ufs As UFSession = UFSession.GetUFSession()
   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   
'---------------------------------------------------------------------
 Function CopyMatrix3x3(ByVal org_mat As Matrix3x3) As Matrix3x3
   Dim mx As Matrix3x3
     mx.Xx = org_mat.Xx
     mx.Xy = org_mat.Xy
     mx.Xz = org_mat.Xz
     mx.Yx = org_mat.Yx
     mx.Yy = org_mat.Yy
     mx.Yz = org_mat.Yz
     mx.Zx = org_mat.Zx
     mx.Zy = org_mat.Zy
     mx.Zz = org_mat.Zz
   Return mx
 End Function
'---------------------------------------------------------------------
 Public Function GetUnloadOption(ByVal dummy As String) As Integer  
  'Unloads the image when the NX session terminates
  GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately  
 End Function  

End Module
'*********************************  Class  ****************************
' Form Class
' A form for the user to input data
' 
Public Class UserInputDialog
             Inherits System.Windows.Forms.Form

 Private Sub UserInputDialog_Load(ByVal sender As System.Object, _
                                     ByVal e As System.EventArgs) Handles MyBase.Load

   'ttHint.SetToolTip(Me.XZRadioButton, "Create plane on XC-ZC")
   'ttHint.SetToolTip(CancelButton, "Cancel Button")

   If dPlaneDir = 1 Then
     XYRadioButton.Checked = True
   Else If dPlaneDir = 2
     XZRadioButton.Checked = True
   Else
     YZRadioButton.Checked = True
   End If
   PlaneSizeTextBox.Text = dPlaneSize
 End Sub

    ''''''''''''
    ' Public Variables
    ''''''''''''

    Public dPlaneList() As String
    Public dPlaneDir As Integer
    Public dPlaneSize As Double
    Public dPttrnFileName As String
    Public dSelectedPlane As String
    Public dSelectedIndex As Integer

    ''''''''''''
    ' Public Method
    ''''''''''''

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
 
       'NX icon in top left side
        NXOpenUI.FormUtilities.SetApplicationIcon(Me)
        NXOpenUI.FormUtilities.ReparentForm(Me)

    End Sub


    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'Inserted by the Windows Form Designer
    Friend WithEvents ttHint As System.Windows.Forms.ToolTip
    Friend WithEvents PlaneSizeLabel As System.Windows.Forms.Label
    Friend WithEvents OkButton As System.Windows.Forms.Button
    Friend WithEvents XYRadioButton As System.Windows.Forms.RadioButton
    Friend WithEvents XZRadioButton As System.Windows.Forms.RadioButton
    Friend WithEvents YZRadioButton As System.Windows.Forms.RadioButton
    Friend WithEvents CancelButton As System.Windows.Forms.Button
    Private WithEvents PlaneSizeTextBox As NumericTextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ttHint = New System.Windows.Forms.ToolTip(Me.components)
        Me.OkButton = New System.Windows.Forms.Button
        Me.CancelButton = New System.Windows.Forms.Button
        Me.XYRadioButton = New System.Windows.Forms.RadioButton
        Me.XZRadioButton = New System.Windows.Forms.RadioButton
        Me.YZRadioButton = New System.Windows.Forms.RadioButton
        Me.PlaneSizeLabel = New System.Windows.Forms.Label
        Me.PlaneSizeTextBox = New NumericTextBox(1.0)
        Me.SuspendLayout()
        '
        'OkButton
        '
        Me.OkButton.DialogResult = DialogResult.OK
        Me.OkButton.Location = New System.Drawing.Point(30, 90)
        Me.OkButton.Name = "OkButton"
        Me.OkButton.Size = New System.Drawing.Size(130, 26)
        Me.OkButton.TabIndex = 11
        Me.OkButton.Text = "OK"
        '
        'CancelButton
        '
        Me.CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.CancelButton.Location = New System.Drawing.Point(254, 90)
        Me.CancelButton.Name = "CancelButton"
        Me.CancelButton.Size = New System.Drawing.Size(90, 26)
        Me.CancelButton.TabIndex = 12
        Me.CancelButton.Text = "Cancel"
        '
        'XYRadioButton
        '
        Me.XYRadioButton.Location = New System.Drawing.Point(10, 5)
        Me.XYRadioButton.Name = "XYRadioButton"
        Me.XYRadioButton.Size = New System.Drawing.Size(240, 28)
        Me.XYRadioButton.TabIndex = 0
        Me.XYRadioButton.Text = "XY Create plane on XC-YC"
        '
        'XZRadioButton
        '
        Me.XZRadioButton.Location = New System.Drawing.Point(10, 30)
        Me.XZRadioButton.Name = "XZRadioButton"
        Me.XZRadioButton.Size = New System.Drawing.Size(240, 27)
        Me.XZRadioButton.TabIndex = 1
        Me.XZRadioButton.Text = "XZ Create plane on XC-ZC"
        '
        'YZRadioButton
        '
        Me.YZRadioButton.Location = New System.Drawing.Point(10, 55)
        Me.YZRadioButton.Name = "YZRadioButton"
        Me.YZRadioButton.Size = New System.Drawing.Size(240, 27)
        Me.YZRadioButton.TabIndex = 1
        Me.YZRadioButton.Text = "YZ Create plane on YC-ZC"
        '
        'PlaneSizeLabel
        '
        Me.PlaneSizeLabel.Location = New System.Drawing.Point(232, 30)
        Me.PlaneSizeLabel.Name = "PlaneSizeLabel"
        Me.PlaneSizeLabel.Size = New System.Drawing.Size(72, 19)
        Me.PlaneSizeLabel.TabIndex = 7
        Me.PlaneSizeLabel.Text = "Plane Size"
        '
        'PlaneSizeTextBox
        '
        Me.PlaneSizeTextBox.Location = New System.Drawing.Point(302, 30)
        Me.PlaneSizeTextBox.Name = "PlaneSizeTextBox"
        Me.PlaneSizeTextBox.Size = New System.Drawing.Size(60, 20)
        Me.PlaneSizeTextBox.TabIndex = 9
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.CancelButton = Me.CancelButton
        Me.ClientSize = New System.Drawing.Size(374, 130)
        Me.Controls.Add(Me.PlaneSizeTextBox)
        Me.Controls.Add(Me.PlaneSizeLabel)
        Me.Controls.Add(Me.XYRadioButton)
        Me.Controls.Add(Me.XZRadioButton)
        Me.Controls.Add(Me.YZRadioButton)
        Me.Controls.Add(Me.CancelButton)
        Me.Controls.Add(Me.OkButton)
        Me.Name = "Form1"
        Me.Text = "Create Plane     Company : Dev name"
        Me.ResumeLayout(False)

    End Sub

'===========================================================================

    ' A TextBox that only accepts floating point numbers greater than 1.0e-5
    Private Class NumericTextBox
        Inherits TextBox

        Public Sub New(ByVal x As Double)
            MyBase.New()
            m_value = x
            Me.Text = x.ToString()
        End Sub

        Public ReadOnly Property Value() As Double
            Get
                Return m_value
            End Get
        End Property

        Public Overrides Property Text() As String
            Get
                Return MyBase.Text
            End Get
            Set(ByVal text As String)
                m_value = Double.Parse(text)
                MyBase.Text = text
            End Set
        End Property

        Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)

            ' Accept the input only if it is a floating point number
            ' greater than 1.0e-5
            Dim valid As Boolean = False
            Dim value As Double
            Try
                value = Double.Parse(Me.Text)
                If value > 0.00001 Then valid = True
            Catch ex As Exception
            End Try
            If valid Then
                m_value = value
            Else
                Me.Text = m_value.ToString
                e.Cancel = True
                MessageBox.Show( _
                "Invalid Input" & NewLine & "A positive number greater than 1.0e-5 is required")
            End If
            MyBase.OnValidating(e)

        End Sub

        Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
            ' If Enter is pressed, move to the next control
            ' (Attempting to move to the next control will automatically cause
            ' OnValidating to be called for this control, and the move will
            ' not be permitted if validation fails.)
            If e.KeyChar = Chr(13) Then
                Me.Parent.SelectNextControl(Me, True, False, False, True)
            End If
            MyBase.OnKeyPress(e)
        End Sub

        Private m_value As Double

    End Class

'===========================================================================

    Private Sub XYRadioButton_Click(ByVal sender As System.Object, _
                      ByVal e As System.EventArgs) Handles XYRadioButton.Click
        dPlaneDir = 1
    End Sub

    Private Sub XZRadioButton_Click(ByVal sender As System.Object, _
                     ByVal e As System.EventArgs) Handles XZRadioButton.Click
        dPlaneDir = 2
    End Sub

    Private Sub YZRadioButton_Click(ByVal sender As System.Object, _
                     ByVal e As System.EventArgs) Handles YZRadioButton.Click
        dPlaneDir = 3
    End Sub

    Private Sub OkButton_Click(ByVal sender As Object, _
                             ByVal e As System.EventArgs) Handles OkButton.Click
        dPlaneSize = PlaneSizeTextBox.Value
    End Sub

End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor