Sketchup Ruby API引用已删除的Face

时间:2014-02-14 09:33:16

标签: ruby sketchup

大家好

我想创建一个Sketchup工具来移动多边形中的顶点(它与sketchup中的移动工具相同,除了它只是移动顶点而不是边缘)。此多边形位于多边形数组中。

我创建了一个“工具”类,当我选择一个第一个顶点时,将其移动到另一个位置,并创建相应的多边形,替换数组中的旧多边形。但是当我第二次选择一个顶点时,ruby控制台显示错误:引用已删除的Face

。 这是我的课。我在函数中实现了这个工具:onMouseMove,onLButtonDown,draw。

class MoveVertexPolygon
   # an array of polygon
   def gridview
      @gridview
   end
   def gridview=(g)
     @gridview=g
   end

   def initialize
     @polygon = nil
     @bSelected = false
     @arr_vertex = []
     @drawn = false
     @pos= -1
  end

def onLButtonDown(flags, x, y, view)
   #if user don't click any vertex before.
   if(@bSelected == false)
     ip = view.inputpoint x,y
     selected_x = ip.position.x.to_f
     selected_y = ip.position.y.to_f
     pt = Geom::Point3d.new(selected_x, selected_y, 0)

     #get appropriate polygon position in array of polygon 
     @pos = @gridview.getIndexByCoordinate(selected_x,selected_y)
     #reset array of vertex
     @arr_vertex.clear

     #get polygon with given position
     @polygon = @gridview.array_element[@res].polygon

     #select a vertex
     if(@polygon != nil && @polygon.valid? && @polygon.is_a? Sketchup::Face)
        type = @polygon.classify_point(pt)
        if(type == Sketchup::Face::PointOnVertex)
           i = 0
           while( i < @polygon.edges.length)
               @arr_vertex.push(@polygon.edges[i].vertices[0].position)
               if(pt == @polygon.edges[i].vertices[0].position)
                  @selectedVertexIndex = i
                  @bSelected = true
               end
              i = i + 1
           end;
        end
     end
  #if user selected vertex, move it to another position
  else
    @bSelected = false
    @drawn = false
    @gridview.array_element[@res].polygon = @polygon
  end
end

def onMouseMove(flags, x, y, view)
#if user select a vertex, then let's user choose a position of this vertex, display a temporary          #polygon
   if(@bSelected)
     @ip = view.inputpoint x,y
     view.invalidate
   end
end

def draw(view)
    if(@drawn == false)
       entities = view.model.entities

       temp_x = @ip.position.x.to_f
       temp_y = @ip.position.y.to_f
       pt = Geom::Point3d.new(temp_x, temp_y, 0)

       #delete all edges and face of @polygon
       i=0
       temp_array_edges = []
       while(i < @polygon.edges.length)
         temp_array_edges.push(@polygon.edges[i])
         i = i + 1
       end;
       len = temp_array_edges.length
       i=0
       while(i < len)
        entities.erase_entities temp_array_edges[i] 
        i = i + 1
       end;

       #draw polygon again
       @polygon = nil
       @arr_vertex[@selectedVertexIndex] = pt
       @polygon = entities.add_face @arr_vertex

     end
end

错误说@polygon是Deleted Face。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

不是不断地擦除和重新创建面部,而是简单地将顶点转换为它的新位置。 entities.transform_by_vectors会让你这样做。 http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities.php#transform_by_vectors

如果您想在使用工具时暂时显示任何内容,请使用view.draw绘制到视口。

除了在draw事件中绘制到视口之外,你真的应该避免做任何事情。