According to the documentation, it is possible to create an InputPoint from a vertex. However, doing that the #input_path method returns an empty instance (#to_a returns ). Instantiating from vertex.position has the same result (and of course even if it worked would mean the instance_path may be of a different set of entities that just share the same position)
An instance_path describes a single/unique occurence of an instance. An empty instance path means the input point is related to the model root (top level coordinate system). You can only unambiguously obtain an instance path by doing a pick (or raycast etc.).
When given an entity (or vertex), it may occur in many instances and thus have many instance paths. Similarly, a point has no reference to a specific coordinate system, unless you have a specific instance path (or its transformation).
What are you trying to achieve, maybe it can be realized in a different way? Maybe the constructor of InputPoint should have a parameter to initialize it wiith a given instance path (remember, an entity or vertex can occur in many instance paths).
ent, starting_point = dim.start
if ent.parent == Sketchup::active_model
puts "entity in root model context"
return nil
end
#
ip = Sketchup::InputPoint.new(starting_point)
unless ip.valid?
puts "invalid inputpoint"
return false
end
if ip.edge
obj = ip.edge
elsif ip.vertex
obj = ip.vertex
elsif ip.face
obj = ip.face
else
obj = nil
end
#
if obj && ent.respond_to?(:definition)
begin
inst_path = Sketchup::InstancePath.new([ent, obj])
# check for empty path
# if valid use it
rescue ArgumentError => e
# recover
end
end
From his recent posts in the SCF Developers forum, he seems to be working a lot with dimension objects, and having issues getting a reference to what objects the dimensions are attached to. (Hence my snippet above starting from a dimension object.)
So I think this has resulted from his issues regarding that. He keeps using the term “not what he is expecting” with regard to the empty instance path array returned, so what I think is happening, is that InputPoint is returning the dimension object’s path which is empty because it is in the root model context. But he is “expecting” the method to return the instance path of the geometry object to which the vertex belongs.
Perhaps he should be using PickHelper instead and iterating the arrays of picks ?
I’m trying to find the instance path that the start of the dimension is attached to. Specifically, would like to know that in cases where the same component’s instances overlap. The dimension is outside an instance measuring on of its lines (simply create a rectangle, make an instance of it, close and add a dimension)
dim.start[0] returns a Vertex which is part of a ComponentDefinition. So one way is to iterate all the definition’s instances and see which one contains the vertex. There are some caveats with that that I posted in other threads where the bounding box and transformation values from instances change depending on the active entity and there seems to be no way to move to model space (edit_transform returns inconsistent values according to another post). BTW, it would have been nice to be able to change the active path programmatically, so then I’d know inside my code what coordinate system to expect (sorry for “expecting” something again…)
Another way is to take dim.start[1], get the screen coordinates, then instantiate an InputPoint with these and get the instance_path. But then in case of overlapping instances I have no way of knowing which one it’ll pick.
I then tried InputPoint.new(dim.start[0]), thinking that maybe this will give it better sense of the entity I’m interested in or InputPoint.new(dim.start[1]), but the the instance path is empty
The way I’m working now is to scan all instances, calculate their comulative transform and transforming dim.start[0].position and checking if it equals dim.start[1]. The cumulative transform is taking the instance and recursively traverse each of its parents’s instances (if it is nested in a group or another component) combining their transformation. This method returns good results but is a complex piece of code and will return all possible instance paths in cases of overlap. I then fail the tool if there is more than one instance path found (which is not an optimal way, since there really should have been a simpler way to get this information)
Ah - I see. the API’s should have returned an instance path and not just the leaf entity. That would solve your issues I think. We should have an issue logged for this already. I’ll see if I can find it and bump it. (Internal reference: SU-36322)