Select every Nth Component

When modeling things, it’s often needed to select every Nth Component. To make this selections more easy, I have made a temporary PPG that helps you to deselect components.

The PPG has two parameters (start and pattern) and two options (restore selection and apply new selection).

Parameter – Start: Defines the start component
Parameter – Pattern: Defines the the numbers of unselected components – 1. An example: if pattern is set to 4 every fourth component will be selected.

Option – Restore Selection: Restores the start-selection
Option – Apply New Selection: Applies the new selection with the chosen parameters

Usage:
1. Save the script somewhere.
2. Change this line to the file location:”oPPGLayout.SetAttribute(c.siUILogicFile, “\\\\Server\\Scripts\\select_components.py”)”
3. Select one object, tag some components:”Points, Edges, Polygons” and run the script

from win32com.client import constants as c
xsi = Application
log = Application.LogMessage
collSel = xsi.Selection

def selectEveryNth_createPSet(oInObjName, oInComponents, oTypecheck):
	oPSet = XSIFactory.CreateObject("CustomProperty")
	oPSet.Name = "AB_SelectEveryNthComponent"
	oPSet.AddParameter3( "Start", c.siInt4, 0)
	oPSet.AddParameter3( "Pattern", c.siInt4, 2)
	oPSet.AddParameter3( "oObjName", c.siString, str(oInObjName))	
	oPSet.AddParameter3( "oInSelection", c.siString, str(oInComponents))	
	oPSet.AddParameter3( "oTypeCheck", c.siString, str(oTypecheck))	
	return oPSet	

def selectEveryNth_createLayout(oPSet):
	oPPGLayout = oPSet.PPGLayout
	oPPGLayout.AddGroup( "Start/Pattern" ) 
	oPPGLayout.AddItem( "Start", "", "" )
	oPPGLayout.AddItem( "Pattern", "", "" )
	oPPGLayout.EndGroup() 
	
	oPPGLayout.AddGroup( "Selection" )
	oPPGLayout.AddRow() 
	oPPGLayout.AddButton( "ButtonRestore", "Restore Selection" )
	oPPGLayout.AddButton( "ButtonSelectNew", "Apply New Selection" )
	oPPGLayout.EndRow() 
	oPPGLayout.EndGroup()
	oPPGLayout.Language="Python"
	oPPGLayout.SetAttribute(c.siUILogicFile, "\\\\Server\\Scripts\\select_components.py")
	return True
	
def selectEveryNth_Execute(inSelection):
	oMyComponentSel = []
	oTypeCheck = 0
	if inSelection.Count > 0:
		log('debug: Ok, you have selected something')
		if inSelection.Count == 1:
			log('debug: right, one object selected')
			if inSelection(0).Type == 'polySubComponent':
				oTypeCheck = 1
			elif inSelection(0).Type == 'pntSubComponent':
				log('debug: pntSubComponent')
				log(inSelection(0).SubElements2)
				oTypeCheck = 2
			elif inSelection(0).Type == 'edgeSubComponent':
				oTypeCheck = 3
			else:
				log('debug: please select some components')
			if oTypeCheck != 0:
				oParent = inSelection[0].SubComponent.Parent.Parent.Parent.FullName
				oParentSplit = oParent.split(".",1)
				if oParent[0] != "Scene_Root":
					log(oParent)
					oPSet = selectEveryNth_createPSet(str(oParent), inSelection(0).SubElements2,oTypeCheck )										
				else:
					log(oParent)
					oPSet = selectEveryNth_createPSet(str(inSelection[0].Name), inSelection(0).SubElements2,oTypeCheck )					
				selectEveryNth_createLayout(oPSet)
				xsi.InspectObj( oPSet, "", "", c.siLock, False )

		else:
			log('debug: please select just one object')
	else:
		log(' debug: please select something')
	return True
	
def selectEveryNth_selectNew():
	if 'PPG' in globals():
		oPattern = xsi.GetValue(str(PPG.InspectedObjects)+".Pattern")
		oStart = xsi.GetValue(str(PPG.InspectedObjects)+".Start")
		oObjName = xsi.GetValue(str(PPG.InspectedObjects)+".oObjName")
		oInSelection = xsi.GetValue(str(PPG.InspectedObjects)+".oInSelection")
		oTypeCheck = xsi.GetValue(str(PPG.InspectedObjects)+".oTypeCheck")
	xsi.DeselectAll()
	xsi.SelectObj(oObjName, "", True)
	oMyComponentSel = str(oInSelection).replace(')','')
	oMyComponentSel = oMyComponentSel.replace('(','')
	oMyComponentSel = oMyComponentSel.split(',')
	oNewComponentSel = oMyComponentSel[int(oStart)::int(oPattern)]
	oNewComponentSelStr = str(oNewComponentSel).replace(']','')
	oNewComponentSelStr = oNewComponentSelStr.replace('[','')	
	oNewComponentSelStr = oNewComponentSelStr.replace("'",'')	
	if int(oTypeCheck) == 1:
		xsi.SetSelFilter("Polygon")
		xsi.SelectGeometryComponents(str(oObjName) + '.poly[' + oNewComponentSelStr + ']')
	elif int(oTypeCheck) == 2:
		xsi.SetSelFilter("Vertex")
		xsi.SelectGeometryComponents(str(oObjName) + '.pnt[' + oNewComponentSelStr + ']')
	elif int(oTypeCheck) == 3:
		xsi.SetSelFilter("Edge")
		xsi.SelectGeometryComponents(str(oObjName) + '.edge[' + oNewComponentSelStr + ']')

def selectionRestore_selectNew():
	if 'PPG' in globals():
		oPattern = xsi.GetValue(str(PPG.InspectedObjects)+".Pattern")
		oStart = xsi.GetValue(str(PPG.InspectedObjects)+".Start")
		oObjName = xsi.GetValue(str(PPG.InspectedObjects)+".oObjName")
		oInSelection = xsi.GetValue(str(PPG.InspectedObjects)+".oInSelection")
		oTypeCheck = xsi.GetValue(str(PPG.InspectedObjects)+".oTypeCheck")
	xsi.DeselectAll()
	xsi.SelectObj(oObjName, "", True)
	oMyComponentSel = str(oInSelection).replace(')','')
	oMyComponentSel = oMyComponentSel.replace('(','')
	if int(oTypeCheck) == 1:
		xsi.SetSelFilter("Polygon")
		xsi.SelectGeometryComponents(str(oObjName) + '.poly[' + oMyComponentSel + ']')
	elif int(oTypeCheck) == 2:
		xsi.SetSelFilter("Vertex")
		xsi.SelectGeometryComponents(str(oObjName) + '.pnt[' + oMyComponentSel + ']')
	elif int(oTypeCheck) == 3:
		xsi.SetSelFilter("Edge")
		xsi.SelectGeometryComponents(str(oObjName) + '.edge[' + oMyComponentSel + ']')

def ButtonRestore_OnClicked():
	Application.LogMessage("Restore Selection")
	selectionRestore_selectNew()

def ButtonSelectNew_OnClicked():
	Application.LogMessage("Apply New Selection")
	selectEveryNth_selectNew()
	
selectEveryNth_Execute(collSel)