Weird behavior with EntitiesObserver
-
Hi guys,
I need to do a plugin that will be used to outline windows and doors in a matched photo. The idea is that the user click a button before starting the outlining, draw some rectangles and when he is finished, he clicks in the button again.
It looks like a simple thing to do: When the user clicks the button for the first time, I add an EntitiesObserver and add all "onElementsAdded" to an array. When he clicks the button again, I loop through the array and do what I have to do (in my case, simply putting this entities in the deserved layer).
The plugin works as expected when I create rectangles within a surface. The problem is when one of the edges of the rectangle touches one of the edges of a previous modeled object. Sketchup triggers the element added with the face of the previous modeled object.
I know it maybe hard to understand the problem, but I'm leaving instructions on how to reproduce in the end of the post.
I wonder if you guys have any ideas on what I could do.
Thanks!
To reproduce the error:
- Put the "bug.rb" file (uploaded here) in the plugin directory;
- Create a simple box;
- Click on plugin -> Start outlining window;
- Within a face of the box, draw a square, without touching any of the edges of the box;
- Click again on plugin -> Start outlining window;
- Take a look in the layers (activating the "Color by layer" helps a lot). The new square should be in the "Window" layer;
- Now, on another face of the box, redo step 3 to 5, but this time draw a rectangle and the bottom of the rectangle being drawn should touch the bottom of the box;
- Check the layers again, and the box's face should have gone to the window layer, while the window is in the main layer (sometimes it works though @_@);
-
hi
to avoid clashes geometry clashes, you need to add a 'group' , assign it to your new layer, then add your entities to the group.
have a look at linetool.rb and rectangle.rb for the 'tool' approach, and look at wrapping your code in your own namespace.
john
-
Yes as John said, in SketchUp, you ALWAYS should add primitives to Layer0, but within a group whose reference can be on ANY layer.
But there is a trick to using a group. Just after you create the empty group, you need to temporarily add an object in the next Ruby statement. We usually add a cpoint at the
ORIGIN
temp_cpt = grp.entities.add_cpoint(ORIGIN)
If you don't do this the SketchUp engine will delete the empty group, quite quickly.
When done adding entities to the group, simply call
temp_cpt.erase!
Now, what all this means is, you cannot rely upon the native tools, for your plugin, because there is no way, via the API, to enter into a group (or component instance,) editing context. (The API only has a close context function.)
So.. you will need to install the Examples (or Utilites,) from the SketchUp Team via the Extension Warehouse. An use the custom Ruby tool examples. Try modifying the LineTool or CylinderTool examples into you custom Rectangle Outline Tool. Or look search through this forum (or the SkechUcation Plugin Store,) for a custom rectangle tool that you might use as an example.
With a custom tool, you receive the points the user chooses, and then your tool creates the actual geometry within the group.
-
@robert, the SketchUp Team has a rectangle tool available in the Extension Warehouse.
At least it may give you ideas.http://extensions.sketchup.com/en/content/rotated-rectangle-tool
-
Hi Guys,
Thanks for the replies.
I wanted to do the way I described, cause that way the user could use any tool available to him. But, as it turns out, this seems undoable. I ended up creating my own line tool that would do what I wanted.
Advertisement