Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Thursday, August 11, 2011

Hidden Corners of SketchUp's Ruby API: Transforming Vertices

Thursday, March 12, 2009

Deleting Unnecessary Edges



Have you ever opened a SketchUp file that you didn’t create, or imported a model from another application, and found that it has hundreds of needless hidden or softened lines separating coplanar faces? Wish you could get rid of them with a simple ruby script? Now you can!

First, set up a few variables:

def variables
@group_list = []
@component_list = []
@delete_these = []
@model_list = Sketchup.active_model.entities
end

Then write a method that determines if an edge is 1) hidden or softened, and 2) bordered by two co-planar faces. If the edge meets each of those requirements, it is added to @delete_these list.

def delete_soft_lines(e)
# Determine if the edge is soft or hidden, and if it is bordered by
# two faces.

if ((e.soft? and e.faces.size == 2) or (e.hidden? and e.faces.size == 2))
# Determine if the two bordering faces are coplanar.
# Note: This will only catch cases where the front faces are pointing
# in the same direction.

if (e.faces[0].normal.dot(e.faces[1].normal) > 0.999999999)
# Add the edge to the list of entities that will be deleted.
@delete_these.push(e)
end
end
end

Now create a method to find all the edges in the model, including edges that are embedded in groups and components:

def traverse_model_entities
# @model_list will not be empty until all of the groups, components
# and edges have been traversed.

while @model_list != []
@model_list.each do |item|
# Determine if the entity is an edge, group, or component.
type = item.typename
case type
when "Edge"
# Determine if the edge should be deleted, and add it to the
# @delete_these list.

delete_soft_lines(item)
when "Group"
# Add each of the entities in the group into the group_list
# array.

item.entities.each do |entity|
@group_list.push entity
end
when "ComponentInstance"
# .entities can be called on a Component Definition, but not on
# a Component Instance.
# Use the .definition method to access the Component Definition
# that the instance belongs to.

item.definition.entities.each do |entity|
# Add each of the entities in the component into the
# component_list array.

@component_list.push entity
end
end
end
# Update the @model_list so it contains only the entities that were
# embedded in groups or components. These entities haven't been
# counted yet.

@model_list = @group_list + @component_list
# Clear out the group and component lists so they're ready for the
# next level of sub-groups/components.

@group_list = []
@component_list =[]
end
# The edges that need to be deleted have all been added to the
# @delete_these list, so erase those entities from the
# active SketchUp model.

Sketchup.active_model.entities.erase_entities(@delete_these)
end


Finally, add a “Delete unnecessary edges” item to Tools menu, and pop a dialog letting you know the edges were deleted:
def delete_edges
variables
traverse_model_entities
UI.messagebox("Edges deleted.")
end

UI.menu("Tools").add_item("Delete unnecessary edges") {delete_edges}

Monday, February 23, 2009

SketchUp Importers



There's a lot of 3D content out there, and we understand that a lot of it is not in the SketchUp file format. But, if you can read the content, you can use the Ruby Importer plugin API to import it into SketchUp.

It's easy to write and use your own Ruby importers in SketchUp. Just follow these steps:

1. Define a class with the plugin API methods.
2. Register an object of the class with SketchUp.
3. Recover from Mardi Gras festivities (may not be applicable).

An importer class must have the following methods:

def description
  # Returns a brief description of your importer plugin.
end

def file_extension
  # Returns the file extension of the file format your
  # importer can read (e.g. fbx, dae, etc.)
end

def supports_options?
  # Returns true if your plugin supports import options.
end

def do_options
  # Presents the import options dialog panel to the user.
end

def id
  # Returns the id string of your importer.
end

def load_file(file_name, show_summary)
  # Reads the contents of location on disk specified
  # by file_name.
  # Shows the summary dialog panel if show_summary is true.
  # Returns 0 on success, returns 1 on failure.
  0
end

Once you have defined your importer class, register your importer with SketchUp:

Sketchup.register_importer MyImporter.new

Now your importer plugin will be available when you select the Import item under the File menu of SketchUp. The Ruby Importer capability is available in both SketchUp Free and Pro. Now go forth and import.

Thursday, October 23, 2008

wxSU and You



A lot of you are already using the WebDialog of the SketchUp Ruby API to extend the user interface of SketchUp. Let's say you want something with a more native-application feel to it. Thanks to Peter Ellis of NREL, there's a neat plugin out there that allows you to do this. It comes with useful GUI elements that you can use in your own plugins such as a file-open dialog box, progress bar, etc.



It's built on top of wxRuby, so your UI implementation is cross-platform and you won't have to do any extra work. Best of all, it's FREE! Hats off to Peter.