Best practices for working with cummulative transformations
-
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?
-
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
'... -
Sidenote: Avoid using
.typename
because it's terrible slow!
More details here: http://www.thomthom.net/thoughts/2011/12/never-ever-use-typename/ -
@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::ModelI 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).
-
I see your point.
What is it exactly that you want to find the transformation of ? -
I wonder if AdamB not posted an example once... damned if I can recall the thread...
-
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
-
@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 )
Advertisement