[Code] wrapping operations

[Code] wrapping operations

Postby Dan Rathbun » Mon Oct 03, 2011 2:08 am

    ALWAYS use a begin .. rescue .. end block for your operation.

    If an error occurs, the operation can be aborted, and (hopefully,) prevent corruption of the model.

    Code: Select all
    begin
      op_name = "My Operation"
      model.start_operation( op_name )
        #
        ### indented code statements here
        #
      model.commit_operation
    rescue => e
      model.abort_operation
      puts("Error #<#{e.class.name}:#{e.message}.>")
      puts(e.backtrace) if $VERBOSE
    else
      #
      # Do code here ONLY if NO errors occur.
      #
    ensure
      #
      # ALWAYS do code here errors or not.
      #
    end
    2
    Last edited by Dan Rathbun on Mon Oct 28, 2013 11:26 pm, edited 2 times in total.
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] wrapping operations

      Postby jolran » Mon Oct 03, 2011 8:10 am

      Thanks.

      /Joel
      0
      User avatar
      jolran 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] wrapping operations

      Postby Francis Mc Shane » Mon Oct 05, 2015 9:18 pm

      Dan,
      thanks for this. It works. I don't know why though!
      Best,
      Francis
      0

      Francis Mc Shane 
       

      Re: [Code] wrapping operations

      Postby Dan Rathbun » Tue Oct 06, 2015 2:47 pm

      Francis Mc Shane wrote:Dan,
      thanks for this. It works. I don't know why though!

      http://ruby-doc.org/core-2.0.0/doc/syntax/exceptions_rdoc.html

      Ya should probably read the docs on page
      http://ruby-doc.org/core-2.0.0/index.html
      from "doc/syntax.rdoc" on down. There is only 11.
      1
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: [Code] wrapping operations

        Postby MSP_Greg » Sat Oct 10, 2015 4:47 pm

        One can also wrap Dan's code in a method

        Code: Select all
        def self.operation(opName, procElse = nil, procEnsure = nil)
          if (block_given? && String === opName)
            am = Sketchup.active_model
            begin
              am.start_operation(opName, true)
              yield
              am.commit_operation
            rescue => e
              am.abort_operation
              puts("Error operation block #<#{e.class.name}:#{e.message}.>")
              puts(e.backtrace) if $VERBOSE
            else
              procElse.call()   if (Proc === procElse)
            ensure
              procEnsure.call() if (Proc === procEnsure)
            end
          else
            raise "Error operation parameters - No block given or opName not a String!"
          end
        end

        # call by
        # operation("My operation") { operation block }


        Note that if either procElse and procEnsure are used, they should probably be lambdas. One should also be careful about return statements in the block passed to the method.

        BTW, I'll use snake case for methods, maybe not variables...

        Greg
        1

        MSP_Greg 
         

        SketchUcation One-Liner Adverts

        by Ad Machine » 5 minutes ago



        Ad Machine 
        Robot
         



         

        Return to Developers' Forum

        Who is online

        Users browsing this forum: Bing [Bot] and 13 guests