SketchUcation Plugin Store

 

 

One-liners

One-liners

Postby noelwarr » Sun May 06, 2012 10:53 am

Here is a chance to show off your ruby skills. One line of code that does something cool that you can copy and paste into the console. I'll keep editing this post adding your contributions so you can find the one-liners easily. The first is mine (very simple I know).

Reconstruct all possible faces from selected entities
Sketchup::active_model.selection.each{|ent| ent.find_faces if ent.is_a?(Sketchup::Edge)}

Run this befor printing
(av = Sketchup.active_model.active_view).camera.perspective = false; av.zoom_extents.zoom(1.052)

And here is a challenge: ArcCurves get lost on solid operations. Can you recreate them in one line of ruby? (Hint: If 3 or more edges are on the same plane and the angle between each is the same and less than or equal to 15, we can assume it is part of a lost ArcCurve)
Last edited by noelwarr on Thu May 10, 2012 12:37 pm, edited 6 times in total.
noelwarr
 
Posts: 64
Joined: Mon Jun 20, 2011 5:07 pm
Name: noelwarr

Re: One-liners

Postby Dan Rathbun » Sun May 06, 2012 3:05 pm

    The each iterator returns the receiver (upon which it acts,) not the result of the block.

    Try using one of the iterators from the Enumerable mixin module (which is mixed into most of the API collection classes.)

    I might as well give it to you:

    find_faces_of_selected_entities = Sketchup.active_model.selection.find_all {|ent| ent.is_a?(Sketchup::Face)}
    User avatar
    Dan Rathbun
    Top SketchUcator
     
    Posts: 4101
    Joined: Tue Oct 06, 2009 3:06 am
    Location: Florida, USA
    Name: Dan Rathbun
    Operating system: Windows
    SketchUp version: 2013
    License type: Pro
    SketchUp use: education
    Level of SketchUp: Advanced

    Re: One-liners

    Postby Dan Rathbun » Sun May 06, 2012 3:22 pm

    Wouldn't it be cool if there was a Chrome plugin, where you could hilite a Ruby statement on SCF, right-click the mouse, and choose "Send to SketchUp Console" ??
    User avatar
    Dan Rathbun
    Top SketchUcator
     
    Posts: 4101
    Joined: Tue Oct 06, 2009 3:06 am
    Location: Florida, USA
    Name: Dan Rathbun
    Operating system: Windows
    SketchUp version: 2013
    License type: Pro
    SketchUp use: education
    Level of SketchUp: Advanced

    Re: One-liners

    Postby noelwarr » Sun May 06, 2012 6:56 pm

    Dan Rathbun wrote:
      The each iterator returns the receiver (upon which it acts,) not the result of the block.

      Try using one of the iterators from the Enumerable mixin module (which is mixed into most of the API collection classes.)

      I might as well give it to you:

      find_faces_of_selected_entities = Sketchup.active_model.selection.find_all {|ent| ent.is_a?(Sketchup::Face)}


      I was unaware of the find_all method. You live and learn. The one-liner you propose, however, does nothing. It would be useful in the context of more code. I think the problem is the variable name I chose. It is misleading. Here is the original code I posted

      find_faces_of_selected_entities = Sketchup::active_model.selection.each{|ent| ent.find_faces if ent.is_a?(Sketchup::Edge)}

      I say find_faces_of_selected_entities because the Edge class has the find_faces() method. I think both should be renamed to reconstruct_faces or something. In the hope everything is more clear we'll get rid of the variable alltogether. I'm changing my first post to...

      #Reconstruct all possible faces from selection
      Sketchup::active_model.selection.each{|ent| ent.find_faces if ent.is_a?(Sketchup::Edge)}

      Try it out. Draw something, delete a few faces, select everything and run the one-liner.
      noelwarr
       
      Posts: 64
      Joined: Mon Jun 20, 2011 5:07 pm
      Name: noelwarr

      Re: One-liners

      Postby Dan Rathbun » Mon May 07, 2012 3:56 am

      noelwarr wrote:Try it out. Draw something, delete a few faces, select everything and run the one-liner.

      YOUR CODE DOES NOT WORK ! .. as I had said.

      It returns nil
      User avatar
      Dan Rathbun
      Top SketchUcator
       
      Posts: 4101
      Joined: Tue Oct 06, 2009 3:06 am
      Location: Florida, USA
      Name: Dan Rathbun
      Operating system: Windows
      SketchUp version: 2013
      License type: Pro
      SketchUp use: education
      Level of SketchUp: Advanced

      Re: One-liners

      Postby noelwarr » Mon May 07, 2012 8:31 am

      What it returns is irrelevant. It's what it does that matters, Dan. Please see attatched video for proof (Sorry its in wmv). There was a moment of fear when the code did nothing. Then I realised I had nothing selected :D

      I know it is bothersome when people post code that doesn't work. I'm sory if I didn't explain myself correctly giving the impression that that was the case. It is also, however bothersome when people say
      Dan Rathbun wrote:YOUR CODE DOES NOT WORK !
      without considering the possibility that it might.

      Rest assured I shall be checking the oneliners people post before I include it in my first post.
      Please, register (free) to access all the attachments on the forums.
      noelwarr
       
      Posts: 64
      Joined: Mon Jun 20, 2011 5:07 pm
      Name: noelwarr

      Re: One-liners

      Postby TIG » Mon May 07, 2012 11:23 am

      It does work, in that it adds faces to selected edges.
      For me it works, then returns the selection, not the new faces or nil.
      I think the initial confusion was that the code had something like "add_faces_to_selected_lines=..." to show what it did, and of course add_faces_to_selected_lines is set to be the selection, I assume this was used because of the arbitrary rule that you can't use a ';' - which makes it impossible to do a one-liner with its intent shown unless you add '#' and then the description at the end of the code line...
      TIG
      User avatar
      TIG
      Global Moderator
       
      Posts: 14008
      Joined: Mon Nov 12, 2007 7:24 pm
      Location: Northumbria UK
      Name: TIG
      Operating system: Windows
      SketchUp version: 2013
      License type: Pro
      SketchUp use: architecture
      Level of SketchUp: Advanced

      Re: One-liners

      Postby noelwarr » Mon May 07, 2012 12:10 pm

      You got it TIG. Could you suggest a way to rephrase the whole "challenge" so it is readily apparent?
      noelwarr
       
      Posts: 64
      Joined: Mon Jun 20, 2011 5:07 pm
      Name: noelwarr

      Re: One-liners

      Postby Dan Rathbun » Mon May 07, 2012 2:14 pm

      Yes I was confused by the original wording you chose.
      User avatar
      Dan Rathbun
      Top SketchUcator
       
      Posts: 4101
      Joined: Tue Oct 06, 2009 3:06 am
      Location: Florida, USA
      Name: Dan Rathbun
      Operating system: Windows
      SketchUp version: 2013
      License type: Pro
      SketchUp use: education
      Level of SketchUp: Advanced

      Re: One-liners

      Postby daiku » Mon May 07, 2012 2:36 pm

      OBFUSCATORS!

      Your goal should be to write the most readable code possible. This is the opposite.
      daiku
       
      Posts: 213
      Joined: Mon Nov 12, 2007 2:54 pm
      Location: Minneapolis
      Name: Clark Bremer

      Re: One-liners

      Postby noelwarr » Mon May 07, 2012 2:46 pm

      大工ですか。すごい。何年前日本に住んだけど。大阪に。それに、私も大工ですよ。面白い、なぁ。
      Anyhow, I'm not advocating for this type of code, though I think it's cool that ruby allows for it. All I wanted was a list of cool lines that one might copy and paste into the console to do something useful. Instead it would seem I've started a flame war or something :o
      noelwarr
       
      Posts: 64
      Joined: Mon Jun 20, 2011 5:07 pm
      Name: noelwarr

      Re: One-liners

      Postby daiku » Mon May 07, 2012 2:57 pm

      Sorry, but I don't speak a lick of Japanese. I can tell from the first two characters that this was addressed to me, but the rest is lost on me. As a professional timber framer, the Daiku are my heroes. We had a group come to one of our conferences, and the level of skill and craftsmanship was amazing.

      And of course I was only trying to be clever with my first post. Nothing personal :-)
      daiku
       
      Posts: 213
      Joined: Mon Nov 12, 2007 2:54 pm
      Location: Minneapolis
      Name: Clark Bremer

      Re: One-liners

      Postby noelwarr » Mon May 07, 2012 3:07 pm

      No offence was taken! :P The japanese just says I'm a carpenter too and lived (a short period of time) in Osaka a few years back. I was also not left unmoved by Japanese craftmanship when it comes to woodworking. We even ended up selling one of their products a few years back, here in Europe. http://mitaka.eu/
      It's still the traditional side that gets to the heart 8-)
      noelwarr
       
      Posts: 64
      Joined: Mon Jun 20, 2011 5:07 pm
      Name: noelwarr

      Re: One-liners

      Postby thomthom » Wed May 09, 2012 11:34 pm

      Why no semi-colons? Seems that we're doing one-statements instead of one-liners then.
      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund
      User avatar
      thomthom
      Global Moderator
       
      Posts: 17686
      Joined: Tue Nov 13, 2007 12:47 pm
      Location: Trondheim, Norway
      Name: thomthom
      Operating system: Windows
      SketchUp version: 8
      License type: Pro
      SketchUp use: architecture
      Level of SketchUp: Advanced

      Re: One-liners

      Postby Jeff Hammond » Thu May 10, 2012 12:01 am

      thomthom wrote:Why no semi-colons?

      meh..


      view = Sketchup.active_model.active_view ; view = view.zoom 1.052

      parallel projection -> zoom extents -> use the code for true zoom extents…
      i use it sometimes for printing.
      (also part of an applescript i use for easy printing to scale from sketchup)
      dotdotdot
      User avatar
      Jeff Hammond
      Global Moderator
       
      Posts: 4326
      Joined: Thu Jan 24, 2008 11:16 pm
      Location: newyorkcity
      Operating system: Mac
      SketchUp version: 8
      License type: Pro
      SketchUp use: architecture
      Level of SketchUp: Advanced

      Re: One-liners

      Postby noelwarr » Thu May 10, 2012 7:54 am

      Why not...

      Sketchup.active_model.active_view.zoom(1.052)

      Or to include the zoom extents...

      Sketchup.active_model.active_view.zoom_extents.zoom(1.052)

      Don't know how to activate the parallel projection though. Anyone?
      noelwarr
       
      Posts: 64
      Joined: Mon Jun 20, 2011 5:07 pm
      Name: noelwarr

      Re: One-liners

      Postby Dan Rathbun » Thu May 10, 2012 9:21 am

        Activate Parallel Projection for the current View's Camera:

        noelwarr wrote:Don't know how to activate the parallel projection though. Anyone?

        Sketchup.active_model.active_view.camera.perspective = false
        User avatar
        Dan Rathbun
        Top SketchUcator
         
        Posts: 4101
        Joined: Tue Oct 06, 2009 3:06 am
        Location: Florida, USA
        Name: Dan Rathbun
        Operating system: Windows
        SketchUp version: 2013
        License type: Pro
        SketchUp use: education
        Level of SketchUp: Advanced

        Re: One-liners

        Postby thomthom » Thu May 10, 2012 9:46 am

        Jeff Hammond wrote:view.zoom 1.052

        Where does this magic number come from?
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom
        Global Moderator
         
        Posts: 17686
        Joined: Tue Nov 13, 2007 12:47 pm
        Location: Trondheim, Norway
        Name: thomthom
        Operating system: Windows
        SketchUp version: 8
        License type: Pro
        SketchUp use: architecture
        Level of SketchUp: Advanced

        Re: One-liners

        Postby Dan Rathbun » Thu May 10, 2012 10:14 am

        thomthom wrote:
        Jeff Hammond wrote:view.zoom 1.052

        Where does this magic number come from?

        .. and it seems to work in Parallel Projection mode, but not in Perspective mode.
        User avatar
        Dan Rathbun
        Top SketchUcator
         
        Posts: 4101
        Joined: Tue Oct 06, 2009 3:06 am
        Location: Florida, USA
        Name: Dan Rathbun
        Operating system: Windows
        SketchUp version: 2013
        License type: Pro
        SketchUp use: education
        Level of SketchUp: Advanced

        Re: One-liners

        Postby Jeff Hammond » Thu May 10, 2012 12:30 pm

        thomthom wrote:
        Jeff Hammond wrote:view.zoom 1.052

        Where does this magic number come from?

        by trying .05... then .06... then .055.. then .054.. etc :D

        [edit-- hmm.. just checked my AppleScript and I'm actually using .053 in it.. that's the one I tested with actual paper/printer]
        Last edited by Jeff Hammond on Thu May 10, 2012 1:11 pm, edited 1 time in total.
        dotdotdot
        User avatar
        Jeff Hammond
        Global Moderator
         
        Posts: 4326
        Joined: Thu Jan 24, 2008 11:16 pm
        Location: newyorkcity
        Operating system: Mac
        SketchUp version: 8
        License type: Pro
        SketchUp use: architecture
        Level of SketchUp: Advanced

        Re: One-liners

        Postby Jeff Hammond » Thu May 10, 2012 1:09 pm

        noelwarr wrote:Why not...

        Sketchup.active_model.active_view.zoom(1.052)


        Because I don't know ruby ;)
        I just found something that worked for what I needed.
        but your version doesn't use a semicolon so I'll make that my thread entry :D
        dotdotdot
        User avatar
        Jeff Hammond
        Global Moderator
         
        Posts: 4326
        Joined: Thu Jan 24, 2008 11:16 pm
        Location: newyorkcity
        Operating system: Mac
        SketchUp version: 8
        License type: Pro
        SketchUp use: architecture
        Level of SketchUp: Advanced

        Re: One-liners

        Postby noelwarr » Thu May 10, 2012 2:24 pm

        I've actually gone and changed my first post so that semi colons are allowed, and have modified your magic formula so the 3 steps are all in one line. I wanted no semicolons to avoid people going crazy with them (you could essentially write a whole program with them in one line of text). How about 3 semi colons maximum?
        noelwarr
         
        Posts: 64
        Joined: Mon Jun 20, 2011 5:07 pm
        Name: noelwarr

        SketchUcation One-Liner Adverts

        by Ad Machine » 5 minutes ago

        Need SketchUp Books, Models, Styles or Textures? Check out our One Stop Shop for SketchUp.

        Premium Members get 20% discount!

        Ad Machine
        Robot
         
        Posts: 2012


        Return to Developers' Forum

        Who is online

        Users browsing this forum: jolran, liquid98 and 4 guests