2022年8月29日星期一

NX vb

 


Option Strict Off


Imports System

Imports NXOpen

Imports NXOpen.UF

Imports NXOpenUI


Module NXJournal

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 markId3 As Session.UndoMarkId

markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Edit Object Display")


Dim displayModification1 As DisplayModification

displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.ApplyToAllFaces = False

displayModification1.ApplyToOwningParts = False

displayModification1.NewColor = 103


Dim objects1(0) As DisplayableObject

'Dim body1 As Body = CType(workPart.Bodies.FindObject("BLOCK(0)"), Body)

Dim body1 As Body = SelectSolid()

objects1(0) = body1

displayModification1.Apply(objects1)

displayModification1.Dispose()


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


End Module




Option Explicit On 

Imports Snap 

Module MyProgram 

Sub Main() 

Dim radius As Double = 3.75 

Dim area As Double 

area = CircleArea(radius) ' Call function to calculate area 

Dim message As String = "Area is: " 

InfoWindow.WriteLine(message & area) ' Write out the area value 

End Sub 

' Function to calculate the area of a circle 

Function CircleArea(r As Double) As Double 

Dim pi As Double = System.Math.PI 

Dim area As Double = pi * r * r 

Return area 

End Function 

End Module






Option Explicit off

Imports Snap,snap.create

Module MyProgram 

Sub Main() 



p1 = Point(5,7) ' Create a point called p1 at x=5, y=7, z=0

p2 = Point(9,2) ' Create a point called p2 at x=9, y=2, z=0

Line(p1, p2) ' Create a line between p1 and p2


End sub

End Module



Option explicit off

imports snap, snap.Create


Module SnapSample

Sub Main()

p1 = Point (5,7)

p2 = Point (9,2)

p3 = Point (15,6)

Line (p1, p2)

End sub

End Module




Option Explicit off

Imports Snap,snap.create

Module MyProgram 

Sub Main() 



p1 = Point(5,7) ' Create a point called p1 at x=5, y=7, z=0

p2 = Point(9,2) ' Create a point called p2 at x=9, y=2, z=0

Line(p1, p2) ' Create a line between p1 and p2


End sub

End Module







''''Create a cylinder

Imports System

Imports NXOpen

Module Points_01

    Sub Main()

       Dim theSession As Session = Session.GetSession()

       Dim workPart As Part = theSession.Parts.Work

       Dim lw As ListingWindow = theSession.ListingWindow

       lw.Open()

       'create Point3d using the constructor

       Dim myPt1 As New Point3d(0, 0, 0)

       'create center point

       Dim ptObj As Point

       ptObj = workPart.Points.CreatePoint(myPt1)

       ptObj.SetVisibility(SmartObject.VisibilityOption.Visible)

       Dim myVector As Vector3d

       'enter values for vector

       myVector.X = (1)

       myVector.Y = (2)

       myVector.Z = (3)

       'create offset

       Dim myOffset As Offset

       myOffset = workPart.Offsets.CreateOffset(ptObj.Coordinates, myVector, SmartObject.UpdateOption.WithinModeling)

       'create new point offset from first point

       Dim offsetPoint As Point

       offsetPoint = workPart.Points.CreatePoint(myOffset, ptObj, SmartObject.UpdateOption.WithinModeling)

       offsetPoint.RemoveParameters()

       offsetPoint.SetVisibility(SmartObject.VisibilityOption.Visible)

       lw.Close()

' Create circle

Dim curves As NXOpen.CurveCollection = workPart.Curves

Dim center As NXOpen.Point3d

center = New Point3d(-25,18,0)

Dim pi as Double =System.Math.PI

Dim twopi As Double = 2*pi

Dim length As Double = 8

Dim axisX = New Vector3d(1,0,0)

Dim axisY = New Vector3d(0,1,0)

Dim circle = workPart.curves.CreateArc(center, axisX, axisY, length, 0, twopi)

Dim ctol = 0.0095 ' Chaining tolerance

Dim dtol = 0.01 ' Distance tolerance

Dim atol = 0.5 ' Angle tolerance

'Create a circular section

Dim circ As NXOpen.Section = workPart.Sections.CreateSection(ctol, dtol, atol)

Dim helpPoint As New NXOpen.Point3d(0,0,0)

Dim nullObj As NXOpen.NXObject = Nothing

Dim noChain As Boolean = False

Dim createMode As NXOpen.Section.Mode = Section.Mode.Create

' Create rules to add the circle to the section

Dim circl As NXOpen.CurveDumbRule = workPart.ScRuleFactory.CreateRuleBaseCurveDumb({circle})

circ.AddToSection({circl}, circle, nullObj, nullObj, helpPoint, createMode, noChain)

Dim builder = workPart.Features.CreateExtrudeBuilder(Nothing)

builder.Section = circ

'Define the direction of the Extrude

Dim origin As New NXOpen.Point3d(0,0,0)

Dim axisZ As New NXOpen.Vector3d(0,0,1)

Dim updateOption = SmartObject.UpdateOption.DontUpdate

builder.Direction = workPart.Directions.CreateDirection(origin, axisZ, updateOption)

builder.Limits.StartExtend.Value.RightHandSide = "0"

builder.Limits.EndExtend.Value.RightHandSide = "50"

Dim extrudeFeature As NXOpen.Features.Extrude = builder.CommitFeature

builder.Destroy

'Get the displayable object of the Extrude feature

Dim bodies As NXOpen.Body() = extrudeFeature.GetBodies

' Change its color

bodies(0).Color = 178 ' Usually red, by default

'Show object

 bodies(0).RedisplayObject

    End Sub

End Module






Imports System

Imports NXOpen

Module NXJournal

Sub Main()

Dim theSession = NXOpen.Session.GetSession()

Dim workPart As NXOpen.Part = theSession.Parts.Work


' if nx is not in the modeling application, switch to it

If theSession.ApplicationName IsNot "UG_APP_MODELING" Then theSession.ApplicationSwitchImmediate("UG_APP_MODELING")


' create simple points (not smartpoints)

Dim p0 As New NXOpen.Point3d(1,2,3)

Dim p1 As New NXOpen.Point3d(4,7,5)


' create a line in the part

Dim line1 As NXOpen.Line = workPart.Curves.CreateLine(p0, p1)


'set the layer

line1.layer = 1


' fit to the line

workPart.ModelingViews().WorkView().Fit()


End Sub

End Module




Option Strict Off  

Imports System  

Imports NXOpen  


Module NXJournal  

Sub Main  


Dim theSession As Session = Session.GetSession()  

Dim workPart As Part = theSession.Parts.Work  


Const DatumLayer as Integer = 61  

Const CurveLayer as Integer = 256 

Const SketchLayer as Integer = 250

Const SheetLayer as Integer = 256


'move datums

for each datumObj as DisplayableObject in workPart.Datums  

if typeof(datumObj) is DatumPlane then  

datumObj.Layer = DatumLayer  

datumObj.RedisplayObject  

end if  

next  


'move curves

for each curveObj as Curve in workPart.Curves  

curveObj.Layer = CurveLayer  

curveObj.RedisplayObject  

next  


'move sketches

'do this after moving curves, sketch curves will update to sketch layer

for each sketchObj as Sketch in workPart.Sketches  

sketchObj.Activate(False)  

sketchObj.Layer = SketchLayer  

sketchObj.RedisplayObject  

sketchObj.Deactivate(False, Sketch.UpdateLevel.SketchOnly)  

next  


'move sheet bodies

for each bodyObj as Body in workPart.Bodies  

if bodyObj.IsSheetBody then  

bodyObj.Layer = SheetLayer  

bodyObj.RedisplayObject  

end if  

next  


End Sub  

End Module 


Option Strict Off
Imports NXOpen
Imports NXOpenUI
Imports NXOpen.Utilities
Module move_solid_bodies_to_new_layer
 Dim s As Session = Session.GetSession()
 Sub Main()
 Dim workPart As Part = s.Parts.Work
 Dim bodies As BodyCollection = workPart.Bodies
 Dim solid_bodies(-1) As Body
 Dim counter As Integer = 0
 Dim newLayer As Integer = 100
 Dim bodyCount As Integer = bodies.ToArray.Length
 MsgBox("All Bodies in Work Part: " & bodyCount.ToString())
 newLayer = NXInputBox.GetInputNumber("Destination Layer Number", “Destination Layer number”, newLayer.ToString)

 If bodyCount > 0 Then
 For Each thisBody As Body In bodies
 If thisBody.IsSolidBody.Equals(True) Then
 ReDim Preserve solid_bodies(counter)
 solid_bodies(counter) = thisBody
  ' Change its color
solid_bodies(counter).Color = 178 ' Usually red, by default
'Show object
 solid_bodies(counter).RedisplayObject
counter += 1
 
 End If
 Next
  
 workPart.Layers.MoveDisplayableObjects(newLayer, solid_bodies)
 Dim solidBodyCount As Integer = solid_bodies.Length()
 MsgBox(solidBodyCount.ToString() & "Solid Bodies moved to layer: " & newLayer.ToString)
 

 End If
 End Sub
 Public Function GetUnloadOption(ByVal dummy As String) As Integer
 Return Session.LibraryUnloadOption.Immediately
 End Function
End Module

Option Strict Off

Imports NXOpen

Imports NXOpenUI

Imports NXOpen.Utilities

Module move_solid_bodies_to_new_layer

 Dim s As Session = Session.GetSession()

 Sub Main()

 Dim workPart As Part = s.Parts.Work

 Dim bodies As BodyCollection = workPart.Bodies

 Dim solid_bodies(-1) As Body

 Dim counter As Integer = 0

 Dim newLayer As Integer = 100

 Dim bodyCount As Integer = bodies.ToArray.Length

 MsgBox("All Bodies in Work Part: " & bodyCount.ToString())

 newLayer = NXInputBox.GetInputNumber("Destination Layer Number", “Destination Layer number”, newLayer.ToString)


 If bodyCount > 0 Then

 For Each thisBody As Body In bodies

 If thisBody.IsSolidBody.Equals(True) Then

 ReDim Preserve solid_bodies(counter)

 solid_bodies(counter) = thisBody

  ' Change its color

solid_bodies(counter).Color = 178 ' Usually red, by default

'Show object

 solid_bodies(counter).RedisplayObject

counter += 1

 

 End If

 Next

  

 workPart.Layers.MoveDisplayableObjects(newLayer, solid_bodies)

 Dim solidBodyCount As Integer = solid_bodies.Length()

 MsgBox(solidBodyCount.ToString() & "Solid Bodies moved to layer: " & newLayer.ToString)

 


 End If

 End Sub

 Public Function GetUnloadOption(ByVal dummy As String) As Integer

 Return Session.LibraryUnloadOption.Immediately

 End Function

End Module





Option Strict Off

Imports System

Imports NXOpen

Imports NXOpen.UF

Imports NXOpen.UI

Imports NXOpen.Utilities

Imports NXOpen.Features

Module report_journal_identifiers

Dim theSession As Session = Session.GetSession()

Dim theUFSession As UFSession = UFSession.GetUFSession()

Sub Main()

Dim dp As Part = theSession.Parts.Display

Dim theBodies() As Body = dp.Bodies.ToArray()

Dim theFeatures() As Feature = dp.Features.ToArray()

Echo("Journal Identifiers for the Body objects:")

For Each thisBody As Body In theBodies

Echo(thisBody.JournalIdentifier())

Next

Echo("=========================================")

Echo("Journal Identifiers for the Feature objects:")

For Each thisFeat As Feature In theFeatures

Echo(thisFeat.JournalIdentifier())

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

End Function

End Module




Option Strict Off

Imports System

Imports NXOpen

Imports NXOpen.UF

Imports NXOpen.Features

Module LayerProgramming3

Sub Main()

Dim s As Session = Session.GetSession()

Dim ui As UI = ui.GetUI()

Dim ufs As UFSession = UFSession.GetUFSession()

Dim lw As ListingWindow = s.ListingWindow

lw.Open()

' We look only at the current work part

Dim workPart As Part = s.Parts.Work

' Get all objects on the work layer

Dim objs() As NXObject = workPart.Layers.GetAllObjectsOnLayer(workPart.Layers.WorkLayer)

lw.WriteLine(objs.Length.ToString & " objects on layer: " & workPart.Layers.WorkLayer.ToString)

' Now list all the objects

For Each b As NXObject In objs

lw.WriteLine(b.ToString)

Next

lw.WriteLine(vbCrLf)

' Another listing for each body in the work part we can list the features and the body layer

For Each abody As Body In workPart.Bodies

For Each afeat As Feature In abody.GetFeatures

lw.WriteLine(afeat.GetFeatureName & " on layer : " & abody.Layer.ToString)

Next

Next

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX

GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

Option Strict Off  
Imports System  
Imports NXOpen  

Module NXJournal  
Sub Main  

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

Const DatumLayer as Integer = 61  
Const CurveLayer as Integer = 256 
Const SketchLayer as Integer = 250
Const SheetLayer as Integer = 256

'move datums
for each datumObj as DisplayableObject in workPart.Datums  
if typeof(datumObj) is DatumPlane then  
datumObj.Layer = DatumLayer  
datumObj.RedisplayObject  
end if  
next  

'move curves
for each curveObj as Curve in workPart.Curves  
curveObj.Layer = CurveLayer  
curveObj.RedisplayObject  
next  

'move sketches
'do this after moving curves, sketch curves will update to sketch layer
for each sketchObj as Sketch in workPart.Sketches  
sketchObj.Activate(False)  
sketchObj.Layer = SketchLayer  
sketchObj.RedisplayObject  
sketchObj.Deactivate(False, Sketch.UpdateLevel.SketchOnly)  
next  

'move sheet bodies
for each bodyObj as Body in workPart.Bodies  
if bodyObj.IsSheetBody then  
bodyObj.Layer = SheetLayer  
bodyObj.RedisplayObject  
end if  
next  

End Sub  
End Module 



 





没有评论:

发表评论