How to convert face to component without the change of material and position

sketchup

#1
  def faces_to_components(parent, faces)
    entities = nil
    if parent.is_a? Sketchup::Model
      entities = parent.entities
    elsif parent.is_a? Sketchup::ComponentInstance
      entities = parent.definition.entities
    elsif parent.is_a? Sketchup::Group
      entities = parent.entities
    else
      return 
    end

    while !faces.empty?
      connected_faces = faces.first.all_connected.grep(Sketchup::Face)
      component = entities.add_group(connected_faces).to_component
      puts "create component #{component.definition.name}-#{parent.definition.name}"
      faces -= connected_faces
    end
  end

but the components which are converted from faces are all in origin and they haven’t been any materials.


#2

If the material is positioned on the face it should stay in the same place. When faces have no given texture positioning however the texture is rendered according to the local axes. Since newly created group gets their origin in the bottom left corner of the bounding box the un-positioned materials will appear to have moved.

I haven’t been using the texture positioning part of the API for many years but remember I found it could hard to use. I don’t even think there is a way to determine whether a face actually have a texture position saved to it or not :open_mouth: (but I could remember it wrong). One way to solve this could however be to make sure all faces have a defined texture position.

Another solution is to make sure the newly created groups’ axes all line up perfectly with the axes of the parent drawing context. To achieve this just transform all entities inside each group with the group’s own transformation and set the groups transformation to the identity transformation.


#3

can you give me an example for your last method? i have tried many method, but none works.


#4
def faces_to_components(faces)
  parent = faces.first.parent
  entities = parent.entities

  while !faces.empty?
    connected_faces = faces.first.all_connected.grep(Sketchup::Face)
    component = entities.add_group(connected_faces).to_component
    child_entities = component.definition.entities
    child_entities.transform_entities(component.transformation, child_entities.to_a)
    component.transformation = IDENTITY
    faces -= connected_faces
  end
end

This code works for me. Beware that when faces are located inside a group or component and the user is inside that drawing context, SketchUp will suddenly use global coordinates, as opposed to returning the actual internal value. This may result in textures being positioned according to the model axes, not according to their former parent axes.


#5

thank you. your code will be very helpful for me.


#6

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