Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

NX VB

Status
Not open for further replies.

stytgat

Automotive
Mar 29, 2010
15
Hello,

In a little VB program, I would like to count the number of sketches in my active part, for move each sketch in his layer (skt_000 in layer 256, skt_001 in layer 255 etc...).
In my part, each sketch have the name : skt_000 / skt_001 / ... / skt_0xx.

Thank you very much for your support,

Sébastien Tytgat
 
Replies continue below

Recommended for you

Even for journals it is important to know the nx version. Below are the journals for this. The first is suitable for NX6 or NX7.5. The second is for NX8.

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Features

Module organize_sketches
    Dim s As Session = Session.GetSession()
    Dim workPart As Part = S.Parts.Work
    Dim dispPart As Part = s.Parts.Display
    Dim lw As ListingWindow = s.ListingWindow
    Dim i As Integer
    Dim objArray(0) As DisplayableObject
    Sub Main()
        ' Get all sketches
        Dim allsketches As SketchCollection = workPart.Sketches
        ' Get the sketch feature name and then move the sketch to the approriate layer
        Dim sketchname As String = Nothing
        Dim sketchnumberstring As String = Nothing
        Dim sketchnumber As Integer = Nothing
        Dim length1 As Integer = Nothing
        Dim layernumber As Integer = Nothing
        Dim objArray(0) As DisplayableObject
        For Each sk As Sketch In allsketches
            sketchname = sk.Name.ToString
            length1 = Len(sketchname)
            sketchnumberstring = sketchname.Substring(length1 - 3, 3)
            Try
                sketchnumber = CInt(sketchnumberstring) ' Get the sketch number as an integer
                layernumber = 256 - sketchnumber ' The destination layer number
                objArray(0) = sk
                workPart.Layers.MoveDisplayableObjects(layernumber, objArray) ' Move the sketch
            Catch ex As Exception
                MsgBox("Invalid sketch name: " & sketchname)
            End Try
        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

Code:
Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.Features

Module organize_file
    Dim s As Session = Session.GetSession()
    Dim workPart As Part = S.Parts.Work
    Dim dispPart As Part = s.Parts.Display
    Dim lw As ListingWindow = s.ListingWindow
    Dim i As Integer
    Dim objArray(0) As DisplayableObject
    Sub Main()
        ' Get all sketches
        Dim allsketches As SketchCollection = workPart.Sketches
        ' Get the sketch feature name and then move the sketch to the approriate layer
        Dim sketchname As String = Nothing
        Dim sketchnumberstring As String = Nothing
        Dim sketchnumber As Integer = Nothing
        Dim skfeature As Feature
        Dim index1 As Integer = Nothing
        Dim layernumber As Integer = Nothing
        Dim objArray(0) As DisplayableObject
        For Each sk As Sketch In allsketches
            skfeature = sk.Feature ' Get the sketch feature
            sketchname = skfeature.GetFeatureName ' Get the sketch feature name
            Try
                index1 = sketchname.IndexOf(":") ' To extract the number portion
                sketchnumberstring = sketchname.Substring(index1 - 3, 3) ' To extract the number portion
                sketchnumber = CInt(sketchnumberstring) ' Get the sketch number as an integer
                layernumber = 256 - sketchnumber ' The destination layer number
                objArray(0) = sk
                workPart.Layers.MoveDisplayableObjects(layernumber, objArray) ' Move the sketch
            Catch ex As Exception
                MsgBox("Invalid sketch name: " & sketchname)
            End Try
        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

Frank Swinkels
 
Dear all,

Thanks a lot for your quick answers and thanks for your solutions.
Best regards,
 
Hi again,

i would like to continue my program by selected all sheets bodies and move them in a specific layer.
i can selected all bodies (volume) but not specifics sheets (or surfaces).
How can i do?
Please find in attached file my code (Module1.vb).
My Nx version is 7.5

Thanks for your support and best regards,

Sébastien Tytgat
 
 http://files.engineering.com/getfile.aspx?folder=eb0fb789-3263-4938-8a90-7fb552cfa83e&file=Module1.vb
One way is to iterate through the part's body collection, which contains both sheet and solid bodies. The body class has .IsSheetBody and .IsSolidBody properties to distinguish sheets from solids. Here is some example code:

Code:
[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] NXOpen  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Sub[/color] Main()  

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  
        [COLOR=blue]Dim[/color] lw [COLOR=blue]As[/color] ListingWindow [COLOR=blue]=[/color] theSession.ListingWindow  
        lw.Open()  

        [COLOR=blue]Dim[/color] partBodies() [COLOR=blue]As[/color] Body [COLOR=blue]=[/color] workPart.Bodies.ToArray  
        [COLOR=blue]For[/color] [COLOR=blue]Each[/color] partBody [COLOR=blue]As[/color] Body [COLOR=blue]In[/color] partBodies  
            [COLOR=blue]If[/color] partBody.IsSheetBody [COLOR=blue]Then[/color]  
                lw.WriteLine("Sheet body, layer: " [COLOR=blue]&[/color] partBody.Layer)  
            [COLOR=blue]Else[/color]  
                lw.WriteLine("Solid body, layer: " [COLOR=blue]&[/color] partBody.Layer)  
            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

        lw.Close()  

    End [COLOR=blue]Sub[/color]  


    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]

www.nxjournaling.com
 
Thanks a lot!

BR,

Sébastien Tytgat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor