Ejecución condicional

Conditional execution

  • if: what if questions for varying complexities. Nested conditions. A good way to solve nesting conditions is iterate (if, elif, else).

  

        dblCurveLength = rs.CurveLength(strObjectID)

                if (dblCurveLength != None):

                        if (dblCurveLength < rs.UnitAbsoluteTolerance()):

                                rs.DeleteObject(strObjectID)

                        elif (dblCurveLength < (10 * rs.UnitAbsoluteTolerance())):

                                rs.SelectObject(strObjectID)

                        else:

                                rs.UnselectObject(strObjectID)
  • looping: conditional looping keep repeating until some condition is met. Incremental loops will run a predefined number of times. (there are more). Conditional looping 'do...loop' if we don't abort the loop, omit the break statement or the condition never happen the loop will continue forever. Incremental loops when the number of iterations is known in advance (item in group or range)

  

        ## Conditional looping

        import rhinoscriptsyntax as rs

        import datetime as dt

  

        def viewportclock():

                now = dt.datetime.now()

                textobject_id = rs.AddText(str(now), (0,0,0), 20)

                if textobject_id is None: return                        # cast operation for a datetime object is a well known and solid operation, we do not have to check for a Null variable and we can put it 'inline'

                rs.ZoomExtents(None, True)

                while True:

                        rs.Sleep(1000)

                        now = dt.datetime.now()

                        rs.TextObjectText(textobject_id, str(now))

  

        if __name__=="__main__":

                viewportclock() # function is called. Python files can act as either reusable modules or a standalone programs

  
  

        import rhinoscriptsyntax as rs

        # Iteratively scale down a curve until it becomes shorter than a certain length

  

        def fitcurvetolength():

                curve_id = rs.GetObject("Select a curve to fit to length", rs.filter.curve, True, True)

                if curve_id is None: return

                length = rs.CurveLength(curve_id)

                length_limit = rs.GetReal("Length limit", 0.5 * length, 0.01 * length, length)

                if length_limit is None: return

                while True:

                        if rs.CurveLength(curve_id)<=length_limit: break

                        curve_id = rs.ScaleObject(curve_id, (0,0,0), (0.95, 0.95, 0.95))

                        if curve_id is None:

                                print "Something went wrong..."

                                return

                print "New curve length: ", rs.CurveLength(curve_id)

  

        if __name__=="__main__":

                fitcurvetolength()

  
  

        # Incremental looping

        import math

        import rhinoscriptsyntax as rs

  

        pi = math.Pi()

        dblTwistAngle = 0.0

  

        rs.EnableRedraw(False)

        for z in rs.frange(0.0, 5.0, 0.5):

                dblTwistAngle = dblTwistAngle + (pi/30)

                for a in rs.frange(0.0, 2*pi, (pi/15)):

                        x = 5 * math.sin(a + dblTwistAngle)

                        y = 5 * math.cos(a + dblTwistAngle)

                        rs.AddSphere([x,y,z], 0.5)

  

        rs.EnableRedraw(True)

Última actualización

¿Te fue útil?