# View refresh problem

**URL:** <https://forums.sketchup.com/t/view-refresh-problem/152840>\
**Category:** Ruby API\
**Created:** [February 11, 2021, 8:16am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840 "2021-02-11T08:16:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 11, 2021, 8:16am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/1 "2021-02-11T08:16:46Z")

</div>

My plugin operation time is long and maybe boring for users so I wish to show steps of operation as an animation. Following is a sample code for it.

```ruby
grp = Sketchup.active_model.active_entities.add_group
for i in 0...10
  cp = [0, i, i]
  cir = grp.entities.add_circle cp, [0, 0, 1], (1)
  face1 = grp.entities.add_face(cir)
  face1.reverse! if face1.normal == [0, 0, -1]
  Sketchup.active_model.active_view.refresh
  sleep 0.1
# Sketchup.active_model.active_view.invalidate
end

```

As you can check view.invalidate doesn’t work and in this case, I have to use view.refresh.  
API has following explanation about refresh.  
**Note:**

**This method might impact performance and if used incorrectly cause instability or crashes. Don’t use this unless you have verified that you cannot use [#invalidate](https://ruby.sketchup.com/Sketchup/View.html#invalidate-instance_method) instead.**

When I used refresh in my plugin caused crashes. Following you can see code and crashes.  
code:

```ruby
        p @pt
        Sketchup.active_model.active_view.refresh
        p "----"
        p @pt

```

result:  
[Point3d(47.2441, 0, 5.90551), [48.818897637795274, 11.811023622047244, 43.30708661417323], [50.39370078740158, 23.62204724409449, 49.21259842519685], [51.96850393700788, 35.43307086614173, 55.118110236220474], [53.54330708661418, 47.24409448818898, 61.023622047244096], [53.54330708661418, 47.24409448818898, 5.905511811023622]]  
“----”  
[Point3d(47.2441, 0, 5.90551), [47.24409448818898, 0.0, 5.905511811023622], [48.818897637795274, 11.811023622047244, 5.905511811023622], [50.39370078740158, 23.62204724409449, 5.905511811023622], [51.96850393700788, 35.43307086614173, 5.905511811023622], [53.54330708661418, 47.24409448818898, 5.905511811023622]]

As you can see my data changed after a refresh.  
How can I solve my problem. Your help will be highly appreciated.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 11, 2021, 9:51am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/2 "2021-02-11T09:51:28Z")

</div>

(I moved this topic to right category)  
Something like this? :

```ruby
def create_circle( cp )
  cir = @grp.entities.add_circle cp, [0, 0, 1], (1)
  face1 = @grp.entities.add_face(cir)
  face1.reverse! if face1.normal == [0, 0, -1]
end

def add_geometry
  @grp = Sketchup.active_model.active_entities.add_group
  i = -1 
  mytimer = UI.start_timer(0.1, true) {
    i += 1
    cp = [0, i, i]
    create_circle( cp )
    Sketchup.active_model.active_view.invalidate
    UI.stop_timer(mytimer) if i > 10
  }
end
add_geometry

```

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 11, 2021, 10:40am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/3 "2021-02-11T10:40:13Z")

</div>

It seems “UI.stop\_timer” doesn’t work.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 11, 2021, 10:44am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/4 "2021-02-11T10:44:30Z")

</div>

I just copy-pasted my code above to Ruby Console and this is the result:

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/1/1/1130379f5c1a32e023a5d33e28145828a927485c.png)

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 11, 2021, 10:48am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/5 "2021-02-11T10:48:32Z")

</div>

![Screenshot 2021-02-11 141704](https://global.discourse-cdn.com/sketchup/original/3X/6/2/62820b616934d97575261ee0529583cad94e6a87.png)

“i” was 184 and increasing.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 11, 2021, 10:57am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/6 "2021-02-11T10:57:40Z")

</div>

Try to paste directly into **Ruby Console**. NOT to Ruby Code Editor!!

The problem is caused by Ruby Code Editor.

If you paste the code to Ruby Code Editor without the last line, then run it.  
then type to **Ruby Console**. : `add_geometry`  
it will work.

BTW: As far as I remember Dan already warned you about the restriction and worries about Ruby Code Editor…

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 11, 2021, 3:07pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/7 "2021-02-11T15:07:56Z")

</div>

Sorry, I don’t have access to EWH!!! Let me check Ruby Console later…

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 11, 2021, 4:36pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/9 "2021-02-11T16:36:20Z")

</div>

I run it in consul and it works!!! I didn’t know we can run code in that consul. What is the ruby code editor problem? For a long time, I tried to used “UI.start\_timer” but the same problem.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 12, 2021, 5:40am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/11 "2021-02-12T05:40:51Z")

</div>

I changed my plugin to “rb” format and add it to SU extensions but unfortunately, the same problem happens!!!

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 12, 2021, 8:20am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/12 "2021-02-12T08:20:21Z")

</div>

Try this:  
[Dezmo\_test\_ani.rbz](https://forums.sketchup.com/uploads/short-url/vKk4Tkdr9AegeOH81POFrYzurQX.rbz) (1.6 KB)

> **Code**
>
> ```auto
> #main.rb
> module Dezmo_test_ani
> extend self
> @@loaded = false unless defined?(@@loaded)
> 
> def create_circle( cp )
> cir = @grp.entities.add_circle cp, [0, 0, 1], (1)
> face1 = @grp.entities.add_face(cir)
> face1.reverse! if face1.normal == [0, 0, -1]
> end
> 
> def add_geometry
> @grp = Sketchup.active_model.active_entities.add_group
> i = -1
> mytimer = UI.start_timer(0.1, true) {
> i += 1
> cp = [0, i, i]
> create_circle( cp )
> Sketchup.active_model.active_view.invalidate
> UI.stop_timer(mytimer) if i > 9
> }
> end
> 
> def run_plugin
> Sketchup.active_model.start_operation('Dezmo test ani', false)
> add_geometry
> mtimer = UI.start_timer(2, false) {Sketchup.active_model.commit_operation}
> end
>   
> unless @@loaded
> cmd1 = UI::Command.new("Dezmo Test Animation"){ run_plugin }
> menu = UI.menu("Plugins").add_item(cmd1)
> @@loaded = true
> end
> 
> end # module
> 
> ```
> 
> ```auto
> #Dezmo_test_ani.rb
> module Dezmo_test_ani
> 
> extension_name = 'Dezmo test_ani'
> EXT_SW = SketchupExtension.new(extension_name, 'Dezmo_test_ani/main.rb')
> 
> EXT_SW.description = 'Dezmo test_ani'
> EXT_SW.version = 'Beta1 2021_0212_xxxx'
> EXT_SW.creator = 'Dezmo (dezmo at freemail dot hu)'
> EXT_SW.copyright = '2021, Dezmo'
> # Register the extension with Sketchup.
> Sketchup.register_extension EXT_SW, true
>   
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 12, 2021, 9:47am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/13 "2021-02-12T09:47:21Z")

</div>

Dear Dezmo,  
In following codes, when I used for loop, it works well in Code Editor and Console and rbz. But When I use the timer loop it cannot work in all. Also it shows endless error loop same as problem that I had in Ruby Code Editor.

```ruby
        #i = 0
        #mytimer = UI.start_timer(0, true) {
        for i in 0...@rn
          cp = [@pt[i].x, @pt[i].y, @pt[0].z + i * @rr]
          cir = grp1.entities.add_circle cp, [0, 0, 1], (2.5.cm)
          face1 = grp1.entities.add_face(cir)
          face1.reverse! if face1.normal == [0, 0, -1]
          face1.pushpull @rh
          cp = [@pt[i].x, @pt[i].y, @pt[0].z + i * @rr + @rh]
          @pt[i].z = @pt[0].z + i * @rr + @rh if i != 0
          cir = grp1.entities.add_circle cp, [0, 0, 1], (4.cm)
          face1 = grp1.entities.add_face(cir)
          cir = grp1.entities.add_circle cp, [1, 0, 0], (4.cm)
          face1.followme cir
          #i += 1
          #Sketchup.active_model.active_view.invalidate
          #UI.stop_timer(mytimer) if i > @rn
        #}
        end

```

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 12, 2021, 11:33am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/14 "2021-02-12T11:33:10Z")

</div>

The answer can be find by in my previous post.  
When you call the add geometries method from other methods which is contain `start_operation` -` commit_operation` you have wait for the timer loop to end. Therefore you should put the `commit_operation` into other timer (with repeat = false parameter) which is longer than drawing the rail.

Beside that:

> [@majid866](#):
>
> `UI.start\_timer(_ **0** _, true)

makes no sense here, you shout put non zero value for the timer

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 12, 2021, 1:08pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/15 "2021-02-12T13:08:53Z")

</div>

… for example something like :

```auto
      def onLButtonDown(flags, x, y, view)
        Sketchup.active_model.start_operation "onLButtonDown"
.
.

        draw_rail if @st == @rn
        timer_time = (@rn * 0.1) + 1
        mtimer = UI.start_timer(timer_time, false) {Sketchup.active_model.commit_operation}
      end

```

So actually the “waiting time” (`timer_time` ) should be more then the loop time during creating the geometry plus some safety time. (I assume 0.1 sec is the timer loop time in the circles creation loop)

BTW. I think better to change the “onLButtonDown” for the operation to more talkative words like “Drawing rail”.  
You know it will be shown in the UI when the user hover the mouse over the undo icon.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 12, 2021, 1:19pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/16 "2021-02-12T13:19:48Z")

</div>

Following codes work well.

```ruby
  @grp = Sketchup.active_model.active_entities.add_group
  i = 0
  mytimer = UI.start_timer(0, true) {
    cp = [0, i, i]
    cir = @grp.entities.add_circle cp, [0, 0, 1], (1)
    face1 = @grp.entities.add_face(cir)
    face1.reverse! if face1.normal == [0, 0, -1]
    Sketchup.active_model.active_view.invalidate
    i += 1
    UI.stop_timer(mytimer) if i > 10
  }

```

You can make a longer loop and set timer time to zero but it works. Before I used timer and ruby code editor works well but now cannot!!! Before I used to refresh and works well but now cannot!!! Maybe I have another problem with my own system.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 12, 2021, 1:34pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/17 "2021-02-12T13:34:14Z")

</div>

> [@majid866](#):
>
> timer time to zero but it works.

Hm. 🤔 Then, I guess there is a default minimum time “build in” to this method…

> [@majid866](#):
>
> Before I used timer and ruby code editor works well but now cannot!!! Before I used to refresh and works well but now cannot!!! Maybe I have another problem with my own system.

Probably there is it’s own `start_operation` - ` commit_operation` inside the Ruby Code editor. Therefore the timer loop will affect it.

It seams if the operation still running when you call ` commit_operation` make this problem. So if you wait till the timer loop ends before put the ` commit_operation` , you should be okay…😉

…but not with Ruby code editor ☹

---

<div class="post-metadata">

**Author:** ![sWilliams](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sWilliams](https://forums.sketchup.com/u/sWilliams)\
**Post date:** [February 12, 2021, 2:33pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/18 "2021-02-12T14:33:31Z")

</div>

This snip of code will demonstrate your problem

```ruby
  @grp = Sketchup.active_model.active_entities.add_group

  mytimer = UI.start_timer(0, true) {
   puts @grp.deleted?
   UI.stop_timer(mytimer) 
  }
 

```

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 12, 2021, 3:02pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/19 "2021-02-12T15:02:39Z")

</div>

Thank you for your attention. Answer is “true” when I use “Ruby Code Editor” and answer is “false” when I use Ruby Console!!! Can you tell me what is my problem?  
Following codes work well in Ruby Code Editor…

```ruby
  i = 0
  mytimer = UI.start_timer(0, true) {
    @grp = Sketchup.active_model.active_entities.add_group if i == 0
    cp = [0, i, i]
    cir = @grp.entities.add_circle cp, [0, 0, 1], (1)
    face1 = @grp.entities.add_face(cir)
    face1.reverse! if face1.normal == [0, 0, -1]
    Sketchup.active_model.active_view.invalidate
    i += 1
    UI.stop_timer(mytimer) if i > 10
  }

```

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [February 12, 2021, 4:34pm UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/20 "2021-02-12T16:34:45Z")

</div>

SketchUp is probably deleting the group because there is nothing it in (from it’s point of view.)

ALWAYS add a **temporary** cpoint at the origin of the group immediately after creating a new group. You can delete the cpoint after the geometry has been successfully created.

---

<div class="post-metadata">

**Author:** ![majid866](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/majid866/32/210257_2.png) [@majid866](https://forums.sketchup.com/u/majid866)\
**Post date:** [February 13, 2021, 4:50am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/21 "2021-02-13T04:50:21Z")

</div>

Unfortunately, my problem is bigger than it. timer and refresh both crash my data. Please see my following data and code.  
Code:

```ruby
        p @pt
        mytimer = UI.start_timer(0, false) {
        p "------"
        p @pt        

```

Data:  
[Point3d(48.1948, 0.254743, 5.90551), [49.53193343476063, 12.002080988018594, 5.905511811023622], [50.86905798537848, 23.74941882140872, 5.905511811023622], [52.206182535996334, 35.49675665479885, 5.905511811023622], [53.54330708661418, 47.24409448818898, 5.905511811023622], [53.54330708661418, 47.24409448818898, 5.905511811023622]]  
“------”  
  
As you can see @pt deleted. Also, I check it with SU older version the same problem happens.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 13, 2021, 9:11am UTC](https://forums.sketchup.com/t/view-refresh-problem/152840/22 "2021-02-13T09:11:36Z")

</div>

It is really difucult to guess what is the problem. Personally I do not understand what "_crash my data. "_ means?

I don’t know too: what should be the `@pt` originally? In the printout I see it is a `Array` with a mixed type of objects in it. The first element is `Point3D`, all the other are `Array`s with coordinates. Why do you mix it?

At last but not least, your code contains a syntax error. The 2nd line does not contain end-of-input, expecting ‘}’, and there is nothing to do in the timer method.

As far as I see you does not take care about the syntax in the forum post, that could be one case in your code too.

[Next page](https://forums.sketchup.com/t/view-refresh-problem/152840.md?page=2)
