Get orientation of face under cursor

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

Use view.pick_helper (PickHelper) 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 that index.

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?

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…

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 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) and move it. To avoid cluttering the undo stack, wrap it into an operation 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.

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?

is the material in the model?

if not you need to load it…

# 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

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 values or create a material and set its color from name or RGB.

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

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.

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.

There are some threads here already:

1 Like

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