Submitted by BSP on Thu, 09/10/2015 - 08:13
Forums:
Hello,
I would like to write a journal for moving wave link features to the top of the feature tree.
Since a wave link feature has no parents within the part itself this can always be done without errors making it easy to find them always on top.
I've done some journals with help of examples shown on this website, that is great!, but for this one I'm not sure how it can be done.
If anyone can point me in the right direction that would be great!
Many thanks in advance
re: reorder wave link features
I'd suggest starting by recording a journal while reordering a wave link feature to the top of the feature tree. The resulting code will show you how to reorder a given feature. Then you can add to that code by iterating through the features and moving any wave link features to the top.
Reorder wavelink features
Thanks for the reply!
I recorded a journal and notices that the reorderFeature method is used.
The NXOpen reference tells me that I need to specify a target feature:
public void ReorderFeature(
Feature[] features,
Feature target,
FeatureCollection..::..ReorderType beforeOrAfter
)
NX Open reference parameters:
features (array[]()[][])
Features.Feature to be reordered
target (Feature)
Target feature
beforeOrAfter (FeatureCollection..::..ReorderType)
Reorder Before/After
How do I get the feature name of the feature at timestamp 1 so that I can specify a before target?
This is the recorded journal
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
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Reorder Feature")
Dim features1(0) As Features.Feature
Dim extractFace1 As Features.ExtractFace = CType(workPart.Features.FindObject("LINKED_BODY(29)"), Features.ExtractFace)
features1(0) = extractFace1
Dim extractFace2 As Features.ExtractFace = CType(workPart.Features.FindObject("LINKED_BODY(28)"), Features.ExtractFace)
workPart.Features.ReorderFeature(features1, extractFace2, Features.FeatureCollection.ReorderType.Before)
theSession.Preferences.Modeling.UpdatePending = False
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId1)
theSession.Preferences.Modeling.UpdatePending = False
End Sub
End Module
Many thanks for your advice!
re: first feature
You do not need to know the feature name, you only need a reference to the first feature in the part navigator. The journal recorder uses the feature name along with the .Find function to get the reference. Using the name and .Find function causes the recorded code to be "sticky", in other words, the recorded code will only work on the part file that was open when the journal was recorded. By getting a reference to the first feature in the feature collection, we can avoid the "stickiness" and make a journal that can be used on any part file.
Option Strict Off
Imports System
Imports NXOpen
Module Module1
Dim theSession As Session = Session.GetSession()
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "report first feature"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'get a reference to the first feature in the current work part
Dim partFirstFeature As Features.Feature = theSession.Parts.Work.Features.ToArray(0)
'prove that we got the correct feature
lw.WriteLine("feature name: " & partFirstFeature.GetFeatureName)
lw.WriteLine("feature type: " & partFirstFeature.FeatureType)
lw.WriteLine("time stamp: " & partFirstFeature.Timestamp)
lw.Close()
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
Reorder wavelink features
Thanks for your help.
I've been trying to create a for loop that looks for all wavelink features and reorders them to position After timestamp 0 (WCS). Probably due to a beginner mistake I can't get it to work.
Any advice would be great!
Thanks in advance
This is what I've made so far:
Option Strict Off
Imports System
Imports NXOpen
Module Module1
Dim theSession As Session = Session.GetSession()
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Const undoMarkName As String = "report first feature"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'get a reference to the first feature in the current work part
Dim partFirstFeature As Features.Feature = theSession.Parts.Work.Features.ToArray(0)
Dim feature1 As Features.Feature
For Each myFeature As Features.Feature In workPart.Features
If myFeature.FeatureType = "LINKED_BODY" Then
workPart.Features.ReorderFeature(feature1, partfirstfeature, Features.FeatureCollection.ReorderType.After)
End If
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
re: reorder features
I don't recommend reordering features while you are iterating through the feature collection. Doing so can confuse the iterator position and produce undesired results.
Instead, I suggest iterating through the features, keeping track of the linked bodies that you find (a list object is good for this) and finally performing the reorder operation.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Module Module1
Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
lw.Open()
Const undoMarkName As String = "report first feature"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'get a reference to the first feature in the current work part
Dim partFirstFeature As Features.Feature = theSession.Parts.Work.Features.ToArray(0)
'lw.WriteLine("first feature: " & partFirstFeature.GetFeatureName)
Dim linkedBodies As New List(Of Features.Feature)
'add linked bodies to a list for later processing
For Each myFeature As Features.Feature In workPart.Features
If myFeature.FeatureType = "LINKED_BODY" Then
linkedBodies.Add(myFeature)
End If
Next
'lw.WriteLine("number of linked bodies: " & linkedBodies.Count)
'For Each tempFeat As Features.Feature In linkedBodies
' lw.WriteLine(tempFeat.GetFeatureName)
'Next
'reorder the features that we found
workPart.Features.ReorderFeature(linkedBodies.ToArray, partFirstFeature, Features.FeatureCollection.ReorderType.After)
Dim id1 As Session.UndoMarkId
id1 = theSession.NewestVisibleUndoMark
Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(id1)
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
Reorder features
Great! thanks for your help!
The journal does indeed reorder all linked bodies but strange thing is that it only works ones.
After using the journal I reordered the linked bodies manually to test the journal again, this time it doesn't work anymore.
The listing window shows a correct list of all linked bodies with the correct timestamp but the reordering is not working anymore. I have no clue why this is happening. There is no error message displayed and the NX log says the Execution was successful.
Any ideas how to solve this?
Many thanks in advance
Reorder features
Does anyone have a clue why this journal only work ones.
Thanks in advance for your help
re: reorder features
Seems to work every time for me, I can't seem to reproduce the behavior you describe.
I don't know that it will make a difference, but what version of NX are you using? I tested on NX 9.0.3.4
reorder features
I work with 9.0.3.4 also but via Teamcenter.
I found out that in native NX this problem doesn't exist.
Strange isn't it..
reorder features
With help from Siemens I found out what is causing the problem.
The journal works if there is no linked curve at the top of the tree.
If I create a linked curve and run the journal it is moved to timestamp 1.
If I now create another linked curve and run the journal, the new curve is not moved.
If I drag the first curve to a later timestamp and run the journal all curves are moved to the top of the feature tree.
So it doesn't have anything to do with Native vs TC managed
How can I make the journal ignore the feature on timestamp 1 if it is of the type that is reordered?
Many thanks for your help!
re: reorder features
The code that I previously posted attempts to reorder all of the linked bodies at the same time. I think what is happening is that if one of the features occupies the spot we are attempting to move to, the function assumes success and doesn't move any of the other features.
Rather than moving them all at once, we can try to move them individually. Try the code below:
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Module Module1
Dim theSession As Session = Session.GetSession()
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
lw.Open()
Const undoMarkName As String = "report first feature"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
'get a reference to the first feature in the current work part
Dim partFirstFeature As Features.Feature = theSession.Parts.Work.Features.ToArray(0)
'lw.WriteLine("first feature: " & partFirstFeature.GetFeatureName)
Dim linkedBodies As New List(Of Features.Feature)
'add linked bodies to a list for later processing
For Each myFeature As Features.Feature In workPart.Features
If myFeature.FeatureType = "LINKED_BODY" Then
linkedBodies.Add(myFeature)
End If
Next
'lw.WriteLine("number of linked bodies: " & linkedBodies.Count)
'For Each tempFeat As Features.Feature In linkedBodies
' lw.WriteLine(tempFeat.GetFeatureName)
'Next
'reorder the features that we found
For Each temp As Features.Feature In linkedBodies
workPart.Features.ReorderFeature({temp}, partFirstFeature, Features.FeatureCollection.ReorderType.After)
Next
Dim id1 As Session.UndoMarkId
id1 = theSession.NewestVisibleUndoMark
Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(id1)
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
reorder features
Fantastic, works perfect, thank you so much for your help!