Knowing the ComponentInstance a DimensionLinear is attached
-
I have a dimension attached to a component instance (it moves with it, changes when it is stretched, etc.). Using the API,
dim.start[1].parent
returns a ComponentDefinition. This of course doesn't tell me which instance it is attached to. Is there a way of knowing? Or is it a bug?(A brute force method is to go over every instance, iterate over all its vertices and compare with the one of the dimension. I'm looking for something more elegant, and that will work when attaching to an edge etc.)
-
@ittayd said:
I have a dimension attached to a component instance (it moves with it, changes when it is stretched, etc.).
Using the API,
dim.start[1].parent
returns aComponentDefinition
. This of course doesn't tell me which instance it is attached to. Is there a way of knowing?Yes.
dim.start[0]
Test it. If it is
nil
the dimension is unassociated.dim.start[0].nil?
or simply in Ruby:
` inst_start = dim.start[0]
if inst_startuse it
else
assume it is nil as nil evals as FALSE in Ruby
end`
You can also use Ruby's multiple assignment:
ent, point = dim.start
Ref: http://ruby-doc.org/core-2.0.0/doc/syntax/assignment_rdoc.html
@ittayd said:
Or is it a bug?
Sounds like a brain bug. (Reference to "Starship Troopers II".)
As the API doc is quite clear that the first member of the returned array is supposed to be a reference to the entity.
-
dim.start[0]
will return a Vertex or Edge. How do I know the ComponentInstancedim.start[0]
is part of? As I wrotedim.start[0].parent
returns a ComponentDefinition. -
FYI, it's been shown By ittayd that this is bugged. Thomas Thomassen has filed the bug internally.
The method is supposed (according to the API docs) to return the instance in favor of any primitive (vertex, edge, etc.)
See:
https://forums.sketchup.com/t/how-can-i-tell-to-which-instance-a-dimension-is-attached/47309 -
Until it's fixed how about a clunky and convoluted workaround ?
If the pa=dim.start[0].parent is a component-definition then you need to see if there is pa.instances[1] if not then the one instance is the answer.
If there are more than one instance you need to iterate those, and assemble a list of their transformation.origin positions [ensuring the transformation steps back to match the dim's parent's and of course takes into account any instance scaling etc...] - let's call that 'pvs'.
The pt=dim.start[0] gives you a point inside the definition.
You have ps=dim.start[1] as the point of the dim in 3d space.
Transform the 'pvs' inversely and compare that point with ps, if they match then you could have a hit.
Of course two or more instances where some are rotated or flipped so that vertex is coincident in 3d returns two or more 'hit' points, so it'd not be possible to determine the attachment that way...So, it's better if it worked like it should !!!
Advertisement