Tuples, listas y diccionarios

Tuples, lists and dictionaries

  • tuples: tuples, list and dictionaries are just a collection of things. First element has index 0. Tuple consist of a number of values separated by commas. Can be use for coordinates. May contain multiple variables, nested tuples, list or other object. Are immutable. Use parenthesis.

  • lists: Are mutable. Use brackets. List can be added to, items can be removed, sorted, sliced, nested with multiple levels of inner lists and packed with other objects. Useful methods for lists (append, insert, remove, count, sort, reverse).

  • list comprehension: utilise the functionality of lists and for loops. Can contain loops, expressions and conditional statements.

  

        import rhinoscriptsyntax as rs

  

        def myfavoritethings():

                things = []

                while True:

                        count = len(things)

                        prompt = "What is your {0}th most favorite thing?".format(count+1)

                        if len(things)==0:

                                prompt = "What is your most favorite thing?"

                        elif count==1:

                                prompt = "What is your second most favorite thing?"

                        elif count==2:

                                prompt = "What is your third most favourite thing?"

                        answer = rs.GetString(prompt)

                        if answer is None: break

                        things.append(answer)

                if len(things)==0: return

  

                print "Your", len(things)+1, "favorite things are:"

                for i,thing in enumerate(things):       # one to keep track of the index of the list item, and one to get the actual list item

                        print i+1, ".", thing

  
  • dictionaries: store key and associated value. Value can be lists or other dictionaries, also value. Potential of list and dictionaries for geometric information.

  • points and vectors:

  

        import rhinoscriptsyntax as rs

        import math

        #Call rs.EnableRedraw(False)

        for t in rs.frange(-50,50,1.25):

                arrPoint = [t * math.sin(5*t), t * math.cos(5*t),t]

                print(arrPoint)

                rs.AddPoint(arrPoint)

        #Call rs.EnableRedraw(True)

  
  

        def AddVector(vecdir, base_point=[0,0,0]):

                tip_point = rs.PointAdd(base_point, vecdir)

                line = rs.AddLine(base_point, tip_point)

                if line: return rs.CurveArrows(line, 2)

  
  

        # an AddVector() example

        import rhinoscriptsyntax as rs

        # This script will compute a bunch of cross-product vector based on a pointcloud

        def vectorfield():

                cloud_id = rs.GetObject("Input pointcloud", 2, True, True)

                if cloud_id is None: return

                listpoints = rs.PointCloudPoints(cloud_id)      # contains all the coordinates of a pointcloud object

                base_point = rs.GetPoint("Vector field base point")

                if base_point is None: return

  

                for point in listpoints:        #taken from listpoints contain array of three doubles, used to construct a new vector

                        vecbase = rs.VectorCreate(point, base_point)

                        vecdir = rs.VectorCrossProduct(vecbase, (0,0,1))        # perpendicular to vecBase and the world z axis

                        if vecdir:

                                vecdir = rs.VectorUnitize(vecdir)

                                vecdir = rs.VectorScale(vecdir, 2.0)

                                AddVector(vecdir, point)  
  • nested lists: list inside another list. Can be done with tuples, lists or dictionaries. Can be easily done by utilizing a loop. Accessing nested lists follow the same rules that regular list.

        def smoothingvector(point, prev_point, next_point, s):

                pm = (prev_point+next_point)/2.0

                va = rs.VectorCreate(pm, point)

                vm = rs.VectorScale(va, s)

                return vm

  
  

        def smoothcurve(curve_id, s):

                curve_points = rs.CurvePoints(curve_id)

                new_curve_points = []

  

                for i in range(1, len(curve_points)-1):

                        vm = smoothingvector(curve_points[i], curve_points[i-1], curve_points[i+1], s)

                        new_curve_points.append( rs.PointAdd(curve_points[i], vm) )

                knots = rs.CurveKnots(curve_id)

                degree = rs.CurveDegree(curve_id)

                weights = rs.CurveWeights(curve_id,0)

                newcurve_id = rs.AddNurbsCurve(new_curve_points, knots, degree, weights)

                if newcurve_id: rs.DeleteObject(curve_id)

                return newcurve_id

  
  

        def iterativeshortencurve():

                curve_id = rs.GetObject("Open curve to smooth", 4, True)

                if curve_id is None or rs.IsCurveClosed(curve_id): return

                min = rs.Distance(rs.CurveStartPoint(curve_id), rs.CurveEndPoint(curve_id))

                max = rs.CurveLength(curve_id)

                goal = rs.GetReal("Goal length", 0.5*(min+max) , min, max)

                if goal is None: return

  

                while rs.CurveLength(curve_id)>goal:

                        rs.EnableRedraw(False)

                        curve_id = smoothcurve(curve_id, 0.1)

                        rs.EnableRedraw(True)

                        if curve_id is None: break

Última actualización

¿Te fue útil?