• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Best practices for working with cummulative transformations

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 3 Posters 164 Views
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.
  • N Offline
    noelwarr
    last edited by 4 Apr 2012, 16:22

    My model has a bunch of nested groups and instances. That's all fine. I also understand that I can't really say "where is entity X" in my model because it might be present in various instances.

    For my purposes I need to have a kind of flattened tree of my model so I can effectively ask "Give me a list of all groups and instances(nested and not nested) in groupX with their corresponding cummulative transformations". I have a vague idea of how to do this but I just want to make sure I am taking the right approach. Something like

    
    def gather(entity,transformation)
      result = {;entities => [], ;transformations => []}
      if entity.typename == "Group" || entity.typename == "Model"
        entities = entity.entities
      elsif entity.typename == "ComponentInstance"
        entities = entity.definition.entities
      else
        return []
      end
      result[;entities].push entity
      result[;transformations].push transformation
      entities.each{|ent|
        gathered = gather(entity, (entity.transformation * transformation)) 
        result[;entities] += gathered[;entities]
        result[;transformations] += gathered[;transformations]
      }
      return result
    end
    
    gather(Sketchup;;active_model, Geom;;Transformation.new)
    
    

    I haven't tested it out but you get the idea. It's an itterative function that collects a couple of arrays, one with the groups and intsnces, the other has the transformations. So, any views?

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 4 Apr 2012, 17:05

      Could you not inspect each thing as you need?
      Something like this [it's untested - I typed it from memory]...
      ### container=some_group_or_instance tr=container.transformation parent=container.parent cont=container until parent==Sketchup.active_model cont=cont.parent tr=tr*cont.transformation parent=cont.parent end return tr

      you use ' tr' [OR ' tr.inverse'] as needed - it's the cumulative transformation of the ' container'...

      TIG

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 4 Apr 2012, 17:37

        Sidenote: Avoid using .typename because it's terrible slow!
        More details here: http://www.thomthom.net/thoughts/2011/12/never-ever-use-typename/

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

        1 Reply Last reply Reply Quote 0
        • N Offline
          noelwarr
          last edited by 4 Apr 2012, 19:14

          @Thomthom. Fantastic little writeup. Had no idea about that. Performance isn't really an issue at the moment but I'll change my code anyway. I'll sleep better knowing its faster.

          @TIG. I don't see that working. Indeed, you do get to the model but not through all the nested objects. I get the following with both nested instances and groups...

          entity.parent
          => Sketchup::ComponentDefinition
          entity.parent.parent
          => Sketchup::DefinitionList
          entity.parent.parent.parent
          => Sketchup::Model

          I think the only way is to go "inwards". You can't go "backwards" because entities only exist in definitions and definitions only exist in definition lists (If that makes any sense).

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 4 Apr 2012, 19:18

            I see your point. 😒
            What is it exactly that you want to find the transformation of ? 😕

            TIG

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 4 Apr 2012, 19:18

              I wonder if AdamB not posted an example once... damned if I can recall the thread...

              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
                thomthom
                last edited by 4 Apr 2012, 19:23

                hmm.. closest I find is this thread which talks about traversing backwards: http://forums.sketchucation.com/viewtopic.php?f=180&t=26046&p=255969#p224606

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

                1 Reply Last reply Reply Quote 0
                • N Offline
                  noelwarr
                  last edited by 4 Apr 2012, 20:02

                  @Thomthom: Great thread! All this OOP and geometry, I can't help but feel it verges on mental masturbation 😆 . It's almost metaphysical. I'll be using a bit of code from that thread. Thanks

                  @TIG: The short answer is I'm trying to find the transformation of a lot of things. I'll give you some background. I'm trying to create a kind of project space for CNC manufacturing. The hiearchy looks something like this.

                  ->MODEL
                  --->ASSEMLY (Group/Component)
                  ----->WORKPIECE (Component)
                  ------->DESIGN (Group)
                  --------->TOOLPATH (Group/Component)
                  ----------->HOLE (Group/Component)

                  Some things are forbidden. A toolpath can't contain any other groups or instances, a workpiece can't contain another workpiece. But I want to allow the user to further nest whatever they want. This means a workpiece can have a group of toolpaths or an assembly can have workpieces that are nested in other groups.

                  At the risk of rambling on, I'll leave it at that. Let me know if I'm not clear (which is often the case when I try to explain anything related to OOP 😕 )

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

                  Advertisement