# Get orientation of face under cursor

**URL:** <https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803>\
**Category:** Ruby API\
**Created:** [July 6, 2017, 7:34am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803 "2017-07-06T07:34:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![b.muench](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@b.muench](https://forums.sketchup.com/u/b.muench)\
**Post date:** [July 6, 2017, 7:34am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/1 "2017-07-06T07:34:45Z")

</div>

Hi all,

I’m trying to build a tool that lets the user configure an object via dialog box and then place this object using the mouse.  
I’m using SketchUp Make, so dynamic components ar not an option.  
Now, what I’d like to have is that the object sticks to the cursor before being placed and changes orientation based on the orientation of the face under the cursor.  
At this point I’m able to draw the object so that it moves with the cursor, but it does not have the correct materials applied until placed, it’s just white.  
The other problem I have is I don’t know how to get the orientation of faces under the cursor.

Could somewone point me the direction?

Thanks  
Benny

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [July 6, 2017, 8:19am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/2 "2017-07-06T08:19:21Z")

</div>

> [@b.muench](#):
>
> The other problem I have is I don’t know how to get the orientation of faces under the cursor.

Use [`view.pick_helper`](http://ruby.sketchup.com/Sketchup/View.html#pick_helper-instance_method) ([PickHelper](http://ruby.sketchup.com/Sketchup/PickHelper.html)) to get the picked face. Since the face’s normal is in the local coordinate system, you need to apply the transformation. So you better search `all_picked` to find a face and its index, then lookup the [`transformation_at`](http://ruby.sketchup.com/Sketchup/PickHelper.html#transformation_at-instance_method) that index.

> [@b.muench](#):
>
> but it does not have the correct materials applied until placed, it’s just white.

Is this display degradation (for performance reasons, just like when orbiting quickly a complex model) or is the component definition imported from a model without materials?

---

<div class="post-metadata">

**Author:** ![b.muench](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@b.muench](https://forums.sketchup.com/u/b.muench)\
**Post date:** [July 6, 2017, 9:46am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/3 "2017-07-06T09:46:38Z")

</div>

Thank you, Aerilius, for your answer. the object now rotates just how I wanted it.

Regarding the other problem, some more information:  
At the moment I view.invalidate onMouseMove and draw the object in def draw(view) using view.draw(GL\_POLYGON…  
What I don’t know is how to apply a material to the polygon or if this it the right approach at all.  
Because onLButtonDown I create all the faces etc. again to actually place them using almost the same code…

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [July 6, 2017, 10:44am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/4 "2017-07-06T10:44:16Z")

</div>

So you are drawing a preview of the object (using “virtual geometry” like vectors and Open GL methods) instead of actually having real entities. This is generally good practice because entities can interact with other entities (edge breaking and merging) or trigger observers.

The problem is that we can draw polygons only with a color. There was discussion about adding a method to draw textures (for `draw2d` as rectangular image) but I don’t see in the docs that this has been implemented.

As a preview, a simplified visual representation might be acceptable. You can [add shading](https://github.com/thomthom/shader-tools) by computing the polygon color depending on its normal (and the light source’s position).

If you really need to display it with textures, you can insert the object as a component entity (textured using [`Face#position_material`](http://ruby.sketchup.com/Sketchup/Face.html#position_material-instance_method)) and move it. To avoid cluttering the undo stack, wrap it into an [operation](http://ruby.sketchup.com/Sketchup/Model.html#start_operation-instance_method) where either each subsequent move is in a “transparent” operation (where subsequent moves are merged into the previous operation) or where you abort the operation before the next move and start a new one.

---

<div class="post-metadata">

**Author:** ![b.muench](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@b.muench](https://forums.sketchup.com/u/b.muench)\
**Post date:** [July 6, 2017, 11:15am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/5 "2017-07-06T11:15:14Z")

</div>

Alright, thank you very much for clarifying!  
I think I will use simple colours for the preview. I don’t really want to work with components if I don’t have to. I just wondered if it was not possible to use materials with OpenGL methods or I did anything wrong.

While I’m here: I have another question regarding materials:  
When I try to assign any standard material (like "Farbe “03” in a german environment, in english probably “Color M03”) to a face it won’t work. I have to save this material with a custom name and use this one. What could I possibly do wrong?

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [July 6, 2017, 11:46am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/6 "2017-07-06T11:46:51Z")

</div>

is the material in the model?

if not you need to load it…

```ruby
# Load a material from the shipped SketchUp library. (SketchUp 2016)
# I think the shipped files will all use English filenames...
filename = 'Materials/Colors-Named/0004_HotPink.skm'
path = Sketchup.find_support_file(filename)
materials = Sketchup.active_model.materials
material = materials.load(path)

# I assume this will be translated
puts material.display_name

```

once it’s in the model you may be able to use the either `material.name` or `material.display_name` to call it…

it’s always best to use single quotes unless using `"#{ expand_some_var }"`

john

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [July 6, 2017, 12:13pm UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/7 "2017-07-06T12:13:32Z")

</div>

You were trying to use a material from a materials library that is not in the model. If you are just using plain colors, you can of course also [directly assign a color name or RGB](http://ruby.sketchup.com/Sketchup/Material.html) values or [create a material](http://ruby.sketchup.com/Sketchup/Materials.html#add-instance_method) and set its color from name or RGB.

---

<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:** [July 6, 2017, 1:13pm UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/8 "2017-07-06T13:13:34Z")

</div>

> [@b.muench](#):
>
> I’m trying to build a tool that lets the user configure an object via dialog box and then place this object using the mouse.

It sounds like to me you would learn a lot by examining the example parametric extensions by the SketchUp Team. One is the Shapes extension, another is a Window extension. There are several others as well.

[http://extensions.sketchup.com/en/users/sketchup-team](http://extensions.sketchup.com/en/users/sketchup-team)

> [@Aerilius](#):
>
> If you really need to display it with textures, you can insert the object as a component entity (textured using Face#position\_material) and move it.

Just for general information, a material can be assigned to a component definition, and this definition material is used _temporarily_ as the instance is attached to the cursor, but as soon as the instance is placed the use of the definition material ceases. This is good way to use highlight colors.

> [@b.muench](#):
>
> Now, what I’d like to have is that the object sticks to the cursor before being placed and changes orientation based on the orientation of the face under the cursor.

Component Instances will do this **automatically** , if you set the **`snap_to`** property of the definition’s behavior. Say for example that you wish to place a framed photograph component instance whose definition is set to **`snap_to`** vertical.

- [**`Sketchup::Behavior.snap_to()`**](http://ruby.sketchup.com/Sketchup/Behavior.html#snapto=-instance_method)

- [**`Sketchup::Model.place_component()`**](http://ruby.sketchup.com/Sketchup/Model.html#place_component-instance_method)

There are some threads here already:

> [@\[code\] undoable place\_component() "MoveTool" clone](https://forums.sketchup.com/t/code-undoable-place-component-movetool-clone/27193):
>
> Continuing the discussion from: [Model.place\_component component\_definition, boolean cannot be undone?](http://forums.sketchup.com/t/model-place-component-component-definition-boolean-cannot-be-undone/17165): Here is an example of a single undoable place component operation. It is not a tool, rather it is a command. It achieves the single undo, via 3 linked operations. The first is attached to the following model#place\_component() operation, and the last is attached to it’s predecessor. It can move both compound (groups, images and component instances,) and primitive objects. [move\_selection\_se…](https://forums.sketchup.com/uploads/short-url/llfZG7k6oVZGf4L0mFjN4KAXWe3.rb)

> [@I am trying to figure out how to place a component with a button press](https://forums.sketchup.com/t/i-am-trying-to-figure-out-how-to-place-a-component-with-a-button-press/43326):
>
> Hello All I am pretty good at working with sketch up. I am trying to get into dynamic components and ruby programming I am a wood worker, I have made a dynamic component which will create pieces of lumber that has all the parameters I need. I would like to do the following via ruby Add a button to the toolbar on Sketchup 2017 Upon Button Press Take a (Specific Dynamic Components) and put it at some Point (origin or other) in the Model Show Component Options Window for the just placed ob…

> [@Recreate Move Tool](https://forums.sketchup.com/t/recreate-move-tool/26659/):
>
> I’m new to making Tools, and I need a tool that essentially does what the move tool does, it would be easy to make a tool that moves from point A to point B, but where I am lost is actually showing the object you are moving while you are moving it.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sketchup/original/3X/f/2/f2a121500b085ed60806607a82720b092d800bb3.png) [@system](https://forums.sketchup.com/u/system)\
**Post date:** [October 6, 2017, 5:13am UTC](https://forums.sketchup.com/t/get-orientation-of-face-under-cursor/47803/9 "2017-10-06T05:13:39Z")

</div>

Closed on October 6, 2017, 5:13am UTC

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.
