Suface scaled component instances
-
Hello,
I work on a script to get information about window and doors components placed in a SUmodel.
One of the things a need to know is the current surface of the window instance, even if a user has changed its original surface by scaling it etc. I tried with this piece of code:component.definition.entities.each { |subentity| if subentity.typename == "Face" surface = subentity.area.inch_to_m @surf = @opp + oppervlakte end }
But this gives me -off course- the original surface of the component definition.
I have been looking for a similar method in the SU api to get the entities of a component instance, but didn't find one.I suppose I should do something with the transformation matrix of the component instance, but I really don't know how to do so. How can you know with which matrices the transformation matrix has been multiplied, and in which order, to get the final matrix? And how can you see which transformations have been done?
For now, I wrote this methode to get by exploding the component and then undoing the operation.
def surface?(entity) #entity = a face in the SUmodel @surf = 0 i = 0 # counter entity.get_glued_instances.each{ |component| # Explode components to get its entities entities = component.explode i += 1 entities.each { |subentity| if subentity.typename == "Face" surface = subentity.area.inch_to_m @surf = @surf + surface end } } # Undo the operation i times i.times { Sketchup.undo } return @opp end
It works, but i don't think this kind of destructive operation is a good solution.
Does anyone have another idea of a way to approach this problem?
I don't have SUpro, so can't work with dynamic components.Or maybe someone has allready written a scipt that uses transformation matrices in this way?
thanks a lot,
greets, kat -
I just wrote a snippet of code two nights ago that does this. I needed to find the y length of a component, even if it was scaled differently than the original, and if it is rotated off axis.
The way I came up with was to look at the transformation matris for that instance. Find the y vector info (which is the 4,5,6 elements of the transformation array) and get that vectors length. If it is 1, then that means the component is not scaled. If it more than 1, then it has been scaled larger, less than 1 means it is scaled smaller. So the vector length is a multiplier number against the definitions y-length. If the definition's y length is 20, and the instance vector length is .8, then the instance's y length is 16.
I'll see if I can find a piece of code that works and might be more informative than my verbal description.
Chris
-
Instead of exploding and undoing why not make a group of the instance, copy it explode it inside the group get the areas and erase the group...
[assumes some variables have been predefined e.g. entities, instance etc]
Something like...group=entities.add_group(instance) ### add instance to a group group_copy=group.copy ### copy the group group.explode ### set instance back as it was... group_copy_entities.to_a[0].explode ### explode instance inside the group_copy ### now look at "group_copy.entities" for faces, areas etc... ###... ### finally destroy the evidence ! group_copy.erase! if group_copy.valid?
-
Adam Billyard provided this snippet:
-
Hey thanks, this last one was a very good tip.
I think what AdamB proposed is a very professional approach of the problem.
I 've tried it out in my code and it works fantastically.
Yet, it would be interesting to understand WHY this actually works,
does someone know a bit more about the matematical background of this vector based solution?
Advertisement