Skip to content

Instantly share code, notes, and snippets.

@linuxaged
Created May 2, 2013 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxaged/5501057 to your computer and use it in GitHub Desktop.
Save linuxaged/5501057 to your computer and use it in GitHub Desktop.
Clip or Trim Terrian Mesh(SketchUp Pro)
#------------------------------------------------------------------------------------------------
# Permission to use, copy, modify, and distribute this software for
# any purpose and without fee is hereby granted.
#------------------------------------------------------------------------------------------------
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#------------------------------------------------------------------------------------------------
# IT WAS CREATED AND TESTED IN A WINDOWS ENVIRONMENT ONLY AND MAY NOT FUNCTION ON A MAC.
#------------------------------------------------------------------------------------------------
# THIS PLUGIN IS ANOTHER FIGMENT OF MY IMAGENATION AND IS NOT BASED ON OR COMPLY WITH ANYTHING!
#------------------------------------------------------------------------------------------------
# Name: Clip or Trim Terrian Mesh
# By: sdmitch
# Usage: To clip the overlapping portion of a GE terrain mesh or trim off the outer row/col.
# Note: Select the mesh you wish to keep then the mesh you wish to clip. In steep areas where
# a higher resolution patch is placed upon a larger low resolution, there will be vertical
# gaps. No attempt is made to close them by this plugin.
# Date: Oct 2011
#------------------------------------------------------------------------------------------------
#
require 'sketchup'
#
module SDM
class Clip_or_Trim_Terrain
def initialize
@ip = Sketchup::InputPoint.new
@ip1 = Sketchup::InputPoint.new
@Mod = Sketchup.active_model
@Ent = @Mod.active_entities
@Sel = @Mod.selection
begin
layers=@Mod.layers
layers["Google Earth Snapshot"].visible=false
layers["Google Earth Terrain"].visible=true
rescue
UI.messagebox "Model does not contain a Google Earth Terrain layer"
Sketchup.send_action "selectSelectionTool:"
end
@Trim_It=true
self.reset
end
def reset
@grps=[]
@state = 0
@best = nil
@ip1.clear
@Sel.clear
@drawn = false
Sketchup::set_status_text("", SB_VCB_LABEL)
Sketchup::set_status_text("", SB_VCB_VALUE)
end
def set_current_point(x, y, view)
if( !@ip.pick(view, x, y, @ip1) )
return false
end
@ip1.copy! @ip
view.tooltip = @ip.tooltip
Sketchup::set_status_text("Select the mesh to Trim. Press Shift to Clip", SB_PROMPT) if @state == 0 && @Trim_It
Sketchup::set_status_text("Select the mesh to keep. Press Shift to Trim", SB_PROMPT) if @state == 0 && !@Trim_It
Sketchup::set_status_text("Select the mesh to clip or Right Click in space to start over", SB_PROMPT) if @state == 1
end
def onMouseMove(flags, x, y, view)
self.set_current_point(x, y, view)
ph = view.pick_helper;ph.do_pick(x, y)
@best = ph.best_picked if ph.best_picked != @best
view.invalidate
end
def onLButtonDown(flags, x, y, view)
case @state
when 0
if @best.is_a?(Sketchup::Group)
@grps[0]=@best; @Sel.add @best
bb=@grps[0].bounds;zmin=bb.min.z-100.feet;zmax=bb.max.z+100.feet
verts=[];@cpts=[]
@grps[0].entities.each{|e|
if e.is_a?(Sketchup::Edge)
verts<<e.vertices
end
}
verts.flatten!.uniq!
verts.collect!{|v| v.position}
verts.each{|v| v.z=bb.min.z}
for i in 0..3
min_dist=1e9;
for v in verts
dist=v.distance bb.corner(i)
if dist < min_dist
min_dist=dist
@cpts[i]=Geom::Point3d.new(v.x,v.y,zmin)
@cpts[i+4]=Geom::Point3d.new(v.x,v.y,zmax)
end
end
end
end
if @Trim_It && UI.messagebox("Trim this Mesh?",MB_YESNO)==6
@Mod.start_operation "Trim Mesh",true
vec=Geom::Vector3d.new(0,0,100.feet);plns=[];@Sel.clear
plns[0]=Geom.fit_plane_to_points(@cpts[0],@cpts[1],@cpts[1]+vec)
plns[1]=Geom.fit_plane_to_points(@cpts[2],@cpts[0],@cpts[0]+vec)
plns[2]=Geom.fit_plane_to_points(@cpts[3],@cpts[2],@cpts[2]+vec)
plns[3]=Geom.fit_plane_to_points(@cpts[1],@cpts[3],@cpts[3]+vec)
@grps[0].entities.each{|e|
if e.is_a?(Sketchup::Edge)
for i in 0..3
if ((e.start.position.distance_to_plane(plns[i])<3.feet) or (e.end.position.distance_to_plane(plns[i])<3.feet))
@Sel.add e
end
end
end
}
@Ent.erase_entities(@Sel)
@Mod.commit_operation
self.reset
else
@state=1
end
when 1
if @best.is_a?(Sketchup::Group) && @best!=@grps[0]
@grps[1]=@best; @Sel.clear
@Mod.start_operation "Clip Mesh",true
poly=[@cpts[0],@cpts[2],@cpts[3],@cpts[1]]
fgrp=@Ent.add_group ;fent=fgrp.entities;
fent.add_face(@cpts[0],@cpts[4],@cpts[5],@cpts[1]);# FRONT
fent.add_face(@cpts[0],@cpts[2],@cpts[6],@cpts[4]);# LEFT
fent.add_face(@cpts[1],@cpts[5],@cpts[7],@cpts[3]);# RIGHT
fent.add_face(@cpts[2],@cpts[3],@cpts[7],@cpts[6]);# BACK
fent.intersect_with(true,fgrp.transformation,@grps[1],@grps[1].transformation,true,@grps[1])
@grps[1].entities.each{|e|
if e.is_a?(Sketchup::Edge)
if e.hidden?
p0=e.start.position;p1=e.end.position
if (Geom.point_in_polygon_2D(p0,poly,false) && Geom.point_in_polygon_2D(p1,poly,true)) || \
(Geom.point_in_polygon_2D(p1,poly,false) && Geom.point_in_polygon_2D(p0,poly,true))
@Sel.add e
end
else
e.soft=true;e.smooth=true
end
end
}
@Sel.add fent.to_a; @Ent.erase_entities(@Sel)
@Mod.commit_operation
self.reset
else
UI.beep; puts "same group selected"
end
end
view.lock_inference
end
def onCancel(flag, view)
view.invalidate if @drawn
Sketchup.send_action "selectSelectionTool:"
end
def onRButtonDown(flags,x,y,view)
Sketchup.send_action "selectSelectionTool:" if @state == 0
self.reset
end
def draw(view)
@drawn = false
if( @ip.valid? && @ip.display? )
@ip.draw(view)
@drawn = true
end
# view.invalidate if @drawn
end
def onKeyDown(key, repeat, flags, view)
puts "Key #{key} pressed"
if key==CONSTRAIN_MODIFIER_KEY #16 || 131072 #shift PC || MAC
@Trim_It=!@Trim_It
end
end
end # of class Clip_or_Trim
end
# ------------------ MENU SETUP ---------------------- #
unless $sdm_tools_menu
$sdm_tools_menu = UI.menu("Plugins").add_submenu("SDM Tools")
$sdm_Edge_tools = $sdm_tools_menu.add_submenu("Edge Tool")
$sdm_Face_tools = $sdm_tools_menu.add_submenu("Face Tool")
$sdm_CorG_tools = $sdm_tools_menu.add_submenu("CorG Tool")
$sdm_Misc_tools = $sdm_tools_menu.add_submenu("Misc Tool")
end
unless file_loaded?(__FILE__)
$sdm_Misc_tools.add_item("Clip/Trim GE") { Sketchup.active_model.select_tool SDM::Clip_or_Trim_Terrain.new }
file_loaded(__FILE__)
end
# ------------------------------------------------------ #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment