Showing results for 
Search instead for 
Did you mean: 

Group Merger Journal Not Deleting Groups After Adding To Delete list and Updating

Valued Contributor
Valued Contributor

Hi everyone, I created a journal designed to merge groups with the same name inside an AFEM. (e.g. if an AFEM has three copies of a component, each containing Group1, the AFEM would now contain three lots of Group1, owned by their respective FEM parts. This journal would merge those three groups into a single group inside the AFEM) It successfully creates the union groups, but inexplicably some of the old groups are not deleted, despite confirming their presence on the delete list with prints and DoUpdate() being called multiple times. Does anyone have any idea why?

import math
import NXOpen
import NXOpen.CAE
import NXOpen.UF
import ctypes

RemakeGroups = True # All groups will be moved to be a child of the current work part.

##Get objects
theSession  = NXOpen.Session.GetSession()
update = theSession.UpdateManager
theUFSession = NXOpen.UF.UFSession.GetUFSession();
workPart = theSession.Parts.BaseWork
fieldManager = workPart.FindObject("FieldManager")
FilterType = NXOpen.CAE.CaeSetGroupFilterType

undo1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Group merge")
ctypes.windll.user32.MessageBoxW(0, "This journal WILL REMOVE GROUPS IN COMPONENTS! Make sure you have a back up. Hit cancel in the NX 'in progess' window if you don't.", "Warning!", 0) 

##Arrange groups into lists of groups with the same name
Groups = {}
for Group in workPart.CaeGroups:
    strippedName = Group.Name.rsplit(":")[0]    
    strippedName = strippedName.rsplit("//")[0]
    if Groups.get(strippedName) is not None: # Multiple groups of same name put into list
        theSession.ListingWindow.WriteFullline("Unique group: "+Group.Name)
        Groups[strippedName] = [Group]
##Arange Groups into lists of groups with the same owning component
GroupsByComponent = {}
for Group in workPart.CaeGroups:
    com = Group.OwningComponent
    if com is not None:
        theSession.ListingWindow.WriteFullline("Recording groups in component: "+com.Name)
        if GroupsByComponent.get(com) is not None:
            GroupsByComponent[com] = [Group]

##Create merged groups
for GroupName in Groups:  #Loop through lists of groups with same name
    GroupList = Groups[GroupName]
    if len(GroupList) > 1: # If there is more than one group in the list, merge them
        theSession.ListingWindow.WriteFullline("Merging " + GroupName + " with " + str(len(GroupList)) + " groups") #Create union group of the groups in the list
        newGroup = workPart.CaeGroups.CreateUnionGroup(GroupName+" temporaryname", GroupList) # Need to use a different name to avoid the NX name already exists error. Name will be returned to normal at end of script.
        # theSession.ListingWindow.WriteFullline("Merged "+GroupName+" and deleted duplicates")
        Groups[GroupName] = newGroup # Replace the group list with the new group so we can go back and rename it later
    elif RemakeGroups == True: # Create new group under the work part if the remake groups bool is true, so that all groups are parented to the same component.
        newGroup = workPart.CaeGroups.CreateGroup(GroupName+" temporaryname", GroupList[0].GetEntities())
        Groups[GroupName] = newGroup

##Delete old groups
for Component in GroupsByComponent: # Go through each component with groups in
    GroupList = GroupsByComponent[Component]
    theSession.Parts.SetWorkComponent(Component) # Set the component as the work part
    theSession.ListingWindow.WriteFullline("Deleting groups in: "+Component.Name)
    update.AddToDeleteList(GroupList) # Add the unmerged groups inside this component to the delete list
    for Group in GroupList:
        theSession.ListingWindow.WriteFullline(Group.Name + "Deleted")
    NumErrors = update.DoUpdate(undo1) # Do update to delete them
    theSession.ListingWindow.WriteFullline("Number of errors: "+str(NumErrors))

for GroupName in Groups: # Return names to normal
    if Groups[GroupName].__class__ == NXOpen.CAE.CaeGroup:



Re: Group Merger Journal Not Deleting Groups After Adding To Delete list and Updating


Try a part cleanup with delete empty groups option.


Technisites Australia
Production: NX10.0.3 Developing: NX11.0.2.7, NX12.0.2, NX1847,
Development: C++, C# .Net, VB .Net, Python, KF