Hi all.... Me again (with a hard-to-put-in-text question, I'm afraid),
I'm trying to write a script that selects all Edges in a model that have exactly one Face attached to them. I've got the basics of the code right: it works fine, provided that the Edges that are being selected are in the same "hierarchical level" as the "level" that the call is being made from. That's possibly a confusing sentence; let me try to explain it by example. Suppose that I have selection = Sketchup.active_model.selection, and that top_level_entity = Sketchup.active_model.entities[0] is a Face. If I call selection.add top_level_entity, then there's no problem.
However, I seem to have a problem if I call selection.add nested_entity, where nested_entity is part of a composite Entity (e.g. Group, ComponentInstance). For example, if group_ent = Sketchup.active_model.entities[1] is a Group, then I'll have problems calling selection.add group_ent.entities[0] (suppose, just for the purpose of this example, that group_ent.entities[0] is a Face, but the behaviour is the same for any Entity). The call does "work" -- i.e., I don't get an error or a nil or anything. However, it exhibits some "weird" behaviour: in particular, Sketchup doesn't actually highlight the Face represented by group_ent.entities[0]; instead, it highlights "where the Face would be in the world coordinate system if you forgot to apply the Group's transformation". Again, trying to give an (overly simplified) example: suppose the Face is centered at [3,2,5] within the Group, and the Group itself is centered at [5,5,5], then this means that the the Face is actually located at [8,7,10] in the world coordinate system; however, selection.add is highlighting a "phantom Face" at [3,2,5] in the world coordinate system.
I haven't been able to figure out a way around this behaviour, programmatically. The only thing I have discovered is that selection.add seems to work if you're editing the deepest level Group/Component to which the Entity you want to select belongs. "Easy enough then," I figured. "All I have to do is programmatically tell Sketchup to edit the parent of the Entity I want to select." Unfortunately, although I found the call Sketchup.close_active, I can't seem to find the inverse method (something like Sketchup.active= or Sketchup.set_active).
I've looked in other places (including calling [Sketchup_class].methods on virtually every class in the Sketchup API that I've run across [fun exercise, but didn't produce what I wanted]), but I haven't found an answer to "is there an inverse to Sketchup.close_active?". More generally, I haven't seen a thread that deals with this "nested selection" problem. (I don't know if that's a bad sign or not.) I've thought of some other tricks to get Sketchup to select a nested Entity, but all of them are a little kludgey. For example, I've thought of creating a copy of the target Entity and adding that copy to the top-level model (maybe in a different Layer) and selecting that copy -- but now that means I've got duplicate Entities floating around. Other "solutions" I've come up with are similar slight-of-hand kludgey affairs.
If anyone has thoughts on either matter (nested selection in general, or Sketchup.set_active specifically), I'd be interested to hear them. Also, let me know if I need to elaborate on the description of the problem: it's one of those things that's really easy to describe if you're sitting next to the person, but comes out really incoherent when you try to explain it in text alone.
Thanks,
Kevin