Type Change after ShapeProcessor ?

Hi Matthias,

when I have an array of shapes and ask shapes[i].is_polygon? or any of the other .is_???, I usually get true or false.
However, when I pass the array of shapes through a ShapeProcessor, the resultshapes[i].class.to_s still says RBA::Polygon, but resultshapes[i].is_polygon? hangs up the code. Getting the ShapeProcessor to merge already required a strange shape re-collecting. Below is the merge function with the shape processor in it. Do you have a better suggestion on how to implement this ? s1 are shapes of 1 layer, and I would like to merge them no matter what they are (except text of course). The code also worked the same way without the Polygon::new re-assembly after merge. After that, I pass it on to other functions that call .is_xxx? and that is where it sours. If exlude this mergeShapes(s1) function, everything runs fine.
I know this isn't a lot of information go by, but if you have a suggestion that would help me already.

Thank you !

Thomas.


   def mergeShapes(s1)
            retVal = []
            if s1.size>0
                    shapes = []
                    s1.each do |shape|
                            shapes << shape
                    end
                    ep = RBA::ShapeProcessor::new
                    begin
                            newshapes = ep.merge_to_polygon(shapes, 0, true, true)
                                newshapes.each do |newshape|
                                   retVal << RBA::Polygon::new(newshape)
                            end
                    rescue => ex
                            puts "MERGE FAILED: " + ex.to_s
                            retVal = s1
                    end
            else
                    retVal = s1
            end
            return retVal
    end        

Comments

  • I need to explain a basic concept here:

    In KLayout, there is a "Shape" object and then there are "working objects" (Polygon, Path, Box, ...).

    A "Shape" is a geometrical object living inside the database (the "Layout" object). You can think of "Shape" as a pointer. When you manipulate a Shape object, you directly change the layout. When you retrieve geometry from the layout, you will see Shape objects. Shape objects are polymorphic. Then can be polygons, boxes etc. Shape has "is_polygon?" to tell you whether it is a polygon.

    The "working objects" are "free": they are not bound to the database. A Polygon is always a polygon, hence it does not need a "is_polygon?" method.

    The ShapeProcessor processes Shapes - i.e. it takes them directly from the database. It's output will consist "free" polygons. Hence, the input is Shape, the output is Polygon.

    So yes: the type of the shape changes.

    Matthias

  • Gotcha ! This did the trick. Thank you !!!

        def mergeShapes(s1)
                retVal     = RBA::Shapes::new
                textShapes = RBA::Shapes::new
                shapes     = []
                if s1.size>0
                        s1.each do |shape|
                                if !shape.is_text
                                        shapes << shape
                                else
                                        textShapes.insert(shape)
                                end
                        end
                        ep = RBA::ShapeProcessor::new
                        begin
                                newpolygons = ep.merge_to_polygon(shapes, 0, true, true)
                                newpolygons.each do |newpolygon|
                                        #shape = RBA::Polygon::new(newpolygon); retVal.insert(shape)
                                        retVal.insert(newpolygon)
                                end
                        rescue => ex
                                puts "MERGE FAILED: " + ex.to_s
                                retVal = s1
                        end
                        if textShapes.size>0
                                retVal.insert(textShapes)
                        end
                else
                        retVal = s1
                end
                return retVal
        end 
    
Sign In or Register to comment.