XSI Example: Random Component or Object Selections
from win32com.client import constants as c | |
import random | |
xsi = Application | |
XSIDialog = XSIFactory.CreateObject("XSIDial.XSIDialog") | |
def get_random_percentage(iterable, percentage): | |
amount = int( float(len(iterable)) * (percentage/100.0) ) | |
return random.sample(iterable, amount) | |
def selectRandomComponents(coll, percentage, cmpType = 'ask'): | |
allComponents = { | |
'edges':[x for obj in coll for x in obj.ActivePrimitive.Geometry.Edges], | |
'points':[x for obj in coll for x in obj.ActivePrimitive.Geometry.Points], | |
'faces':[x for obj in coll for x in obj.ActivePrimitive.Geometry.Facets] | |
} | |
cmpTypes = allComponents.keys() | |
if cmpType == 'ask': | |
cmpType = cmpTypes[ XSIDialog.ComboEx('Component type?',cmpTypes,2) ] | |
sample = get_random_percentage(allComponents[cmpType], percentage) | |
xsi.SelectObj(sample) | |
def selectRandomObjects(coll, percentage): | |
sample = get_random_percentage(coll, percentage) | |
xsi.SelectObj(sample) | |
def main(): | |
selectComponents = ( | |
XSIUIToolkit.Msgbox( | |
"Select components?\n\nYES = components\nNO = objects", | |
c.siMsgYesNo+c.siMsgQuestion, | |
"Random Selector" ) == c.siMsgYes | |
) | |
percentage = float(xsi.XSIInputBox( | |
"What percentage to select?", | |
"Selection percentage?", | |
50 # default % | |
)) | |
if selectComponents: | |
selectRandomComponents(xsi.Selection, percentage) | |
else: | |
selectRandomObjects(xsi.Selection, percentage) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment