• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. How to sort rectangles/track (shapes) in a list: left to...

Stats

  • Locked Locked
  • Replies 6
  • Subscribers 143
  • Views 2552
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to sort rectangles/track (shapes) in a list: left to right and/or top to bottom?

Sheppie
Sheppie over 2 years ago

Hi,

I'm working on code for a pcell, in which I generate an array of rectangles in, for instance M1, and M2. The M1 rectangles are multiple rows and wider than tall, the M2 in multiple columns, and taller than wide.

I have a list for the M1 rectangles (containing the database-IDs of the rectangles) and a separate list for the M2 rectangles (also database-ID's).

I want to sort the M1 list in such a way that the first entry of the list is the M1 rectangle at the top ("northern" most) and the lowest ("southern" most) rectangle as last entry. All other neatly sorted in between of course.

I want to sort the M2 list in such a way that the first entry of the list is the M2 rectangle at the left ("western" most) and the the wan furthes to the right ("eastern" most) rectangle as last entry. All other neatly sorted in between of course.

I think I could come up with a very nice, but elaborate/long/complicated/not-understandable/over-complicated/highly-specific/not-efficient, piece of SKILL code to do this. However, I can imagine that there is a very neat/general piece of code/build-in-procedure that I can use to do this. Does anybody know the existence of this, or has done this before and is willing to share this with me? That would be highly appreciated.

Thanks in advance.

With kind regards,

Sjoerd

  • Cancel
  • p94todorov
    p94todorov over 2 years ago

    Hi Sjoerd,

    you can sort any data by creating a custom lambda function to compare the desired properties. For your example it would look like this:

    M1shapes = sort(M1shapes lambda((shape1 shape2) topEdge(shape1->bBox) > topEdge(shape2->bBox)))
    M2shapes = sort(M2shapes lambda((shape1 shape2) leftEdge(shape1->bBox) < leftEdge(shape2->bBox)))

    You can put arbitrarily complex expressions within the lambda functions and use them to re-order your data.

    Regards,

    Petar

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • mbracht
    mbracht over 2 years ago

    Like Petar said - customizing sort() is the solution. Below is a chunk of code that orders figures (an instance is a figure)  left to right and top to bottom:

    (sort M1
       (lambda (s1 s2)
          (let  ( (x1 (xCoord (car s1->bBox)))
                   (x2 (xCoord (car s2->bBox)))
                   (y1 (yCoord (car s1->bBox)))
                   (y2 (yCoord (car s2->bBox))))
             (cond
                (y1==y2
                   (if x1<=x2 t nil))
                (y1<=y2 t nil)))))

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to p94todorov

    Hi Petar, Thank you very much for your quick response. I'll test it later today and will report back. I haven't really had a use for lambda before, so I will be most interested in getting this to work for the first time. Once again, thank you. Kind regards, Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to mbracht

    Hi Max, thank you too of course. Like I wrote in my response to Petar, I'll give it a try and will report back. Much appreciated. With kind regards, Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to mbracht

    Hi, It works perfectly! Thank a lot. This is something I wouldn't have come up with myself. Very nice solution. With kind regards, Sjoerd

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Sheppie
    Sheppie over 2 years ago in reply to p94todorov

    Hi, although I ended up using the solution of Max, I first tested it with your version, and that worked flawlessly. Thanks a lot. Now that I have seen how the lambda function can be used, I'll go over some of my recent code to try to apply something similar to other pieces of code. So far, the setof() function was my favorite (very powerful to select anything), but this may be better. With kind regards, Sjoerd

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information