Selected Dim to Component Instance
-
I am starting with the following code (after selecting a dim in the model)
mod = Sketchup.active_model # Open model ent = mod.entities # All entities in model sel = mod.selection # Current selection s_ent = sel[0] type = s_ent.typename if type == "DimensionLinear" arr = s_ent.start if arr[0].nil? UI.messagebox("No attached entity. Point is " + arr[1].to_s) else UI.messagebox("Attached entity; " + arr[0].to_s) type = arr[0].typename end parent1 = arr[0].parent l_name = parent1.layer.name endThis gets me to the Component Definition but I need the Component Instance to find the layer name so I can change the dim layer to the Component Instance layer.
Keith
-
@ktkoh said:
I am starting with the following code (after selecting a dim in the model)
mod = Sketchup.active_model # Open model > ent = mod.entities # All entities in model > sel = mod.selection # Current selection > > s_ent = sel[0] > type = s_ent.typename > if type == "DimensionLinear" > > arr = s_ent.start > if arr[0].nil? > UI.messagebox("No attached entity. Point is " + arr[1].to_s) > else > UI.messagebox("Attached entity; " + arr[0].to_s) > type = arr[0].typename > end > parent1 = arr[0].parent > l_name = parent1.layer.name > > endThis gets me to the Component Definition but I need the Component Instance to find the layer name so I can change the dim layer to the Component Instance layer.
Keith
mod = Sketchup.active_model ent = mod.active_entities sel = mod.selection s_ent = sel[0] type = s_ent.typename if type == "DimensionLinear" arr = s_ent.start if arr[0].parent.is_a?(Sketchup;;ComponentDefinition) parent1 = arr[0].parent; parent1.instances.each{|ci| if ci.bounds.contains?(arr[1]) l_name = ci.layer.name; s_ent.layer=l_name; sel.add ci;break end } else puts "Dimension not associated with component" end end -
Couple of pointers.
I think you really want:
mod.**active_**entities # All entities in **active context**NOT the entities in the model itself: after all, the user might be inside an edit ?
You might also like to check
Sketchup.versionand/or if a particular method is defined [.respond?] because dim's were added to the API quite recently...... arr = s_ent.start ### the array of data that the dim is attached to [at its start] ... parent1 = arr[0].parent ### the vertex inside the definition l_name = parent1.layer.name ### the definition itself won't have a layer ?You have no simple way of knowing the instance from the definition.
if theparent1.instances.length==1
instance = parent1.instances[0]
BUT if not, then you need to find how to spot the instance...We have a point at the end of the dim...
pt = arr[1]You can get its screen xy thus:
view = mod.active_view xy = view.screen_coords(pt) x=xy[0] y=xy[1]Now set up a pickhelper to test it...
ph = view.pick_helper ph.do_pick(x, y) array = ph.all_picked
returns an array of all things at that point, including instance[s], so iterate through thearray.uniqand test if its an instance AND it matchesarr[0] == instance.definition
If so then you have your instance !
Get the layer from that etc...
-
Thanks for the help. I'll try your suggestions.
Keith
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement