• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

[Code] wrapping operations

Scheduled Pinned Locked Moved Developers' Forum
5 Posts 4 Posters 2.3k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    Dan Rathbun
    last edited by Dan Rathbun 3 Oct 2011, 01:08

    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.

    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
    

    I'm not here much anymore.

    1 Reply Last reply Reply Quote 0
    • J Offline
      jolran
      last edited by 3 Oct 2011, 07:10

      Thanks.

      /Joel

      1 Reply Last reply Reply Quote 0
      • F Offline
        Francis Mc Shane
        last edited by 5 Oct 2015, 20:18

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

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 6 Oct 2015, 13:47

          @francis mc shane said:

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

          Link Preview Image
          File: exceptions.rdoc [Ruby 2.0.0]

          favicon

          (ruby-doc.org)

          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.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • M Offline
            MSP_Greg
            last edited by 10 Oct 2015, 15:47

            One can also wrap Dan's code in a method

            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 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement