sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Iterating over the faces of a component - without exploding

    Scheduled Pinned Locked Moved Developers' Forum
    30 Posts 6 Posters 962 Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • fredo6F Offline
      fredo6
      last edited by

      Talig

      A few remarks on your script.

      You must accumulate the transformations to get the real geometric coordinates of the elements you traverse. If your raytest hits a component 'cp3', embedded in 'cp2' and 'cp1', then you should use

      
      t = cp1.transformation * cp2.transformation * cp3.transformation
      
      

      Then, you do as Chris suggests, cycle through the faces of component3, and perform 2 tests with the point 'ptray' returned by model.raytest:

      1. Check if on plane (fast test)
      2. check within the face (more greedy, especially when face has holes)
      
      lfaces = []
      cp3.definition.entities.each do |e|
          if e.class == Sketchup;;Face
             plane = [t * e.center, t * e.normal]
             lfaces.push e if ptray.on_plane?(plane)
          end
      end
      
      myface = nil
      tinv = t.inverse
      ptlocal = tinv * ptray
      lfaces.each do |face|
         status = face.classify_point ptlocal
         if status > 0 && status < 8
            myface = face
            break
         end
      end
      #myface should contain your face
      
      #Note that if cp3 is a group, you should use cp3.entities instead of cp3.definition.entities.
      
      

      The list [cp3, cp2, cp1] is returned by the raytestmethod.

      As a side remark, you can get an array of entities by using the standard Ruby method: entities.to_areturns a list of entities (this also works on a selection).

      Fredo

      1 Reply Last reply Reply Quote 0
      • Chris FullmerC Offline
        Chris Fullmer
        last edited by

        How large of a component does it take for this to break? I just tried it on a simple group of 5 faces and it returned an array 5 of faces. Are you experiencing problems only on large, multiple nested components?

        Chris

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

        1 Reply Last reply Reply Quote 0
        • fredo6F Offline
          fredo6
          last edited by

          I did not test the code (just wrote it on the fly).
          The question is whether Raytest returns the point hit in absolute model coordinates, or in local coordinates of the component or group hit. The doc is unclear. In either case, I wanted just to tell Talig that he should use Transformations to cycle through the model.

          Fredo

          1 Reply Last reply Reply Quote 0
          • T Offline
            talig
            last edited by

            Chris - that's what I said: I get the correctnumber of faces. But I make some calculation about them, and I get a different result when I use explode - though I should get the same one. Even a plain box is not working properly.

            Fredo - I'm a female πŸ˜„
            About the coordinates: What you're saying is interesting. I assumed I get the absolute model coordinates in any case. That's obviously true in the exploded case, but possibly the cause of the problem in what I'm trying to do. I'll check it out. Thanks! πŸ‘ (and thanks for the to_a tip!)

            I already have everything else up and running, so thanks for trying to help - but really, no call for that.
            All I need is an array of faces equivalent to that of an exploded component. Nothing more, nothing less. πŸ˜„

            Avatar: all rights reserved to Bryan Eppihimer

            1 Reply Last reply Reply Quote 0
            • Chris FullmerC Offline
              Chris Fullmer
              last edited by

              @talig said:

              Chris - that's what I said: I get the correctnumber of faces. But I make some calculation about them, and I get a different result when I use explode - though I should get the same one. Even a plain box is not working properly.

              Heehe, yeah, I noticed that after I was re-reading some of Fredo's posts. I went back and re-read your post and realized that you already were getting the number of faces correct. My mistake.

              @talig said:

              All I need is an array of faces equivalent to that of an exploded component. Nothing more, nothing less. πŸ˜„

              I see what your saying now. Makes sense.

              So what is the calculations you're trying to run on the faces? Or maybe you've already got it working with Fredo's great explanation?

              Chris

              Lately you've been tan, suspicious for the winter.
              All my Plugins I've written

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                Have you checked that the list of faces you get is the same as if you exploded them? Not just the number.

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • T Offline
                  talig
                  last edited by

                  thomthom - I don't know how I can check that they are the same.
                  I mean, I can work something out and save the array before explosion, then explode it and compare, but will the object comparison be good enough? Do I need to compare certain fields? I'll obviously need to sort them both to have them in the same order - which may also be non-trivial...
                  Ideas?

                  Avatar: all rights reserved to Bryan Eppihimer

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    The .to_s method of entities returns something like: #<Sketchup::Face:0xae3ce28>
                    You could sort the results of each method, exploded and un-exploded, and write it out to two files. When you have your array just use the .sort method. Then you can compare them for differences. For short lists you can do it yourself. For larger lists if you need to test more complex models you can try with some software that compares two files.

                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • Chris FullmerC Offline
                      Chris Fullmer
                      last edited by

                      I don't think you can .sort a selection set. It always comes back with undefined method '<=>' so I'm guessing they have not built in a way to compare the values of faces to know how to sort them. Even if they implemented sort to work with just the ObjectID string would suffice.

                      Chris

                      Lately you've been tan, suspicious for the winter.
                      All my Plugins I've written

                      1 Reply Last reply Reply Quote 0
                      • Chris FullmerC Offline
                        Chris Fullmer
                        last edited by

                        @chris fullmer said:

                        Even if they implemented sort to work with just the ObjectID string would suffice.

                        Oh, that works to do that: selection.to_a.to_s.sort

                        That will sort it by turning all face ID's into a simple string. Then it can sort them. That will come in handy,

                        Chris

                        Lately you've been tan, suspicious for the winter.
                        All my Plugins I've written

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          @chris fullmer said:

                          Oh, that works to do that: selection.to_a.to_s.sort

                          I was thinking more like selection.to_a.sort
                          Is there a .sort method for a string?

                          Thomas Thomassen β€” SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply Reply Quote 0
                          • T Offline
                            talig
                            last edited by

                            Thanks thomthom, but who promises that that string is a good identifier, when you run different instances of the program, possibly different files (same model saved under different names), if you have the same component twice in the model? Is it documented anywhere what that hex sequence is? (I'm guessing hash, but even so it matters what it takes into account)

                            Avatar: all rights reserved to Bryan Eppihimer

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by

                              You're right. Didn't think of that. The Hash, and .entityID changes, also if you explode the groups.

                              But, another method: You could make a list of the areas of all faces and compare them. Mind you, if any of your groups/components are scaled you have to take that into account.

                              Thomas Thomassen β€” SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • T Offline
                                talig
                                last edited by

                                Yeah, I think I've tried that, though only visually.
                                I'm not sure they match, but visual testing counts for nothing...
                                I'll give it a go, we'll see what the conclusions are πŸ˜„

                                Avatar: all rights reserved to Bryan Eppihimer

                                1 Reply Last reply Reply Quote 0
                                • Chris FullmerC Offline
                                  Chris Fullmer
                                  last edited by

                                  @thomthom said:

                                  I was thinking more like selection.to_a.sort
                                  Is there a .sort method for a string?

                                  Yeah, but that's the thing. There is no selection.to_a.sort method available. But you can sort them alphabetically if you turn all the array items into strings selection.to_a.to_s.sort. Then it sorts them alphabetically.

                                  But of course it was decided this won't help here. But maybe elsewhere in the future.

                                  Chris

                                  Lately you've been tan, suspicious for the winter.
                                  All my Plugins I've written

                                  1 Reply Last reply Reply Quote 0
                                  • Chris FullmerC Offline
                                    Chris Fullmer
                                    last edited by

                                    I'm stilll curious Talig, are looking to find all the face normals? OR looking to find square areas? As Thom and Fredo have mentioned, the different rotation and scale of each group and component need to be taken into account.

                                    Chris

                                    Lately you've been tan, suspicious for the winter.
                                    All my Plugins I've written

                                    1 Reply Last reply Reply Quote 0
                                    • thomthomT Offline
                                      thomthom
                                      last edited by

                                      @chris fullmer said:

                                      Yeah, but that's the thing. There is no selection.to_a.sort method available. But you can sort them alphabetically if you turn all the array items into strings selection.to_a.to_s.sort. Then it sorts them alphabetically.

                                      hmm? .to_s returns an array. And for array objects there's a .sort method. I'm sure I've used this.

                                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • Chris FullmerC Offline
                                        Chris Fullmer
                                        last edited by

                                        But it depends what is in the array. Sketchup does not define a heirarchy for how to sort entities like FaceObjects, Component Objects, etc. So the .sort method breaks when put on an array full of items that ruby can't decide how they should be sorted.

                                        I thought that Sketchup.active_model.selection.to_a.to_s.sort would turn the selection into an array, and then turn each objectID into a separate string and then alphabetize all the strings. But thats not quite right. It turns the whole returned array into a single string, and therefore .sort doesn't change anything anyhow.

                                        So to get around it, you have to make an array of all entitiesID's turned into strings. Something like:
                                        sel = Sketchup.active_model.selection selection_strings = [] sel.each do |e| selection_strings << e.to_s end puts selection_strings.sort
                                        That will effectively create an array of strings, instead of unsortable ObjectIDs. The strings can then be alphabetized by .sort, which makes it much easier to compare - even though the entire idea was thrown out a few posts ago. But if you wanted to sort them, this does work.

                                        Chris

                                        Lately you've been tan, suspicious for the winter.
                                        All my Plugins I've written

                                        1 Reply Last reply Reply Quote 0
                                        • fredo6F Offline
                                          fredo6
                                          last edited by

                                          @talig said:

                                          Fredo - I'm a female πŸ˜„
                                          About the coordinates: What you're saying is interesting. I assumed I get the absolute model coordinates in any case. That's obviously true in the exploded case, but possibly the cause of the problem in what I'm trying to do. I'll check it out. Thanks! πŸ‘ (and thanks for the to_a tip!)

                                          I already have everything else up and running, so thanks for trying to help - but really, no call for that.
                                          All I need is an array of faces equivalent to that of an exploded component. Nothing more, nothing less. πŸ˜„

                                          Sorry for the confusion. I missed your splendid avatar.
                                          What I wanted to say is that if you have instances of a component, each containing one face, you will always get the same face object for each one when scanning the model: this is the face which is stored in their Definition. The only way to make a distinction is to use the transformation property of each instance (when you explode you have 2 components, each with its own definition). So, in short, don't expect to get a list of faces alone, but rather a list of couple [faces, transformation].

                                          Fredo

                                          1 Reply Last reply Reply Quote 0
                                          • T Offline
                                            talig
                                            last edited by

                                            Fredo,
                                            Can't I just apply the transformation and save just the transformed face?
                                            Because that's what I thought I'd do πŸ˜„

                                            • Tali

                                            Avatar: all rights reserved to Bryan Eppihimer

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement