sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Code] wrapping operations

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 4 Posters 2.3k Views 4 Watching
    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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      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
      • jolranJ Offline
        jolran
        last edited by

        Thanks.

        /Joel

        1 Reply Last reply Reply Quote 0
        • F Offline
          Francis Mc Shane
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @francis mc shane said:

            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.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • M Offline
              MSP_Greg
              last edited by

              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